-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileManager.cs
More file actions
338 lines (314 loc) · 13.1 KB
/
FileManager.cs
File metadata and controls
338 lines (314 loc) · 13.1 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
using GlassLoader;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace GlassLoader
{
public enum VersionTypes
{
Unknown,
OriginalEdition,
APIEdition
}
public class VersionInfo
{
public DirectoryInfo directoryInfo;
public FileInfo fileInfo;
public FileInfo ExecutableGame;
public FileVersionInfo AssemblyVersion;
public VersionTypes VersionType = VersionTypes.Unknown;
public Assembly assembly;
public VersionInfo(DirectoryInfo directoryInfo)
{
this.directoryInfo = directoryInfo;
bool dllExists = Path.Exists(directoryInfo + "\\Survivalcraft.dll");
bool exeExists = Path.Exists(directoryInfo + "\\Survivalcraft.exe");
if (!dllExists && !exeExists)
{
throw new Exception($"Game Executable File Not Found! [{directoryInfo.Name}].\n Please check Naming Conventions: Survivalcraft.exe / Survivalcraft.dll");
}
string fileName = "Survivalcraft" + (dllExists ? ".dll" : ".exe");
fileInfo = new FileInfo(directoryInfo + "\\" + fileName);
if (!fileInfo.Exists)
{
throw new Exception($"Game Executable File Not Found! [{fileInfo.Name}].\n Please check Naming Conventions: Survivalcraft.exe / Survivalcraft.dll");
}
ExecutableGame = fileInfo;
FileVersionInfo fileVerInfo = FileVersionInfo.GetVersionInfo(fileInfo.ToString());
AssemblyVersion = fileVerInfo;
}
}
public class GlobalConfig
{
public static string TargetVesrion = null;
public static int LogLevel = 0;
}
public class FileManager
{
public static string CurrentDirectory = "";
public static string DefaultDirectory = "";
public static List<VersionInfo> VersionsDirectories = new List<VersionInfo>();
public static string VersionsPath = "versions";
public static string AssetsPath = "GML\\Assets";
public static string ModAssetsPath = "ModAssets";
public static string ModsPath = "GML\\Mods";
public static string TempPath = "GML\\Temp";
public static string GMLLogoPath = "GML\\Assets\\GML_LOGO.png";
public static string GMLIconPath = "GML\\Assets\\SurvivalcraftGML.ico";
public static string EnginePath = "Engine.dll";
public static string ConfigPath = "GMLConfig.json";
public static string AbsoluteModsPath { get { return DefaultDirectory + "\\" + ModsPath; } }
public static string AbsoluteAssetsPath { get { return DefaultDirectory + "\\" + AssetsPath; } }
public static string AbsoluteGMLIconPath { get { return DefaultDirectory + "\\" + GMLIconPath; } }
public static string AbsoluteGMLLogoPath { get { return DefaultDirectory + "\\" + GMLLogoPath; } }
public static string GMLPath(string relativePath)
{
return DefaultDirectory + "\\" + relativePath;
}
public static void Initialize()
{
FileManager.RecoverFiles();
bool isAssetsIntegrity = FileManager.CheckAssetsIntegrity();
CurrentDirectory = Directory.GetCurrentDirectory();
DefaultDirectory = CurrentDirectory;
new DirectoryInfo($"{CurrentDirectory}\\{VersionsPath}").Create();
LoadConfig();
GetVersions(VersionsDirectories);
}
public static Dictionary<string, List<FileInfo>> GetAssetsPath(ModInstance mod)
{
var Assets = new Dictionary<string, List<FileInfo>>();
string assetsPath = Path.Combine(AbsoluteModsPath, mod.ModDirectory.Name, ModAssetsPath);
DirectoryInfo AssetsDir = new DirectoryInfo(assetsPath);
if (!AssetsDir.Exists) return Assets;
void GetPath(DirectoryInfo dir, string FormatPath = "")
{
var files = dir.GetFiles();
var directories = dir.GetDirectories();
foreach (var file in files)
{
string AssetsPath = FormatPath + file.Name.Substring(0, file.Name.Length - file.Extension.Length);
if (Assets.Keys.Contains(AssetsPath))
{
Assets[AssetsPath].Add(file);
continue;
}
Assets.Add(AssetsPath, new List<FileInfo>() { file });
}
foreach (var ChildDirectory in directories)
{
GetPath(ChildDirectory, FormatPath == string.Empty ? ChildDirectory.Name + "/" : FormatPath + ChildDirectory.Name + "/");
}
}
GetPath(AssetsDir);
return Assets;
}
/// <summary>
/// 获取模组配置
/// </summary>
/// <param name="modDirectory">模组文件夹</param>
/// <returns>无文件或格式错误返回null</returns>
public static ModConfig? LoadModConfig(DirectoryInfo modDirectory)
{
string manifestPath = modDirectory.FullName + "\\" + "manifest.json";
var modConfig = new ModConfig();
if (!File.Exists(manifestPath))
{
GLog.Warn($"Mod {modDirectory.Name} Doesn't contain any manifest file !");
return null;
}
string originJsonText = File.ReadAllText(manifestPath);
string JsonText = GUtil.RemoveJsonComments(originJsonText);
JsonDocument json = null;
JsonElement root;
try
{
json = JsonDocument.Parse(JsonText);
root = json.RootElement;
}
catch (Exception e)
{
GLog.Error(e);
GLog.Error("Incorrect Manifest JSON Format !");
return null;
}
try
{
modConfig.Priority = root.GetProperty("Priority").GetInt32();
}
catch (Exception e) { GLog.Warn($"Item Priority Not Found in manifest.json[{modDirectory.Name}]!"); }
try
{
JsonElement TargetGameVersion = root.GetProperty("TargetGameVersion");
string? Version = TargetGameVersion.GetProperty("Version").GetString();
bool StrictMatch = TargetGameVersion.GetProperty("StrictMatch").GetBoolean();
modConfig.TargetGameVersion = Version;
modConfig.GameVersionStrictMode = StrictMatch;
}
catch(Exception e) { }
try
{
JsonElement TargetGameDirectory = root.GetProperty("TargetGameDirectory");
string? Name = TargetGameDirectory.GetProperty("Name").GetString();
bool StrictMatch = TargetGameDirectory.GetProperty("StrictMatch").GetBoolean();
modConfig.TargetGameDirectory = Name;
modConfig.GameDirectoryStrictMode = StrictMatch;
}
catch (Exception e) { }
try
{
modConfig.Author = root.GetProperty("Author").GetString();
}
catch (Exception e) { }
try
{
JsonElement RuntimeCompilation = root.GetProperty("RuntimeCompilation");
bool RuntimeCompilationEnabled = RuntimeCompilation.GetProperty("enabled").GetBoolean();
if (RuntimeCompilationEnabled)
{
JsonElement.ArrayEnumerator dependencies = RuntimeCompilation.GetProperty("dependencies").EnumerateArray();
List<string> Dependencies = new List<string>();
List<string> DependenciesShared = new List<string>();
foreach (var dependency in dependencies)
{
Dependencies.Add(dependency.GetString());
}
JsonElement.ArrayEnumerator dependencies_shared = RuntimeCompilation.GetProperty("dependencies_shared").EnumerateArray();
foreach (var dependency_shared in dependencies_shared)
{
DependenciesShared.Add(dependency_shared.GetString());
}
modConfig.Dependencies = Dependencies;
modConfig.DependenciesShared = DependenciesShared;
modConfig.RuntimeCompileEnabled = RuntimeCompilationEnabled;
}
}
catch (Exception e)
{
GLog.Warn(e);
GLog.Warn($"Loading Item RuntimeCompilation Error! at: manifest.json[{modDirectory.Name}]");
}
return modConfig;
}
public static void LoadConfig()
{
if (!File.Exists(ConfigPath))
{
GLog.Warn("Config file missing !");
return;
}
string jsontext = File.ReadAllText(ConfigPath, Encoding.UTF8);
jsontext = GUtil.RemoveJsonComments(jsontext);
JsonDocument json = null;
JsonElement root;
try
{
json = JsonDocument.Parse(jsontext);
root = json.RootElement;
}
catch (Exception e)
{
GLog.Error(e);
GLog.Error("Incorrect Config JSON Format !");
return;
}
try
{
int LogLevel = root.GetProperty("LogLevel").GetInt32()!;
GlobalConfig.LogLevel = LogLevel;
GLog.GLogLevel = (GLogType)LogLevel;
}
catch (Exception ex) { GLog.Warn("Json Element 'LogLevel' Not Found !"); }
try
{
string TargetVersion = root.GetProperty("TargetVersion").GetString()!;
GlobalConfig.TargetVesrion = TargetVersion;
}
catch (Exception ex) { GLog.Warn("Json Element 'TargetVersion' Not Found !"); }
try
{
JsonElement versionArray = root.GetProperty("GMLVersion");
foreach (JsonElement version in versionArray.EnumerateArray())
{
int versionNumber = version.GetInt32();
}
}
catch (Exception ex) { GLog.Warn("Json Element 'GMLVersion' Not Found !"); }
}
public static void EnterVersionPath(string relativePath)
{
var a = Assembly.GetEntryAssembly()?.Location;
string pathAfter = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath);
Directory.SetCurrentDirectory(pathAfter);
CurrentDirectory = pathAfter;
}
public static void GetVersionDirectoryByName()
{
}
public static void GetVersions(List<VersionInfo> versionsDirectories)
{
versionsDirectories.Clear();
var dirs = new DirectoryInfo($"{CurrentDirectory}\\{VersionsPath}").GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
GLog.Info($"Loading Version: [{dir.Name}]");
try
{
versionsDirectories.Add(new VersionInfo(dir));
}
catch (Exception ex)
{
GLog.Error(ex);
GLog.Error($"Failed to Load Version: [{dir.Name}]");
}
}
}
public static void RecoverFiles(string assetsPath = null, string modsPath = null, string tempPath = null)
{
assetsPath ??= AssetsPath;
modsPath ??= ModsPath;
tempPath ??= TempPath;
DirectoryInfo Assets = new DirectoryInfo(assetsPath);
Assets.Create();
//FileInfo[] files = Assets.GetFiles();
//DirectoryInfo[] dics = Assets.GetDirectories();
DirectoryInfo Mods = new DirectoryInfo(modsPath);
Mods.Create();
DirectoryInfo TempDir = new DirectoryInfo(tempPath);
TempDir.Create();
}
public static bool CheckAssetsIntegrity(string assetsPath = null)
{
assetsPath ??= AssetsPath;
GLog.Info("Check Assets File integrity...");
Dictionary<string, bool> PresetFiles = new Dictionary<string, bool>();
PresetFiles.Add("GML_LOGO.png", false);
PresetFiles.Add("SurvivalcraftGML.ico", false);
DirectoryInfo Assets = new DirectoryInfo(AssetsPath);
FileInfo[] files = Assets.GetFiles();
foreach (FileInfo file in files)
{
try
{
PresetFiles[file.Name] = true;
}
catch { }
}
foreach (var file in PresetFiles)
{
if (!file.Value)
{
GLog.Error($"Missing File: {AssetsPath}\\{file.Key}");
return false;
}
}
return true;
}
}
}