Repository navigation
Expand file tree
/
Copy pathChatPlexService.cs
More file actions
321 lines (278 loc) · 12 KB
/
Copy pathChatPlexService.cs
File metadata and controls
321 lines (278 loc) · 12 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
using CP_SDK.Network;
using CP_SDK.Unity;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using UnityEngine;
namespace CP_SDK
{
/// <summary>
/// ChatPlexService
/// </summary>
public static class ChatPlexService
{
public enum EState
{
Disconnected,
LinkRequest,
LinkWait,
Connecting,
Connected,
Error
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
private static bool m_ThreadCondition = true;
private static Thread m_Thread;
private static EState m_State = EState.Disconnected;
private static WebClientCore m_WebClientCore;
private static JsonRPCClient m_JsonRPCClient;
private static string m_LinkRequestID;
private static string m_LinkCode;
private static string m_LastError;
private static string m_ActiveSubscription;
private static string[] m_UnlockedFeatures;
private static ConcurrentQueue<Action> m_OnTokenReadyQueue = new ConcurrentQueue<Action>();
private static string m_DeviceName;
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
public static EState State => m_State;
public static string Token => ChatPlexServiceConfig.Instance.Token;
public static string LinkCode => m_LinkCode;
public static string LastError => m_LastError;
public static string ActiveSubscription => m_State == EState.Connected ? m_ActiveSubscription : string.Empty;
public static string[] UnlockedFeatures => m_UnlockedFeatures;
public static event Action<EState, EState> StateChanged;
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Init the service
/// </summary>
internal static void Init()
{
m_WebClientCore = new WebClientCore("https://api.chatplex.org/", TimeSpan.FromSeconds(10), false, true);
m_JsonRPCClient = new JsonRPCClient(m_WebClientCore);
m_Thread = new Thread(ThreadRunner);
m_Thread.IsBackground = true;
m_Thread.Start();
m_DeviceName = SystemInfo.deviceName;
}
/// <summary>
/// Release the service
/// </summary>
internal static void Release()
{
m_ThreadCondition = false;
m_Thread.Join();
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Add a callback to be called when the token is ready (call immediatly if ready)
/// </summary>
/// <param name="action">Callback to be caled</param>
public static void OnTokenReady(Action action)
{
if (m_State == EState.Connected)
action();
else
m_OnTokenReadyQueue.Enqueue(action);
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Start linking procedure
/// </summary>
internal static void StartLinking()
{
if (m_State != EState.Disconnected)
return;
ChangeState(EState.LinkRequest);
}
/// <summary>
/// Stop linking procedure
/// </summary>
internal static void StopLinking()
{
if (m_State != EState.LinkRequest && m_State != EState.LinkWait)
return;
ChangeState(EState.Disconnected);
}
/// <summary>
/// Refresh the session
/// </summary>
internal static void Refresh()
{
ChangeState(EState.Disconnected);
}
/// <summary>
/// Disconnect and erase the saved connected application token
/// </summary>
internal static void Disconnect()
{
ChatPlexServiceConfig.Instance.Token = string.Empty;
ChatPlexServiceConfig.Instance.Save();
ChangeState(EState.Disconnected);
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Change state and notify listenners
/// </summary>
/// <param name="newState"></param>
private static void ChangeState(EState newState)
{
var l_OldState = m_State;
m_State = newState;
MTThreadInvoker.EnqueueOnThread(() => StateChanged?.Invoke(l_OldState, newState));
}
/// <summary>
/// Fire on token ready actions
/// </summary>
private static void FireOnTokenReady()
{
while (m_OnTokenReadyQueue.TryDequeue(out var l_Action))
{
try
{
l_Action();
}
catch (Exception l_Exception)
{
ChatPlexSDK.Logger.Error("[CP_SDK][ChatPlexService.FireOnTokenReady] Error:");
ChatPlexSDK.Logger.Error(l_Exception);
}
}
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Thread function
/// </summary>
private static void ThreadRunner()
{
Thread.Sleep(2000);
while (m_ThreadCondition)
{
try
{
if (m_State == EState.Disconnected && !string.IsNullOrEmpty(ChatPlexServiceConfig.Instance.Token))
{
m_WebClientCore.RemoveHeader("Authorization");
ChangeState(EState.Connecting);
var l_Result = m_JsonRPCClient.Request(
"Account_AuthByConnectedApplicationToken",
new JObject()
{
["ConnectedApplicationToken"] = ChatPlexServiceConfig.Instance.Token
}
);
if (IsRPCSuccess(l_Result))
{
OnAuthed(l_Result);
}
else
{
if (l_Result.Result != null && l_Result.Result.ContainsKey("Result") &&
l_Result.Result["Result"].Value<bool>() == false)
{
ChatPlexServiceConfig.Instance.Token = string.Empty;
ChatPlexServiceConfig.Instance.Save();
ChangeState(EState.Disconnected);
}
else
OnError(l_Result);
}
}
else if (m_State == EState.LinkRequest)
{
var l_Result = m_JsonRPCClient.Request(
"ConnectedApplication_CreateLinkRequest",
new JObject()
{
["ApplicationIdentifier"] = ChatPlexSDK.ProductName,
["ApplicationDeviceName"] = m_DeviceName
}
);
if (IsRPCSuccess(l_Result))
{
m_LinkRequestID = l_Result.Result["RequestID"].Value<string>();
m_LinkCode = l_Result.Result["Code"].Value<string>();
ChangeState(EState.LinkWait);
}
else
OnError(l_Result);
}
else if (m_State == EState.LinkWait)
{
var l_Result = m_JsonRPCClient.Request(
"ConnectedApplication_GetLinkRequestStatus",
new JObject()
{
["RequestID"] = m_LinkRequestID,
}
);
if (IsRPCSuccess(l_Result))
{
if (l_Result.Result["ResultToken"].Value<string>() != null)
{
ChatPlexServiceConfig.Instance.Token = l_Result.Result["ResultToken"].Value<string>();
ChatPlexServiceConfig.Instance.Save();
ChangeState(EState.Disconnected);
}
}
else
OnError(l_Result);
}
}
catch (Exception exception)
{
ChatPlexSDK.Logger.Error(exception);
}
if (m_State == EState.LinkWait)
Thread.Sleep(1500);
else
Thread.Sleep(100);
}
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Is RPC call result a success result?
/// </summary>
/// <param name="rpcResult">Result of the RPC command</param>
/// <returns>True if success</returns>
private static bool IsRPCSuccess(JsonRPCResult rpcResult)
{
return rpcResult.Result != null && rpcResult.Result.ContainsKey("Result") && rpcResult.Result["Result"]?.Value<bool>() == true;
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/// <summary>
/// When we are Authed
/// </summary>
/// <param name="rpcResult">Result of the RPC command</param>
private static void OnAuthed(JsonRPCResult rpcResult)
{
m_ActiveSubscription = rpcResult.Result["ActiveSubscription"]?.Value<string>() ?? string.Empty;
m_UnlockedFeatures = (rpcResult.Result["UnlockedFeatures"] as JArray)?.Values<string>()?.ToArray();
m_WebClientCore.SetHeader("Authorization", $"ConnectedApplicationToken {ChatPlexServiceConfig.Instance.Token}");
ChangeState(EState.Connected);
FireOnTokenReady();
}
/// <summary>
/// On error received
/// </summary>
/// <param name="rpcResult">Result of the RPC command</param>
private static void OnError(JsonRPCResult rpcResult)
{
var l_Error = "Unknow server error!" + rpcResult?.RawResponse?.BodyString;
if (rpcResult.Result != null && rpcResult.Result.ContainsKey("Error"))
l_Error = rpcResult.Result["Error"]?.Value<string>();
m_LastError = l_Error;
ChangeState(EState.Error);
}
}
}