-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsc2_crowd_control.cs
438 lines (391 loc) · 14.8 KB
/
sc2_crowd_control.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// based on https://github.com/DerMitDemRolfTanzt/fs22-twitchevents/blob/master/crowdcontrol/src/fs22effectpack.cs
/*
MIT License
Copyright (c) 2022 DerMitDemRolfTanzt
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using ConnectorLib;
using CrowdControl.Common;
using ConnectorType = CrowdControl.Common.ConnectorType;
using Log = CrowdControl.Common.Log;
namespace CrowdControl.Games.Packs;
public enum Method
{
// not sure if this enum is needed
StartEffect,
StopEffect,
}
public class SC2Randomizer : PCEffectPack<NullConnector>
{
// First argument is an effect pack index assigned internally by Warp World for official effect packs.
// We can use any integer here since it's ignored for any SDK/ccpak plugin.
public override Game Game { get; } = new(132, "StarCraft 2 Randomizer", "SC2Randomizer", "PC", ConnectorType.NullConnector);
public SC2Randomizer(UserRecord player, Func<CrowdControlBlock, bool> responseHandler, Action<object> statusUpdateHandler)
: base(player, responseHandler, statusUpdateHandler)
{
}
// Unfortunately the XML Assembly is not embedded to the CrowdControl SDK, therefore we need to write and parse XML manually.
protected bool XmlWrite(EffectRequest request, Method method)
{
string parameterItems = "";
if (request.Parameters != null)
{
parameterItems = String.Join(",", request.Parameters);
}
string eventsXml = $@"<?xml version=""1.0"" encoding=""utf-8""?>
<Bank version=""1"">
<Section name=""header"">
<Key name=""version"">
<Value string=""{version}""/>
</Key>
<Key name=""date"">
<Value string=""{request.Stamp}""/>
</Key>
<Key name=""ticker"">
<Value int=""{DateTime.Now.Ticks}""/>
</Key>
</Section>
<Section name=""request"">
<Key name=""code"">
<Value string=""{request.EffectID}""/>
</Key>
<Key name=""FinalCode"">
<Value string=""{FinalCode(request)}""/>
</Key>
<Key name=""method"">
<Value string=""{method}""/>
</Key>
<Key name=""DisplayViewer"">
<Value string=""{request.DisplayViewer}""/>
</Key>
<Key name=""id"">
<Value string=""{request.ID}""/>
</Key>
<Key name=""params"">
<Value string=""{parameterItems}""/>
</Key>
<Key name=""quantity"">
<Value string=""{request.Quantity}""/>
</Key>
<Key name=""duration"">
<Value string=""{request.Duration.TotalSeconds}""/>
</Key>
</Section>
</Bank>
";
File.WriteAllText(xmlPathRequests, eventsXml);
return true;
}
protected string GetXmlSection(string xml, string section)
{
try
{
var match = Regex.Match(xml,
"<Bank version=\".*?\">.*"
+ "<Section name=\"" + section + "\">(.*?)</Section>.*</Bank>",
RegexOptions.Singleline);
if (!match.Success) return null;
return match.Groups[1].Value;
}
catch (Exception e)
{
Log.Message(e.ToString());
return null;
}
}
protected string GetXmlString(string section, string key)
{
try
{
var match = Regex.Match(section,
"<Key name=\"" + key + "\">\\s*"
+ "<Value string=\"([^\"]+)\"/>",
RegexOptions.Singleline);
if (!match.Success) return null;
return match.Groups[1].Value;
}
catch (Exception e)
{
Log.Message(e.ToString());
return null;
}
}
protected string XmlCheckStatus(EffectRequest request, Method method)
{
if (!File.Exists(xmlPathResponses))
{
return "fail";
}
string data = File.ReadAllText(xmlPathResponses);
data = GetXmlSection(data, "responses");
return GetXmlString(data, request.ID.ToString());
}
protected string GetGameStatus()
{
try
{
if (xmlPathResponses == "")
{
fileStatus = "fail";
return fileStatus;
}
if (!File.Exists(xmlPathResponses))
{
fileStatus = "fail";
return fileStatus;
}
var dict = ParseXml(xmlPathResponses);
DateTime t = DateTime.Parse(dict["date"]);
if (t < DateTime.Now.AddHours(-24))
{
fileStatus = "expired";
return fileStatus;
}
fileStatus = dict["status"];
return fileStatus;
}
catch (Exception e)
{
Log.Message("error in GetGameStatus() with " + xmlPathResponses + ": " + e.ToString());
}
fileStatus = "fail";
return fileStatus;
}
protected Dictionary<string, string> ParseXml(string file)
{
var dict = new Dictionary<string, string>();
string data = File.ReadAllText(file);
data = GetXmlSection(data, "header");
dict["date"] = GetXmlString(data, "date");
dict["status"] = GetXmlString(data, "status");
return dict;
}
protected bool FindXmlInPath(string root)
{
string[] files = Directory.GetFiles(root, "CrowdControlResponses.SC2Bank", SearchOption.AllDirectories);
string newest_file = "";
string newest_status = "";
DateTime newest = new DateTime(0);
foreach (string file in files)
{
try
{
var dict = ParseXml(file);
DateTime t = DateTime.Parse(dict["date"]);
Log.Message($"{file} status: {dict["status"]}, date: {t}");
// should it care about status starting?
if (t > newest && dict["status"] != "exited")
{
newest = t;
newest_file = file;
newest_status = dict["status"];
}
}
catch (Exception e)
{
Log.Message("error with " + file + ": " + e.ToString());
}
}
// ignore files older than 24 hours
if (newest > DateTime.Now.AddHours(-24))
{
Log.Message($"found file {newest_file} with date: {newest}, status: {newest_status}");
fileStatus = newest_status;
xmlPathResponses = newest_file;
xmlPathRequests = xmlPathResponses.Replace("CrowdControlResponses.SC2Bank", "CrowdControl.SC2Bank");
return true;
}
return false;
}
protected bool FindXml()
{
// we can use the status and date in the file to determine which one to use
// we could also make the mod delete the bank instead of setting the status to exited?
// search in the Accounts folder first, then the test folder if we don't find it
Log.Message("FindXml");
// check normal paths
try
{
string root = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "StarCraft II/Accounts");
if (FindXmlInPath(root)) return true;
}
catch (Exception e)
{
Log.Message("error with searching user path: " + e.ToString());
}
try
{
string onedrive = (string)Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\OneDrive", "UserFolder", "");
string root = Path.Join(onedrive, "Documents/StarCraft II/Accounts");
if (FindXmlInPath(root)) return true;
}
catch (Exception e)
{
Log.Message("error with searching onedrive path: " + e.ToString());
}
// check dev paths...
try
{
string root = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "StarCraft II/Banks");
if (FindXmlInPath(root)) return true;
}
catch (Exception e)
{
Log.Message("error with searching dev path: " + e.ToString());
}
try
{
string onedrive = (string)Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\OneDrive", "UserFolder", "");
string root = Path.Join(onedrive, "Documents/StarCraft II/Banks");
if (FindXmlInPath(root)) return true;
}
catch (Exception e)
{
Log.Message("error with searching dev onedrive path: " + e.ToString());
}
/* I hope I never need this...
EnumerationOptions e = new EnumerationOptions();
e.IgnoreInaccessible = true;
e.RecurseSubdirectories = true;
e.MatchCasing = MatchCasing.CaseSensitive;
string[] gamePaths = Directory.GetDirectories(@"c:\", "StarCraft II", e);
Log.Message("gamePaths length == " + gamePaths.Length.ToString());
foreach (string gamePath in gamePaths) {
Log.Message(gamePath);
}*/
return false;
}
protected bool XmlWait(EffectRequest request, Method method, int millisecondsTimeout = 1000, int millisecondsCheckInterval = 100)
{
// waiting in here seems to block up the queue of other incoming effects
string status = null;
SpinWait.SpinUntil(() =>
{
Thread.Sleep(millisecondsCheckInterval);
status = XmlCheckStatus(request, method);
return status is not null;
}, millisecondsTimeout);
Log.Message($"XmlWait got status {status}");
if (status is null)
{
DelayEffect(request);
return false;
}
else if (status == "retry")
{
DelayEffect(request);
return false;
}
else if (status != "success")
{
return false;
}
return true;
}
protected bool SendEffect(EffectRequest request, Method method)
{
// don't think I need FindXml() here since we already call it in IsReady()
XmlWrite(request, method);
bool success = XmlWait(request, method);
return success;
}
#region Effect List
public override EffectList Effects
{
get
{
List<Effect> result = new List<Effect>
{
new("Musical Chairs", "musicalchairs"),
new("Black Sheep Wall", "fullvision") { Duration = 60 },
new("Terrible, Terrible Damage", "extradamage") { Duration = 60 },
new("Reduced Damage", "reduceddamage") { Duration = 60 },
new("Slow Game Speed", "slowspeed") { Duration = 60 },
new("Super Game Speed", "superspeed") { Duration = 60 },
new("Max Upgrades", "maxupgrades"),
new("Reset Upgrades", "resetupgrades"),
new("Set Upgrades", "setupgrades") { Quantity = 3 },
///new("Mean Things That Kill", "mean", ItemKind.Folder),
new("Nuke All Town Halls", "nukes") { Category = "Rude Things" },
new("Kill All Workers", "killworkers") { Category = "Rude Things" },
new("Kill All Army", "killarmy") { Category = "Rude Things" },
///new("Resources", "resources", ItemKind.Folder),
new("Give Minerals (x100)", "giveminerals") { Category = "Resources", Quantity = 100 },
new("Give Gas (x100)", "givegas") { Category = "Resources", Quantity = 100 },
new("Take Minerals (x100)", "takeminerals") { Category = "Resources", Quantity = 100 },
new("Take Gas (x100)", "takegas") { Category = "Resources", Quantity = 100 },
new("Raise Supply Limit", "raisesupply") { Category = "Resources", Quantity = 50 },
new("Lower Supply Limit", "lowersupply") { Category = "Resources", Quantity = 50 },
};
return result;
}
}
#endregion
protected override bool IsReady(EffectRequest request)
{
if (lastSearch > DateTime.Now.AddSeconds(-30))
{
// this lazy evaluation might cause issues if the game crashes, so it needs a timer
if (GetGameStatus() == "playing") return true;
}
lastSearch = DateTime.Now;
return FindXml() && fileStatus == "playing";
}
protected override void StartEffect(EffectRequest request)
{
if (!IsReady(request))
{
Log.Message("not ready yet");
Respond(request, EffectStatus.FailTemporary, "Not ready yet");
return;
}
Effect? effect = null;
TryEffect(request,
() => Effects.TryGetValue(request.EffectID, out effect), // "condition"
() => // action
{
try
{
return SendEffect(request, Method.StartEffect);
}
catch (Exception e)
{
Log.Message("error with SendEffect: " + e.ToString());
return false;
}
},
() => Connector.SendMessage($"{request.DisplayViewer} invoked {effect.Name}."), // followUp
null, false, FinalCode(request)
); // TimeSpan retryDelay, bool retryOnFail, string mutex name, TimeSpan? holdMutex = null
}
protected override bool StopEffect(EffectRequest request)
{
return true;
}
protected override void RequestData(DataRequest request) => Respond(request, request.Key, null, false, $"Variable name \"{request.Key}\" not known.");
const bool debug = false;
const string version = "1.2";
string xmlPathRequests = "";
string xmlPathResponses = "";
string fileStatus = "";
DateTime lastSearch = new DateTime(0);
}