Repository navigation
Expand file tree
/
Copy pathModMenuManager.cs
More file actions
1045 lines (891 loc) · 50.1 KB
/
Copy pathModMenuManager.cs
File metadata and controls
1045 lines (891 loc) · 50.1 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
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 System.Collections;
using System.IO;
using System.Reflection;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
namespace AoQModManager
{
public class ModMenuManager : MonoBehaviour
{
// ── InteractMenu layout ───────────────────────────────────────────────────
static float MenuFrontPosY = 0.215f;
static float MenuFrontScaleY = 0.34f;
// ─────────────────────────────────────────────────────────────────────────
private static AssetBundle _bundle;
private GameObject _modMenuPanel;
private Transform _interactMenu;
// ── Configure Mods ────────────────────────────────────────────────────────
private Transform _modListContent;
private Transform _configListContent;
private PluginInfo _selfPluginInfo; // AoQ Mod Manager's own PluginInfo for auto-select
private GameObject _modListItemTemplate; // Button_Template from mod scroll
private GameObject _configItemTemplate; // Button_Template from config scroll
private Transform _interactionsHolder; // ConfigInteractionsHolder
private GameObject _boolWidget; // …/bool
private GameObject _intFloatWidget; // …/int/float
private GameObject _stringWidget; // …/string
private TextMeshProUGUI _selectedModLabel; // ModConfigsScrollLabel
private TextMeshProUGUI _selectedCfgLabel; // SelectedConfigName
private TextMeshProUGUI _descriptionLabel; // WhiteBackground/ModCfgDescText
private GameObject _selectedCfgBtn;
// ── Manage Mods ───────────────────────────────────────────────────────────
private Transform _manageListContent;
private GameObject _manageItemTemplate; // Button_Template from manage scroll
// ── Overlay colors ────────────────────────────────────────────────────────
private static readonly Color OverlaySelected = new Color(0.55f, 0.55f, 0.55f, 0.55f);
private static readonly Color OverlayDisabled = new Color(0f, 0f, 0f, 0.45f);
private static readonly Color OverlayEnabled = new Color(0f, 0.7f, 0f, 0.30f);
private static readonly Color OverlayOff = new Color(0.7f, 0f, 0f, 0.30f);
// ─────────────────────────────────────────────────────────────────────────
private bool _isMenuOpen = false;
private void OnEnable() { SceneManager.sceneLoaded += OnSceneLoaded; }
private void OnDisable() { SceneManager.sceneLoaded -= OnSceneLoaded; }
private void OnApplicationFocus(bool hasFocus)
{
if (!hasFocus && _isMenuOpen)
CloseModMenu();
}
private void CloseModMenu()
{
if (_modMenuPanel != null) _modMenuPanel.SetActive(false);
if (_interactMenu != null) _interactMenu.gameObject.SetActive(true);
_isMenuOpen = false;
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
_modMenuPanel = null;
_interactMenu = null;
StartCoroutine(InitMenu());
}
private IEnumerator InitMenu()
{
Transform nonInteractMenu = null;
int attempts = 0;
while (nonInteractMenu == null && attempts < 120)
{
nonInteractMenu = FindByName("NonInteractMenu");
if (nonInteractMenu == null) yield return new WaitForSeconds(0.5f);
attempts++;
}
if (nonInteractMenu == null) { Plugin.Log.LogWarning("[ModMenu] NonInteractMenu not found."); yield break; }
Transform mainMenuCanvasTr = nonInteractMenu.Find("MainMenuCanvas");
if (mainMenuCanvasTr == null) { Plugin.Log.LogError("[ModMenu] MainMenuCanvas not found."); yield break; }
_interactMenu = mainMenuCanvasTr.Find("InteractMenu");
if (_interactMenu == null) { Plugin.Log.LogError("[ModMenu] InteractMenu not found."); yield break; }
_modMenuPanel = LoadModMenuFromBundle(mainMenuCanvasTr);
if (_modMenuPanel == null) yield break;
InjectModButton(_interactMenu);
UpdateDevVersionText(mainMenuCanvasTr);
Plugin.DisabledPluginsFolder.SettingChanged += (_, __) => MigrateDisabledFolder();
Plugin.Log.LogInfo("[ModMenu] Mod menu injected successfully.");
}
// ─────────────────────────────────────────────────────────────────────────
// Bundle loading
// ─────────────────────────────────────────────────────────────────────────
private GameObject LoadModMenuFromBundle(Transform parent)
{
if (_bundle == null)
{
const string resourceName = "AoQModManager.modsmenu";
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
if (stream == null)
{
Plugin.Log.LogError($"[ModMenu] Embedded resource '{resourceName}' not found.");
return null;
}
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
_bundle = AssetBundle.LoadFromMemory(bytes);
}
}
if (_bundle == null) { Plugin.Log.LogError("[ModMenu] Failed to load AssetBundle."); return null; }
GameObject prefab = _bundle.LoadAsset<GameObject>("ModsMenu");
if (prefab == null) { Plugin.Log.LogError("[ModMenu] 'ModsMenu' prefab not found in bundle."); return null; }
GameObject panel = Instantiate(prefab, parent);
panel.name = "ModsMenu";
panel.SetActive(false);
WireModMenu(panel);
return panel;
}
// ─────────────────────────────────────────────────────────────────────────
// Wiring
// ─────────────────────────────────────────────────────────────────────────
private void WireModMenu(GameObject panel)
{
Transform panelTr = panel.transform;
Transform configureMods = panelTr.Find("ConfigureMods");
Transform manageMods = panelTr.Find("ManageMods");
// Back → InteractMenu
Button backBtn = panelTr.Find("Button_Back")?.GetComponent<Button>();
if (backBtn != null)
{
backBtn.onClick = new Button.ButtonClickedEvent();
backBtn.onClick.AddListener(OnBackToMainClick);
}
// Tab buttons
Button cfgTabBtn = panelTr.Find("Button_ConfigureMods")?.GetComponent<Button>();
Button mngTabBtn = panelTr.Find("Button_ManageMods")?.GetComponent<Button>();
if (cfgTabBtn != null)
{
cfgTabBtn.onClick = new Button.ButtonClickedEvent();
cfgTabBtn.onClick.AddListener(() =>
{
configureMods?.gameObject.SetActive(true);
manageMods?.gameObject.SetActive(false);
});
}
if (mngTabBtn != null)
{
mngTabBtn.onClick = new Button.ButtonClickedEvent();
mngTabBtn.onClick.AddListener(() =>
{
manageMods?.gameObject.SetActive(true);
configureMods?.gameObject.SetActive(false);
});
}
Button restartBtn = panelTr.Find("Button_RestartGame")?.GetComponent<Button>();
if (restartBtn != null)
{
restartBtn.onClick = new Button.ButtonClickedEvent();
restartBtn.onClick.AddListener(() => Application.Quit());
}
if (configureMods != null) WireConfigureModsPanel(configureMods, panelTr);
if (manageMods != null) WireManageModsPanel(manageMods, panelTr);
}
private void WireConfigureModsPanel(Transform cfg, Transform panelRoot)
{
// ── Mod list scroll ───────────────────────────────────────────────────
Transform modScrollContent = cfg.Find("Mods Scroll View/Viewport/Content");
_modListContent = modScrollContent;
if (_modListContent == null)
Plugin.Log.LogWarning("[ModMenu] Mod scroll content not found.");
// ── Config entry scroll ───────────────────────────────────────────────
Transform cfgScrollContent = cfg.Find("ConfigScrollView/Viewport/Content");
_configListContent = cfgScrollContent;
if (_configListContent == null)
Plugin.Log.LogWarning("[ModMenu] Config scroll content not found.");
// ── Labels ────────────────────────────────────────────────────────────
_selectedModLabel = cfg.Find("ModConfigsScrollLabel")?.GetComponent<TextMeshProUGUI>();
_selectedCfgLabel = cfg.Find("SelectedConfigName")?.GetComponent<TextMeshProUGUI>();
_descriptionLabel = cfg.Find("WhiteBackground/ModCfgDescText")?.GetComponent<TextMeshProUGUI>();
if (_selectedModLabel == null) Plugin.Log.LogWarning("[ModMenu] ModConfigsScrollLabel not found.");
if (_selectedCfgLabel == null) Plugin.Log.LogWarning("[ModMenu] SelectedConfigName not found.");
if (_descriptionLabel == null) Plugin.Log.LogWarning("[ModMenu] ModCfgDescText not found.");
else
{
_descriptionLabel.enableAutoSizing = true;
_descriptionLabel.fontSizeMin = 6f;
_descriptionLabel.overflowMode = TMPro.TextOverflowModes.Truncate;
}
// ── Interaction widgets ───────────────────────────────────────────────
_interactionsHolder = cfg.Find("ConfigInteractionsHolder");
if (_interactionsHolder != null)
{
// Transform.Find treats '/' as a path separator, so iterate children
// directly to find objects whose names contain a slash.
for (int i = 0; i < _interactionsHolder.childCount; i++)
{
Transform child = _interactionsHolder.GetChild(i);
switch (child.name)
{
case "bool": _boolWidget = child.gameObject; break;
case "number": _intFloatWidget = child.gameObject; break;
case "string": _stringWidget = child.gameObject; break;
}
}
HideAllInteractionWidgets();
}
else Plugin.Log.LogWarning("[ModMenu] ConfigInteractionsHolder not found.");
// ── Templates ─────────────────────────────────────────────────────────
if (_modListContent != null)
{
Transform tpl = _modListContent.Find("Button_Template");
if (tpl != null)
{
_modListItemTemplate = Instantiate(tpl.gameObject, panelRoot);
_modListItemTemplate.SetActive(false);
}
EnsureVerticalLayout(_modListContent);
ClearChildren(_modListContent);
PopulateModList();
}
if (_configListContent != null)
{
Transform tpl = _configListContent.Find("Button_Template");
if (tpl != null)
{
_configItemTemplate = Instantiate(tpl.gameObject, panelRoot);
_configItemTemplate.SetActive(false);
}
EnsureVerticalLayout(_configListContent);
ClearChildren(_configListContent);
}
// Clear labels to default state
if (_selectedModLabel != null) _selectedModLabel.text = "Select a mod";
if (_selectedCfgLabel != null) _selectedCfgLabel.text = "";
if (_descriptionLabel != null) _descriptionLabel.text = "";
}
private void WireManageModsPanel(Transform manage, Transform panelRoot)
{
Transform content = manage.Find("Mods Scroll View/Viewport/Content");
_manageListContent = content;
if (_manageListContent == null) { Plugin.Log.LogWarning("[ModMenu] Manage scroll content not found."); return; }
Transform tpl = content.Find("Button_Template");
if (tpl != null)
{
_manageItemTemplate = Instantiate(tpl.gameObject, panelRoot);
_manageItemTemplate.SetActive(false);
}
EnsureVerticalLayout(_manageListContent);
ClearChildren(_manageListContent);
PopulateManageList();
}
// ─────────────────────────────────────────────────────────────────────────
// Interaction widget visibility
// ─────────────────────────────────────────────────────────────────────────
private void HideAllInteractionWidgets()
{
_boolWidget?.SetActive(false);
_intFloatWidget?.SetActive(false);
_stringWidget?.SetActive(false);
}
// ─────────────────────────────────────────────────────────────────────────
// Populate mod list
// ─────────────────────────────────────────────────────────────────────────
private void PopulateModList()
{
bool any = false;
bool showAll = Plugin.ShowAllMods.Value;
foreach (var kv in Chainloader.PluginInfos)
{
BaseUnityPlugin inst = kv.Value.Instance;
if (inst == null) continue;
bool hasBool = false;
foreach (ConfigDefinition key in inst.Config.Keys)
if (inst.Config[key] is ConfigEntry<bool>) { hasBool = true; break; }
if (!hasBool && !showAll) continue;
var captured = kv.Value;
GameObject row = CloneListItem(_modListContent, kv.Value.Metadata.Name,
hasBool ? () => SelectMod(captured) : (System.Action)null,
_modListItemTemplate);
if (!hasBool) AddStateOverlay(row, OverlayDisabled);
if (kv.Key == Plugin.PluginGuid) _selfPluginInfo = captured;
any = true;
}
if (!any) CloneListItem(_modListContent, "No configurable mods found.", null, _modListItemTemplate);
LayoutRebuilder.ForceRebuildLayoutImmediate(_modListContent.GetComponent<RectTransform>());
}
// ─────────────────────────────────────────────────────────────────────────
// Populate manage list
// ─────────────────────────────────────────────────────────────────────────
private void PopulateManageList()
{
string disabledDir = Path.Combine(Paths.BepInExRootPath, Plugin.DisabledPluginsFolder.Value);
foreach (var kv in Chainloader.PluginInfos)
{
bool isUs = kv.Key == Plugin.PluginGuid;
string name = kv.Value.Metadata.Name;
string dll = kv.Value.Location;
GameObject row = CloneListItem(_manageListContent, name, null, _manageItemTemplate);
AddStateOverlay(row, isUs ? OverlayDisabled : OverlayEnabled);
if (!isUs)
{
string capturedDll = dll;
GameObject capturedRow = row;
Button b = row.GetComponent<Button>() ?? row.GetComponentInChildren<Button>();
if (b != null)
{
b.onClick = new Button.ButtonClickedEvent();
b.onClick.AddListener(() => DisableMod(capturedDll, capturedRow));
}
}
}
if (Directory.Exists(disabledDir))
{
foreach (string dll in Directory.GetFiles(disabledDir, "*.dll"))
{
string name = Path.GetFileNameWithoutExtension(dll);
string capturedName = Path.GetFileName(dll);
GameObject row = CloneListItem(_manageListContent, name, null, _manageItemTemplate);
AddStateOverlay(row, OverlayOff);
Button b = row.GetComponent<Button>() ?? row.GetComponentInChildren<Button>();
if (b != null)
{
b.onClick = new Button.ButtonClickedEvent();
b.onClick.AddListener(() => EnableMod(capturedName, row));
}
}
}
LayoutRebuilder.ForceRebuildLayoutImmediate(_manageListContent.GetComponent<RectTransform>());
}
// ─────────────────────────────────────────────────────────────────────────
// Select mod → populate config list
// ─────────────────────────────────────────────────────────────────────────
private void SelectMod(PluginInfo pluginInfo)
{
if (_configListContent == null) return;
ClearChildren(_configListContent);
HideAllInteractionWidgets();
_selectedCfgBtn = null;
if (_selectedModLabel != null)
_selectedModLabel.text = pluginInfo.Metadata.Name;
if (_selectedCfgLabel != null)
_selectedCfgLabel.text = "";
if (_descriptionLabel != null)
_descriptionLabel.text = "";
BaseUnityPlugin inst = pluginInfo.Instance;
if (inst == null) return;
bool first = true;
foreach (ConfigDefinition key in inst.Config.Keys)
{
ConfigEntryBase entry = inst.Config[key];
var capturedKey = key.Key;
var capturedEntry = entry;
GameObject row = null;
row = CloneListItem(_configListContent, capturedKey,
() => SelectConfigEntry(capturedKey, capturedEntry, row),
_configItemTemplate);
if (first) { SelectConfigEntry(capturedKey, capturedEntry, row); first = false; }
}
LayoutRebuilder.ForceRebuildLayoutImmediate(_configListContent.GetComponent<RectTransform>());
}
// ─────────────────────────────────────────────────────────────────────────
// Select config entry → show correct widget
// ─────────────────────────────────────────────────────────────────────────
private void SelectConfigEntry(string key, ConfigEntryBase entry, GameObject sourceBtn)
{
_selectedCfgBtn = sourceBtn;
// Update labels
if (_selectedCfgLabel != null)
_selectedCfgLabel.text = key;
if (_descriptionLabel != null)
{
string desc = entry.Description?.Description;
_descriptionLabel.text = string.IsNullOrEmpty(desc)
? $"Type: {entry.SettingType.Name}"
: desc;
}
// Show appropriate widget
HideAllInteractionWidgets();
if (entry is ConfigEntry<bool> boolEntry)
{
if (_boolWidget != null)
{
_boolWidget.SetActive(true);
WireBoolWidget(boolEntry);
}
}
else if (entry is ConfigEntry<int> || entry is ConfigEntry<float>)
{
if (_intFloatWidget != null)
{
_intFloatWidget.SetActive(true);
WireNumberWidget(entry);
}
}
else if (entry is ConfigEntry<string> strEntry)
{
if (_stringWidget != null)
{
_stringWidget.SetActive(true);
WireStringWidget(strEntry);
}
}
}
// ─────────────────────────────────────────────────────────────────────────
// Bool widget
// ─────────────────────────────────────────────────────────────────────────
private void WireBoolWidget(ConfigEntry<bool> entry)
{
RefreshBoolWidget(entry);
Button btn = _boolWidget.GetComponent<Button>()
?? _boolWidget.GetComponentInChildren<Button>();
if (btn != null)
{
btn.onClick = new Button.ButtonClickedEvent();
btn.onClick.AddListener(() =>
{
entry.Value = !entry.Value;
entry.ConfigFile.Save();
RefreshBoolWidget(entry);
});
}
}
private void RefreshBoolWidget(ConfigEntry<bool> entry)
{
bool val = entry.Value;
foreach (var t in _boolWidget.GetComponentsInChildren<TextMeshProUGUI>(true))
t.text = val ? "Enabled" : "Disabled";
Image img = _boolWidget.GetComponent<Image>()
?? _boolWidget.GetComponentInChildren<Image>();
if (img != null)
img.color = val ? new Color(0.18f, 0.55f, 0.18f) : new Color(0.55f, 0.18f, 0.18f);
}
// ─────────────────────────────────────────────────────────────────────────
// Number (int / float) widget
// ─────────────────────────────────────────────────────────────────────────
private void WireNumberWidget(ConfigEntryBase entry)
{
bool isInt = entry is ConfigEntry<int>;
float step = 1f;
Transform numTr = _intFloatWidget?.transform;
if (numTr == null) return;
Transform valLabel = numTr.Find("Current Value Label");
Transform stepLabel = numTr.Find("StepPrecisionLabel");
TextMeshProUGUI valTmp = valLabel?.GetComponent<TextMeshProUGUI>();
TextMeshProUGUI stepTmp = stepLabel?.GetComponent<TextMeshProUGUI>();
void Refresh()
{
float cur = isInt ? ((ConfigEntry<int>)entry).Value : ((ConfigEntry<float>)entry).Value;
if (valTmp != null) valTmp.text = isInt
? $"Current Value: {(int)cur}"
: $"Current Value: {cur}";
if (stepTmp != null) stepTmp.text = step >= 1f
? $"Step Precision: {(int)step}"
: $"Step Precision: {step}";
}
Refresh();
// Deep search so we find buttons regardless of exact nesting depth.
Transform DeepFind(Transform root, string name)
{
for (int i = 0; i < root.childCount; i++)
{
Transform c = root.GetChild(i);
if (c.name == name) return c;
Transform found = DeepFind(c, name);
if (found != null) return found;
}
return null;
}
void WireBtn(string btnName, System.Action action)
{
Transform t = DeepFind(numTr, btnName);
if (t == null)
{
Plugin.Log.LogWarning($"[NumWidget] '{btnName}' not found under {numTr.name}");
return;
}
Button b = t.GetComponent<Button>() ?? t.GetComponentInChildren<Button>();
// Only inject Image+Button when there is genuinely nothing interactive here.
// Adding a full-RectTransform Image to a button that already has its own
// hitbox set up causes the clickable area to blow out to the transform size.
if (b == null)
{
if (t.GetComponent<Graphic>() == null)
{
Image img = t.gameObject.AddComponent<Image>();
img.color = Color.clear;
img.raycastTarget = true;
}
b = t.gameObject.AddComponent<Button>();
}
b.onClick = new Button.ButtonClickedEvent();
b.onClick.AddListener(() => action());
}
// Value +/-
WireBtn("Button_ValueMinus", () =>
{
if (isInt) ((ConfigEntry<int>)entry).Value -= Mathf.RoundToInt(step);
else ((ConfigEntry<float>)entry).Value -= step;
entry.ConfigFile.Save();
Refresh();
});
WireBtn("Button_ValuePlus", () =>
{
if (isInt) ((ConfigEntry<int>)entry).Value += Mathf.RoundToInt(step);
else ((ConfigEntry<float>)entry).Value += step;
entry.ConfigFile.Save();
Refresh();
});
// Step precision +/-
WireBtn("Button_PrecisionMinus", () =>
{
step /= 10f;
if (isInt && step < 1f) step = 1f;
Refresh();
});
WireBtn("Button_PrecisionPlus", () =>
{
step *= 10f;
Refresh();
});
}
// ─────────────────────────────────────────────────────────────────────────
// String / keyboard widget
// ─────────────────────────────────────────────────────────────────────────
private void WireStringWidget(ConfigEntry<string> entry)
{
Transform entryTr = _stringWidget?.transform.Find("StringEntry/Entry");
if (entryTr == null) return;
TextMeshProUGUI valueTmp = entryTr.Find("ActualStringValue")?.GetComponent<TextMeshProUGUI>();
if (valueTmp != null) valueTmp.text = entry.Value ?? "";
Transform kbd1 = entryTr.Find("Keyboard/KeyboardGrid1");
Transform kbd2 = entryTr.Find("Keyboard/KeyboardGrid2");
Transform submitTr = entryTr.Find("Keyboard/Button_Submit");
bool shifted = true; // asset bundle keys are uppercase by default
void Append(string s) { if (valueTmp != null) valueTmp.text += s; }
if (kbd1 != null)
{
// Letters A-Z
for (char c = 'A'; c <= 'Z'; c++)
{
char cap = c;
Button b = kbd1.Find($"Button_{cap}")?.GetComponent<Button>();
if (b == null) continue;
b.onClick = new Button.ButtonClickedEvent();
b.onClick.AddListener(() => Append(shifted ? cap.ToString().ToUpper() : cap.ToString().ToLower()));
}
// Dash
Button dashBtn = kbd1.Find("Button_Dash")?.GetComponent<Button>();
if (dashBtn != null) { dashBtn.onClick = new Button.ButtonClickedEvent(); dashBtn.onClick.AddListener(() => Append("-")); }
// Backspace
Button bsBtn = kbd1.Find("Button_BackSpace")?.GetComponent<Button>();
if (bsBtn != null)
{
bsBtn.onClick = new Button.ButtonClickedEvent();
bsBtn.onClick.AddListener(() =>
{
if (valueTmp != null && valueTmp.text.Length > 0)
valueTmp.text = valueTmp.text.Substring(0, valueTmp.text.Length - 1);
});
}
// Shift — toggle and update letter key labels
Button shiftBtn = kbd1.Find("Button_Shift")?.GetComponent<Button>();
if (shiftBtn != null)
{
shiftBtn.onClick = new Button.ButtonClickedEvent();
shiftBtn.onClick.AddListener(() =>
{
shifted = !shifted;
for (char c = 'A'; c <= 'Z'; c++)
{
TextMeshProUGUI t = kbd1.Find($"Button_{c}/Body/Text")?.GetComponent<TextMeshProUGUI>();
if (t != null) t.text = shifted ? c.ToString().ToUpper() : c.ToString().ToLower();
}
});
}
}
if (kbd2 != null)
{
for (int i = 0; i <= 9; i++)
{
int cap = i;
Button b = kbd2.Find($"Button_{cap}")?.GetComponent<Button>();
if (b == null) continue;
b.onClick = new Button.ButtonClickedEvent();
b.onClick.AddListener(() => Append(cap.ToString()));
}
Button plusBtn = kbd2.Find("Button_+")?.GetComponent<Button>();
Button undrBtn = kbd2.Find("Button__")?.GetComponent<Button>();
if (plusBtn != null) { plusBtn.onClick = new Button.ButtonClickedEvent(); plusBtn.onClick.AddListener(() => Append("+")); }
if (undrBtn != null) { undrBtn.onClick = new Button.ButtonClickedEvent(); undrBtn.onClick.AddListener(() => Append("_")); }
}
Button submitBtn = submitTr?.GetComponent<Button>();
if (submitBtn != null)
{
submitBtn.onClick = new Button.ButtonClickedEvent();
submitBtn.onClick.AddListener(() =>
{
if (valueTmp != null) { entry.Value = valueTmp.text; entry.ConfigFile.Save(); }
});
}
}
// ─────────────────────────────────────────────────────────────────────────
// Enable / Disable mods
// ─────────────────────────────────────────────────────────────────────────
private static void DisableMod(string dllPath, GameObject row)
{
try
{
string fileName = Path.GetFileName(dllPath);
string dir = Path.Combine(Paths.BepInExRootPath, Plugin.DisabledPluginsFolder.Value);
Directory.CreateDirectory(dir);
File.Move(dllPath, Path.Combine(dir, fileName));
SetStateOverlayColor(row, OverlayOff);
Button b = row.GetComponent<Button>() ?? row.GetComponentInChildren<Button>();
if (b != null)
{
b.onClick = new Button.ButtonClickedEvent();
// Capture only the filename — resolve full path at call time so
// a folder rename via DisabledPluginsFolder doesn't break the closure.
b.onClick.AddListener(() => EnableMod(fileName, row));
}
}
catch (System.Exception ex) { Plugin.Log.LogError($"[ManageMods] Disable failed: {ex.Message}"); }
}
// fileName only — resolves current disabled folder at call time.
private static void EnableMod(string fileName, GameObject row)
{
try
{
string src = Path.Combine(Paths.BepInExRootPath, Plugin.DisabledPluginsFolder.Value, fileName);
string dest = Path.Combine(Paths.PluginPath, fileName);
File.Move(src, dest);
SetStateOverlayColor(row, OverlayEnabled);
Button b = row.GetComponent<Button>() ?? row.GetComponentInChildren<Button>();
if (b != null)
{
b.onClick = new Button.ButtonClickedEvent();
b.onClick.AddListener(() => DisableMod(dest, row));
}
}
catch (System.Exception ex) { Plugin.Log.LogError($"[ManageMods] Enable failed: {ex.Message}"); }
}
// ─────────────────────────────────────────────────────────────────────────
// Inject Mods button into InteractMenu
// ─────────────────────────────────────────────────────────────────────────
private void InjectModButton(Transform interactMenu)
{
Transform exitTr = null;
Transform templateTr = null;
foreach (Transform child in interactMenu)
{
if (child.name.ToLower().Contains("exit"))
exitTr = child;
else if (templateTr == null && child.GetComponent<Button>() != null)
templateTr = child;
}
if (templateTr == null) { Plugin.Log.LogError("[ModMenu] No template button in InteractMenu."); return; }
foreach (Transform child in interactMenu)
{
if (child == exitTr) continue;
RectTransform crt = child.GetComponent<RectTransform>();
if (crt != null)
crt.anchoredPosition = new Vector2(crt.anchoredPosition.x, crt.anchoredPosition.y + 0.04f);
}
GameObject modBtn = Instantiate(templateTr.gameObject, interactMenu);
modBtn.name = "ModManagerButton";
foreach (var t in modBtn.GetComponentsInChildren<TextMeshProUGUI>(true)) t.text = "Mods";
foreach (var t in modBtn.GetComponentsInChildren<Text>(true)) t.text = "Mods";
RectTransform exitRt = exitTr?.GetComponent<RectTransform>();
RectTransform templateRt = templateTr.GetComponent<RectTransform>();
float exitY = exitRt != null ? exitRt.anchoredPosition.y : templateRt.anchoredPosition.y - 0.08f;
float exitX = exitRt != null ? exitRt.anchoredPosition.x : templateRt.anchoredPosition.x;
modBtn.GetComponent<RectTransform>().anchoredPosition = new Vector2(exitX, exitY + 0.04f);
Button btn = modBtn.GetComponent<Button>();
btn.onClick = new Button.ButtonClickedEvent();
btn.onClick.AddListener(OnModButtonClick);
FixMenuBackground(interactMenu);
}
private static void FixMenuBackground(Transform interactMenu)
{
Transform menuFront = interactMenu.Find("MenuFront")
?? interactMenu.parent?.Find("MenuFront");
if (menuFront == null) { Plugin.Log.LogWarning("[ModMenu] MenuFront not found."); return; }
RectTransform frontRt = menuFront.GetComponent<RectTransform>();
if (frontRt != null)
frontRt.anchoredPosition = new Vector2(frontRt.anchoredPosition.x, MenuFrontPosY);
Vector3 s = menuFront.localScale;
menuFront.localScale = new Vector3(s.x, MenuFrontScaleY, s.z);
}
// ─────────────────────────────────────────────────────────────────────────
// Navigation
// ─────────────────────────────────────────────────────────────────────────
private void OnModButtonClick()
{
_interactMenu.gameObject.SetActive(false);
_modMenuPanel.SetActive(true);
_isMenuOpen = true;
if (_selfPluginInfo != null) SelectMod(_selfPluginInfo);
}
private void OnBackToMainClick()
{
CloseModMenu();
}
private static void MigrateDisabledFolder()
{
string root = Paths.BepInExRootPath;
string newDir = Path.Combine(root, Plugin.DisabledPluginsFolder.Value);
Directory.CreateDirectory(newDir);
// Find any old disabled folder (any sibling folder that isn't plugins/config/etc.)
foreach (string dir in Directory.GetDirectories(root))
{
if (dir == newDir) continue;
string dirName = Path.GetFileName(dir).ToLower();
if (dirName == "plugins" || dirName == "config" || dirName == "cache"
|| dirName == "patchers" || dirName == "core") continue;
foreach (string dll in Directory.GetFiles(dir, "*.dll"))
{
string dest = Path.Combine(newDir, Path.GetFileName(dll));
try { File.Move(dll, dest); }
catch (System.Exception ex) { Plugin.Log.LogError($"[MigrateFolder] {ex.Message}"); }
}
// Delete old dir if now empty
if (Directory.GetFiles(dir).Length == 0 && Directory.GetDirectories(dir).Length == 0)
Directory.Delete(dir);
}
}
// ─────────────────────────────────────────────────────────────────────────
// Dev version text
// ─────────────────────────────────────────────────────────────────────────
private static void UpdateDevVersionText(Transform mainMenuCanvasTr)
{
if (!Plugin.ShowModdedText.Value) return;
Transform devVersionTr = mainMenuCanvasTr.Find("DevVersion (TMP)")
?? mainMenuCanvasTr.Find("DevVersion");
if (devVersionTr == null) { Plugin.Log.LogWarning("[ModMenu] DevVersion text not found."); return; }
TextMeshProUGUI tmp = devVersionTr.GetComponent<TextMeshProUGUI>();
if (tmp == null) return;
int modCount = Chainloader.PluginInfos.Count;
tmp.text = $"AoQMM v{Plugin.PluginVersion} | MODDED ({modCount} mod{(modCount == 1 ? "" : "s")})";
}
// ─────────────────────────────────────────────────────────────────────────
// CloneListItem
// ─────────────────────────────────────────────────────────────────────────
private static GameObject CloneListItem(Transform parent, string label, System.Action onClick, GameObject template)
{
GameObject row;
if (template != null)
{
row = Instantiate(template, parent);
ZeroLocalZ(row);
row.SetActive(true);
row.name = label;
foreach (var t in row.GetComponentsInChildren<TextMeshProUGUI>(true))
t.text = label;
foreach (var t in row.GetComponentsInChildren<Text>(true))
t.text = label;
}
else
{
row = MakeButton(parent, label, label, null);
LayoutElement le = row.AddComponent<LayoutElement>();
le.preferredHeight = 42f;
le.flexibleWidth = 1f;
}
if (onClick != null)
{
Button btn = row.GetComponent<Button>() ?? row.GetComponentInChildren<Button>();
if (btn == null) btn = row.AddComponent<Button>();
btn.onClick = new Button.ButtonClickedEvent();
btn.onClick.AddListener(() => onClick());
}
return row;
}
// ─────────────────────────────────────────────────────────────────────────
// Overlays
// ─────────────────────────────────────────────────────────────────────────
// Permanent state overlay (enabled/disabled/selected-mod tint) — added to root
private static void AddStateOverlay(GameObject row, Color color)
{
if (row == null) return;
GameObject ov = new GameObject("StateOverlay");
ov.transform.SetParent(row.transform, false);
RectTransform rt = ov.AddComponent<RectTransform>();
rt.anchorMin = Vector2.zero;
rt.anchorMax = Vector2.one;
rt.offsetMin = rt.offsetMax = Vector2.zero;
ov.AddComponent<Image>().color = color;
ov.SetActive(color.a > 0f);
}
private static void SetStateOverlayColor(GameObject row, Color color)
{
Transform ov = row?.transform.Find("StateOverlay");
if (ov == null) return;
Image img = ov.GetComponent<Image>();
if (img != null) img.color = color;
ov.gameObject.SetActive(color.a > 0f);
}
// Selection highlight for config list items — toggled on click
private static void AddSelectionOverlay(GameObject row)
{
if (row == null) return;
// Parent inside Body so it sits above Background but below text
Transform body = row.transform.Find("Body");
Transform parent = body != null ? body : row.transform;
GameObject ov = new GameObject("SelectionHighlight");
ov.transform.SetParent(parent, false);
RectTransform rt = ov.AddComponent<RectTransform>();
rt.anchorMin = Vector2.zero;
rt.anchorMax = Vector2.one;
rt.offsetMin = rt.offsetMax = Vector2.zero;
ov.AddComponent<Image>().color = OverlaySelected;
ov.transform.SetSiblingIndex(1); // after Background (0), before text
ov.SetActive(false);
}
private static Transform FindSelectionOverlay(GameObject row)
{
if (row == null) return null;
Transform body = row.transform.Find("Body");
Transform p = body != null ? body : row.transform;
return p.Find("SelectionHighlight");
}
// ─────────────────────────────────────────────────────────────────────────
// Helpers
// ─────────────────────────────────────────────────────────────────────────
private static Transform FindByName(string name)
{
foreach (Transform t in Resources.FindObjectsOfTypeAll<Transform>())
if (t.name == name) return t;
return null;
}
private static void ZeroLocalZ(GameObject go)
{
if (go == null) return;
Vector3 p = go.transform.localPosition;
go.transform.localPosition = new Vector3(p.x, p.y, 0f);
}
private static void ClearChildren(Transform parent)
{
for (int i = parent.childCount - 1; i >= 0; i--)
Destroy(parent.GetChild(i).gameObject);
}
private static void EnsureVerticalLayout(Transform content)
{
LayoutGroup existing = content.GetComponent<LayoutGroup>();
if (existing != null && !(existing is VerticalLayoutGroup))
DestroyImmediate(existing);
if (content.GetComponent<VerticalLayoutGroup>() == null)
{
VerticalLayoutGroup vlg = content.gameObject.AddComponent<VerticalLayoutGroup>();
vlg.spacing = 3f;
vlg.childAlignment = TextAnchor.UpperCenter;
vlg.childControlWidth = true;