-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIPatcher.cs
179 lines (148 loc) · 5.93 KB
/
UIPatcher.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
/*
AutoHackGame
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/>.
*/
using System;
using System.IO;
using System.IO.Compression;
using System.Net.Http;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using DiffMatchPatch;
using HarmonyLib;
using Patch = DiffMatchPatch.Patch;
namespace AutoHackGame;
using C = Constants;
public class UIPatcher
{
private readonly Harmony _harmony = new("org.kidev.ltd2.uipatcher");
private readonly string _baseDirectory;
private readonly string _modsDirectory;
private readonly diff_match_patch _dmp;
private readonly List<string> _patchedFiles;
public UIPatcher(Assembly assembly, string baseDirectory)
{
_baseDirectory = baseDirectory;
_modsDirectory = Path.Combine(_baseDirectory, "mods");
_dmp = new diff_match_patch();
_patchedFiles = new List<string>();
try
{
_harmony.PatchAll(assembly);
}
catch (Exception e)
{
Console.Error.WriteLine($"Can't patch using harmony: {e.Message}");
}
}
public async Task DownloadAndApplyPatchesAsync(string patchZipUrl)
{
string patchZipPath = await DownloadFileAsync(patchZipUrl);
ExtractZip(patchZipPath, _modsDirectory);
ApplyPatches(_modsDirectory);
ApplySpecialPatchToGateway();
}
private async Task<string> DownloadFileAsync(string url)
{
using var client = new HttpClient();
byte[] fileBytes = await client.GetByteArrayAsync(url);
string filePath = Path.Combine(_baseDirectory, "Patches.zip");
await File.WriteAllBytesAsync(filePath, fileBytes);
return filePath;
}
private void ExtractZip(string zipPath, string extractPath)
{
ZipFile.ExtractToDirectory(zipPath, extractPath, true);
File.Delete(zipPath);
}
private void ApplyPatches(string directory)
{
foreach (string filePath in Directory.GetFiles(directory, "*.patch", SearchOption.AllDirectories))
{
string relativePath = Path.GetRelativePath(_modsDirectory, filePath);
string originalFilePath = Path.Combine(_baseDirectory, relativePath.Replace(".patch", ""));
string patchedFilePath = Path.Combine(Path.GetDirectoryName(originalFilePath) ?? string.Empty, $"__{Path.GetFileName(originalFilePath)}");
string patchContent = File.ReadAllText(filePath);
string originalContent = File.Exists(originalFilePath) ? File.ReadAllText(originalFilePath) : "";
List<Patch> patches = _dmp.patch_fromText(patchContent);
Object[] patchResult = _dmp.patch_apply(patches, originalContent);
string patchedContent = (string)patchResult[0];
File.WriteAllText(patchedFilePath, patchedContent);
if (Path.GetFileName(originalFilePath) != "gateway.html")
{
_patchedFiles.Add(relativePath.Replace(".patch", ""));
}
}
}
private void ApplySpecialPatchToGateway()
{
string gatewayPath = Path.Combine(_baseDirectory, "gateway.html");
string patchedGatewayPath = Path.Combine(_baseDirectory, "__gateway.html");
if (File.Exists(patchedGatewayPath))
{
string content = File.ReadAllText(patchedGatewayPath);
foreach (string patchedFile in _patchedFiles)
{
string originalPath = patchedFile;
string patchedPath = Path.Combine(Path.GetDirectoryName(patchedFile) ?? string.Empty, $"__{Path.GetFileName(patchedFile)}");
content = Regex.Replace(content, Regex.Escape(originalPath), patchedPath);
}
File.WriteAllText(patchedGatewayPath, content);
}
}
public void CleanupPatchedFiles()
{
foreach (string filePath in Directory.GetFiles(_baseDirectory, "__*", SearchOption.AllDirectories))
{
File.Delete(filePath);
}
_harmony.UnpatchSelf();
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
[SuppressMessage("ReSharper", "UnusedMember.Local")]
[HarmonyPatch]
internal static class PatchSendCreateView
{
private static Type _typeCoherentUIGTView;
[HarmonyPrepare]
private static void Prepare()
{
_typeCoherentUIGTView = AccessTools.TypeByName("CoherentUIGTView");
}
[HarmonyTargetMethod]
private static MethodBase TargetMethod()
{
return AccessTools.Method(_typeCoherentUIGTView, "SendCreateView");
}
[HarmonyPrefix]
private static bool SendCreateViewPre(ref string ___m_Page)
{
if (___m_Page.Equals(C.GatewayOldPath))
{
___m_Page = C.GatewayNewPath;
}
return true;
}
}
}
internal static class Constants
{
private const string GatewayFileName = "gateway.html";
private const string PatchedFolder = "coui://uiresources/AeonGT/";
internal const string GatewayOldPath = $"{PatchedFolder}/{GatewayFileName}";
internal const string GatewayNewPath = $"{PatchedFolder}/__{GatewayFileName}";
internal const string UpdatedModsDataEvent = "UpdatedModsData";
}