Skip to content

Commit 41817ee

Browse files
authored
#76 + #66 (#84)
Added config settings to polymod hub and added preliminary settings reset on mods change
1 parent c051479 commit 41817ee

5 files changed

Lines changed: 119 additions & 43 deletions

File tree

resources/localization.json

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,28 @@
161161
"Elyrion": "πȱ∫ỹmȱδ ƒƒƒƒƒƒƒ ŋȱŧ ȱrrȱ #₺rr∑ŋŧ ƒƒƒƒƒƒƒ ỹ maỹ ŋȱŧ ~ȱr§ #ȱrr∑#ŧ∫ỹ!",
162162
"German (Germany)": "Diese Version von PolyMod ist nicht für die aktuelle Version der Anwendung ausgelegt und könnte nicht funktionieren!"
163163
},
164+
"polymod_hub_config": {
165+
"English": "SETTINGS"
166+
},
167+
"polymod_hub_debugenable": {
168+
"English": "ENABLE DEBUG"
169+
},
170+
"polymod_hub_debugdisable": {
171+
"English": "DISABLE DEBUG"
172+
},
173+
"polymod_hub_debugswitch": {
174+
"English": "Debug set to {0}."
175+
},
164176
"polymod_hub_updatespriteinfos": {
165177
"English": "UPDATE SPRITES"
166178
},
167-
"polymod.spriteinfo.updated": {
179+
"polymod_spriteinfo_updated": {
168180
"English": "Sprite info for mod {0} updated."
169181
},
170-
"polymod.spriteinfo.notupdated": {
182+
"polymod_spriteinfo_notupdated": {
171183
"English": "No sprite infos were found."
184+
},
185+
"polymod_popup_header": {
186+
"English": "POLYMOD"
172187
}
173188
}

src/Managers/Compatibility.cs

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
namespace PolyMod.Managers;
77
internal static class Compatibility
88
{
9-
internal static string signature = string.Empty;
10-
internal static string looseSignature = string.Empty;
9+
internal static string checksum = string.Empty;
10+
internal static bool shouldResetSettings = false;
1111
private static bool sawSignatureWarning;
1212

13-
public static void HashSignatures(StringBuilder looseSignatureString, StringBuilder signatureString)
13+
public static void HashSignatures(StringBuilder checksumString)
1414
{
15-
looseSignature = Util.Hash(looseSignatureString);
16-
signature = Util.Hash(signatureString);
15+
checksum = Util.Hash(checksumString);
1716
}
1817

1918
private static bool CheckSignatures(Action<int, BaseEventData> action, int id, BaseEventData eventData, Il2CppSystem.Guid gameId)
@@ -24,15 +23,15 @@ private static bool CheckSignatures(Action<int, BaseEventData> action, int id, B
2423
return true;
2524
}
2625

27-
string[] signatures = { string.Empty, string.Empty };
26+
string signature = string.Empty;
2827
try
2928
{
30-
signatures = File.ReadAllLines(Path.Combine(Application.persistentDataPath, $"{gameId}.signatures"));
29+
signature = File.ReadAllText(Path.Combine(Application.persistentDataPath, $"{gameId}.signatures"));
3130
}
3231
catch { }
33-
if (signatures[0] == string.Empty && signatures[1] == string.Empty) return true;
32+
if (signature == string.Empty) return true;
3433
if (Plugin.config.debug) return true;
35-
if (looseSignature != signatures[0])
34+
if (checksum != signature)
3635
{
3736
PopupManager.GetBasicPopup(new(
3837
Localization.Get("polymod.signature.mismatch"),
@@ -43,30 +42,29 @@ private static bool CheckSignatures(Action<int, BaseEventData> action, int id, B
4342
)).Show();
4443
return false;
4544
}
46-
if (signature != signatures[1])
47-
{
48-
PopupManager.GetBasicPopup(new(
49-
Localization.Get("polymod.signature.mismatch"),
50-
Localization.Get("polymod.signature.maybe.incompatible"),
51-
new(new PopupBase.PopupButtonData[] {
52-
new(
53-
"OK",
54-
callback: (UIButtonBase.ButtonAction)((int _, BaseEventData _) => {
55-
sawSignatureWarning = true;
56-
action(id, eventData);
57-
})
58-
)
59-
})
60-
)).Show();
61-
return false;
62-
}
6345
return true;
6446
}
6547

6648
[HarmonyPostfix]
6749
[HarmonyPatch(typeof(StartScreen), nameof(StartScreen.Start))]
6850
private static void StartScreen_Start()
6951
{
52+
string lastChecksum = checksum;
53+
try
54+
{
55+
lastChecksum = new(File.ReadAllText(Plugin.CHECKSUM_PATH));
56+
}
57+
catch (FileNotFoundException){}
58+
59+
File.WriteAllText(
60+
Plugin.CHECKSUM_PATH,
61+
checksum
62+
);
63+
if (lastChecksum != checksum)
64+
{
65+
shouldResetSettings = true;
66+
}
67+
7068
Version incompatibilityWarningLastVersion = Plugin.POLYTOPIA_VERSION.CutRevision();
7169
try
7270
{
@@ -139,10 +137,29 @@ private static void ClientBase_CreateSession(GameSettings settings, Il2CppSystem
139137
{
140138
File.WriteAllLinesAsync(
141139
Path.Combine(Application.persistentDataPath, $"{gameId}.signatures"),
142-
new string[] { looseSignature, signature }
140+
new string[] { checksum }
143141
);
144142
}
145143

144+
[HarmonyPrefix]
145+
[HarmonyPatch(typeof(TribeSelectorScreen), nameof(TribeSelectorScreen.Show))]
146+
private static bool TribeSelectorScreen_Show(bool instant = false)
147+
{
148+
if (shouldResetSettings)
149+
{
150+
RestorePreliminaryGameSettings();
151+
shouldResetSettings = false;
152+
}
153+
return true;
154+
}
155+
156+
internal static void RestorePreliminaryGameSettings()
157+
{
158+
GameManager.PreliminaryGameSettings.disabledTribes.Clear();
159+
GameManager.PreliminaryGameSettings.selectedSkins.Clear();
160+
GameManager.PreliminaryGameSettings.SaveToDisk();
161+
}
162+
146163
internal static void Init()
147164
{
148165
Harmony.CreateAndPatchAll(typeof(Compatibility));

src/Managers/Hub.cs

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text.Json;
12
using Cpp2IL.Core.Extensions;
23
using HarmonyLib;
34
using I2.Loc;
@@ -13,6 +14,7 @@ internal static class Hub
1314
{
1415
private const string HEADER_PREFIX = "<align=\"center\"><size=150%><b>";
1516
private const string HEADER_POSTFIX = "</b></size><align=\"left\">";
17+
private const int POPUP_WIDTH = 1400;
1618
public static bool isLevelPopupActive = false;
1719

1820
[HarmonyPrefix]
@@ -140,6 +142,13 @@ static void PolyModHubButtonClicked(int buttonId, BaseEventData eventData)
140142
"polymod.hub.discord",
141143
callback: (UIButtonBase.ButtonAction)((_, _) =>
142144
NativeHelpers.OpenURL(Plugin.DISCORD_LINK, false))
145+
),
146+
new(
147+
"polymod.hub.config",
148+
callback: (UIButtonBase.ButtonAction)((_, _) =>
149+
{
150+
ShowLevelPopup();
151+
})
143152
)
144153
};
145154
if (Plugin.config.debug)
@@ -186,7 +195,7 @@ static void PolyModHubButtonClicked(int buttonId, BaseEventData eventData)
186195
));
187196
}
188197
popup.buttonData = popupButtons.ToArray();
189-
popup.ShowSetWidth(1000);
198+
popup.ShowSetWidth(POPUP_WIDTH);
190199
}
191200

192201
if (Main.dependencyCycle)
@@ -254,8 +263,7 @@ internal static void ShowLevelPopup()
254263
{
255264
BasicPopup polymodPopup = PopupManager.GetBasicPopup();
256265

257-
polymodPopup.Header = "POLYMOD";
258-
polymodPopup.Description = "";
266+
polymodPopup.Header = Localization.Get("polymod.popup.header");
259267

260268
polymodPopup.buttonData = CreatePopupButtonData();
261269
polymodPopup.Show();
@@ -270,12 +278,48 @@ internal static PopupButtonData[] CreatePopupButtonData()
270278

271279
if (GameManager.Instance.isLevelLoaded)
272280
{
273-
popupButtons.Add(new PopupButtonData("UPDATE SPRITES", PopupButtonData.States.None, (UIButtonBase.ButtonAction)OnUpdateSprites, -1, true, null));
281+
popupButtons.Add(new PopupButtonData(Localization.Get("polymod.hub.spriteinfo"), PopupButtonData.States.None, (UIButtonBase.ButtonAction)OnUpdateSpritesButtonClicked, -1, true, null));
282+
}
283+
else
284+
{
285+
string debugButtonName = Localization.Get("polymod.hub.debugenable");
286+
if (Plugin.config.debug)
287+
{
288+
debugButtonName = Localization.Get("polymod.hub.debugdisable");
289+
}
290+
popupButtons.Add(new PopupButtonData(debugButtonName, PopupButtonData.States.None, (UIButtonBase.ButtonAction)OnDebugButtonClicked, -1, true, null));
291+
//popupButtons.Add(new PopupButtonData("", PopupButtonData.States.None, (UIButtonBase.ButtonAction)OnAutoUpdateButtonClicked, -1, true, null));
292+
//popupButtons.Add(new PopupButtonData("", PopupButtonData.States.Disabled, (UIButtonBase.ButtonAction)OnIncludeAlphasButtonClicked, -1, true, null));
274293
}
275-
276294
return popupButtons.ToArray();
277295

278-
void OnUpdateSprites(int buttonId, BaseEventData eventData)
296+
void OnDebugButtonClicked(int buttonId, BaseEventData eventData)
297+
{
298+
Plugin.config = new(debug: !Plugin.config.debug);
299+
File.WriteAllText(Plugin.CONFIG_PATH, JsonSerializer.Serialize(Plugin.config));
300+
NotificationManager.Notify(Localization.Get("polymod.hub.debugswitch", new Il2CppSystem.Object[] { Plugin.config.debug }));
301+
if (Plugin.config.debug)
302+
{
303+
BepInEx.ConsoleManager.CreateConsole();
304+
}
305+
else
306+
{
307+
BepInEx.ConsoleManager.DetachConsole();
308+
}
309+
isLevelPopupActive = false;
310+
}
311+
312+
void OnAutoUpdateButtonClicked(int buttonId, BaseEventData eventData)
313+
{
314+
isLevelPopupActive = false;
315+
}
316+
317+
void OnIncludeAlphasButtonClicked(int buttonId, BaseEventData eventData)
318+
{
319+
isLevelPopupActive = false;
320+
}
321+
322+
void OnUpdateSpritesButtonClicked(int buttonId, BaseEventData eventData)
279323
{
280324
UpdateSpriteInfos();
281325
isLevelPopupActive = false;

src/Managers/Main.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,13 @@ internal static void Init()
180180
dependencyCycle = !Loader.SortMods(Registry.mods);
181181
if (dependencyCycle) return;
182182

183-
StringBuilder looseSignatureString = new();
184-
StringBuilder signatureString = new();
183+
StringBuilder checksumString = new();
185184
foreach (var (id, mod) in Registry.mods)
186185
{
187186
if (mod.status != Mod.Status.Success) continue;
188187
foreach (var file in mod.files)
189188
{
189+
checksumString.Append(JsonSerializer.Serialize(file));
190190
if (Path.GetExtension(file.name) == ".dll")
191191
{
192192
Loader.LoadAssemblyFile(mod, file);
@@ -198,14 +198,11 @@ internal static void Init()
198198
}
199199
if (!mod.client && id != "polytopia")
200200
{
201-
looseSignatureString.Append(id);
202-
looseSignatureString.Append(mod.version.Major);
203-
204-
signatureString.Append(id);
205-
signatureString.Append(mod.version.ToString());
201+
checksumString.Append(id);
202+
checksumString.Append(mod.version.ToString());
206203
}
207204
}
208-
Compatibility.HashSignatures(looseSignatureString, signatureString);
205+
Compatibility.HashSignatures(checksumString);
209206

210207
stopwatch.Stop();
211208
}

src/Plugin.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using BepInEx.Configuration;
55
using BepInEx.Logging;
66
using PolyMod.Managers;
7+
using UnityEngine;
78

89
namespace PolyMod;
910
[BepInPlugin("com.polymod", "PolyMod", VERSION)]
@@ -20,6 +21,8 @@ internal record PolyConfig(
2021
internal static readonly string CONFIG_PATH = Path.Combine(BASE_PATH, "PolyMod.json");
2122
internal static readonly string INCOMPATIBILITY_WARNING_LAST_VERSION_PATH
2223
= Path.Combine(BASE_PATH, "IncompatibilityWarningLastVersion");
24+
internal static readonly string CHECKSUM_PATH
25+
= Path.Combine(BASE_PATH, "CHECKSUM");
2326
internal static readonly string DISCORD_LINK = "https://discord.gg/eWPdhWtfVy";
2427
internal static readonly List<string> LOG_MESSAGES_IGNORE = new()
2528
{

0 commit comments

Comments
 (0)