-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
379 lines (305 loc) · 15.4 KB
/
Program.cs
File metadata and controls
379 lines (305 loc) · 15.4 KB
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
//@2025 EltanceX
//annularwind@outlook.com
using HarmonyLib;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace GlassLoader;
public class Glass
{
public static VersionInfo CurrentVersion = null;
public static List<ModInstance> ModList = new List<ModInstance>();
public static Harmony GHarmony = new Harmony("com.glass.modloader");
public static int[] GMLVersion = new int[] { 0, 1 };
public static bool ExecuteWithoutPatch = false;
//[STAThread] // 确保 WinForms 运行在单线程单元 (STA) 模式下
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine(@"
________ __ __
/ ____/ /___ ___________/ / ____ ____ _____/ /__ _____
/ / __/ / __ `/ ___/ ___/ / / __ \/ __ `/ __ / _ \/ ___/
/ /_/ / / /_/ (__ |__ ) /___/ /_/ / /_/ / /_/ / __/ /
\____/_/\__,_/____/____/_____/\____/\__,_/\__,_/\___/_/
");
Console.ResetColor();
GLog.Info($"Glass ModLoader: Version {GMLVersion[0]}.{GMLVersion[1]}");
GLog.Info("Supported Survivalcraft Game Version: 2.4.0.0");
GLog.Info("EltanceX [annularwind@outlook.com]");
GLog.Info("");
FileManager.Initialize();
GModsManager.Initialize();
GLog.Display("LogLevel: " + GLog.GLogLevel);
//return;
// WinForms线程
Thread uiThread = new Thread(LoadingForm.CreateForm);
uiThread.SetApartmentState(ApartmentState.STA);
uiThread.Start();
if (FileManager.VersionsDirectories.Count == 0)
{
GLog.Fatal("No Game Versions Found.");
GUtil.ErrorTerminate();
}
CurrentVersion = FileManager.VersionsDirectories[0];
if (GlobalConfig.TargetVesrion != null)
{
var target = FileManager.VersionsDirectories.Where(x => x.directoryInfo.Name == GlobalConfig.TargetVesrion).FirstOrDefault();
if (target == null)
{
GLog.Warn($"Required Version '{GlobalConfig.TargetVesrion}'({FileManager.ConfigPath}) not found in Path:{FileManager.VersionsPath}");
GLog.Warn($"Automatically selecting the FIRST executable version...");
}
else
{
CurrentVersion = target;
}
}
//CurrentVersion = VersionGame;
Assembly game;
Assembly engine;
Assembly gameEntitySystem = null;
try
{
//if (Path.Exists(GamePathAPI))
// game = Assembly.LoadFrom(GamePathAPI);
FileManager.EnterVersionPath(FileManager.VersionsPath + "\\" + CurrentVersion.directoryInfo.Name);
game = Assembly.LoadFrom(CurrentVersion.ExecutableGame.ToString());
CurrentVersion.assembly = game;
GModsManager.ClassAssemblys.Add("Game", game);
engine = Assembly.LoadFrom(FileManager.EnginePath);
GModsManager.ClassAssemblys.Add("Engine", engine);
//var assemblies = AppDomain.CurrentDomain.GetAssemblies();
if (Path.Exists("EntitySystem.Android.Net.dll")) gameEntitySystem = Assembly.LoadFrom("EntitySystem.Android.Net.dll");
if (Path.Exists("EntitySystem.dll")) gameEntitySystem = Assembly.LoadFrom("EntitySystem.dll");
LoadingForm.UpdateLoadingData("Loading Game program...");
}
catch (Exception ex)
{
GLog.Error(ex);
LoadingForm.UpdateLoadingData("Loading Filed: " + ex.Message);
GUtil.ErrorTerminate();
return;
}
LoadingForm.UpdateLoadingData("Getting Game Version...");
//FileVersionInfo fileInfo = FileVersionInfo.GetVersionInfo(GamePathRuntime);
//GameVersion = fileInfo.FileVersion ?? GameVersion;
GLog.Info($"Game Version: {CurrentVersion.AssemblyVersion.FileVersion}");
//加载模组
LoadingForm.UpdateLoadingData("Adding Mods...");
GModsManager.LoadModsToAssembly(FileManager.AbsoluteModsPath, ModList);
GModsManager.ModList = ModList;
GamePatch.GMLLoading();
Type program = game.GetType("Game.Program");
Type engine_Window = engine.GetType("Engine.Window");
Type engine_Storage = engine.GetType("Engine.Storage");
//var a = game.GetTypes().Where(x=>x.Name.Contains(""));
Type gameEntitySystem_Entity = gameEntitySystem.GetType("GameEntitySystem.Entity");
Type gameEntitySystem_Project = gameEntitySystem.GetType("GameEntitySystem.Project");
Type gameEntitySystem_Component = gameEntitySystem.GetType("GameEntitySystem.Component");
GUtil.NullVerification(engine_Window);
GUtil.NullVerification(engine_Storage);
GModsManager.ClassTypes.Add("Game.Program", program);
GModsManager.ClassTypes.Add("Engine.Window", engine_Window);
GModsManager.ClassTypes.Add("GameEntitySystem.Entity", gameEntitySystem_Entity);
GModsManager.ClassTypes.Add("GameEntitySystem.Project", gameEntitySystem_Project);
GModsManager.ClassTypes.Add("GameEntitySystem.Component", gameEntitySystem_Component);
if (program == null || gameEntitySystem_Entity == null || gameEntitySystem_Project == null || gameEntitySystem_Component == null)
{
GLog.Error("Failed to find Game/Engine Entry");
GUtil.ErrorTerminate();
return;
}
MethodInfo GameEntry_Method = program.GetMethod("EntryPoint") ?? program.GetMethod("Main");
if (ExecuteWithoutPatch) goto entry;
MethodInfo GameInit_Method = program.GetRuntimeMethod("Initialize", Type.EmptyTypes) ?? program.GetRuntimeMethods().Where(mi => mi.Name == "Initialize").FirstOrDefault();
//MethodInfo Entity_InteralLoad = gameEntitySystem_Entity.GetRuntimeMethods().Where(x => x.Name == "InternalLoadEntity").FirstOrDefault();
//MethodInfo Project_EntityLoad = gameEntitySystem_Project.GetRuntimeMethods().Where(x => x.Name == "LoadEntities").FirstOrDefault();
MethodInfo Project_EntityLoad = GUtil.GetNonPublicMethod(gameEntitySystem_Project, "LoadEntities");
MethodInfo Project_CreateEntity = GUtil.GetMethodByName(gameEntitySystem_Project, "CreateEntity");
MethodInfo Project_LoadEntities = GUtil.GetMethodByName(gameEntitySystem_Project, "LoadEntities");
ConstructorInfo Project_Ctor = GUtil.GetFirstConstructor(gameEntitySystem_Project);
MethodInfo GetAppDirectory_Method = GUtil.GetNonPublicMethod(engine_Storage, "GetAppDirectory");
MethodInfo GetDataDirectory_Method = GUtil.GetNonPublicMethod(engine_Storage, "GetDataDirectory");
GUtil.NullVerification(GameEntry_Method, true);
GUtil.NullVerification(GameInit_Method);
GUtil.NullVerification(Project_EntityLoad);
GUtil.NullVerification(GetAppDirectory_Method);
GUtil.NullVerification(GetDataDirectory_Method);
GUtil.NullVerification(Project_Ctor);
LoadingForm.UpdateLoadingData("Injecting init method...");
//游戏初始化
GModsManager.TryPatch(GHarmony, GameInit_Method, GUtil.GetMethodByName(typeof(GamePatch), nameof(GamePatch.GameInit)), printError: true);
GModsManager.TryPatch(GHarmony, GetAppDirectory_Method, postfix: GUtil.GetMethodByName(typeof(GamePatch), nameof(GamePatch.HandleGetAppDirectory)), printError: true);
PatchWindowTitle(GHarmony, engine_Window);
LoadingForm.UpdateLoadingData("Injecting Game Entry Hooks...");
//harmony0.Patch(GameEntry_Method, GUtil.GetMethodByName(typeof(GamePatch), "EntryPrefix"));
//游戏入口前缀
GModsManager.TryPatch(GHarmony, GameEntry_Method, GUtil.GetMethodByName(typeof(GamePatch), nameof(GamePatch.EntryPrefix)), printError: true);
LoadingForm.UpdateLoadingData("Injecting Entity Hooks...");
PatchEntityLoad(GHarmony, Project_EntityLoad);
//MethodInfo MainMethod2 = program.GetMethod("EntryPoint");
//初始化日志
PatchInitLog(GHarmony, GameInit_Method);
//Entity构造
PatchEntityCotr(GHarmony, gameEntitySystem_Entity);
//原版资源加载器
PatchContent(GHarmony);
PatchProject(GHarmony, Project_Ctor, Project_CreateEntity, Project_LoadEntities);
LoadingForm.UpdateLoadingData("Finished");
Thread.Sleep(300);
LoadingForm.LoadingFinished();
entry:
GameEntry_Method?.Invoke(null, null);
GLog.Info("Process End.");
//GLog.Info("Press any key to exit.");
//Console.ReadKey();
}
public static void PatchContent(Harmony harmony)
{
Assembly engine = GModsManager.ClassAssemblys["Engine"];
Assembly game = GModsManager.ClassAssemblys["Game"];
if (engine == null) throw new Exception("Engine cannot be null!");
if (game == null) throw new Exception("Game cannot be null!");
Type ContentCache = engine.GetType("Engine.Content.ContentCache");
CurrentVersion.VersionType = ContentCache == null ? VersionTypes.APIEdition : VersionTypes.OriginalEdition;
if (CurrentVersion.VersionType == VersionTypes.APIEdition)
{
GLog.Warn("GML Assets Loader is Only Supported for Origin Edition!");
return;
}
//harmony.Patch()
if (ContentCache != null)
{
GModsManager.ClassTypes.Add("Engine.Content.ContentCache", ContentCache);
Type ContentDescription = ContentCache.GetNestedType("ContentDescription", BindingFlags.NonPublic | BindingFlags.Public);
if (ContentDescription != null) GModsManager.ClassTypes.Add("Engine.Content.ContentCache.ContentDescription", ContentDescription);
MethodInfo RootGetMethod = ContentCache.GetMethod("Get", 0, new Type[] { typeof(string), typeof(bool) });
if (RootGetMethod != null) GModsManager.MethodInfos.Add("Engine.Content.ContentCache.Get[0.string.bool]", RootGetMethod);
Type ContentStream = engine.GetType("Engine.Content.ContentStream");
if (ContentStream != null) GModsManager.ClassTypes.Add("Engine.Content.ContentStream", ContentStream);
//MethodInfo SetContentDescription = GUtil.GetNonPublicMethod(ContentCache, "SetContentDescription");
//if (SetContentDescription != null) GModsManager.MethodInfos.Add("SetContentDescription", SetContentDescription);
//harmony.Patch(
// SetContentDescription,
// prefix: typeof(OfficialEditionPatch).GetMethod("ContentCache_SetContentDescription")
//);
harmony.Patch(
RootGetMethod,
prefix: typeof(OfficialEditionPatch).GetMethod(nameof(OfficialEditionPatch.HandleRootGet))
);
//Type LabelWidget = game.GetType("Game.LabelWidget");
//PropertyInfo LabelWidget_Text = LabelWidget.GetProperty("Text");
//MethodInfo LabelWidget_TextSet = LabelWidget_Text.SetMethod;
//harmony.Patch(
// LabelWidget_TextSet,
// prefix: typeof(OfficialEditionPatch).GetMethod("HandleLabelTextSet")
//);
ContentLoader.Initialize();
}
}
public static void PatchWindowTitle(Harmony harmony, Type engine_Window)
{
MethodInfo EngineWindow_Method = null;
try
{
EngineWindow_Method = engine_Window.GetProperty("TitlePrefix").GetSetMethod();
}
catch (Exception ex)
{
try { EngineWindow_Method = engine_Window.GetProperty("Title").GetSetMethod(); }
catch (Exception ex2) { GLog.Error(ex2); }
}
if (EngineWindow_Method != null)
{
LoadingForm.UpdateLoadingData("Modifying Title Setter...");
//窗口标题
harmony.Patch(EngineWindow_Method, prefix: GUtil.GetMethodByName(typeof(GamePatch), nameof(GamePatch.WindowTitleSet)));
}
}
public static void PatchInitLog(Harmony harmony, MethodInfo GameInit_Method)
{
//Log捕获
try
{
LoadingForm.UpdateLoadingData("Handling Log Type...");
//var harmonyX = new Harmony("com.glass.dynamic.loginformation");
harmony.Patch(
original: GameInit_Method,
transpiler: new HarmonyMethod(typeof(Patch_Log).GetMethod(nameof(Patch_Log.Transpiler)))
);
}
catch (Exception ex) { GLog.Error(ex); }
}
public static void PatchEntityCotr(Harmony harmony, Type gameEntitySystem_Entity)
{
try
{
var entityCtors = gameEntitySystem_Entity.GetConstructors();
if (entityCtors.Length == 0) entityCtors = gameEntitySystem_Entity.GetDeclaredConstructors().ToArray();
//var p = entityCtors[1].GetParameters();
ConstructorInfo entityCtor = entityCtors.Where(item => item.GetParameters().Length == 2).FirstOrDefault();
if (entityCtor == null) throw new Exception("Entity Constructor with 2 paramaters not found.");
LoadingForm.UpdateLoadingData("Handling Log Type...");
harmony.Patch(
original: entityCtor,
transpiler: new HarmonyMethod(typeof(Patch_EntityLoad).GetMethod(nameof(Patch_EntityLoad.Transpiler)))
);
harmony.Patch(
original: entityCtor,
postfix: typeof(GamePatch).GetMethod(nameof(GamePatch.SpreadOnAfterEntityCtor))
);
}
catch (Exception ex) { GLog.Error(ex); }
}
public static void PatchEntityLoad(Harmony harmony, MethodInfo Project_EntityLoad)
{
MethodInfo patchMethodEntityLoad = GUtil.GetMethodByName(typeof(GamePatch), nameof(GamePatch.HandleEntityLoad));
try
{
harmony.Patch(
original: Project_EntityLoad,
postfix: new HarmonyMethod(patchMethodEntityLoad)
);
}
catch (Exception e)
{
harmony.Unpatch(Project_EntityLoad, patchMethodEntityLoad); // 不稳定?
harmony.Patch(
original: Project_EntityLoad,
postfix: typeof(GamePatch).GetMethod(nameof(GamePatch.HandleEntityLoad2))
);
}
}
public static void PatchProject(Harmony harmony, ConstructorInfo Project_Ctor, MethodInfo Project_CreateEntity, MethodInfo Project_LoadEntities)
{
GModsManager.TryPatch(harmony,
original: Project_Ctor,
postfix: typeof(GamePatch).GetMethod(nameof(GamePatch.HandleProjectCtor)),
printError: true
);
GModsManager.TryPatch(harmony,
original: Project_CreateEntity,
postfix: typeof(GamePatch).GetMethod(nameof(GamePatch.HandleCreateEntity)),
printError: true
);
if (CurrentVersion.VersionType == VersionTypes.APIEdition)
{
GModsManager.TryPatch(harmony,
original: Project_LoadEntities,
postfix: typeof(APIPatch).GetMethod(nameof(APIPatch.HandleLoadEntities)),
printError: true
);
}
else
{
GModsManager.TryPatch(harmony,
original: Project_LoadEntities,
postfix: typeof(GamePatch).GetMethod(nameof(GamePatch.HandleLoadEntities)),
printError: true
);
}
}
}