-
Notifications
You must be signed in to change notification settings - Fork 2
/
PolyTechMain.cs
1049 lines (933 loc) · 42.8 KB
/
PolyTechMain.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Timers;
using UnityEngine;
using TMPro;
using Logger = BepInEx.Logging.Logger;
namespace PolyTechFramework
{
[BepInPlugin(PluginGuid, PluginName, PluginVersion)]
[BepInProcess("Poly Bridge 2")]
[BepInDependency(ConfigurationManager.ConfigurationManager.GUID, BepInDependency.DependencyFlags.HardDependency)]
public class PolyTechMain : PolyTechMod
{
public new const string
PluginGuid = "polytech.polytechframework",
PluginName = "PolyTech Framework",
PluginVersion = "0.9.7";
private static BindingList<PolyTechMod>
noncheatMods = new BindingList<PolyTechMod> { },
cheatMods = new BindingList<PolyTechMod> { };
private static List<PolyTechMod> modsDisabledByPTF = new List<PolyTechMod> { };
private static string modsToggledSummary = "";
public static ConfigDefinition
moddedWatermarkDef = new ConfigDefinition("PolyTech Framework", "Modded Watermark"),
vanillaWatermarkDef = new ConfigDefinition("PolyTech Framework", "Vanilla Watermark"),
modEnabledDef = new ConfigDefinition("PolyTech Framework", "Enabled"),
forceCheatDef = new ConfigDefinition("PolyTech Framework", "Force Cheat Flag"),
sandboxEverywhereDef = new ConfigDefinition("PolyTech Framework", "Sandbox Everywhere"),
globalToggleHotkeyDef = new ConfigDefinition("PolyTech Framework", "Global Toggle Hotkey"),
checkModUpdatesDef = new ConfigDefinition("PolyTech Framework", "Check for Mod Updates"),
leaderboardProtMinDef = new ConfigDefinition("Leaderboard Protection", "Minimum Score"),
leaderboardCheckDef = new ConfigDefinition("Leaderboard Protection", "Confirm Before Upload"),
leaderboardBlockDef = new ConfigDefinition("Leaderboard Protection", "Block All Scores");
public static ConfigEntry<watermarks>
moddedWatermark,
vanillaWatermark;
public static ConfigEntry<bool>
modEnabled,
forceCheat,
sandboxEverywhere,
leaderboardCheck,
leaderboardBlock,
checkModUpdates;
public static ConfigEntry<int>
leaderboardProtMin;
public static ConfigEntry<BepInEx.Configuration.KeyboardShortcut>
globalToggleHotkey;
public static int
enabledCheatTweaks;
public static PolyTechMain
ptfInstance;
public BepInEx.Logging.ManualLogSource ptfLogger;
public bool modCheated;
GameObject
autoDraw,
sandbox,
sandboxSettings,
sandboxCreate,
sandboxVehicles,
sandboxWorkshop,
sandboxResources;
public enum watermarks
{
[Description("PolyTech Style Watermark")]
polytech,
[Description("PolyBridge Style Watermark")]
polybridge
}
Harmony harmony;
public void Awake()
{
moddedWatermark = Config.Bind(moddedWatermarkDef, watermarks.polytech, new ConfigDescription("Selected Watermark"));
vanillaWatermark = Config.Bind(vanillaWatermarkDef, watermarks.polytech, new ConfigDescription("Selected Watermark"));
modEnabled = Config.Bind(modEnabledDef, true, new ConfigDescription("Enable Mod"));
modEnabled.SettingChanged += onEnableDisable;
forceCheat = Config.Bind(forceCheatDef, false, new ConfigDescription("Force Cheat Flag"));
forceCheat.SettingChanged += onForceCheatToggle;
sandboxEverywhere = Config.Bind(sandboxEverywhereDef, false, new ConfigDescription("Allow sandbox resource editor and scene changer in any level (enables cheat flag)"));
sandboxEverywhere.SettingChanged += sandboxEverywhereToggle;
globalToggleHotkey = Config.Bind(globalToggleHotkeyDef, new BepInEx.Configuration.KeyboardShortcut(KeyCode.BackQuote, KeyCode.LeftAlt), new ConfigDescription("Keybind used to toggle mods without opening the config menu."));
checkModUpdates = Config.Bind(checkModUpdatesDef, true, new ConfigDescription("Checks if new versions of installed mods are available on game startup"));
leaderboardProtMin = Config.Bind(leaderboardProtMinDef, 71, new ConfigDescription("Minimum value allowed to upload to leaderboard. 71 is the minimum to protect from automatic shadowbans."));
leaderboardProtMin.SettingChanged += onLeaderboardProtChange;
leaderboardCheck = Config.Bind(leaderboardCheckDef, false, new ConfigDescription("If checked, the game will confirm with the user before uploading scores to the leaderboard."));
leaderboardBlock = Config.Bind(leaderboardBlockDef, false, new ConfigDescription("If checked, the game will never upload a score to the leaderboard."));
noncheatMods.ListChanged += onCosmeticsChanged;
cheatMods.ListChanged += onCheatsChanged;
enabledCheatTweaks = 0 + (forceCheat.Value ? 1 : 0) + (sandboxEverywhere.Value ? 1 : 0);
this.modCheated = false;
this.repositoryUrl = "https://github.com/PolyTech-Modding/PolyTechFramework/";
harmony = new Harmony(PluginGuid);
harmony.PatchAll(typeof(PolyTechMain));
harmony.PatchAll(typeof(PolyTechMain).Assembly);
PolyTechUtils.setModdedSimSpeeds();
PolyTechUtils.setReplaysModded();
PolyTechUtils.setVersion();
this.ptfLogger = Logger;
Logger.LogInfo($"Loaded {PluginName} v{PluginVersion}");
this.isCheat = false;
this.isEnabled = modEnabled.Value;
ptfInstance = this;
this.authors = new string[] {"MoonlitJolty", "Conqu3red", "Razboy20", "Tran Fox", "nitsuga5124", "hippolippo"};
registerMod(this);
}
bool flag = false;
private void Update()
{
PopupQueue.TryShowNextPopup();
if (numEnabledCheatMods() > 0 && Bridge.IsSimulating() && !BridgeCheat.m_Cheated){
GameStateSim.m_BudgetUsed = Mathf.RoundToInt(Budget.CalculateBridgeCost());
BridgeCheat.m_Cheated = BridgeCheat.CheckForCheating((float)GameStateSim.m_BudgetUsed);
}
if (!flag && globalToggleHotkey.Value.IsDown())
{
flag = true;
if (this.isEnabled)
{
this.isEnabled = modEnabled.Value = false;
this.disableMod();
}
else
{
this.isEnabled = modEnabled.Value = true;
this.enableMod();
}
if (modsToggledSummary.Length > 0) PopUpWarning.Display(modsToggledSummary);
//Logger.LogMessage(modsToggledSummary);
}
else if (flag & globalToggleHotkey.Value.IsUp())
{
flag = false;
}
if (autoDraw == null)
{
autoDraw = GameObject.Find("GameUI/Panel_TopBar/HorizontalLayout/GridStress/ButtonsHorizontalLayout/ButtonContainer_AutoDraw");
if (autoDraw == null) return;
autoDraw.SetActive(true);
sandbox = GameObject.Find("GameUI/Panel_TopBar/HorizontalLayout/CenterInfo/Sandbox");
sandboxSettings = GameObject.Find("GameUI/Panel_TopBar/HorizontalLayout/CenterInfo/Sandbox/ButtonsHorizontalLayout/Button_SandboxSettings");
sandboxCreate = GameObject.Find("GameUI/Panel_TopBar/HorizontalLayout/CenterInfo/Sandbox/ButtonsHorizontalLayout/Button_Create");
sandboxVehicles = GameObject.Find("GameUI/Panel_TopBar/HorizontalLayout/CenterInfo/Sandbox/ButtonsHorizontalLayout/Button_Vehicles");
sandboxWorkshop = GameObject.Find("GameUI/Panel_TopBar/HorizontalLayout/CenterInfo/Sandbox/ButtonsHorizontalLayout/Button_Workshop");
sandboxResources = GameObject.Find("GameUI/Panel_TopBar/HorizontalLayout/CenterInfo/Sandbox/ButtonsHorizontalLayout/Button_Resources");
}
sandboxCreate.SetActive(GameStateManager.GetState() == GameState.SANDBOX);
sandboxVehicles.SetActive(GameStateManager.GetState() == GameState.SANDBOX);
sandboxWorkshop.SetActive(GameStateManager.GetState() == GameState.SANDBOX);
sandboxSettings.SetActive(true);
sandboxResources.SetActive(true);
sandbox.SetActive((sandboxEverywhere.Value && PolyTechMain.modEnabled.Value) || GameStateManager.GetState() == GameState.SANDBOX);
}
public void onCosmeticsChanged(object sender, ListChangedEventArgs e)
{
if (e.ListChangedType == ListChangedType.ItemDeleted) return;
PolyTechMod mod = noncheatMods[noncheatMods.Count - 1];
string nameAndVers = $"{mod.Info.Metadata.Name} v{mod.Info.Metadata.Version}";
Logger.LogInfo("Registered cosmetic mod: " + nameAndVers);
}
public void onCheatsChanged(object sender, ListChangedEventArgs e)
{
if (e.ListChangedType == ListChangedType.ItemDeleted) return;
PolyTechMod mod = cheatMods[cheatMods.Count - 1];
string nameAndVers = $"{mod.Info.Metadata.Name} v{mod.Info.Metadata.Version}";
Logger.LogInfo("Registered cheat mod: " + nameAndVers);
}
public static int numEnabledCheatMods()
{
int result = 0;
foreach (PolyTechMod mod in cheatMods)
{
if (mod.isEnabled) result++;
}
return result;
}
public static void registerMod(PolyTechMod plugin)
{
if (plugin.isCheat)
{
cheatMods.Add(plugin);
}
else
{
noncheatMods.Add(plugin);
}
checkForModUpdate(plugin);
}
public static void setCheat(PolyTechMod plugin, bool isCheat){
if(plugin.isCheat == isCheat) return;
if (isCheat)
{
noncheatMods.Remove(plugin);
cheatMods.Add(plugin);
}
else
{
cheatMods.Remove(plugin);
noncheatMods.Add(plugin);
}
plugin.isCheat = isCheat;
}
public override void enableMod()
{
string summary = "Mods Enabled: PolyTechFramework";
foreach (PolyTechMod mod in modsDisabledByPTF)
{
mod.enableMod();
summary += $" - {mod.Info.Metadata.Name}";
}
modsToggledSummary = summary;
PolyTechUtils.setReplaysModded();
}
public override void disableMod()
{
string summary = "Mods Disabled: PolyTechFramework";
modsDisabledByPTF.Clear();
foreach (PolyTechMod mod in cheatMods)
{
if (!mod.isEnabled) continue;
modsDisabledByPTF.Add(mod);
mod.disableMod();
summary += $" - {mod.Info.Metadata.Name}";
}
foreach (PolyTechMod mod in noncheatMods)
{
if (!mod.isEnabled) continue;
modsDisabledByPTF.Add(mod);
mod.disableMod();
summary += $" - {mod.Info.Metadata.Name}";
}
PolyTechUtils.setReplaysVanilla();
}
public static void checkForModUpdate(PolyTechMod plugin)
{
if(!checkModUpdates.Value) return;
if (plugin.repositoryUrl == null) return;
var client = new WebClient();
client.Headers.Add("User-Agent", "Nothing");
// get latest release version
string repoReleaseUri = "https://api.github.com/repos" + new Uri(plugin.repositoryUrl).AbsolutePath + "releases";
string content;
try
{
content = client.DownloadString(repoReleaseUri);
}
catch (Exception e)
{
ptfInstance.ptfLogger.LogError(e.Message);
return;
}
// deserialize incoming JSON from repo api
List<Release> releases = null;
using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(content)))
{
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(List<Release>));
releases = (List<Release>)jsonSerializer.ReadObject(ms);
}
if (releases == null) return;
if (releases.Count <= 0) return;
Release latestRelease = releases[0];
if (latestRelease.GetVersion().CompareTo(plugin.Info.Metadata.Version) > 0)
{
ModUpdate modUpdate = new ModUpdate(plugin, latestRelease);
if (patchGameStart.game_started)
{
modUpdatePopup(modUpdate);
}
else
{
patchGameStart.modUpdates.Add(modUpdate);
}
}
}
private static void modUpdatePopup(ModUpdate modUpdate)
{
ptfInstance.ptfLogger.LogInfo($"\n------------------------\n{modUpdate.mod.Info.Metadata.Name} has an update available!\n{modUpdate.old_version} -> {modUpdate.new_version}\n------------------------\n");
PopUpMessage.Display($"{modUpdate.mod.Info.Metadata.Name} has an update available!\n{modUpdate.old_version} -> {modUpdate.new_version}", () => System.Diagnostics.Process.Start(modUpdate.latest_release.html_url));
}
public void onForceCheatToggle(object sender, EventArgs e)
{
updateCheatTweaks(forceCheat.Value);
}
public void sandboxEverywhereToggle(object sender, EventArgs e)
{
updateCheatTweaks(sandboxEverywhere.Value);
}
public void onLeaderboardProtChange(object sender, EventArgs e)
{
if(leaderboardProtMin.Value < 71) leaderboardProtMin.Value = 71;
}
private void updateCheatTweaks(bool value)
{
enabledCheatTweaks += value ? 1 : -1;
this.isCheat = enabledCheatTweaks > 0;
if (enabledCheatTweaks > 0) this.modCheated = true;
}
public void onEnableDisable(object sender, EventArgs e)
{
this.isEnabled = modEnabled.Value;
if (modEnabled.Value)
{
enableMod();
}
else
{
disableMod();
}
}
[HarmonyPatch(typeof(GameManager), "StartManual")]
[HarmonyPostfix]
private static void GameStartPostfix(){
patchGameStart.game_started = true;
if (patchGameStart.modUpdates == null) return;
foreach (ModUpdate modUpdate in patchGameStart.modUpdates)
{
modUpdatePopup(modUpdate);
}
}
private class patchGameStart
{
public static List<ModUpdate> modUpdates = new List<ModUpdate>();
public static bool game_started = false;
}
[HarmonyPatch(typeof(BridgeCheat), "CheckForCheating")]
[HarmonyPrefix]
private static bool PatchCheats(ref bool __result)
{
__result = true;
ptfInstance.modCheated = ptfInstance.modCheated || (modEnabled.Value && numEnabledCheatMods() > 0) || (PolyTechMain.modEnabled.Value && enabledCheatTweaks > 0);
return !ptfInstance.modCheated;
}
[HarmonyPatch(typeof(BridgeCheat), "GetLocalizedCheatReason")]
[HarmonyPrefix]
private static bool PatchCheatTooltop(ref string __result)
{
__result = "Cheat Mods Enabled.";
return !ptfInstance.modCheated;
}
[HarmonyPatch(typeof(GameStateManager), "ChangeState")]
[HarmonyPrefix]
private static void PatchGetState(GameState state)
{
if (state == GameState.MAIN_MENU) ptfInstance.modCheated = false;
}
[HarmonyPatch(typeof(Panel_ShareReplay), "DoFFMpeg")]
[HarmonyPrefix]
private static void PatchWatermark(string inputBasePath, ref string watermarkPath, int videoKbps)
{
string watermarkFolder = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "watermarks");
string watermarkName;
if (ptfInstance.modCheated)
{
switch (moddedWatermark.Value)
{
case watermarks.polytech:
watermarkName = "polytechModdedWatermark.png";
break;
case watermarks.polybridge:
watermarkName = "polybridgeModdedWatermark.png";
break;
default:
watermarkName = "polytechModdedWatermark.png";
break;
}
}
else
{
switch (vanillaWatermark.Value)
{
case watermarks.polytech:
watermarkName = "polytechWatermark.png";
break;
case watermarks.polybridge:
watermarkName = "polybridgeWatermark.png";
break;
default:
watermarkName = "polytechWatermark.png";
break;
}
}
watermarkPath = Path.Combine(watermarkFolder, watermarkName);
}
public static class PopupQueue {
public static Queue<Popup> queue = new Queue<Popup> ();
public static void TryShowNextPopup(){
if (queue.Count > 0 && !PopupIsActive()){
var popup = queue.Dequeue();
popup.Display();
}
}
public static bool PopupIsActive(){
return GameUI.m_Instance.m_PopUpMessage.m_Animator.isActiveAndEnabled || PopUpMessage.IsActive() ||
GameUI.m_Instance.m_PopUpInputField.m_Animator.isActiveAndEnabled || PopupInputField.IsActive() ||
GameUI.m_Instance.m_PopUpTwoChoices.m_Animator.isActiveAndEnabled || PopUpTwoChoices.IsActive() ||
GameUI.m_Instance.m_PopUpWarning.m_Animator.isActiveAndEnabled || PopUpWarning.IsActive();
}
}
public interface Popup {
void Display();
}
public class PopupMessageQueueItem : Popup {
public PopupMessageQueueItem(){}
public PopupMessageQueueItem(
string message,
Panel_PopUpMessage.OnChoiceDelegate okDelegate,
Panel_PopUpMessage.OnChoiceDelegate cancelDelegate,
PopUpWarningCategory warningCategory
){
this.message = message;
this.okDelegate = okDelegate;
this.cancelDelegate = cancelDelegate;
this.warningCategory = warningCategory;
}
public void Display(){
PopUpMessage.Display(message, okDelegate, cancelDelegate, warningCategory);
GameUI.m_Instance.m_PopUpMessage.m_NeverShowAgainToggle.transform.parent.gameObject.SetActive(false);
if (cancelDelegate == null){
GameUI.m_Instance.m_PopUpMessage.m_CancelButton.gameObject.SetActive(false);
}
}
public string message;
public Panel_PopUpMessage.OnChoiceDelegate okDelegate;
public Panel_PopUpMessage.OnChoiceDelegate cancelDelegate;
public PopUpWarningCategory warningCategory;
}
[HarmonyPatch(typeof(PopUpMessage), "Display", new Type[] {
typeof(string),
typeof(Panel_PopUpMessage.OnChoiceDelegate),
})]
[HarmonyPrefix]
public static bool PopupMessageCancelButtonFix(
string message,
Panel_PopUpMessage.OnChoiceDelegate okDelegate
){
PopUpMessage.Display(message, okDelegate, () => {}, PopUpWarningCategory.NONE);
GameUI.m_Instance.m_PopUpMessage.m_NeverShowAgainToggle.transform.parent.gameObject.SetActive(false);
return false;
}
[HarmonyPatch(typeof(PopUpMessage), "Display", new Type[] {
typeof(string),
typeof(Panel_PopUpMessage.OnChoiceDelegate),
typeof(Panel_PopUpMessage.OnChoiceDelegate),
typeof(PopUpWarningCategory)
})]
[HarmonyPrefix]
public static bool PopupMessagePatch(
string message,
Panel_PopUpMessage.OnChoiceDelegate okDelegate,
Panel_PopUpMessage.OnChoiceDelegate cancelDelegate,
PopUpWarningCategory warningCategory
){
if (PopupQueue.PopupIsActive()){
ptfInstance.ptfLogger.LogInfo("popup is already active, queueing!");
PopupMessageQueueItem QueueItem = new PopupMessageQueueItem(
message,
okDelegate,
cancelDelegate,
warningCategory
);
PopupQueue.queue.Enqueue(QueueItem);
return false;
}
return true;
}
public class PopupInputFieldQueueItem : Popup {
public PopupInputFieldQueueItem(){}
public PopupInputFieldQueueItem(
string title,
string defaultText,
Panel_PopUpInputField.OnOkDelegate okDelegate
){
this.title = title;
this.defaultText = defaultText;
this.okDelegate = okDelegate;
}
public void Display(){
PopupInputField.Display(
title,
defaultText,
okDelegate
);
}
public string title;
public string defaultText;
public Panel_PopUpInputField.OnOkDelegate okDelegate;
}
[HarmonyPatch(typeof(PopupInputField), "Display", new Type[] {
typeof(string),
typeof(string),
typeof(Panel_PopUpInputField.OnOkDelegate)
})]
[HarmonyPrefix]
public static bool PopupMessageInputFieldPatch(
string title,
string defaultText,
Panel_PopUpInputField.OnOkDelegate okDelegate
){
if (PopupQueue.PopupIsActive()){
ptfInstance.ptfLogger.LogInfo("popup is already active, queueing!");
PopupInputFieldQueueItem QueueItem = new PopupInputFieldQueueItem(
title,
defaultText,
okDelegate
);
PopupQueue.queue.Enqueue(QueueItem);
return false;
}
return true;
}
public class PopupTwoChoicesQueueItem : Popup {
public PopupTwoChoicesQueueItem() {}
public PopupTwoChoicesQueueItem(
string message,
string choiceA,
string choiceB,
Panel_PopUpTwoChoices.OnChoiceDelegate callbackA,
Panel_PopUpTwoChoices.OnChoiceDelegate callbackB
){
this.message = message;
this.choiceA = choiceA;
this.choiceB = choiceB;
this.callbackA = callbackA;
this.callbackB = callbackB;
}
public void Display(){
PopUpTwoChoices.Display(
message,
choiceA,
choiceB,
callbackA,
callbackB,
PopUpWarningCategory.NONE
);
GameUI.m_Instance.m_PopUpTwoChoices.m_NeverShowAgainToggle.transform.parent.gameObject.SetActive(false);
}
string message;
string choiceA;
string choiceB;
Panel_PopUpTwoChoices.OnChoiceDelegate callbackA;
Panel_PopUpTwoChoices.OnChoiceDelegate callbackB;
}
[HarmonyPatch(typeof(PopUpTwoChoices), "Display", new Type[] {
typeof(string),
typeof(string),
typeof(string),
typeof(Panel_PopUpTwoChoices.OnChoiceDelegate),
typeof(Panel_PopUpTwoChoices.OnChoiceDelegate),
typeof(PopUpWarningCategory)
})]
[HarmonyPrefix]
public static bool PopupTwoChoicesPatch(
string message,
string choiceA,
string choiceB,
Panel_PopUpTwoChoices.OnChoiceDelegate callbackA,
Panel_PopUpTwoChoices.OnChoiceDelegate callbackB,
PopUpWarningCategory warningCategory
){
if (PopupQueue.PopupIsActive()){
ptfInstance.ptfLogger.LogInfo("popup is already active, queueing!");
PopupTwoChoicesQueueItem QueueItem = new PopupTwoChoicesQueueItem(
message,
choiceA,
choiceB,
callbackA,
callbackB
);
PopupQueue.queue.Enqueue(QueueItem);
return false;
}
return true;
}
public class PopupWarningQueueItem : Popup {
public PopupWarningQueueItem() {}
public PopupWarningQueueItem(
string message,
PopUpWarningCategory category
){
this.message = message;
this.category = category;
}
public void Display(){
PopUpWarning.Display(
message,
category
);
GameUI.m_Instance.m_PopUpWarning.m_NeverShowAgainToggle.transform.parent.gameObject.SetActive(false);
}
public string message;
public PopUpWarningCategory category;
}
[HarmonyPatch(typeof(PopUpWarning), "Display", new Type[] {
typeof(string),
typeof(PopUpWarningCategory)
})]
[HarmonyPrefix]
public static bool PopupWarningPatch(
string message,
PopUpWarningCategory category
){
if (PopupQueue.PopupIsActive()){
ptfInstance.ptfLogger.LogInfo("popup is already active, queueing!");
PopupWarningQueueItem QueueItem = new PopupWarningQueueItem(
message,
category
);
PopupQueue.queue.Enqueue(QueueItem);
return false;
}
return true;
}
[HarmonyPatch(typeof(Replays), "CreateReplayMovieDirectory")]
[HarmonyPrefix]
private static bool PatchReplays()
{
Replays.m_Path = Path.Combine(GamePersistentPath.GetPersistentDataDirectory(), Replays.REPLAYS_DIRECTORY);
Utils.TryToCreateDirectory(Replays.m_Path);
Replays.m_Path = Path.Combine(Replays.m_Path, "modded");
Utils.TryToCreateDirectory(Replays.m_Path);
Replays.m_PathForPublicDisplay = Path.Combine(GamePersistentPath.GetCensoredPersistentDataDirectory(), Replays.REPLAYS_DIRECTORY);
return !ptfInstance.modCheated;
}
[HarmonyPatch(typeof(Panel_ShareReplay), "CreateGalleryUploadBody")]
[HarmonyPostfix]
private static void PatchGalleryUploadBody(ref GalleryUploadBody __result)
{
//ptfInstance.ptfLogger.LogMessage($"Uploading video to gallery, modCheated = {ptfInstance.modCheated}");
if (ptfInstance.modCheated)
{
__result.m_MaxStress = 42069;
__result.m_BudgetUsed = int.MaxValue;
}
}
public static bool isSavingAnyModData() {
return noncheatMods.Where(x => x.isEnabled && x.shouldSaveData).Count() + cheatMods.Where(x => x.isEnabled).Count() > 0;
}
[HarmonyPatch(typeof(SandboxLayoutData), "SerializePreBridgeBinary")]
[HarmonyPrefix]
static void patchSerializerOne(SandboxLayoutData __instance, List<byte> bytes)
{
ptfInstance.ptfLogger.LogMessage($"Layout pre version: {__instance.m_Version}");
if (GameStateManager.GetState() != GameState.BUILD && GameStateManager.GetState() != GameState.SANDBOX) return;
if (!isSavingAnyModData()) return;
__instance.m_Version *= -1;
//PopUpMessage.Display("You have cheat mods enabled, do you want to store them?\n(This will make the layout incompatible with vanilla PB2)", yes, no);
ptfInstance.ptfLogger.LogMessage($"Version after cheat question: {__instance.m_Version.ToString()}");
}
[HarmonyPatch(typeof(SandboxLayoutData), "SerializePostBridgeBinary")]
[HarmonyPostfix]
private static void patchSerializerTwo(SandboxLayoutData __instance, List<byte> bytes)
{
ptfInstance.ptfLogger.LogMessage($"Layout post version: {__instance.m_Version}");
// add number of mods stored
//bytes.AddRange(ByteSerializer.SerializeInt(noncheatMods.Where(x => x.shouldSaveData).Count() + cheatMods.Where(x => x.shouldSaveData).Count()));
// add mod data for each mod
// make sure to be backwards compatible!
List<string> modData = cheatMods.Where(x => x.isEnabled).Select(x => $"{x.Info.Metadata.Name}\u058D{x.Info.Metadata.Version}\u058D{x.getSettings()}").ToList();
modData.AddRange(noncheatMods.Where(x => x.shouldSaveData && x.isEnabled).Select(x => $"{x.Info.Metadata.Name}\u058D{x.Info.Metadata.Version}\u058D{x.getSettings()}").ToList());
string[] mods = modData.ToArray();
if (__instance.m_Version >= 0) return;
bytes.AddRange(ByteSerializer.SerializeStrings(mods));
// add an int indicating the number of mods that will save binary data
int modsSavingBinary = noncheatMods.Where(x => x.shouldSaveData).Count() + cheatMods.Where(x => x.shouldSaveData).Count();
bytes.AddRange(ByteSerializer.SerializeInt(modsSavingBinary));
foreach (var mod in noncheatMods){
if (mod.isEnabled && mod.shouldSaveData){
bytes.AddRange(ByteSerializer.SerializeString(
$"{mod.Info.Metadata.Name}\u058D{mod.Info.Metadata.Version}"
));
bytes.AddRange(ByteSerializer.SerializeByteArray(
mod.saveData()
));
}
}
foreach (var mod in cheatMods){
if (mod.isEnabled && mod.shouldSaveData){
bytes.AddRange(ByteSerializer.SerializeString(
$"{mod.Info.Metadata.Name}\u058D{mod.Info.Metadata.Version}"
));
bytes.AddRange(ByteSerializer.SerializeByteArray(
mod.saveData()
));
}
}
ptfInstance.ptfLogger.LogMessage($"Serialized {mods.Length.ToString()} Mod Names");
}
[HarmonyPatch(typeof(SandboxLayoutData), "DeserializeBinary")]
[HarmonyPrefix]
public static void patchDeserializerPrefix(SandboxLayoutData __instance, byte[] bytes, ref int offset, ref bool __state)
{
__state = false;
var startOffset = offset;
__instance.m_Version = ByteSerializer.DeserializeInt(bytes, ref offset);
offset = startOffset;
//ptfInstance.ptfLogger.LogMessage($"Layout version pre-modcheck: {__instance.m_Version}");
if (__instance.m_Version > 0) return;
__instance.m_Version *= -1;
__state = true;
byte[] new_ver = ByteSerializer.SerializeInt(__instance.m_Version);
for (int i = 0; i < new_ver.Length; i++)
{
bytes[i] = new_ver[i];
}
//ptfInstance.ptfLogger.LogMessage($"Layout version post-modcheck: {__instance.m_Version}");
}
[HarmonyPatch(typeof(SandboxLayoutData), "DeserializeBinary")]
[HarmonyPostfix]
public static void patchDeserializerPostfix(SandboxLayoutData __instance, byte[] bytes, ref int offset, bool __state)
{
//ptfInstance.ptfLogger.LogMessage($"Layout version pre-load: {__instance.m_Version}");
if (!__state) return;
string[] strings = ByteSerializer.DeserializeStrings(bytes, ref offset);
ptfInstance.ptfLogger.LogInfo($"Layout created with mod{(strings.Length > 1 ? "s" : "")}: ");
foreach (string str in strings)
{
string[] partsOfMod = str.Split('\u058D');
string name = partsOfMod.Length >= 1 ? partsOfMod[0] : null;
string version = partsOfMod.Length >= 2 ? partsOfMod[1] : null;
string settings = partsOfMod.Length >= 3 ? partsOfMod[2] : null;
ptfInstance.ptfLogger.LogInfo($" -- {str.Replace("\u058D", " - v")}");
var currMod = cheatMods.Where(p => p.Info.Metadata.Name == name).FirstOrDefault();
if (currMod == null) currMod = noncheatMods.Where(p => p.Info.Metadata.Name == name).FirstOrDefault();
ptfInstance.checkMods(0, name, version, settings, currMod);
}
if (offset == bytes.Length) return;
int extraSaveDataCount = ByteSerializer.DeserializeInt(bytes, ref offset);
if (extraSaveDataCount == 0) return;
ptfInstance.Logger.LogInfo($"Layout created with custom data from mods: ");
for (int i = 0; i < extraSaveDataCount; i++){
string modIdentifier = ByteSerializer.DeserializeString(bytes, ref offset);
byte[] customModSaveData = ByteSerializer.DeserializeByteArray(bytes, ref offset);
string[] partsOfMod = modIdentifier.Split('\u058D');
string name = partsOfMod.Length >= 1 ? partsOfMod[0] : null;
string version = partsOfMod.Length >= 2 ? partsOfMod[1] : null;
ptfInstance.Logger.LogInfo($" -- {name} - v{version}");
var currMod = cheatMods.Where(p => p.Info.Metadata.Name == name).FirstOrDefault();
if (currMod == null) currMod = noncheatMods.Where(p => p.Info.Metadata.Name == name).FirstOrDefault();
if (currMod == null) return;
if (currMod.Info.Metadata.Version.ToString() != version) return;
currMod.loadData(customModSaveData);
}
}
void checkMods(int step, string name, string version, string settings, PolyTechMod currMod)
{
if (currMod == null || (step <= 0 && currMod.Info.Metadata.Name != name)) missingMod(name, version, settings, currMod);
else if (step <= 1 && currMod.Info.Metadata.Version.ToString() != version) wrongVersion(name, version, settings, currMod);
else if (step <= 2 && !currMod.isEnabled) notEnabled(name, version, settings, currMod);
else if (step <= 3 && currMod.getSettings() != settings) wrongSettings(name, version, settings, currMod);
}
void missingMod(string name, string version, string settings, PolyTechMod currMod)
{
ptfInstance.ptfLogger.LogWarning("Mod in layout not present.");
PopUpMessage.Display(
$"Mod ({name}) in layout not present.",
() => {}
);
}
void wrongVersion(string name, string version, string settings, PolyTechMod currMod)
{
ptfInstance.ptfLogger.LogWarning("Mod in layout present, but not the correct version.");
PopUpMessage.Display(
$"Mod ({name}) in layout present, but not the correct version. (Made with {version}, Currently has {cheatMods.Where(p => p.Info.Metadata.Name == name).First().Info.Metadata.Version.ToString()})",
() => checkMods(2, name, version, settings, currMod)
);
}
void notEnabled(string name, string version, string settings, PolyTechMod currMod)
{
ptfInstance.ptfLogger.LogWarning("Mod in layout present but not enabled.");
PopUpTwoChoices.Display(
$"Mod ({name}) in layout present but not enabled.",
"Enable Mod",
"Ignore Warning", () =>
{
currMod.enableMod();
checkMods(3, name, version, settings, currMod);
},
() =>
{
ptfInstance.ptfLogger.LogWarning("Ignored the mod being disabled");
checkMods(3, name, version, settings, currMod);
}
);
}
void wrongSettings(string name, string version, string settings, PolyTechMod currMod)
{
ptfInstance.ptfLogger.LogWarning("Mod in layout but settings are not correct.");
PopUpTwoChoices.Display(
$"Mod ({name}) but settings are not correct.",
"Fix Settings Automatically",
"Ignore Warning",
() =>
{
currMod.setSettings(settings);
},
() =>
{
ptfInstance.ptfLogger.LogWarning("Ignored the mod being disabled");
}
);
}
[HarmonyPatch(typeof(Version), "VERSION", MethodType.Getter)]
[HarmonyPostfix]
private static void PatchVersion(ref string __result)
{
__result = $"{__result} - PTF v{PolyTechMain.PluginVersion}";
}
[HarmonyPatch(typeof(Main), "Awake")]
[HarmonyPostfix]
private static void PatchMainAwake()
{
string cosmetics = "";
string cheats = "";
foreach (PolyTechMod mod in PolyTechMain.noncheatMods)
{
cosmetics += $"\n{mod.Info.Metadata.Name} - v{mod.Info.Metadata.Version}";
}
foreach (PolyTechMod mod in PolyTechMain.cheatMods)
{
cheats += $"\n{mod.Info.Metadata.Name} - v{mod.Info.Metadata.Version}";
}
ptfInstance.ptfLogger.LogMessage($"Game Started with the following Cosmetic mods: {cosmetics}");
ptfInstance.ptfLogger.LogMessage($"Game Started with the following Cheat mods: {cheats}");
}
[HarmonyPatch]
static class uploadSteamScorePatch
{
static bool Prepare()
{
return TargetMethod() != null;
}
static MethodInfo TargetMethod()
{
var steamStatsType = typeof(GameStateManager).Assembly.GetType("SteamStatsAndAchievements");
if (steamStatsType == null) return null;
return AccessTools.Method(steamStatsType, "UploadLeaderboardScore");
}
static bool Prefix(int score, bool didBreak) {
return score >= leaderboardProtMin.Value;
}
}
[HarmonyPatch(typeof(LeaderBoards), "UploadScoreAsync")]
[HarmonyPrefix]
public static bool uploadScorePatch(
LeaderboardsUploadBody body,
string levelID,
bool didBreak,
LeaderboardUploadScore.OnUploadScoreDelegate callback,
Queue<LeaderboardUploadScore> ___m_ScoresToUpload
)
{
int score = body.m_Value;
int minScore = leaderboardProtMin.Value;
bool allowedBudget = score >= minScore;
if(leaderboardBlock.Value)
{
if(allowedBudget) PopUpWarning.Display($"Your score would be {score}, however you have blocked all scores from being uploaded in the PTF settings.");
else PopUpWarning.Display($"Your score ({score}) was below the minimum set in the PTF settings ({minScore}).");
GameUI.m_Instance.m_LevelComplete.m_LeaderboardPanel.ForceRefresh();
return false;
}
if(!allowedBudget)
{
PopUpWarning.Display($"Your score {score} was below the minimum budget {minScore} and as such will not be submitted.");
GameUI.m_Instance.m_LevelComplete.m_LeaderboardPanel.ForceRefresh();
return false;
}
//PopUpMessage.Display($"Your score {score} was above or equal to the minimum budget {leaderboardProtMin.Value}.", () => {});
if (leaderboardCheck.Value)
{
PopUpMessage.Display($"Would you like to upload your score of {score} to the leaderboard?",
() => {
// On Yes
LeaderboardUploadScore item = new LeaderboardUploadScore(body, didBreak, levelID, callback);
___m_ScoresToUpload.Enqueue(item);
},
() => {
// On No
GameUI.m_Instance.m_LevelComplete.m_LeaderboardPanel.ForceRefresh();
});
return false;
}
return true;
}