-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModsGate.cs
424 lines (358 loc) · 14.1 KB
/
ModsGate.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
/*
Mods Gate
Copyright (C) 2024 Alexandre 'kidev' Poumaroux
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// ReSharper disable SuggestVarOrType_BuiltInTypes
// ReSharper disable SuggestVarOrType_SimpleTypes
// ReSharper disable SuggestVarOrType_Elsewhere
using System.Reflection;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Newtonsoft.Json;
using System.IO.Compression;
using System.Text;
using AutoHackGame;
namespace ModsGate;
using C = Constants;
[BepInPlugin("org.kidev.ltd2.modsgate", "Mods Gate", "1.0.0")]
public class ModsGate : BaseUnityPlugin
{
private static readonly HttpClient Client = new HttpClient();
private static ManualLogSource _logger;
private UIPatcher _patcher;
private List<Mod> _mods;
private Mod GetMod(string modName)
{
return _mods.FirstOrDefault(m => m.Name == modName);
}
public void Awake()
{
_logger = Logger;
_logger.LogInfo("Mods gate loaded!");
_patcher = new UIPatcher(Assembly.GetExecutingAssembly(), Path.Combine(Paths.GameRootPath, "Legion TD 2_Data", "uiresources", "AeonGT"));
_ = CheckForUpdatesAsync();
}
public void OnDestroy()
{
_patcher.CleanupPatchedFiles();
}
private async Task CheckForUpdatesAsync()
{
try
{
string jsonContent = await FetchFileContentAsync(C.JsonURL);
ModsConfig modsConfig = JsonConvert.DeserializeObject<ModsConfig>(jsonContent);
await _patcher.DownloadAndApplyPatchesAsync(modsConfig.Core.UIPatches);
List<PluginInfo> installedPlugins = GetInstalledPlugins();
_logger.LogInfo("Before...");
_logger.LogInfo(modsConfig);
modsConfig.Mods = modsConfig.Mods.Prepend(
new Mod(modsConfig.Core.Name,
modsConfig.Core.GUID,
modsConfig.Core.Author,
modsConfig.Core.IconUrl,
modsConfig.Core.Url,
modsConfig.Core.Version,
modsConfig.Core.GameVersion,
modsConfig.Core.Description
)
).ToList();
foreach (var mod in modsConfig.Mods)
{
var installedPlugin = installedPlugins.FirstOrDefault(p => p.Metadata.Name == mod.Name);
if (installedPlugin == null) continue;
Version installedVersion = new Version(installedPlugin.Metadata.Version.ToString());
Version jsonVersion = new Version(mod.Version);
mod.ReplaceVersionInUrls();
HudApi.TriggerHudEvent(C.UpdatedModsDataEvent, mod.GUID, mod.ToString());
if (jsonVersion <= installedVersion) continue;
_logger.LogInfo($"Update available for {mod.Name}: {installedVersion} -> {jsonVersion}");
await UpdateModAsync(mod, installedPlugin);
}
_mods = modsConfig.Mods;
_logger.LogInfo("After...");
_logger.LogInfo(modsConfig);
}
catch (Exception ex)
{
_logger.LogError($"Error during update check: {ex.Message}");
}
}
private static async Task<string> FetchFileContentAsync(string url)
{
HttpResponseMessage response = await Client.GetAsync(url);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
private static List<PluginInfo> GetInstalledPlugins()
{
return BepInEx.Bootstrap.Chainloader.PluginInfos.Values.ToList();
}
public async Task InstallMod(string modName)
{
var mod = GetMod(modName);
if (mod == null)
{
throw new Exception($"Mod {modName} not found");
}
using var client = new HttpClient();
var response = await client.GetAsync(mod.Url["*"]);
response.EnsureSuccessStatusCode();
var dllBytes = await response.Content.ReadAsByteArrayAsync();
await File.WriteAllBytesAsync(mod.GetDllFilePath(), dllBytes);
_logger.LogInfo($"Mod {modName} installed successfully.");
}
public void UninstallMod(string modName)
{
var mod = GetMod(modName);
if (mod == null)
{
throw new Exception($"Mod {modName} not found");
}
var dllPath = mod.GetDllFilePath();
if (File.Exists(dllPath))
{
File.Delete(dllPath);
_logger.LogInfo($"Mod {modName} uninstalled successfully.");
}
else
{
_logger.LogInfo($"Mod {modName} not found in plugins folder.");
}
}
public void DeactivateMod(string modName)
{
var mod = GetMod(modName);
if (mod == null)
{
throw new Exception($"Mod {modName} not found");
}
var dllPath = mod.GetDllFilePath();
var dllDeactivatedPath = $"{dllPath}.deactivated";
if (File.Exists(dllPath) && !File.Exists(dllDeactivatedPath))
{
File.Move(dllPath, dllDeactivatedPath);
_logger.LogInfo($"Mod {modName} deactivated successfully.");
}
else
{
_logger.LogInfo($"Mod {modName} is already deactivated or not found.");
}
}
public void ReactivateMod(string modName)
{
var mod = GetMod(modName);
if (mod == null)
{
throw new Exception($"Mod {modName} not found");
}
var dllPath = mod.GetDllFilePath();
var dllDeactivatedPath = $"{dllPath}.deactivated";
if (File.Exists(dllDeactivatedPath) && !File.Exists(dllPath))
{
File.Move(dllDeactivatedPath, dllPath);
_logger.LogInfo($"Mod {modName} reactivated successfully.");
}
else
{
_logger.LogInfo($"Mod {modName} is not deactivated or the activated file already exists.");
}
}
private static async Task UpdateModAsync(Mod mod, PluginInfo installedPlugin)
{
try
{
string downloadUrl = mod.GetUrlForOS();
string zipPath = Path.Combine(Paths.PluginPath, $"{mod.Name}_update.zip");
string extractPath = Path.Combine(Paths.PluginPath, mod.Name);
_logger.LogInfo($"Downloading {downloadUrl} to {extractPath}");
using (var response = await Client.GetAsync(downloadUrl))
await using (var fs = new FileStream(zipPath, FileMode.CreateNew))
{
await response.Content.CopyToAsync(fs);
}
string backupPath = Path.Combine(Paths.PluginPath,
$"{mod.Name}_v{installedPlugin.Metadata.Version}.outdated");
Directory.Move(extractPath, backupPath);
ZipFile.ExtractToDirectory(zipPath, extractPath);
File.Delete(zipPath);
_logger.LogInfo($"Successfully updated {mod.Name} to version {mod.Version}");
}
catch (Exception ex)
{
_logger.LogError($"Error updating {mod.Name}: {ex.Message}");
}
}
}
public class Mod(
string name,
string guid,
string author,
string iconUrl,
Dictionary<string, string> url,
string version,
string gameVersion,
string description)
{
[JsonProperty("name")] public string Name { get; set; } = name;
[JsonProperty("guid")] public string GUID { get; set; } = guid;
[JsonProperty("author")] public string Author { get; set; } = author;
[JsonProperty("icon_url")] public string IconUrl { get; set; } = iconUrl;
[JsonProperty("url")] public Dictionary<string, string> Url { get; set; } = url;
[JsonProperty("version")] public string Version { get; set; } = version;
[JsonProperty("game_version")] public string GameVersion { get; set; } = gameVersion;
[JsonProperty("description")] public string Description { get; set; } = description;
public string GetUrlForOS()
{
if (Url.TryGetValue("*", out var fromMap))
{
return fromMap;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Url.TryGetValue("win", out var valueFromMap))
{
return valueFromMap;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && Url.TryGetValue("linux", out var map1))
{
return map1;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) && Url.TryGetValue("mac", out var fromMap1))
{
return fromMap1;
}
throw new Exception($"Unsupported OS: {RuntimeInformation.OSDescription}");
}
public void ReplaceVersionInUrls()
{
if (Url == null || Version == null)
return;
var keys = new List<string>(Url.Keys); // To avoid modifying the dictionary while iterating
foreach (var key in keys)
{
// Replace all occurrences of '$' with the value of Version
Url[key] = Url[key].Replace("$", Version);
}
}
public string GetDllFilePath()
{
return Path.Combine(Paths.PluginPath, $"{Name}.dll");
}
public override string ToString()
{
var sb = new StringBuilder();
sb.AppendLine($"Mod: {Name}");
sb.AppendLine($" GUID: {GUID}");
sb.AppendLine($" Author: {Author}");
sb.AppendLine($" Icon URL: {IconUrl}");
sb.AppendLine($" URLs:");
foreach (var kvp in Url)
{
sb.AppendLine($" {kvp.Key}: {kvp.Value}");
}
sb.AppendLine($" Version: {Version}");
sb.AppendLine($" Game Version: {GameVersion}");
sb.AppendLine($" Description: {Description}");
return sb.ToString();
}
}
public class Core
{
[JsonProperty("name")] public string Name { get; set; }
[JsonProperty("guid")] public string GUID { get; set; }
[JsonProperty("author")] public string Author { get; set; }
[JsonProperty("icon_url")] public string IconUrl { get; set; }
[JsonProperty("url")] public Dictionary<string, string> Url { get; set; }
[JsonProperty("version")] public string Version { get; set; }
[JsonProperty("game_version")] public string GameVersion { get; set; }
[JsonProperty("description")] public string Description { get; set; }
[JsonProperty("ui_patches")] public string UIPatches { get; set; }
[JsonProperty("dependencies")] public List<Dictionary<string, string>> Dependencies { get; set; }
[JsonProperty("dependencies_versions")] public List<DependencyVersion> DependencyVersions { get; set; }
[JsonProperty("installers")] public Dictionary<string, string> Installers { get; set; }
[JsonProperty("signatures")] public string Signatures { get; set; }
public override string ToString()
{
var sb = new StringBuilder();
sb.AppendLine($"Core: {Name}");
sb.AppendLine($" GUID: {GUID}");
sb.AppendLine($" Author: {Author}");
sb.AppendLine($" Icon URL: {IconUrl}");
sb.AppendLine($" URLs:");
foreach (var kvp in Url)
{
sb.AppendLine($" {kvp.Key}: {kvp.Value}");
}
sb.AppendLine($" Version: {Version}");
sb.AppendLine($" Game Version: {GameVersion}");
sb.AppendLine($" Description: {Description}");
sb.AppendLine($" UI Patches: {UIPatches}");
sb.AppendLine(" Dependencies:");
foreach (var dep in Dependencies)
{
sb.AppendLine($" {string.Join(", ", dep.Select(kvp => $"{kvp.Key}: {kvp.Value}"))}");
}
sb.AppendLine(" Dependency Versions:");
foreach (var depVer in DependencyVersions)
{
sb.AppendLine($" {depVer}");
}
sb.AppendLine(" Installers:");
foreach (var kvp in Installers)
{
sb.AppendLine($" {kvp.Key}: {kvp.Value}");
}
sb.AppendLine($" Signatures: {Signatures}");
return sb.ToString();
}
}
public class DependencyVersion
{
[JsonProperty("name")] public string Name { get; set; }
[JsonProperty("version")] public string Version { get; set; }
public override string ToString()
{
return $"DependencyVersion: {Name} (Version: {Version})";
}
}
public class ModsConfig
{
[JsonProperty("core")] public Core Core { get; set; }
[JsonProperty("mods")] public List<Mod> Mods { get; set; }
public override string ToString()
{
var sb = new StringBuilder();
sb.AppendLine("### CONFIG.JSON ###:");
sb.AppendLine("Core:");
sb.AppendLine(Core.ToString());
sb.AppendLine("Mods:");
foreach (var mod in Mods)
{
sb.AppendLine(mod.ToString());
}
return sb.ToString();
}
}
internal static class Constants
{
internal const string JsonURL = "https://raw.githubusercontent.com/LegionTD2-Modding/.github/main/mods/config.json";
internal const string UpdatedModsDataEvent = "UpdatedModsData";
}