Skip to content

Commit

Permalink
feat: fixed remaining errors with being loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Dec 11, 2022
1 parent 067b084 commit 121269a
Show file tree
Hide file tree
Showing 8 changed files with 11,592 additions and 26 deletions.
Binary file added NewtonsoftAlias.Json.dll
Binary file not shown.
11,363 changes: 11,363 additions & 0 deletions NewtonsoftAlias.Json.xml

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions Oxide.Ext.GamingApi/Account.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Reference: NewtonsoftAlias.Json

using System.Collections.Generic;
using System;
using NewtonsoftAlias.Json;
using NewtonsoftAlias.Json.Linq;

namespace Oxide.Ext.GamingApi
{
public class AccountConverter : JsonConverter<Account>
{
public override void WriteJson(JsonWriter writer, Account value, JsonSerializer serializer)
{
writer.WriteValue(value.ToString());

JObject jo = new JObject();
if (value.PackageId != null)
{
jo.Add("id", JToken.FromObject(value.PackageId, serializer));
}
jo.WriteTo(writer);
}

public override Account ReadJson(JsonReader reader, Type objectType, Account existingValue, bool hasExistingValue, JsonSerializer serializer)
{
string s = (string)reader.Value;
Account acc = new Account();
acc.PackageId = "test";
return acc;
}
}

public class Account
{
public string PackageId { get; set; }
}
}
53 changes: 48 additions & 5 deletions Oxide.Ext.GamingApi/GamingApiExtension.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
namespace Oxide.Ext.GamingApi
// Reference: NewtonsoftAlias.Json
// Reference: NATS.Client
// Reference: RustGameAPI

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using NewtonsoftAlias.Json;
using Oxide.Core;
using Oxide.Core.Extensions;

namespace Oxide.Ext.GamingApi
{
using System;
using System.Reflection;
using Oxide.Core;
using Oxide.Core.Extensions;

public class GamingApiExtension : Extension
{
Expand All @@ -12,6 +20,41 @@ public class GamingApiExtension : Extension
public GamingApiExtension(ExtensionManager manager) : base(manager)
{
Interface.Oxide.LogInfo("[GamingApi Ext] Started");
Interface.Oxide.LogInfo("[GamingApi Ext] Libraries " + manager.GetLibraries().ToString());
Account account = new Account
{
PackageId = "test"
};

string json2 = JsonConvert.SerializeObject(account, Formatting.Indented, new AccountConverter());
// {
// "Email": "[email protected]",
// "Active": true,
// "CreatedDate": "2013-01-20T00:00:00Z",
// "Roles": [
// "User",
// "Admin"
// ]
// }

Interface.Oxide.LogInfo("[GamingApi Ext] Serialized message " + json2);
Player p = new Player() { Name = "Test", Id = "TEST", Address = "123", AdditionalProperties = new Dictionary<string, object>()};
p.AdditionalProperties.Add("TEST", 123);

JsonSerializerSettings settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
ContractResolver = OmitTypeNamesOnDynamicsResolver.Instance,
Formatting = Formatting.Indented,
Converters= new List<JsonConverter> { new PlayerConverter() }
};
var json = JsonConvert.SerializeObject(p, settings);
Interface.Oxide.LogInfo("[GamingApi Ext] Serialized message " + json);

Asyncapi.Nats.Client.Models.Player p2 = new Asyncapi.Nats.Client.Models.Player() { Name = "Test", Id = "TEST", Address = "123", AdditionalProperties = new Dictionary<string, object>() };
p2.AdditionalProperties.Add("TEST", 123);
var json3 = JsonConvert.SerializeObject(p2, settings);
Interface.Oxide.LogInfo("[GamingApi Ext] Serialized message " + json3);
}

////public override bool SupportsReloading => true;
Expand Down
6 changes: 5 additions & 1 deletion Oxide.Ext.GamingApi/GamingApiNats.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using NATS.Client;
// Reference: NewtonsoftAlias.Json
// Reference: NATS.Client
// Reference: RustGameAPI

using NATS.Client;
using System;
using System.Diagnostics;
using System.Threading;
Expand Down
23 changes: 23 additions & 0 deletions Oxide.Ext.GamingApi/OmitTypeNamesOnDynamicsResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Reflection;
using NewtonsoftAlias.Json;
using NewtonsoftAlias.Json.Serialization;

namespace Oxide.Ext.GamingApi
{
public class OmitTypeNamesOnDynamicsResolver : DefaultContractResolver
{

public static readonly OmitTypeNamesOnDynamicsResolver Instance = new OmitTypeNamesOnDynamicsResolver();

protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty prop = base.CreateProperty(member, memberSerialization);
if (member.GetCustomAttribute<System.Runtime.CompilerServices.DynamicAttribute>() != null)
{
prop.TypeNameHandling = TypeNameHandling.None;
}
return prop;
}
}
}
47 changes: 27 additions & 20 deletions Oxide.Ext.GamingApi/Oxide.Ext.GamingApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,49 @@
<AssemblyVersion>0.20.0.0</AssemblyVersion>
<FileVersion>0.20.0.0</FileVersion>
<RepositoryUrl>https://github.com/GamingAPI/umod-rust-server-extension.git</RepositoryUrl>
<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
</PropertyGroup>
<ItemGroup>
<None Remove="NATS.Client"/>
<None Remove="RustPublicAPI"/>
<None Remove="RustGameAPI"/>
<None Remove="NATS.Client" />
<None Remove="RustPublicAPI" />
<None Remove="RustGameAPI" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp"/>
<Reference Include="Microsoft.CSharp" />
<Reference Include="NewtonsoftAlias.Json">
<HintPath>..\..\rust-csharp-game-api\NewtonsoftAlias.Json.dll</HintPath>
</Reference>
<Reference Include="Oxide.Core, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\dependencies\Oxide.Core.dll</HintPath>
</Reference>
<Reference Include="Oxide.References">
<HintPath>..\dependencies\Oxide.References.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>..\dependencies\NewtonsoftAlias.Json</HintPath>
<Aliases>NewtonsoftAlias</Aliases>
</Reference>
<Reference Include="Oxide.Rust">
<HintPath>..\dependencies\Oxide.Rust.dll</HintPath>
</Reference>
<Reference Include="Oxide.Unity">
<HintPath>..\dependencies\Oxide.Unity.dll</HintPath>
</Reference>
<Reference Include="RestSharp">
<HintPath>..\dependencies\RestSharp.dll</HintPath>
</Reference>
<Reference Include="Rust.UI">
<HintPath>..\dependencies\Rust.UI.dll</HintPath>
</Reference>
<PackageReference Include="NATS.Client" Version="1.0.2"/>
<Reference Include="System"/>
<Reference Include="System.Configuration"/>
<Reference Include="System.Configuration.Install"/>
<Reference Include="System.Data"/>
<Reference Include="System.Drawing"/>
<Reference Include="System.Numerics"/>
<Reference Include="System.Runtime.Serialization"/>
<Reference Include="System.Security"/>
<Reference Include="System.Transactions"/>
<Reference Include="System.Xml"/>
<Reference Include="System.Xml.Linq"/>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Configuration.Install" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Numerics" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Security" />
<Reference Include="System.Transactions" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="Unity.TextMeshPro">
<HintPath>..\dependencies\Unity.TextMeshPro.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -227,6 +234,6 @@
<Reference Include="UnityEngine.XRModule">
<HintPath>..\dependencies\UnityEngine.XRModule.dll</HintPath>
</Reference>
<PackageReference Include="RustGameAPI" Version="0.14.0"/>
<PackageReference Include="RustGameAPI" Version="0.16.0" />
</ItemGroup>
</Project>
89 changes: 89 additions & 0 deletions Oxide.Ext.GamingApi/Player.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Reference: NewtonsoftAlias.Json

using System.Collections.Generic;
using NewtonsoftAlias.Json;
using NewtonsoftAlias.Json.Linq;
using System.Linq;
using System.Reflection;

namespace Oxide.Ext.GamingApi
{

[JsonConverter(typeof(PlayerConverter))]
public class Player
{
private string id;
private string name;
private string address;
private Dictionary<string, dynamic> additionalProperties;

public string Id
{
get { return id; }
set { id = value; }
}

public string Name
{
get { return name; }
set { name = value; }
}

public string Address
{
get { return address; }
set { address = value; }
}

public Dictionary<string, object> AdditionalProperties
{
get { return additionalProperties; }
set { additionalProperties = value; }
}
}

public class PlayerConverter : JsonConverter<Player>
{
public override Player ReadJson(JsonReader reader, System.Type objectType, Player existingValue, bool hasExistingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);
Player value = new Player();
return value;
}
public override void WriteJson(JsonWriter writer, Player value, JsonSerializer serializer)
{
JObject jo = new JObject();
if (value.Id != null)
{
jo.Add("id", JToken.FromObject(value.Id, serializer));
}
if (value.Name != null)
{
jo.Add("name", JToken.FromObject(value.Name, serializer));
}
if (value.Address != null)
{
jo.Add("address", JToken.FromObject(value.Address, serializer));
}
if (value.AdditionalProperties != null)
{
for (int index = 0; index < value.AdditionalProperties.Count; index++)
{
KeyValuePair<string, object> unwrapProperty = value.AdditionalProperties.ElementAt(index);
if (unwrapProperty.Key != null && unwrapProperty.Value != null)
{
if (!jo.ContainsKey(unwrapProperty.Key))
{
jo.Add(unwrapProperty.Key, JToken.FromObject(unwrapProperty.Value, serializer));
}
}
}
}

jo.WriteTo(writer);
}

public override bool CanRead => true;
public override bool CanWrite => true;
}
}

0 comments on commit 121269a

Please sign in to comment.