Skip to content

Commit

Permalink
Merge feature/implementMoreConvinientMethods int develop (#10)
Browse files Browse the repository at this point in the history
* move proto file
* small improve on Dispose
* remove proto file
* move .proto files
* fix examples folder
* clear using
* first working websocket thanks to @Versette
* work on clean up
* Set up readme and so
* add some events
* Still need to debug because some are not working idk why
* totaly rework the app logic by removing the callback principe
implement entityinfo
* add ServerInfo and ServerMap
* add segregation between legacy code and explicit objects
* rework RustPlus socket
* add examples
* add more test examples
  • Loading branch information
HandyS11 authored May 30, 2024
1 parent 5188e49 commit eb20a33
Show file tree
Hide file tree
Showing 109 changed files with 5,358 additions and 326 deletions.
71 changes: 68 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 📊 Features

### RustPlusApi

This is a list of the features that the Rust+ API provides:

- `GetEntityInfo` Get current state of a Smart Device
Expand All @@ -16,6 +18,10 @@ This is a list of the features that the Rust+ API provides:

Feel free to **explore** the `./RustPlusApi/Examples/` folder to see how to **use** the API.

### RustPlusApi.Fcm



## 🖊️ Versions

![skills](https://skillicons.dev/icons?i=cs,dotnet)
Expand All @@ -24,14 +30,20 @@ Feel free to **explore** the `./RustPlusApi/Examples/` folder to see how to **us

## 📍 NuGet

Simply use this nuget by running the following command:
Simply use this library in your project by running the following commands:

```dotnet
dotnet add package RustPlusApi
```

```dotnet
dotnet add package RustPlusApi.Fcm
```

## ⚙️ Usage

### RustPlusApi

First, instantiate the `RustPlus` class with the necessary parameters:

```csharp
Expand All @@ -43,9 +55,11 @@ Parameters:
- `server`: The IP address of the Rust+ server.
- `port`: The port dedicated for the Rust+ companion app (not the one used to connect in-game).
- `playerId`: Your Steam ID.
- `playerToken`: Your player token acquired with FCM.
- `playerToken`\*: Your player token acquired with FCM.
- `useFacepunchProxy`: Specifies whether to use the Facepunch proxy. Default is false.

\* To aquired the player token, you can use the `FcmListener` and received at least one notification. Go to the next section to see how to use it.

Then, connect to the Rust+ server:

```csharp
Expand All @@ -68,8 +82,59 @@ Remember to dispose the `RustPlus` instance when you're done:
rustPlusApi.Dispose();
```

---

### RustPlusApi.Fcm

First, instantiate the `FcmListener` class with the necessary parameters:

```csharp
var fcmListener = new FcmListener(credentials, persistentIds);
```

Parameters:

- `credentials`: The `Credentials`\* object containing the FCM & GCM credentials + the keys to decrypt the notification.
- `persistentIds`: A list of notification IDs that should be ignored. Default is null.

\* Go to the [Credentials](#credentials) section to know how to get it.

Then, connect to the FCM socket:

```csharp
await fcmListener.ConnectAsync();
```

You can subscribe to events to handle connection, disconnection, errors, and received messages:

```csharp
fcmListener.Connecting += (sender, e) => { /* handle connecting event */ };
fcmListener.Connected += (sender, e) => { /* handle connected event */ };
fcmListener.Disconnected += (sender, e) => { /* handle disconnected event */ };
fcmListener.ErrorOccurred += (sender, e) => { /* handle error event */ };
fcmListener.MessageReceived += (sender, e) => { /* handle received message event */ };
```

Remember to dispose the `FcmListener` instance when you're done:

```csharp
fcmListener.Dispose();
```

### Credentials

Currenlty, there is not simple way to get the FCM & GCM credentials using **.NET**.
I've planned to implement a solution but it's not ready yet.

To use this library, you need to get the FCM & GCM credentials manually.
To do so I recommand you to use [this project](https://github.com/liamcottle/rustplus.js) to get the credentials.

I'm sorry for the inconvenience but since the API is not fully complete it's the easiest way.

## 🖼️ Credits

This project is grandly inspired by [liamcottle/rustplus.js](https://github.com/liamcottle/rustplus.js).
*This project is grandly inspired by [liamcottle/rustplus.js](https://github.com/liamcottle/rustplus.js).*

Special thanks to [**Versette**](https://github.com/Versette) for his work on the `RustPlusApi.Fcm` socket.

* Author: [**HandyS11**](https://github.com/HandyS11)
215 changes: 194 additions & 21 deletions RustPlusApi.sln

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions RustPlusApi/Examples/Fcm/FcmListener/FcmListener.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\RustPlusApi.Fcm\RustPlusApi.Fcm.csproj" />
</ItemGroup>

</Project>
38 changes: 38 additions & 0 deletions RustPlusApi/Examples/Fcm/FcmListener/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using RustPlusApi.Fcm;
using RustPlusApi.Fcm.Data;

var credentials = new Credentials
{
Keys = new Keys
{
PrivateKey = "",
PublicKey = "",
AuthSecret = "",
},
Fcm = new FcmCredentials
{
Token = "",
PushSet = "",
},
Gcm = new GcmCredentials
{
Token = "",
AndroidId = 0,
SecurityToken = 0,
AppId = "",
}
};

var listener = new FcmListener(credentials);

listener.NotificationReceived += (_, message) =>
{
Console.WriteLine($"[MESSAGE]: {DateTime.Now}:\n{message}");
};

listener.ErrorOccurred += (_, error) =>
{
Console.WriteLine($"[ERROR]: {error}");
};

await listener.ConnectAsync();
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\RustPlusApi\RustPlusApi.csproj" />
<ProjectReference Include="..\__Constants\__Constants.csproj" />
<ProjectReference Include="..\..\..\RustPlusApi.Fcm\RustPlusApi.Fcm.csproj" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions RustPlusApi/Examples/Fcm/FcmRegister/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using RustPlusApi.Fcm;
using Newtonsoft.Json;

// NOT WORKING, NEEDS TO BE FIXED
return;

/* var senderId = "976529667804";
var credentials = await FcmRegister.RegisterAsync(senderId);
Console.WriteLine(JsonConvert.SerializeObject(credentials, Formatting.Indented));*/
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\RustPlusApi\RustPlusApi.csproj" />
<ProjectReference Include="..\__Constants\__Constants.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\RustPlusApi\RustPlusApi.csproj" />
<ProjectReference Include="..\__Constants\__Constants.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
using static __Constants.ExamplesConst;

var rustPlus = new RustPlus(Ip, Port, PlayerId, PlayerToken);
const uint entityId = 0;

rustPlus.Connected += async (_, _) =>
{
await rustPlus.GetEntityInfoAsync(EntityId, message =>
{
Console.WriteLine($"Infos:\n{JsonConvert.SerializeObject(message, JsonSettings)}");
rustPlus.Dispose();
return true;
});
var message = await rustPlus.GetAlarmInfoAsync(entityId);

Console.WriteLine($"Infos:\n{JsonConvert.SerializeObject(message, JsonSettings)}");

rustPlus.Dispose();
};

await rustPlus.ConnectAsync();
26 changes: 0 additions & 26 deletions RustPlusApi/Examples/GetEntityChanges/Program.cs

This file was deleted.

11 changes: 5 additions & 6 deletions RustPlusApi/Examples/GetInfo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@

rustPlus.Connected += async (_, _) =>
{
await rustPlus.GetInfoAsync(message =>
{
Console.WriteLine($"Infos:\n{JsonConvert.SerializeObject(message, JsonSettings)}");
rustPlus.Dispose();
return true;
});
var message = await rustPlus.GetInfoAsync();

Console.WriteLine($"Infos:\n{JsonConvert.SerializeObject(message, JsonSettings)}");

rustPlus.Dispose();
};

await rustPlus.ConnectAsync();
25 changes: 15 additions & 10 deletions RustPlusApi/Examples/GetMap/Program.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
using RustPlusApi;
using Newtonsoft.Json;

using RustPlusApi;

using static __Constants.ExamplesConst;

var rustPlus = new RustPlus(Ip, Port, PlayerId, PlayerToken);

rustPlus.Connected += async (_, _) =>
{
await rustPlus.GetMapAsync(message =>
{
var imageData = message.Response.Map.JpgImage.ToByteArray();
if (imageData == null) return false;
File.WriteAllBytes("map.jpg", imageData);
Console.WriteLine(@"Saved under .\RustPlusApi\RustPlusApi\Examples\GetMap\bin\Debug\net8.0");
rustPlus.Dispose();
return true;
});
var message = await rustPlus.GetMapAsync();

if (!message.IsSuccess) return;

File.WriteAllBytes("map.jpg", message.Data?.JpgImage!);

message.Data!.JpgImage = null;
Console.WriteLine($"Infos:\n{JsonConvert.SerializeObject(message, JsonSettings)}");

Console.WriteLine($"Image saved under: {Directory.GetCurrentDirectory()}");

rustPlus.Dispose();
};

await rustPlus.ConnectAsync();
12 changes: 6 additions & 6 deletions RustPlusApi/Examples/GetMapMarkers/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

rustPlus.Connected += async (_, _) =>

Check warning on line 9 in RustPlusApi/Examples/GetMapMarkers/Program.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
await rustPlus.GetMapMarkersAsync(message =>
{
Console.WriteLine($"Infos:\n{JsonConvert.SerializeObject(message, JsonSettings)}");
rustPlus.Dispose();
return true;
});
//await rustPlus.GetMapMarkersAsync(message =>
//{
// Console.WriteLine($"Infos:\n{JsonConvert.SerializeObject(message, JsonSettings)}");
// rustPlus.Dispose();
// return true;
//});
};

await rustPlus.ConnectAsync();
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\RustPlusApi\RustPlusApi.csproj" />
<ProjectReference Include="..\__Constants\__Constants.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\RustPlusApi\RustPlusApi.csproj" />
<ProjectReference Include="..\__Constants\__Constants.csproj" />
</ItemGroup>

</Project>
22 changes: 22 additions & 0 deletions RustPlusApi/Examples/GetSmartSwitchChanges/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Newtonsoft.Json;

using RustPlusApi;

using static __Constants.ExamplesConst;

var rustPlus = new RustPlus(Ip, Port, PlayerId, PlayerToken);
const uint entityId = 0;

rustPlus.Connected += async (_, _) =>
{
var message = await rustPlus.GetSmartSwitchInfoAsync(entityId);

Console.WriteLine($"Infos:\n{JsonConvert.SerializeObject(message, JsonSettings)}");
};

rustPlus.OnSmartSwitchTriggered += (_, message) =>
{
Console.WriteLine($"SmartSwitch:\n{JsonConvert.SerializeObject(message, JsonSettings)}");
};

await rustPlus.ConnectAsync();
15 changes: 15 additions & 0 deletions RustPlusApi/Examples/GetSmartSwitchInfo/GetSmartSwitchInfo.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\RustPlusApi\RustPlusApi.csproj" />
<ProjectReference Include="..\__Constants\__Constants.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit eb20a33

Please sign in to comment.