forked from phorcys/Taiwu_mods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cs
419 lines (374 loc) · 14 KB
/
Main.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
using Harmony12;
using System.IO;
using System.Linq;
using System.Reflection;
using System;
using UnityEngine;
using UnityModManagerNet;
namespace Sth4nothing.SLManager
{
public class Settings : UnityModManager.ModSettings
{
public override void Save(UnityModManager.ModEntry modEntry)
{
Save(this, modEntry);
}
public bool blockAutoSave = false;
public int maxBackupToLoad = 8;
public int maxBackupsToKeep = 1000;
public bool enableTurboQuickLoadAfterLoad = false;
public bool enableTurboQuickLoadAfterSave = false;
public bool regenerateRandomSeedAfterLoad = false;
}
public class ThreadSafeLogger
{
UnityModManager.ModEntry.ModLogger _baseLogger;
private object _writeLock = new object();
public ThreadSafeLogger(UnityModManager.ModEntry.ModLogger baseLogger)
{
_baseLogger = baseLogger;
}
public virtual void Critical(string str)
{
lock (_writeLock) _baseLogger.Critical(str);
}
public virtual void Error(string str)
{
lock (_writeLock) _baseLogger.Error(str);
}
public virtual void Log(string str)
{
lock (_writeLock) _baseLogger.Log(str);
}
public virtual void Warning(string str)
{
lock (_writeLock) _baseLogger.Warning(str);
}
}
public static class Main
{
public static bool Enabled { get; private set; }
public static bool ForceSave = false;
public static bool isBackuping = false;
private static string logPath;
public static readonly string[] Off_And_On = {"关闭", "启用"};
public static Settings settings;
public static ThreadSafeLogger Logger;
public static bool Load(UnityModManager.ModEntry modEntry)
{
Logger = new ThreadSafeLogger(modEntry.Logger);
try
{
var userProfile = System.Environment.GetEnvironmentVariable("USERPROFILE");
logPath = Path.Combine(userProfile,
@"AppData\LocalLow\Conch Ship Game\The Scroll Of Taiwu Alpha V1.0\output_log.txt"
);
settings = UnityModManager.ModSettings.Load<Settings>(modEntry);
HarmonyInstance.Create(modEntry.Info.Id).PatchAll(Assembly.GetExecutingAssembly());
}
catch (Exception ex)
{
Logger.Error(ex.ToString());
throw;
}
modEntry.OnToggle = OnToggle;
modEntry.OnGUI = OnGUI;
modEntry.OnSaveGUI = OnSaveGUI;
return true;
}
static void OnSaveGUI(UnityModManager.ModEntry modEntry)
{
settings.Save(modEntry);
}
public static void OnGUI(UnityModManager.ModEntry modEntry)
{
GUILayout.BeginHorizontal();
GUILayout.Label("每个存档槽最大保留备份数量(0-1000):");
if (int.TryParse(GUILayout.TextField(settings.maxBackupsToKeep.ToString()),
out int maxBackupsToKeep))
{
if (maxBackupsToKeep <= 1000 && maxBackupsToKeep >= 0)
settings.maxBackupsToKeep = maxBackupsToKeep;
}
GUILayout.Label("读档列表的最大存档数(0表示不受限制)");
if (int.TryParse(GUILayout.TextField(settings.maxBackupToLoad.ToString()),
out int maxBackupToLoad))
{
if (maxBackupToLoad >= 0)
settings.maxBackupToLoad = maxBackupToLoad;
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("禁用游戏换季存档", GUILayout.Width(250));
settings.blockAutoSave = GUILayout.SelectionGrid(settings.blockAutoSave ? 1 : 0,
Off_And_On, 2, GUILayout.Width(150)) == 1;
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("讀檔後重置亂數種子", GUILayout.Width(250));
settings.regenerateRandomSeedAfterLoad = GUILayout.SelectionGrid(settings.regenerateRandomSeedAfterLoad ? 1 : 0,
Off_And_On, 2, GUILayout.Width(150)) == 1;
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
HyperQuickLoad.InitClass.OnGUI(modEntry);
GUILayout.BeginHorizontal();
if (GUILayout.Button("打印log", GUILayout.Width(100)))
{
Log();
}
if (GUILayout.Button("显示log路径", GUILayout.Width(100)))
{
if (logPath != null && File.Exists(logPath))
{
var p = new System.Diagnostics.Process();
p.StartInfo.FileName = "explorer.exe";
p.StartInfo.Arguments = "/e,/select,\"" + logPath + "\"";
p.Start();
}
}
GUILayout.EndHorizontal();
}
public static bool OnToggle(UnityModManager.ModEntry modEntry, bool value)
{
Enabled = value;
return true;
}
/// <summary>
/// 用于调试
/// </summary>
public static void Log()
{
Debug.Log("version: " + MainMenu.instance.gameVersionText.text);
Debug.Log("dateId: " + SaveManager.DateId);
Debug.Log("dirpath: " + SaveManager.SavePath);
Debug.Log("backpath: " + SaveManager.BackPath);
Debug.Log("savedFiles: ");
if (LoadFile.SavedFiles != null)
{
foreach (var file in LoadFile.SavedFiles)
{
Debug.Log("\t" + file);
}
}
Debug.Log("savedInfos: ");
if (LoadFile.SavedInfos != null)
{
foreach (var pair in LoadFile.SavedInfos)
{
Debug.Log("\t" + pair.Key + ": " +
Newtonsoft.Json.JsonConvert.SerializeObject(pair.Value));
}
}
using (var stream = new MemoryStream())
{
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(Settings));
serializer.Serialize(stream, Main.settings);
stream.Seek(0, System.IO.SeekOrigin.Begin);
Debug.Log(System.Text.Encoding.UTF8.GetString(stream.ToArray()));
}
Debug.Log(settings);
}
}
public class ReflectionMethod
{
private const BindingFlags Flags = BindingFlags.Instance
| BindingFlags.Static
| BindingFlags.NonPublic
| BindingFlags.Public;
/// <summary>
/// 反射执行方法
/// </summary>
/// <param name="instance">类实例(静态方法则为null)</param>
/// <param name="method">方法名</param>
/// <param name="args">方法的参数类型列表</param>
/// <typeparam name="T1">类</typeparam>
/// <typeparam name="T2">返回值类型</typeparam>
/// <returns></returns>
public static T2 Invoke<T1, T2>(T1 instance, string method, params object[] args)
{
return (T2) typeof(T1).GetMethod(method, Flags)?.Invoke(instance, args);
}
/// <summary>
/// 反射执行方法 (静态型別)
/// </summary>
/// <param name="instance">类实例(静态方法则为null)</param>
/// <param name="method">方法名</param>
/// <param name="args">方法的参数类型列表</param>
/// <typeparam name="T1">类</typeparam>
/// <typeparam name="T2">返回值类型</typeparam>
/// <returns></returns>
public static T2 Invoke<T2>(Type type, string method, params object[] args)
{
return (T2)type.GetMethod(method, Flags)?.Invoke(null, args);
}
/// <summary>
/// 反射执行方法
/// </summary>
/// <param name="instance">类实例(静态方法则为null)</param>
/// <param name="method">方法名</param>
/// <param name="args">方法的参数类型列表</param>
/// <typeparam name="T1">类</typeparam>
public static void Invoke<T1>(T1 instance, string method, params object[] args)
{
typeof(T1).GetMethod(method, Flags)?.Invoke(instance, args);
}
/// <summary>
/// 反射执行方法
/// </summary>
/// <param name="instance">类实例(静态方法则为null)</param>
/// <param name="method">方法名</param>
/// <param name="argTypes">方法的参数类型列表</param>
/// <param name="args">参数</param>
/// <typeparam name="T">类</typeparam>
/// <returns>函数的返回值(void则返回null)</returns>
public static object Invoke<T>(T instance, string method, System.Type[] argTypes, params object[] args)
{
argTypes = argTypes ?? new System.Type[0];
var methods = typeof(T).GetMethods(Flags).Where(m =>
{
if (m.Name != method)
return false;
return m.GetParameters()
.Select(p => p.ParameterType)
.SequenceEqual(argTypes);
});
if (methods.Count() != 1)
{
throw new AmbiguousMatchException("cannot find method to invoke");
}
return methods.First()?.Invoke(instance, args);
}
/// <summary>
/// 反射获取类字段的值
/// </summary>
/// <param name="instance">类实例(静态字段则为null)</param>
/// <param name="field">字段名</param>
/// <typeparam name="T1">类</typeparam>
/// <typeparam name="T2">返回值类型</typeparam>
/// <returns>字段的值</returns>
public static T2 GetValue<T1, T2>(T1 instance, string field)
{
return (T2) typeof(T1).GetField(field, Flags)?.GetValue(instance);
}
/// <summary>
/// 反射获取类字段的值
/// </summary>
/// <param name="instance">类实例(静态字段则为null)</param>
/// <param name="field">字段名</param>
/// <typeparam name="T">类</typeparam>
/// <returns>字段的值</returns>
public static object GetValue<T>(T instance, string field)
{
return typeof(T).GetField(field, Flags)?.GetValue(instance);
}
/// <summary>
/// 反射设置类字段的值
/// </summary>
/// <param name="instance">类实例(静态字段则为null)</param>
/// <param name="field">字段名</param>
/// <param name="value">设置的字段的值</param>
/// <typeparam name="T">类</typeparam>
public static void SetValue<T>(T instance, string field, object value)
{
typeof(T).GetField(field, Flags)?.SetValue(instance, value);
}
public static void SetProperty<T>(T instance, string property, object value)
{
typeof(T).GetProperty(property, Flags)?.SetValue(instance, value);
}
public static T2 GetProperty<T1, T2>(T1 instance, string property)
{
return (T2) typeof(T1).GetProperty(property, Flags)?.GetValue(instance);
}
}
/// <summary>
/// 修正Ionic.Zip.OffsetStream的错误
/// </summary>
internal class OffsetStream : Stream, IDisposable
{
public OffsetStream(Stream s)
{
this._originalPosition = s.Position;
this._innerStream = s;
}
public override int Read(byte[] buffer, int offset, int count)
{
return this._innerStream.Read(buffer, offset, count);
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
public override bool CanRead
{
get
{
return this._innerStream.CanRead;
}
}
public override bool CanSeek
{
get
{
return this._innerStream.CanSeek;
}
}
public override bool CanWrite
{
get
{
return false;
}
}
public override void Flush()
{
this._innerStream.Flush();
}
/// <summary>
/// error 1
/// </summary>
/// <value></value>
public override long Length
{
get
{
return this._innerStream.Length - this._originalPosition;
}
}
public override long Position
{
get
{
return this._innerStream.Position - this._originalPosition;
}
set
{
this._innerStream.Position = this._originalPosition + value;
}
}
/// <summary>
/// error 2
/// </summary>
/// <param name="offset"></param>
/// <param name="origin"></param>
/// <returns></returns>
public override long Seek(long offset, SeekOrigin origin)
{
return this._innerStream.Seek(offset + (origin == SeekOrigin.Begin ? this._originalPosition : 0), origin) - this._originalPosition;
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
void IDisposable.Dispose()
{
this.Close();
}
public override void Close()
{
base.Close();
}
private readonly long _originalPosition;
private readonly Stream _innerStream;
}
}