-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModsManager.cs
665 lines (619 loc) · 17.7 KB
/
ModsManager.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
using Engine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace Game
{
/// <summary>
/// Mod信息
/// </summary>
[Serializable]
public struct ModInfo : IEquatable<ModInfo>
{
/// <summary>
/// 名称
/// </summary>
public string Name;
/// <summary>
/// 描述
/// </summary>
public string Description;
/// <summary>
/// 版本
/// </summary>
public uint Version;
/// <summary>
/// 适用的sc版本
/// </summary>
public string ScVersion;
/// <summary>
/// url
/// </summary>
public string Url;
/// <summary>
/// 更新链接
/// </summary>
public string UpdateUrl;
/// <summary>
/// 作者名单
/// </summary>
public string AuthorList;
/// <summary>
/// credits
/// </summary>
public string Credits;
/// <summary>
/// logo
/// </summary>
public string Logo;
/// <summary>
/// 截图
/// </summary>
public string Screenshots;
/// <summary>
/// 父集
/// </summary>
public string Parent;
/// <summary>
/// 依赖项
/// </summary>
public string Dependency;
/// <summary>
/// 子集
/// </summary>
public string Dependants;
/// <summary>
/// 使用依赖项信息
/// </summary>
public bool UseDependencyInfo;
/// <summary>
/// 默认构造函数
/// </summary>
/// <param name="name">名称</param>
/// <param name="description">描述</param>
/// <param name="version">版本</param>
/// <param name="scversion">适用的sc版本</param>
/// <param name="url">url</param>
/// <param name="updateUrl">更新链接</param>
/// <param name="authorList">作者名单</param>
/// <param name="credits">credits</param>
/// <param name="logo">logo</param>
/// <param name="screenshots">截图</param>
/// <param name="parent">父集</param>
/// <param name="dependency">依赖项</param>
/// <param name="dependants">子集</param>
/// <param name="usedependencyInfo">使用依赖项信息</param>
public ModInfo(
string name,
string description,
uint version,
string scversion,
string url,
string updateUrl,
string authorList,
string credits,
string logo,
string screenshots,
string parent,
string dependency,
string dependants = null,
bool usedependencyInfo = false)
{
Name = name;
Description = description;
Version = version;
ScVersion = scversion;
Url = url;
UpdateUrl = updateUrl;
AuthorList = authorList;
Credits = credits;
Logo = logo;
Screenshots = screenshots;
Parent = parent;
Dependency = dependency;
Dependants = dependants;
UseDependencyInfo = usedependencyInfo;
}
public override bool Equals(object obj)
{
return obj is ModInfo && ToString() == ((ModInfo)obj).ToString();
}
public bool Equals(ModInfo other)
{
return ToString() == other.ToString();
}
public override int GetHashCode()
{
return ToString().GetHashCode();
}
public override string ToString()
{
return string.Format("{0} {1} - {2} ({3})", new object[]
{
Name,
Version,
Description,
Url
});
}
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class PluginLoaderAttribute : Attribute
{
protected readonly ModInfo info;
/// <summary>
/// <see cref="Game.ModInfo"/>
/// </summary>
public ModInfo ModInfo { get { return info; } }
/// <summary>
/// 完整
/// </summary>
/// <param name="name">名称</param>
/// <param name="description">描述</param>
/// <param name="version">版本</param>
/// <param name="scversion">适用的sc版本</param>
/// <param name="url">url</param>
/// <param name="updateUrl">更新链接</param>
/// <param name="authorList">作者名单</param>
/// <param name="credits">credits</param>
/// <param name="logo">logo</param>
/// <param name="screenshots">截图</param>
/// <param name="parent">父集</param>
/// <param name="dependency">依赖项</param>
/// <param name="dependants">子集</param>
/// <param name="usedependencyInfo">使用依赖项信息</param>
public PluginLoaderAttribute(string name, string description, uint version, string scversion, string url, string updateUrl, string authorList, string credits, string logo, string screenshots, string parent, string dependency = null, string dependants = null, bool usedependencyInfo = false)
{
info = new ModInfo(name, description, version, scversion, url, updateUrl, authorList, credits, logo, screenshots, parent, dependency, dependants, usedependencyInfo);
}
/// <summary>
/// 使用依赖项信息
/// </summary>
/// <param name="name">名称</param>
/// <param name="description">描述</param>
/// <param name="version">版本</param>
public PluginLoaderAttribute(string name, string description, uint version)
{
info.Name = name;
info.Description = description;
info.Version = version;
}
}
/// <summary>
/// 文件条目
/// </summary>
public class FileEntry
{
/// <summary>
/// 文件名
/// </summary>
public string Filename;
/// <summary>
/// 文件流
/// </summary>
public Stream Stream;
}
/// <summary>
/// Mods Manager
/// </summary>
public static class ModsManager
{
private static List<Assembly> loadedAssemblies;
/// <summary>
/// Extension name.
/// </summary>
public static string Extension;
/// <summary>
/// File whose name is in the set won't be loaded.
/// </summary>
public static HashSet<string> DisabledMods;
/// <summary>
/// Cached Mods
/// </summary>
public static HashSet<string> CachedMods;
/// <summary>
/// If
/// </summary>
public static bool ReadZip, AutoCleanCache;
/// <summary>
/// The maximum search pepth, default value is 3.
/// </summary>
public static int SearchDepth;
/// <summary>
/// The
/// </summary>
public static List<string> Files, Directories;
/// <summary>
/// Archives
/// </summary>
public static Dictionary<string, ZipArchive> Archives;
/// <summary>
/// The path of cache directory or <c>null</c> if not used.
/// </summary>
public static string CacheDir;
/// <summary>
/// Error handler
/// </summary>
public static Action<FileEntry, Exception> ErrorHandler;
/// <summary>
/// Call after config saved
/// </summary>
public static Action<StreamWriter> ConfigSaved;
/// <summary>
/// Call before <see cref="Initialize"/> returns.
/// </summary>
public static Action Initialized;
/// <summary>
/// ModInfo of loaded Mod
/// </summary>
public static List<ModInfo> LoadedMods;
/*
/// <summary>
/// Loaded Assemblies
/// </summary>
public static Dictionary<string, AppDomain> LoadedAssemblies;
*/
public static void Initialize()
{
loadedAssemblies = new List<Assembly>();
LoadedMods = new List<ModInfo>();
#if ENV_ANDROID
CachedMods = new HashSet<string>();
#endif
DisabledMods = new HashSet<string>();
Files = new List<string>();
Directories = new List<string>();
LabelWidget.Strings = new Dictionary<string, string>();
ReadZip = true;
AutoCleanCache = true;
SearchDepth = 3;
ErrorHandler = LogException;
try
{
var str = Combine(ContentManager.Path, "mods.cfg");
if (File.Exists(str))
using (var reader = new StreamReader(str))
if (string.Equals(reader.ReadLine(), "Ver 1.1") && (str = reader.ReadLine()) != null)
{
ReadZip = str.Contains(nameof(ReadZip));
AutoCleanCache = str.Contains(nameof(AutoCleanCache));
if (!int.TryParse(reader.ReadLine(), out SearchDepth))
SearchDepth = 3;
do
if ((str = reader.ReadLine()) == null)
goto loadmods;
while (!string.Equals(str, "Disabled:"));
while ((str = reader.ReadLine()) != null && !string.Equals(str, "Last loaded mods:"))
DisabledMods.Add(str);
do
if ((str = reader.ReadLine()) == null)
goto loadmods;
while (!string.Equals(str, "File\tModifyTime"));
var notExist = new DateTime(1601, 1, 1).ToLocalTime();
while ((str = reader.ReadLine()) != null && str.Length != 0)
{
#if ENV_ANDROID
var name = str.
#if Bugs
Substring(0,
#else
Remove(
#endif
str.IndexOf('\t'));
var time = File.GetLastWriteTimeUtc(Combine(ContentManager.Path, name));
if (time != notExist && time.ToString() == str.Substring(str.IndexOf('\t') + 1))
CachedMods.Add(name);
#endif
}
}
loadmods:
EnumerateDirectory(ContentManager.Path, Files, Directories, SearchDepth);
if (ReadZip)
{
Archives = new Dictionary<string, ZipArchive>();
var enumerator2 = GetFiles(".zip").GetEnumerator();
while (enumerator2.MoveNext())
{
str = enumerator2.Current;
Archives.Add(str, ZipArchive.Open(File.OpenRead(str)));
}
}
var enumerator = GetEntries(".dll").GetEnumerator();
while (enumerator.MoveNext())
{
//str = enumerator.Current.Filename;
//var domain = AppDomain.CreateDomain(str);
//Assembly asm;
try
{
#if ENV_ANDROID
//asm = domain.Load(str);
LoadMod(Assembly.LoadFrom(enumerator.Current.Filename, null));
#else
var buf = new byte[enumerator.Current.Stream.Length];
enumerator.Current.Stream.Read(buf, 0, buf.Length);
//asm = domain.Load(buf);
LoadMod(Assembly.Load(buf));
#endif
//LoadMod(asm);
//LoadedAssemblies.Add(str, domain);
}
catch (Exception e)
{
ErrorHandler(enumerator.Current, e);
}
}
}
catch (Exception e)
{
ExceptionManager.ReportExceptionToUser("Initialize failed.", e);
}
Log.Information(string.Format("Found {0} files, loaded {1} dlls ({2} mods)", Files.Count, loadedAssemblies.Count, LoadedMods.Count));
Initialized?.Invoke();
SaveConfig();
#if ENV_ANDROID
if (AutoCleanCache)
CleanCache();
#endif
}
/// <summary>
/// Default error handler
/// </summary>
/// <param name="file">filename</param>
/// <param name="ex">the caught exception</param>
public static void LogException(FileEntry file, Exception ex)
{
Log.Warning("Loading \"" + file.Filename.Substring(ContentManager.Path.Length + 1) + "\" failed: " + ex.ToString());
file.Stream.Close();
}
/// <summary>
/// Load a mod
/// </summary>
/// <param name="asm">Assembly</param>
/// <exception cref="ReflectionTypeLoadException"></exception>
public static void LoadMod(Assembly asm)
{
var attr = typeof(PluginLoaderAttribute);
var types = asm.GetTypes();
for (int i = 0; i < types.Length; i++)
{
var pluginLoaderAttr = (PluginLoaderAttribute)Attribute.GetCustomAttribute(types[i], attr);
if (pluginLoaderAttr != null)
{
MethodInfo m;
if ((m = types[i].GetMethod("Initialize", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)) != null)
m.Invoke(Activator.CreateInstance(types[i]), null);
LoadedMods.Add(pluginLoaderAttr.ModInfo);
}
}
loadedAssemblies.Add(asm);
}
/*
/// <summary>
/// Unload a mod
/// </summary>
/// <param name="name"></param>
/// <exception cref="Exception"></exception>
/// <exception cref="ArgumentNullException"></exception>
public static void Unload(string name)
{
if (LoadedAssemblies.TryGetValue(name, out AppDomain domain))
{
AppDomain.Unload(domain);
LoadedAssemblies.Remove(name);
}
}
*/
/// <summary>
/// Save the config
/// </summary>
public static void SaveConfig()
{
StreamWriter writer = null;
try
{
writer = new StreamWriter(
#if ENV_ANDROID
File.Open(Combine(ContentManager.Path, "mods.cfg"), FileMode.Create)
#else
Combine(ContentManager.Path, "mods.cfg"), false
#endif
);
writer.WriteLine("Ver 1.1");
writer.Write("Flags:");
if (ReadZip) writer.Write(" " + nameof(ReadZip));
if (AutoCleanCache) writer.Write(" " + nameof(AutoCleanCache));
writer.WriteLine();
writer.WriteLine("SearchDepth:" + SearchDepth.ToString());
writer.WriteLine("Disabled:");
for (var enumerator = DisabledMods.GetEnumerator(); enumerator.MoveNext();)
writer.WriteLine(enumerator.Current);
writer.WriteLine("Last loaded mods:");
writer.WriteLine("File\tModifyTime");
#if ENV_ANDROID
for (var enumerator = loadedAssemblies.GetEnumerator(); enumerator.MoveNext();)
writer.WriteLine(enumerator.Current.Location.Substring(ContentManager.Path.Length + 1) + '\t' + File.GetLastWriteTimeUtc(enumerator.Current.Location));
#endif
ConfigSaved?.Invoke(writer);
}
catch (Exception e)
{
Log.Warning("Saving config failed: " + e);
}
finally
{
writer?.Close();
}
}
/// <summary>
/// Enumerate the directory
/// </summary>
/// <param name="dirName"></param>
/// <param name="files"></param>
/// <param name="directories"></param>
/// <param name="searchDepth">The maximum search pepth</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="IOException"></exception>
/// <exception cref="UnauthorizedAccessException"></exception>
public static void EnumerateDirectory(string dirName, ICollection<string> files = null, ICollection<string> directories = null, int searchDepth = int.MaxValue)
{
var q = new Queue<KeyValuePair<string, int>>();
q.Enqueue(new KeyValuePair<string, int>(dirName, 0));
try
{
while (q.Count > 0)
{
var t = q.Dequeue();
if (t.Value < searchDepth)
{
IEnumerator<string> i;
for (i = Directory.EnumerateDirectories(t.Key).GetEnumerator(); i.MoveNext();)
{
q.Enqueue(new KeyValuePair<string, int>(i.Current, t.Value + 1));
directories?.Add(i.Current);
}
if (files != null)
for (i = Directory.EnumerateFiles(t.Key).GetEnumerator(); i.MoveNext();)
files.Add(i.Current);
}
}
}
catch (Exception ex)
{
Log.Error("Error enumerating files/directories: " + ex);
}
}
/// <summary>
/// Delete the cache directory if exists
/// </summary>
public static void CleanCache()
{
#if ENV_ANDROID
if (!Directory.Exists(CacheDir))
return;
var files = new List<string>();
var directories = new List<string>();
EnumerateDirectory(CacheDir, files, directories, SearchDepth);
var enumerator = files.GetEnumerator();
try
{
while (enumerator.MoveNext())
File.Delete(enumerator.Current);
enumerator = directories.GetEnumerator();
while (enumerator.MoveNext())
Directory.Delete(enumerator.Current);
Directory.Delete(CacheDir);
}
catch (Exception e)
{
Log.Error("Error deleting " + enumerator.Current + ": " + e.ToString());
}
#endif
}
/// <summary>
/// Is Target File
/// </summary>
/// <param name="name">filename</param>
/// <returns><c>True</c> if the extension name matches target extension, otherwise <c>False</c>.</returns>
/// <exception cref="NullReferenceException"></exception>
public static bool IsTargetFile(string name)
{
return Storage.GetExtension(name).Equals(Extension, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Get files with specified extension name
/// </summary>
/// <param name="ext">Extension name</param>
/// <param name="files"></param>
/// <returns>Files with specified extension name</returns>
public static IEnumerable<string> GetFiles(string ext, IEnumerable<string> files = null)
{
Extension = ext;
return (files ?? Files).Where(IsTargetFile);
}
/// <summary>
/// Combine two paths quickly without checking
/// </summary>
/// <param name="path1"></param>
/// <param name="path2"></param>
/// <returns>The combined path</returns>
/// <exception cref="NullReferenceException"></exception>
public static string Combine(string path1, string path2)
{
var c = path1[path1.Length - 1];
return path1 + (c != Path.DirectorySeparatorChar && c != Path.AltDirectorySeparatorChar && c != Path.VolumeSeparatorChar
? char.ToString(Path.DirectorySeparatorChar) + path2
: path2);
}
/// <summary>
/// Get all entries with specified extension name.
/// </summary>
/// <param name="ext">extension name</param>
/// <returns>an empty list if <paramref name="ext"/> is null</returns>
/// <exception cref="IOException"></exception>
/// <exception cref="UnauthorizedAccessException"></exception>
public static List<FileEntry> GetEntries(string ext)
{
var list = new List<FileEntry>();
FileEntry entry;
if (ReadZip)
{
Extension = ext;
#if ENV_ANDROID
bool extract = ".dll".Equals(ext, StringComparison.OrdinalIgnoreCase);
#endif
//Extension = ext;
var enumerator = Archives.GetEnumerator();
while (enumerator.MoveNext())
{
var zipArchive = enumerator.Current.Value;
var enumerator2 = zipArchive.ReadCentralDir().GetEnumerator();
while (enumerator2.MoveNext())
{
ext = enumerator2.Current.FilenameInZip;
if (IsTargetFile(ext) && !DisabledMods.Contains(Storage.GetFileName(ext)))
{
entry = new FileEntry();
Stream stream;
#if ENV_ANDROID
if (extract)
{
if (CacheDir == null)
Directory.CreateDirectory(CacheDir = Combine(ContentManager.Path, "Cache"));
stream = File.Open(entry.Filename = Combine(CacheDir, ext), FileMode.OpenOrCreate);
CachedMods.Add(ext);
}
else
#endif
{
stream = new MemoryStream();
entry.Filename = Combine(enumerator.Current.Key, ext);
}
zipArchive.ExtractFile(enumerator2.Current, stream);
stream.Position = 0L;
entry.Stream = stream;
list.Add(entry);
}
}
}
}
var enumerator3 = GetFiles(Extension).GetEnumerator();
while (enumerator3.MoveNext())
{
ext = enumerator3.Current;
#if ENV_ANDROID
if (CachedMods.Contains(Storage.GetFileName(ext))) continue;
#endif
entry = new FileEntry();
entry.Filename = ext;
entry.Stream = File.OpenRead(ext);
list.Add(entry);
}
return list;
}
}
}