-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModuleManager.cs
400 lines (338 loc) · 12.5 KB
/
ModuleManager.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
393
394
395
396
397
398
399
400
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using SteamKit2;
using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
using IronPython.Runtime.Operations;
namespace SteamNerd
{
public class ModuleManager
{
public static string ModulesDirectory = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
@"SteamNerd\Modules");
private SteamNerd _steamNerd;
private FileSystemWatcher _watcher;
private ScriptEngine _pyEngine;
private List<string> _localModulePaths;
private Dictionary<string, Module> _globalModules;
private Dictionary<SteamID, Dictionary<string, Module>> _chatroomModules;
private Dictionary<string, Module> _adminModules;
public ModuleManager(SteamNerd steamNerd)
{
_steamNerd = steamNerd;
_pyEngine = Python.CreateEngine();
_localModulePaths = new List<string>();
_globalModules = new Dictionary<string, Module>();
_chatroomModules = new Dictionary<SteamID, Dictionary<string, Module>>();
_adminModules = new Dictionary<string, Module>();
InterpretDirectory();
_watcher = new FileSystemWatcher(ModulesDirectory);
_watcher.Filter = "*.py";
_watcher.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite|
NotifyFilters.FileName |
NotifyFilters.DirectoryName;
_watcher.Created += OnCreated;
_watcher.Changed += OnChanged;
_watcher.Deleted += OnDelete;
_watcher.EnableRaisingEvents = true;
}
/// <summary>
/// Prints a Python-esque stack frame.
/// </summary>
/// <param name="e">The Python exception</param>
public static void PrintStackFrame(Exception e)
{
var pyStackFrame = PythonOps.GetDynamicStackFrames(e);
Console.WriteLine("Traceback (most recent call last):");
foreach (var frame in pyStackFrame)
{
Console.WriteLine(" File \"{0}\", line {1}, in {2}", frame.GetFileName(), frame.GetFileLineNumber(), frame.GetMethodName());
}
Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message);
}
/// <summary>
/// Interpret all of the modules in the directory.
/// </summary>
private void InterpretDirectory()
{
foreach (var file in Directory.EnumerateFiles(ModulesDirectory, "*.py"))
{
CreateModule(file);
}
}
private void CreateModule(string file)
{
var module = new Module(_steamNerd, file);
var scope = module.Interpret(_steamNerd, _pyEngine);
if (scope != null && CheckModule(module))
{
AddModule(module);
}
}
/// <summary>
/// Checks the module to see if it has everything that SteamNerd needs
/// for a module.
/// </summary>
/// <param name="module"></param>
/// <returns></returns>
private bool CheckModule(Module module)
{
var fileName = Path.GetFileName(module.Path);
// If it doesn't have a name, it's probably not a module.
if (module.Name == null)
{
return false;
}
return true;
}
public void AddModule(Module module)
{
if (module.Global)
{
_globalModules[module.Name] = module;
// Run start on global modules whenever they're added.
module.OnStart();
}
else
{
_localModulePaths.Add(module.Path);
foreach (var chatroom in _chatroomModules.Keys)
{
var chatModule = AddModuleToChatroom(module.Path, chatroom);
chatModule.OnStart();
}
}
}
public void AddChatroom(SteamID chatroom)
{
_chatroomModules[chatroom] = new Dictionary<string, Module>();
// Add every local module to this chat.
foreach (var path in _localModulePaths)
{
AddModuleToChatroom(path, chatroom);
}
// Run start on each local module.
foreach (var module in _chatroomModules[chatroom].Values)
{
module.OnStart();
}
}
private Module AddModuleToChatroom(string path, SteamID chatroom)
{
var module = new Module(_steamNerd, path);
module.Chatroom = chatroom;
module.Interpret(_steamNerd, _pyEngine);
_chatroomModules[chatroom][module.Name] = module;
return module;
}
/// <summary>
/// Get a module's properties by it's name and chatroom.
/// </summary>
/// <param name="moduleName"></param>
/// <param name="chatroom">
/// The chatroom where the instance of the module exists. If it's null,
/// it searches the global modules.
/// </param>
/// <returns></returns>
public dynamic GetModule(string moduleName, SteamID chatroom = null)
{
// Find in globals.
if (_globalModules.ContainsKey(moduleName))
{
return _globalModules[moduleName].Variables;
}
// Find in locals.
if (chatroom != null)
{
if (_chatroomModules.ContainsKey(chatroom))
{
if (_chatroomModules[chatroom].ContainsKey(moduleName))
{
return _chatroomModules[chatroom][moduleName].Variables;
}
}
}
return null;
}
public dynamic[] GetModules(SteamID chatroom = null)
{
var modules = _globalModules.Select(moduleKV => moduleKV.Value.Variables).ToList();
if (chatroom != null && _chatroomModules.ContainsKey(chatroom))
{
modules.AddRange(_chatroomModules[chatroom].Select(moduleKV => moduleKV.Value.Variables));
}
return modules.ToArray();
}
/// <summary>
/// When a chat message is received, signal the modules. Also checks
/// against every module's commands.
/// </summary>
/// <param name="callback"></param>
/// <param name="args"></param>
public void ChatMessageSent(SteamFriends.ChatMsgCallback callback, string[] args)
{
// Get both the global modules and the modules specific to that chatroom.
var modules = _globalModules.Values.ToList();
modules.AddRange(_chatroomModules[callback.ChatRoomID].Values);
foreach (var module in modules)
{
// Find the command
var command = module.FindCommand(args);
// If FindCommand returned something, execute it.
if (command.HasValue)
{
try
{
command.Value.Callback(callback, args);
}
catch (Exception e)
{
PrintStackFrame(e);
}
}
}
foreach (var module in modules)
{
module.OnChatMessage(callback, args);
}
}
/// <summary>
/// When a private message is received, signal the modules.
/// </summary>
/// <param name="callback"></param>
/// <param name="args"></param>
public void FriendMessageSent(SteamFriends.FriendMsgCallback callback, string[] args)
{
var modules = _globalModules.Values.ToList();
// Get all modules
foreach (var chatKV in _chatroomModules)
{
modules.AddRange(chatKV.Value.Values);
}
foreach (var module in modules)
{
module.OnFriendMessage(callback, args);
var command = module.FindCommand(args);
// If FindCommand returned something, execute it.
if (command.HasValue)
{
try
{
command.Value.Callback(callback, args);
}
catch (Exception e)
{
PrintStackFrame(e);
}
}
}
}
/// <summary>
/// When a chat is entered by the bot, signal the modules.
/// </summary>
/// <param name="callback"></param>
public void SelfEnteredChat(SteamFriends.ChatEnterCallback callback)
{
// Get both the global modules and the modules specific to that chatroom.
var modules = _globalModules.Values.Union(_chatroomModules[callback.ChatID].Values);
foreach (var module in modules)
{
module.OnSelfChatEnter(callback);
}
}
/// <summary>
/// When someone other than the bot enters the chat, signal the
/// modules.
/// </summary>
/// <param name="chatRoom"></param>
/// <param name="user"></param>
/// <param name="callback"></param>
public void EnteredChat(
ChatRoom chatRoom,
User user,
SteamFriends.PersonaStateCallback callback
)
{
foreach (var module in chatRoom.GetAllModules())
{
module.OnChatEnter(callback);
}
}
/// <summary>
/// When someone leaves the chat, signal the modules.
/// </summary>
/// <param name="callback"></param>
public void LeftChat(SteamFriends.ChatMemberInfoCallback callback)
{
// Get both the global modules and the modules specific to that chatroom.
var modules = _globalModules.Values.Union(_chatroomModules[callback.ChatRoomID].Values);
foreach (var module in modules)
{
module.OnChatLeave(callback);
}
}
private void OnCreated(object source, FileSystemEventArgs e)
{
Console.WriteLine("{0} created.", e.Name);
CreateModule(e.FullPath);
}
private void OnChanged(object source, FileSystemEventArgs e)
{
// Make OnChanged "atomic," so we don't get two OnChanged events.
_watcher.EnableRaisingEvents = false;
Console.WriteLine("{0} changed.", e.Name);
var modules = _globalModules.Values.ToList();
// Sleep to avoid reading while text editor is writing (?).
Thread.Sleep(100);
RemoveModule(e.FullPath);
CreateModule(e.FullPath);
_watcher.EnableRaisingEvents = true;
}
private void OnDelete(object source, FileSystemEventArgs e)
{
Console.WriteLine("{0} deleted.", e.Name);
RemoveModule(e.FullPath);
}
private void RemoveModule(string path)
{
// Remove from global modules.
var globalRemoval = new List<string>();
foreach (var module in _globalModules.Values)
{
if (module.Path == path)
{
globalRemoval.Add(module.Name);
}
}
foreach (var name in globalRemoval)
{
_globalModules.Remove(name);
}
// Remove from local modules.
var localRemoval = new List<string>();
foreach (var chatKV in _chatroomModules)
{
// Get the dictionary of modules in this chat
foreach (var moduleKV in chatKV.Value)
{
if (moduleKV.Value.Path == path)
{
localRemoval.Add(moduleKV.Value.Name);
}
}
}
foreach (var chatKV in _chatroomModules)
{
foreach (var name in localRemoval)
{
chatKV.Value.Remove(name);
}
}
}
}
}