-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cs
More file actions
271 lines (237 loc) · 10.9 KB
/
main.cs
File metadata and controls
271 lines (237 loc) · 10.9 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
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using static RussianTranslation.RussianTranslation;
using static RussianTranslation.SharedState;
using RussianTranslation.Helpers;
namespace RussianTranslation
{
public static class SharedState
{
public static Dictionary<string, string> CurrentRussianDictionary = new Dictionary<string, string>()
{
};
public static bool changedLanguage = false;
}
[BepInPlugin(pluginGuid, pluginName, pluginVersion)]
public class RussianTranslation : BaseUnityPlugin
{
public static ManualLogSource logger;
public const string pluginGuid = "shushu.casualtiesunknown.russiantranslation";
public const string pluginName = "Russian Translation";
// Year.Month.Version.Bugfix
public const string pluginVersion = "26.1.1.0";
public static RussianTranslation Instance;
public static int isOkayToPatch = 0;
public static string activeVersion = "";
public void Awake()
{
Instance = this;
logger = Logger;
logger.LogInfo("Awake() ran - mod loaded!");
Harmony harmony = new Harmony(pluginGuid);
(string, byte[]) fileInfo = FileLoader.LoadFileBytes("RU.json");
string langDir = Path.Combine(Application.dataPath, "Lang");
Directory.CreateDirectory(langDir);
string filePath = Path.Combine(langDir, fileInfo.Item1);
SHA256 sha256 = SHA256.Create();
if (!File.Exists(filePath))
File.WriteAllBytes(filePath, fileInfo.Item2);
else
{
byte[] existingFileSha256 = sha256.ComputeHash(File.OpenRead(filePath));
byte[] embeddedFileSha256 = sha256.ComputeHash(fileInfo.Item2);
if (!embeddedFileSha256.SequenceEqual(existingFileSha256))
{
Log("File hash isn't the same, overwriting");
File.WriteAllBytes(filePath, fileInfo.Item2);
}
}
StartCoroutine(CheckGameVersion(harmony));
}
public static void Log(string message)
{
logger.LogInfo(message);
}
public static IEnumerator CheckGameVersion(Harmony harmony)
{
harmony.Patch(AccessTools.Method(typeof(PreRunScript), "Awake"), prefix: new HarmonyMethod(typeof(RussianTranslation).GetMethod("VersionCheck")));
while (true)
{
if (isOkayToPatch == 1)
{
break;
}
if (isOkayToPatch == -1)
{
harmony.Unpatch(AccessTools.Method(typeof(PreRunScript), "Awake"), HarmonyPatchType.Prefix);
logger.LogError($"Game version is not {activeVersion}, mod exiting...");
yield break;
}
yield return null;
}
harmony.Unpatch(AccessTools.Method(typeof(PreRunScript), "Awake"), HarmonyPatchType.Prefix);
List<MethodInfo> patches = typeof(MyPatches).GetMethods(BindingFlags.Static | BindingFlags.Public).ToList();
foreach (MethodInfo patch in patches)
{
try
{
string[] splitName = patch.Name.Replace("__", "$").Split('_');
for (int i = 0; i < splitName.Length; i++)
splitName[i] = splitName[i].Replace("$", "_");
if (splitName.Length < 3)
throw new Exception($"Patch method is named incorrectly\nPlease make sure the Patch method is named in the following pattern:\n\tTargetClass_TargetMethod_PatchType[_Version]");
if (splitName.Length >= 4)
if (splitName[3] != activeVersion)
{
Log($"{patch.Name} is not supported by version {activeVersion}");
continue;
}
string targetType = splitName[0];
MethodType targetMethodType;
if (splitName[1].Contains("get_"))
targetMethodType = MethodType.Getter;
else if (splitName[1].Contains("set_"))
targetMethodType = MethodType.Setter;
else
targetMethodType = MethodType.Normal;
string ogTargetMethod = splitName[1];
string targetMethod = splitName[1].Replace("get_", "").Replace("set_", "");
string patchType = splitName[2];
MethodInfo patchScript = typeof(MyPatches).GetMethod(patch.Name);
ParameterInfo[] parameters = patchScript.GetParameters();
Type[] scriptArgTypes = parameters.Where(p => p.ParameterType != AccessTools.TypeByName(targetType)).Select(p => p.ParameterType).ToArray();
MethodInfo ogScript = null;
switch (targetMethodType)
{
case MethodType.Enumerator:
case MethodType.Normal:
try
{
ogScript = AccessTools.Method(AccessTools.TypeByName(targetType), targetMethod);
}
catch (Exception)
{
ogScript = AccessTools.Method(AccessTools.TypeByName(targetType), targetMethod, scriptArgTypes);
}
break;
case MethodType.Getter:
ogScript = AccessTools.PropertyGetter(AccessTools.TypeByName(targetType), targetMethod);
break;
case MethodType.Setter:
case MethodType.Constructor:
case MethodType.StaticConstructor:
default:
throw new Exception($"Unknown patch method\nPatch method type \"{targetMethodType}\" currently has no handling");
}
List<string> validPatchTypes = new List<string>
{
"Prefix",
"Postfix",
"Transpiler"
};
if (ogScript == null || patchScript == null || !validPatchTypes.Contains(patchType))
{
throw new Exception("Patch method is named incorrectly\nPlease make sure the Patch method is named in the following pattern:\n\tTargetClass_TargetMethod_PatchType[_Version]");
}
HarmonyMethod harmonyMethod = new HarmonyMethod(patchScript)
{
methodType = targetMethodType
};
HarmonyMethod postfix = null;
HarmonyMethod prefix = null;
HarmonyMethod transpiler = null;
switch (patchType)
{
case "Prefix":
prefix = harmonyMethod;
break;
case "Postfix":
postfix = harmonyMethod;
break;
case "Transpiler":
transpiler = harmonyMethod;
break;
}
harmony.Patch(ogScript, prefix: prefix, postfix: postfix, transpiler: transpiler);
Log("Patched " + targetType + "." + targetMethod + " as a " + patchType);
}
catch (Exception exception)
{
logger.LogError($"Failed to patch {patch.Name}");
logger.LogError(exception);
}
}
// If you have any PreRunScript Awake/Start patches, uncomment next line
//SceneManager.LoadScene("PreGen");
}
public static void VersionCheck()
{
Dictionary<string, string[]> supportedVersions = new Dictionary<string, string[]>
{
["Text (TMP) (18)"] = new string[] { "V5 Pre-testing 5", "5_5" },
["Text (TMP) (17)"] = new string[] { "V5 Pre-testing 4", "5_4" }
};
foreach (var supportedVersion in supportedVersions)
{
if (isOkayToPatch == 0)
{
GameObject obj = GameObject.Find(supportedVersion.Key);
if (obj == null)
continue;
if (obj.GetComponent<TextMeshProUGUI>().text.Contains(supportedVersion.Value[0]))
{
activeVersion = supportedVersion.Value[1];
isOkayToPatch = 1;
break;
}
}
}
if (isOkayToPatch == 0)
isOkayToPatch = -1;
}
}
public class MyPatches
{
[HarmonyPatch(typeof(TextMeshProUGUI), "Awake")]
[HarmonyPrefix]
public static void TextMeshProUGUI_Awake_Prefix(TextMeshProUGUI __instance)
{
GameObject gameObject = __instance.transform.gameObject;
string text = __instance.text;
if (gameObject == null || text == null || text == "")
return;
if (!CurrentRussianDictionary.ContainsKey(gameObject.name))
return;
}
[HarmonyPatch(typeof(WoundView), "Start")]
[HarmonyPrefix]
public static void WoundView_Start_Prefix(WoundView __instance)
{
Image StatMenu = __instance.gameObject.transform.Find("StatMenu/Image/Image")?.GetComponent<Image>();
Image SkillsBack = __instance.gameObject.transform.Find("SkillsBack/Image")?.GetComponent<Image>();
Image LimbView = __instance.gameObject.transform.Find("LimbView/Image/Image (1)")?.GetComponent<Image>();
Sprite StatMenuSprite = FileLoader.LoadEmbeddedSprite("screenStatic.png");
Sprite SkillsBackSprite = FileLoader.LoadEmbeddedSprite("screenSkillsGlow.png");
Sprite LimbViewSprite = FileLoader.LoadEmbeddedSprite("screenSmallStatic.png");
StatMenu.sprite = StatMenuSprite;
SkillsBack.sprite = SkillsBackSprite;
LimbView.sprite = LimbViewSprite;
}
}
}