forked from LeagueSharp/LeagueSharp.Common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotifications.cs
392 lines (340 loc) · 12.4 KB
/
Notifications.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#region LICENSE
/*
Copyright 2014 - 2014 LeagueSharp
Notifications.cs is part of LeagueSharp.Common.
LeagueSharp.Common is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LeagueSharp.Common is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LeagueSharp.Common. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
#region
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
#endregion
namespace LeagueSharp.Common
{
public class Notifications
{
/// <summary>
/// The notifications list
/// </summary>
private static readonly ConcurrentDictionary<string, INotification> NotificationsList =
new ConcurrentDictionary<string, INotification>();
/// <summary>
/// Initializes static members of the <see cref="Notifications"/> class.
/// </summary>
static Notifications()
{
Game.OnUpdate += Game_OnGameUpdate;
Drawing.OnDraw += Drawing_OnDraw;
Drawing.OnPostReset += Drawing_OnPostReset;
Drawing.OnPreReset += Drawing_OnPreReset;
Game.OnWndProc += Game_OnWndProc;
if (!Directory.Exists(Path))
{
Directory.CreateDirectory(Path);
}
}
/// <summary>
/// Fired when the game recieves a window event.
/// </summary>
/// <param name="args">The <see cref="WndEventArgs"/> instance containing the event data.</param>
private static void Game_OnWndProc(WndEventArgs args)
{
foreach (var notification in NotificationsList)
{
notification.Value.OnWndProc(args);
}
}
/// <summary>
/// Fired when the game is drawn.
/// </summary>
/// <param name="args">The <see cref="EventArgs"/> instance containing the event data.</param>
private static void Drawing_OnDraw(EventArgs args)
{
foreach (var notification in NotificationsList)
{
notification.Value.OnDraw();
}
}
/// <summary>
/// Fired before the DirectX device is reset.
/// </summary>
/// <param name="args">The <see cref="EventArgs"/> instance containing the event data.</param>
private static void Drawing_OnPreReset(EventArgs args)
{
foreach (var notification in NotificationsList)
{
notification.Value.OnPreReset();
}
}
/// <summary>
/// Fired when the DirectX device is reset.
/// </summary>
/// <param name="args">The <see cref="EventArgs"/> instance containing the event data.</param>
private static void Drawing_OnPostReset(EventArgs args)
{
foreach (var notification in NotificationsList)
{
notification.Value.OnPostReset();
}
}
/// <summary>
/// Fired when the game updates.
/// </summary>
/// <param name="args">The <see cref="EventArgs"/> instance containing the event data.</param>
private static void Game_OnGameUpdate(EventArgs args)
{
foreach (var notification in NotificationsList)
{
notification.Value.OnUpdate();
}
}
/// <summary>
/// Adds a notification to the notification list
/// </summary>
/// <param name="notification">Notification Instance</param>
/// <returns>Boolean</returns>
public static bool AddNotification(INotification notification)
{
return (notification != null) && NotificationsList.TryAdd(notification.GetId(), notification);
}
/// <summary>
/// Adds a simple notification to the notification list
/// </summary>
/// <param name="text">Display Text</param>
/// <param name="duration">Duration (-1 for infinite)</param>
/// <param name="dispose">Dispose upon ending</param>
/// <returns>Notification.</returns>
public static Notification AddNotification(string text, int duration = -0x1, bool dispose = true)
{
var notification = new Notification(text, duration, dispose);
NotificationsList.TryAdd(notification.GetId(), notification);
return notification;
}
/// <summary>
/// Removes a notification from the notification list
/// </summary>
/// <param name="notification">Notification Instance</param>
/// <returns>Boolean</returns>
public static bool RemoveNotification(INotification notification)
{
INotification dump;
return NotificationsList.TryRemove(notification.GetId(), out dump);
}
/// <summary>
/// Removes a notification from the notification list
/// </summary>
/// <param name="id">Notification GUID</param>
/// <param name="notification">Notification Instance</param>
/// <returns>Boolean</returns>
public static bool RemoveNotification(string id, out INotification notification)
{
return NotificationsList.TryRemove(id, out notification);
}
/// <summary>
/// Validates if a notification currently exists inside the list.
/// </summary>
/// <param name="notification">Notification Instance</param>
/// <returns>Boolean</returns>
public static bool IsValidNotification(INotification notification)
{
return NotificationsList.ContainsKey(notification.GetId());
}
/// <summary>
/// Validates if a notification currently exists inside the list.
/// </summary>
/// <param name="id">Notification GUID</param>
/// <returns><c>true</c> if the specified identifier is a valid notification; otherwise, <c>false</c>.</returns>
public static bool IsValidNotification(string id)
{
return NotificationsList.ContainsKey(id);
}
/// <summary>
/// Removes a notification from the notification list
/// </summary>
/// <param name="id">Notification GUID</param>
/// <returns>Boolean</returns>
public static bool RemoveNotification(string id)
{
INotification dump;
return NotificationsList.TryRemove(id, out dump);
}
#region Memory
/// <summary>
/// Reserves a location slot for a GUID
/// </summary>
/// <param name="id">GUID</param>
/// <param name="old">Old Slot</param>
/// <returns>FileStream Handler</returns>
public static FileStream Reserve(string id, FileStream old = null)
{
var loc = GetLocation();
if (loc != -0x1)
{
try
{
var path = Path + "\\" + loc + ".lock";
if (!File.Exists(path))
{
var stream = File.Create(path, 0x1, FileOptions.DeleteOnClose);
if (old != null)
{
Free(old);
}
return stream;
}
}
catch (Exception)
{
return null;
}
}
return null;
}
/// <summary>
/// Frees a location slot
/// </summary>
/// <param name="stream">FileStream Handler</param>
/// <returns>Boolean</returns>
public static bool Free(FileStream stream)
{
if (stream != null)
{
stream.Dispose();
stream.Close();
return true;
}
return false;
}
/// <summary>
/// Returns the next free location
/// </summary>
/// <returns>Location</returns>
public static int GetLocation()
{
var files = Directory.GetFiles(Path, "*.lock", SearchOption.TopDirectoryOnly);
if (!files.Any())
{
return 0x55;
}
var array = new List<int>();
foreach (var i in files)
{
try
{
var length = i.IndexOf("Notifications\\", StringComparison.Ordinal) + "Notifications\\".Length;
var str = i.Substring(length, i.Length - length);
var @int = int.Parse(str.Substring(0x0, str.IndexOf('.')));
array.Add(@int);
}
catch (Exception)
{
// ignored
}
}
array.Sort();
// Find a free slot if array does not start from the zero-based location (85)
if (array.Count > 0x0 && array[0] > 0x55)
{
for (var i = 0x55; i < array[0]; i += 0x1E)
{
if (File.Exists(Path + "\\" + (i + 0x1E) + ".lock"))
{
// If slot found, return it as value.
return i;
}
}
}
// Find a free slot between the current locked locations
for (var i = 0x0; i < array.Count - 0x1; ++i)
{
if (array[i] + 0x1E != array[i + 0x1])
{
// Return free slot which was found between current locked locations
return array[i] + 0x1E;
}
}
// Return (last slot + 30) as value
return array[array.Count - 0x1] + 0x1E;
}
/// <summary>
/// Gets the location.
/// </summary>
/// <param name="stream">The stream.</param>
/// <returns>System.Int32.</returns>
public static int GetLocation(FileStream stream)
{
var i = stream.Name;
var length = i.IndexOf("Notifications\\", StringComparison.Ordinal) + "Notifications\\".Length;
var str = i.Substring(length, i.Length - length);
var @int = int.Parse(str.Substring(0x0, str.IndexOf('.')));
return @int;
}
/// <summary>
/// Validates if current position is first in line
/// </summary>
/// <param name="position">Position</param>
/// <returns>Boolean</returns>
public static bool IsFirst(int position)
{
if (position == 0x55)
{
return true;
}
var files = Directory.GetFiles(Path, "*.lock", SearchOption.TopDirectoryOnly);
if (!files.Any())
{
return true;
}
var array = new List<int>();
foreach (var i in files)
{
try
{
var length = i.IndexOf("Notifications\\", StringComparison.Ordinal) + "Notifications\\".Length;
var str = i.Substring(length, i.Length - length);
var @int = int.Parse(str.Substring(0x0, str.IndexOf('.')));
array.Add(@int);
}
catch (Exception)
{
// ignored
}
}
if (array.Count > 0x0)
{
for (var i = position - 0x1E; i > GetLocation(); i -= 0x1E)
{
if (array.Contains(i))
{
return false;
}
}
}
return true;
}
/// <summary>
/// Gets the path.
/// </summary>
/// <value>The path.</value>
private static string Path
{
get
{
return System.IO.Path.Combine(Config.AppDataDirectory, "Notifications");
}
}
#endregion
}
}