-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathLeagueConnection.cs
261 lines (225 loc) · 9.12 KB
/
LeagueConnection.cs
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
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Authentication;
using System.Text;
using System.Threading.Tasks;
using WebSocketSharp;
namespace Conduit
{
/**
* Class that manages a connection with the LCU. This will automatically connect to the League
* client once it starts, and exposes a simple API for listening to requests, making requests
* and lifetime events.
*/
class LeagueConnection
{
private static HttpClient HTTP_CLIENT;
private WebSocket socketConnection;
private Tuple<Process, string, string> processInfo;
private bool connected;
public event Action OnConnected;
public event Action OnDisconnected;
public event Action<OnWebsocketEventArgs> OnWebsocketEvent;
/**
* Returns if this connection is currently connected.
*/
public bool IsConnected => connected;
/**
* Creates a new LeagueConnection instance. This will immediately start trying
* to connect to League.
*/
public LeagueConnection()
{
try
{
HTTP_CLIENT = new HttpClient(new HttpClientHandler()
{
SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls,
ServerCertificateCustomValidationCallback = (a, b, c, d) => true
});
}
catch
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
HTTP_CLIENT = new HttpClient(new HttpClientHandler()
{
ServerCertificateCustomValidationCallback = (a, b, c, d) => true
});
}
// Run after a slight delay.
Task.Delay(2000).ContinueWith(e => TryConnectOrRetry());
}
/**
* Clears all listeners for websocket events, to ensure that they can get garbage collected.
*/
public void ClearAllListeners()
{
OnWebsocketEvent = null;
}
/**
* Tries to connect to a currently running league process. Called
* by the connection timer every 5 seconds.
*/
private void TryConnect()
{
try
{
// We're already connected.
if (connected) return;
// Check league status, abort if league is not running.
var status = LeagueUtils.GetLeagueStatus();
if (status == null) return;
// Set the password and base address for our httpclient so we don't have to specify it every time.
var byteArray = Encoding.ASCII.GetBytes("riot:" + status.Item2);
HTTP_CLIENT.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
// Connect to our websocket.
socketConnection = new WebSocket("wss://127.0.0.1:" + status.Item3 + "/", "wamp");
socketConnection.SetCredentials("riot", status.Item2, true);
socketConnection.SslConfiguration.EnabledSslProtocols = SslProtocols.Tls12;
socketConnection.SslConfiguration.ServerCertificateValidationCallback = (a, b, c, d) => true;
socketConnection.OnMessage += HandleMessage;
socketConnection.OnClose += HandleDisconnect;
socketConnection.Connect();
// Subscribe to Json API events from the LCU.
socketConnection.Send("[5,\"OnJsonApiEvent\"]");
// Update local state.
processInfo = status;
connected = true;
// Emit our events.
OnConnected?.Invoke();
}
catch (Exception e)
{
processInfo = null;
connected = false;
DebugLogger.Global.WriteError($"Exception occurred trying to connect to League of Legends: {e.ToString()}");
}
}
/**
* Wrapper around TryConnect that will retry after a fixed delay if the connection fails.
*/
private void TryConnectOrRetry()
{
if (connected) return;
TryConnect();
// Call this function again 2s later.
Task.Delay(2000).ContinueWith(a => TryConnectOrRetry());
}
/**
* Called when our websocket connection is closed. Responsible for updating internal state.
*/
private void HandleDisconnect(object sender, CloseEventArgs args)
{
// Update internal state.
processInfo = null;
connected = false;
socketConnection = null;
// Notify observers.
OnDisconnected?.Invoke();
TryConnectOrRetry();
}
/**
* Called when we receive a websocket message. Responsible for invoking event handlers.
*/
private void HandleMessage(object sender, MessageEventArgs args)
{
// Abort if we get an invalid payload.
if (!args.IsText) return;
var payload = SimpleJson.DeserializeObject<JsonArray>(args.Data);
// Abort if this is not a OnJsonApiEvent.
if (payload.Count != 3) return;
if ((long)payload[0] != 8 || !((string)payload[1]).Equals("OnJsonApiEvent")) return;
// Invoke our listeners.
var ev = (dynamic)payload[2];
OnWebsocketEvent?.Invoke(new OnWebsocketEventArgs()
{
Path = ev["uri"],
Type = ev["eventType"],
Data = ev["eventType"] == "Delete" ? null : ev["data"]
});
}
/**
* Functionally similar to Get, but does not attempt to transform the
* result into a JSON object. Instead, it returns the raw bytes.
*/
public async Task<byte[]> GetAsset(string url)
{
if (!connected) throw new InvalidOperationException("Not connected to LCU");
var res = await HTTP_CLIENT.GetAsync("https://127.0.0.1:" + processInfo.Item3 + url);
return await res.Content.ReadAsByteArrayAsync();
}
/**
* Performs a GET request on the specified URL.
*/
public async Task<dynamic> Get(string url)
{
if (!connected) throw new InvalidOperationException("Not connected to LCU");
var res = await HTTP_CLIENT.GetAsync("https://127.0.0.1:" + processInfo.Item3 + url);
var stringContent = await res.Content.ReadAsStringAsync();
if (res.StatusCode == System.Net.HttpStatusCode.NotFound) return null;
return SimpleJson.DeserializeObject(stringContent);
}
/**
* Observes the specified path. The specified handler gets invoked with the initial, current value.
* After that, the handler will get invoked whenever the value at the specified URL changes.
*/
public async void Observe(string url, Action<dynamic> handler)
{
// Listen to new websocket events and handle them if they're appropriate.
OnWebsocketEvent += data =>
{
if (data.Path == url) handler(data.Data);
};
// If we are currently connected, initially populate.
// Else, wait for the connect and then initially populate.
if (connected)
{
// We are currently connected, handle it immediately.
handler(await Get(url));
}
else
{
Action connectHandler = null;
connectHandler = async () =>
{
OnConnected -= connectHandler;
handler(await Get(url));
};
// Wait for the next connected event.
OnConnected += connectHandler;
}
}
/**
* Performs a request with the specified method on the specified URL with the specified body.
*/
public Task<HttpResponseMessage> Request(string method, string url, string body)
{
if (!connected) throw new InvalidOperationException("Not connected to LCU");
return HTTP_CLIENT.SendAsync(new HttpRequestMessage(new HttpMethod(method), "https://127.0.0.1:" + processInfo.Item3 + url)
{
Content = body == null ? null : new StringContent(body, Encoding.UTF8, "application/json")
});
}
}
/**
* Represents an OnJsonApiEvent sent by the websocket API.
*/
public class OnWebsocketEventArgs : EventArgs
{
/**
* The path that was modified.
*/
public string Path { get; set; }
/**
* The type of the modification (Create/Update/Delete).
*/
public string Type { get; set; }
/**
* The payload of the event. May be null.
*/
public dynamic Data { get; set; }
}
}