-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathHub.cs
More file actions
394 lines (362 loc) · 17.3 KB
/
Copy pathHub.cs
File metadata and controls
394 lines (362 loc) · 17.3 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
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
using System.Text.Json;
using Cpp2IL.Core.Extensions;
using HarmonyLib;
using I2.Loc;
using Il2CppInterop.Runtime;
using Polytopia.Data;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using static PopupBase;
namespace PolyMod.Managers;
internal static class Hub
{
private const string HEADER_PREFIX = "<align=\"center\"><size=150%><b>";
private const string HEADER_POSTFIX = "</b></size><align=\"left\">";
private const int POPUP_WIDTH = 1400;
public static bool isConfigPopupActive = false;
[HarmonyPrefix]
[HarmonyPatch(typeof(SplashController), nameof(SplashController.LoadAndPlayClip))]
private static bool SplashController_LoadAndPlayClip(SplashController __instance)
{
string name = "intro.mp4";
string path = Path.Combine(Application.persistentDataPath, name);
File.WriteAllBytesAsync(path, Plugin.GetResource(name).ReadBytes());
__instance.lastPlayTime = Time.realtimeSinceStartup;
__instance.videoPlayer.url = path;
__instance.videoPlayer.Play();
return false;
}
[HarmonyPostfix]
[HarmonyPatch(typeof(PopupButtonContainer), nameof(PopupButtonContainer.SetButtonData))]
private static void PopupButtonContainer_SetButtonData(PopupButtonContainer __instance)
{
int num = __instance.buttons.Length;
for (int i = 0; i < num; i++)
{
UITextButton uitextButton = __instance.buttons[i];
Vector2 vector = new((num == 1) ? 0.5f : (i / (num - 1.0f)), 0.5f);
uitextButton.rectTransform.anchorMin = vector;
uitextButton.rectTransform.anchorMax = vector;
uitextButton.rectTransform.pivot = vector;
}
}
[HarmonyPrefix]
[HarmonyPatch(typeof(StartScreen), nameof(StartScreen.Start))]
private static void StartScreen_Start()
{
Il2CppInterop.Runtime.InteropTypes.Arrays.Il2CppReferenceArray<UnityEngine.Object> allLocalizers = GameObject.FindObjectsOfTypeAll(Il2CppType.From(typeof(TMPLocalizer)));
foreach (UnityEngine.Object item in allLocalizers)
{
TMPLocalizer? localizer = item.TryCast<TMPLocalizer>();
if (localizer == null)
{
continue;
}
Transform? parent = localizer?.gameObject?.transform?.parent;
if (parent == null)
{
continue;
}
string parentName = parent.name;
if (parentName == "SettingsButton")
{
Transform? textTransform = parent.FindChild("DescriptionText");
if (textTransform == null)
{
return;
}
GameObject originalText = textTransform.gameObject;
GameObject text = GameObject.Instantiate(originalText, originalText.transform.parent.parent.parent);
text.name = "PolyModVersion";
RectTransform rect = text.GetComponent<RectTransform>();
rect.anchoredPosition = new Vector2(265, 40);
rect.sizeDelta = new Vector2(500, rect.sizeDelta.y);
rect.anchorMax = Vector2.zero;
rect.anchorMin = Vector2.zero;
TextMeshProUGUI textComponent = text.GetComponent<TextMeshProUGUI>();
textComponent.fontSize = 18;
textComponent.alignment = TextAlignmentOptions.BottomLeft;
text.GetComponent<TMPLocalizer>().Text = $"PolyMod {Plugin.VERSION}";
text.AddComponent<LayoutElement>().ignoreLayout = true;
}
else if (parentName == "NewsButton")
{
GameObject originalButton = parent.gameObject;
GameObject button = GameObject.Instantiate(originalButton, originalButton.transform.parent);
button.name = "PolyModHubButton";
button.transform.position = originalButton.transform.position - new Vector3(90, 0, 0);
UIRoundButton buttonComponent = button.GetComponent<UIRoundButton>();
buttonComponent.bg.sprite = Visual.BuildSprite(Plugin.GetResource("polymod_icon.png").ReadBytes());
buttonComponent.bg.transform.localScale = new Vector3(1.2f, 1.2f, 0);
buttonComponent.bg.color = Color.white;
GameObject.Destroy(buttonComponent.icon.gameObject);
GameObject.Destroy(buttonComponent.outline.gameObject);
buttonComponent.OnClicked += (UIButtonBase.ButtonAction)PolyModHubButtonClicked;
}
}
static void PolyModHubButtonClicked(int buttonId, BaseEventData eventData)
{
BasicPopup popup = PopupManager.GetBasicPopup();
popup.Header = Localization.Get("polymod.hub");
popup.Description = Localization.Get("polymod.hub.header", new Il2CppSystem.Object[] {
HEADER_PREFIX,
HEADER_POSTFIX
}) + "\n\n";
foreach (var mod in Registry.mods.Values)
{
popup.Description += Localization.Get("polymod.hub.mod", new Il2CppSystem.Object[] {
mod.name,
Localization.Get("polymod.hub.mod.status."
+ Enum.GetName(typeof(Mod.Status), mod.status)!.ToLower()),
string.Join(", ", mod.authors),
mod.version.ToString(),
mod.description ?? ""
});
popup.Description += "\n\n";
}
popup.Description += Localization.Get("polymod.hub.footer", new Il2CppSystem.Object[] {
HEADER_PREFIX,
HEADER_POSTFIX
});
List<PopupBase.PopupButtonData> popupButtons = new()
{
new("buttons.back"),
new(
"polymod.hub.discord",
callback: (UIButtonBase.ButtonAction)((_, _) =>
NativeHelpers.OpenURL(Plugin.DISCORD_LINK, false))
),
new(
"polymod.hub.config",
callback: (UIButtonBase.ButtonAction)((_, _) =>
{
ShowConfigPopup();
})
)
};
if (Plugin.config.debug)
{
popupButtons.Add(new(
"polymod.hub.dump",
callback: (UIButtonBase.ButtonAction)((_, _) =>
{
Directory.CreateDirectory(Plugin.DUMPED_DATA_PATH);
File.WriteAllTextAsync(
Path.Combine(Plugin.DUMPED_DATA_PATH, "gameLogicData.json"),
PolytopiaDataManager.provider.LoadGameLogicData(VersionManager.GameLogicDataVersion)
);
File.WriteAllTextAsync(
Path.Combine(Plugin.DUMPED_DATA_PATH, "avatarData.json"),
PolytopiaDataManager.provider.LoadAvatarData(1337)
);
foreach (var category in LocalizationManager.Sources[0].GetCategories())
File.WriteAllTextAsync(
Path.Combine(Plugin.DUMPED_DATA_PATH, $"localization_{category}.csv"),
LocalizationManager.Sources[0].Export_CSV(category)
);
foreach (KeyValuePair<string, Mod> entry in Registry.mods)
{
foreach (Mod.File file in entry.Value.files)
{
if (Path.GetFileName(file.name) == "sprites.json")
{
File.WriteAllBytes(Path.Combine(Plugin.DUMPED_DATA_PATH, $"sprites_{entry.Key}.json"), file.bytes);
}
}
}
foreach (TribeData.Type type in Enum.GetValues(typeof(TribeData.Type)))
{
List<Visual.PreviewTile> previewTiles = new();
SelectTribePopup popup = PopupManager.GetSelectTribePopup();
for (int x = -3; x <= 3; x++)
{
for (int y = -7; y <= 7; y++)
{
Vector2Int pos = new Vector2Int(x, y);
if (popup.UIWorldPreview.worldPreviewData.TryGetData(pos, type, out UITileData tileData))
{
Visual.PreviewTile previewTile = new Visual.PreviewTile
{
x = tileData.Position.x,
y = tileData.Position.y,
terrainType = tileData.terrainType,
resourceType = tileData.resourceType,
unitType = tileData.unitType,
improvementType = tileData.improvementType
};
previewTiles.Add(previewTile);
}
}
}
File.WriteAllTextAsync(
Path.Combine(Plugin.DUMPED_DATA_PATH, $"preview_{type}.json"),
JsonSerializer.Serialize(previewTiles, new JsonSerializerOptions { WriteIndented = true })
);
}
NotificationManager.Notify(Localization.Get("polymod.hub.dumped"));
}),
closesPopup: false
));
popupButtons.Add(new(
"polymod.hub.spriteinfo.update",
callback: (UIButtonBase.ButtonAction)((_, _) =>
{
UpdateSpriteInfos();
}),
closesPopup: false
));
}
popup.buttonData = popupButtons.ToArray();
popup.ShowSetWidth(POPUP_WIDTH);
}
if (Main.dependencyCycle)
{
var popup = PopupManager.GetBasicPopup(new(
Localization.Get("polymod.cycle"),
Localization.Get("polymod.cycle.description"),
new(new PopupBase.PopupButtonData[] {
new(
"buttons.exitgame",
PopupBase.PopupButtonData.States.None,
(Il2CppSystem.Action)Application.Quit,
closesPopup: false,
customColorStates: ColorConstants.redButtonColorStates
)
})
));
popup.IsUnskippable = true;
popup.Show();
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(GameManager), nameof(GameManager.Update))]
private static void GameManager_Update()
{
if (Input.GetKey(KeyCode.LeftControl) && Input.GetKeyDown(KeyCode.Tab) && !isConfigPopupActive)
{
ShowConfigPopup();
}
}
internal static void UpdateSpriteInfos()
{
string message = string.Empty;
Directory.CreateDirectory(Plugin.DUMPED_DATA_PATH);
foreach (var file in Directory.GetFiles(Plugin.DUMPED_DATA_PATH))
{
string? name = Path.GetFileNameWithoutExtension(file);
List<string> subnames = new();
if (name.Contains("sprites_"))
{
subnames = name.Split('_').ToList();
Mod.File spriteInfo = new(Path.GetFileNameWithoutExtension(file), File.ReadAllBytes(file));
Dictionary<string, Visual.SpriteInfo>? deserialized = Loader.LoadSpriteInfoFile(Registry.mods[subnames[1]], spriteInfo);
if (deserialized != null)
{
foreach (var kvp in deserialized)
{
Loader.UpdateSprite(kvp.Key);
}
message += Localization.Get("polymod.spriteinfo.updated", new Il2CppSystem.Object[] { subnames[1] });
}
}
}
if (message == string.Empty)
{
message = Localization.Get("polymod.spriteinfo.notupdated");
}
NotificationManager.Notify(message);
}
internal static void ShowConfigPopup()
{
BasicPopup polymodPopup = PopupManager.GetBasicPopup();
polymodPopup.Header = Localization.Get("polymod.hub.config");
polymodPopup.Description = "";
polymodPopup.buttonData = CreateConfigPopupButtonData();
polymodPopup.ShowSetWidth(POPUP_WIDTH);
polymodPopup.Show();
}
internal static PopupButtonData[] CreateConfigPopupButtonData()
{
List<PopupButtonData> popupButtons = new()
{
new(Localization.Get("buttons.back"), PopupButtonData.States.None, (UIButtonBase.ButtonAction)OnBackButtonClicked, -1, true, null)
};
if (GameManager.Instance.isLevelLoaded)
{
popupButtons.Add(new PopupButtonData(Localization.Get("polymod.hub.spriteinfo.update"), PopupButtonData.States.None, (UIButtonBase.ButtonAction)OnUpdateSpritesButtonClicked, -1, true, null));
}
else
{
string debugButtonName = Localization.Get(
Plugin.config.debug ? "polymod.hub.config.disable" : "polymod.hub.config.enable",
new Il2CppSystem.Object[] { Localization.Get("polymod.debug",
new Il2CppSystem.Object[]{}).ToUpperInvariant() }
);
string autoUpdateButtonName = Localization.Get(
Plugin.config.autoUpdate ? "polymod.hub.config.disable" : "polymod.hub.config.enable",
new Il2CppSystem.Object[] { Localization.Get("polymod.autoupdate",
new Il2CppSystem.Object[]{}).ToUpperInvariant() }
);
string includeAlphasButtonName = Localization.Get(
Plugin.config.updatePrerelease ? "polymod.hub.config.disable" : "polymod.hub.config.enable",
new Il2CppSystem.Object[] { Localization.Get("polymod.autoupdate.alpha",
new Il2CppSystem.Object[]{}).ToUpperInvariant() }
);
popupButtons.Add(new PopupButtonData(debugButtonName, PopupButtonData.States.None, (UIButtonBase.ButtonAction)OnDebugButtonClicked, -1, true, null));
popupButtons.Add(new PopupButtonData(autoUpdateButtonName, PopupButtonData.States.None, (UIButtonBase.ButtonAction)OnAutoUpdateButtonClicked, -1, true, null));
popupButtons.Add(new PopupButtonData(includeAlphasButtonName, Plugin.config.autoUpdate ? PopupButtonData.States.None : PopupButtonData.States.Disabled, (UIButtonBase.ButtonAction)OnIncludeAlphasButtonClicked, -1, true, null));
}
return popupButtons.ToArray();
void OnDebugButtonClicked(int buttonId, BaseEventData eventData)
{
Plugin.config = new(debug: !Plugin.config.debug, autoUpdate: Plugin.config.autoUpdate, updatePrerelease: Plugin.config.updatePrerelease);
Plugin.WriteConfig();
Plugin.UpdateConsole();
NotificationManager.Notify(Localization.Get(
"polymod.config.setto",
new Il2CppSystem.Object[] { Localization.Get("polymod.debug",
new Il2CppSystem.Object[]{}), Plugin.config.debug }
));
isConfigPopupActive = false;
}
void OnAutoUpdateButtonClicked(int buttonId, BaseEventData eventData)
{
Plugin.config = new(debug: Plugin.config.debug, autoUpdate: !Plugin.config.autoUpdate, updatePrerelease: Plugin.config.updatePrerelease);
Plugin.WriteConfig();
Plugin.UpdateConsole();
NotificationManager.Notify(Localization.Get(
"polymod.config.setto",
new Il2CppSystem.Object[] { Localization.Get("polymod.autoupdate",
new Il2CppSystem.Object[]{}), Plugin.config.autoUpdate }
));
isConfigPopupActive = false;
}
void OnIncludeAlphasButtonClicked(int buttonId, BaseEventData eventData)
{
Plugin.config = new(debug: Plugin.config.debug, autoUpdate: Plugin.config.autoUpdate, updatePrerelease: !Plugin.config.updatePrerelease);
Plugin.WriteConfig();
Plugin.UpdateConsole();
NotificationManager.Notify(Localization.Get(
"polymod.config.setto",
new Il2CppSystem.Object[] { Localization.Get("polymod.autoupdate.alpha",
new Il2CppSystem.Object[]{}), Plugin.config.updatePrerelease }
));
isConfigPopupActive = false;
}
void OnUpdateSpritesButtonClicked(int buttonId, BaseEventData eventData)
{
UpdateSpriteInfos();
isConfigPopupActive = false;
}
void OnBackButtonClicked(int buttonId, BaseEventData eventData)
{
isConfigPopupActive = false;
}
}
internal static void Init()
{
Harmony.CreateAndPatchAll(typeof(Hub));
}
}