-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathModLoader.cs
More file actions
207 lines (194 loc) · 7.71 KB
/
ModLoader.cs
File metadata and controls
207 lines (194 loc) · 7.71 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
using System.Collections.Generic;
using ModShardLauncher.Mods;
using System.Linq;
using System.Windows;
using UndertaleModLib;
using System.Diagnostics;
using UndertaleModLib.Decompiler;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Reflection;
using UndertaleModLib.Models;
using ModShardLauncher.Controls;
using Serilog;
using ModShardLauncher.Core.Errors;
namespace ModShardLauncher
{
public static class ModLoader
{
internal static UndertaleData Data => DataLoader.data;
public static string ModPath => Path.Join(Environment.CurrentDirectory, "Mods");
public static string ModSourcesPath => Path.Join(Environment.CurrentDirectory, "ModSources");
private static List<Menu> Menus = new();
public static List<string> Weapons = new();
public static List<string> WeaponDescriptions = new();
private static List<(string, string[])> Credits = new();
private static List<(string, UndertaleRoom.GameObject)> Disclaimers = new();
public static Dictionary<string, Action<string>> ScriptCallbacks = new Dictionary<string, Action<string>>();
public static void ShowMessage(string msg)
{
Trace.Write(msg);
}
public static void Initalize()
{
Weapons = Msl.ThrowIfNull(GetTable("gml_GlobalScript_table_weapons"));
WeaponDescriptions = Msl.ThrowIfNull(GetTable("gml_GlobalScript_table_equipment"));
}
internal static void AddCredit(string modNameShort, string[] authors)
{
Credits.Add((modNameShort, authors));
}
internal static void AddDisclaimer(string modNameShort, UndertaleRoom.GameObject overlay)
{
Disclaimers.Add((modNameShort, overlay));
}
public static void AddMenu(string name, params UIComponent[] components)
{
Menus.Add(new Menu(name, components));
}
public static List<string>? GetTable(string name)
{
try
{
UndertaleCode table = Data.Code.First(t => t.Name.Content == name);
return table.Instructions
.Where(i => AssemblyWrapper.IsPushString(i))
.Select(i => (i.Value as UndertaleResourceById<UndertaleString, UndertaleChunkSTRG>)!.Resource.Content)
.Reverse()
.ToList();
}
catch(Exception ex)
{
Log.Error(ex, "Something went wrong");
throw;
}
}
public static void SetTable(List<string> table, string name)
{
string ret = JsonConvert.SerializeObject(table).Replace("\n", "");
UndertaleCode target = Data.Code.First(t => t.Name.Content == name);
GlobalDecompileContext context = new(Data, false);
string text = Decompiler.Decompile(target, context);
text = Regex.Replace(text, "\\[.*\\]", ret);
target.ReplaceGML(text, Data);
Log.Information("Successfully set table: {0}", name);
}
public static void LoadFiles()
{
List<ModFile> mods = Main.Instance.ModPage.Mods;
List<ModSource> modSources = Main.Instance.ModSourcePage.ModSources;
foreach(ModFile i in mods)
i.Stream?.Close();
List<ModFile> modCaches = new();
modSources.Clear();
// List all folders being a C# project
// Currently only test the existence of a .csproj file
// TODO: test framework
// TODO: test inclusion of ModShardLauncher as a reference
IEnumerable<string> sources = Directory
.GetDirectories(ModSourcesPath)
.Where(
x => Directory
.EnumerateFiles(x, "*.csproj", SearchOption.TopDirectoryOnly)
.FirstOrDefault()
!= null
);
foreach(string source in sources)
{
ModSource info = new()
{
Name = source.Split("\\")[^1],
Path = source
};
modSources.Add(info);
}
string[] files = Directory.GetFiles(ModPath, "*.sml");
foreach (string file in files)
{
ModFile? f = null;
try
{
f = FileReader.Read(file);
}
catch(Exception ex)
{
Log.Information(ex, "Cannot read the mod {0}", file);
}
if (f == null) continue;
Assembly assembly = f.Assembly;
// for array or list, use the available search method instead of Linq one
// use the Linq ones for IEnumerable
Type? modType = Array.Find(assembly.GetTypes(), t => t.IsSubclassOf(typeof(Mod)));
if (modType == null)
{
// MessageBox.Show("Loading error: " + assembly.GetName().Name + " This Mod need a Mod class");
MessageBox.Show("加载错误: " + assembly.GetName().Name + " 此Mod需要一个Mod类");
continue;
}
else
{
if (Activator.CreateInstance(modType) is not Mod mod) continue;
mod.LoadAssembly();
mod.ModFiles = f;
f.instance = mod;
ModFile? old = mods.Find(t => t.Name == f.Name);
if (old != null) f.Enabled = old.Enabled;
modCaches.Add(f);
}
}
mods.Clear();
modCaches.ForEach(i => {
mods.Add(i);
});
}
public static void PatchMods()
{
Credits = new();
Disclaimers = new();
List<ModFile> mods = ModInfos.Instance.Mods;
Menus = new();
foreach (ModFile mod in mods)
{
if (!mod.Enabled) continue;
if (!mod.Existed)
{
Log.Warning("The mod {0} which was located at {1} does not exist anymore.", mod.Name, mod.Path);
continue;
}
Main.Settings.EnabledMods.Add(mod.Name);
mod.PatchStatus = PatchStatus.Patching;
if (mod.Version != Main.Instance.mslVersion)
{
Log.Warning("Mod {{{0}}} was built with msl {{{1}}} which is different from the current msl {{{2}}}", mod.Name, mod.Version, Main.Instance.mslVersion);
}
TextureLoader.LoadTextures(mod);
mod.instance.PatchMod();
mod.PatchStatus = PatchStatus.Success;
Main.LogModStatus(mod);
}
Msl.AddDisclaimerRoom(Credits.Select(x => x.Item1).ToArray(), Credits.SelectMany(x => x.Item2).Distinct().ToArray());
Msl.ChainDisclaimerRooms(Disclaimers);
Msl.CreateMenu(Menus);
}
public static MSLDiagnostic? PatchFile()
{
try
{
// add new msl log function
LogUtils.InjectLog();
PatchMods();
// add the new loot related functions if there is any
LootUtils.InjectLootScripts();
return null;
}
catch (Exception ex)
{
MSLDiagnostic diag = new(ex, Main.Instance.GetFailingMod());
diag.ToLog();
return diag;
}
}
}
}