-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleJSONMessage.cs
More file actions
67 lines (58 loc) · 2.38 KB
/
Copy pathSimpleJSONMessage.cs
File metadata and controls
67 lines (58 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#if NETSTANDARD2_0_OR_GREATER && !NO_REFLECTION
using System;
using System.IO;
using System.Reflection;
#endif
using System.Threading;
using Newtonsoft.Json;
namespace ConnectorLib.JSON;
/// <summary>Base class for all Crowd Control SimpleJSON messages.</summary>
public abstract class SimpleJSONMessage
{
#if NETSTANDARD2_0_OR_GREATER && !NO_REFLECTION
static SimpleJSONMessage()
{
AppDomain.CurrentDomain.AssemblyResolve += (_, args) =>
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), new AssemblyName(args.Name).Name + ".dll");
return File.Exists(path) ? Assembly.LoadFrom(path) : null;
};
}
#endif
/// <summary>The JSON serializer settings required by the Crowd Control SimpleJSON protocol.</summary>
public static readonly JsonSerializerSettings JSON_SERIALIZER_SETTINGS = new()
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore,
Formatting = Formatting.None
};
/// <summary>The JSON serializer settings required by the Crowd Control SimpleJSON protocol.</summary>
public static readonly JsonSerializer JSON_SERIALIZER = new()
{
NullValueHandling = JSON_SERIALIZER_SETTINGS.NullValueHandling,
MissingMemberHandling = JSON_SERIALIZER_SETTINGS.MissingMemberHandling,
Formatting = JSON_SERIALIZER_SETTINGS.Formatting
};
private static int _next_id = 0;
/// <summary>Gets the next block ID.</summary>
/// <remarks>
/// This field is intended for use by the Crowd Control client.
/// It is not intended for use by game/plugin developers.
/// </remarks>
public static uint NextID
{
get
{
uint id;
// ReSharper disable once EmptyEmbeddedStatement
while ((id = unchecked((uint)Interlocked.Increment(ref _next_id))) == 0) ;
return id;
}
}
/// <summary>The message ID of the message to which this is a response.</summary>
public abstract uint ID { get; }
/// <summary><c>true</c> if this message is a keepalive/heartbeat; otherwise, <c>false</c>.</summary>
public abstract bool IsKeepAlive { get; }
/// <summary>Serializes this message to a JSON string.</summary>
public string Serialize() => JsonConvert.SerializeObject(this, JSON_SERIALIZER_SETTINGS);
}