-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBetterModList.cs
More file actions
168 lines (155 loc) · 6.24 KB
/
BetterModList.cs
File metadata and controls
168 lines (155 loc) · 6.24 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
using Modding;
using System;
using System.Reflection;
using Mono.Cecil.Cil;
using MonoMod.Cil;
using MonoMod.RuntimeDetour;
using UnityEngine;
using Satchel.BetterMenus;
namespace BetterModList {
public class BetterModList: Mod, ICustomMenuMod, IGlobalSettings<GlobalSettings> {
new public string GetName() => "BetterModList";
public override string GetVersion() => "1.0.0.0";
private Menu MenuRef;
public static GlobalSettings gs = new();
private GUIStyle styleRef;
private static MethodInfo mvdOnGUI = typeof(ModVersionDraw).GetMethod("OnGUI", BindingFlags.Public | BindingFlags.Instance);
private ILHook ilMvdOnGUI;
private string firstHalfStorage = "";
private string secondHalfStorage = "";
private string fullList = "";
private bool isUpdatingGUI = false;
public override void Initialize() {
styleRef = typeof(ModVersionDraw).GetField("style", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null) as GUIStyle;
ilMvdOnGUI = new ILHook(mvdOnGUI, OnGUI);
}
private void OnGUI(ILContext il) {
ILCursor cursor = new ILCursor(il).Goto(0);
cursor.GotoNext(i => i.MatchCall<GUI>("Label"));
cursor.GotoNext(i => i.MatchRet());
cursor.Emit(OpCodes.Ldarg_0);
cursor.EmitDelegate<Action<ModVersionDraw>>(AddNewUI);
}
private void AddNewUI(ModVersionDraw self) {
bool skip = true;
if(isUpdatingGUI) {
isUpdatingGUI = false;
skip = false;
}
else if(self.drawString != firstHalfStorage) {
fullList = self.drawString;
skip = false;
}
if(!skip) {
(string, string) halves = parseDrawString(fullList);
self.drawString = halves.Item1;
firstHalfStorage = halves.Item1;
secondHalfStorage = halves.Item2;
}
styleRef.fontSize = gs.fontSize;
GUI.Label(new Rect(0f, 0f, Screen.width, Screen.height), secondHalfStorage, new GUIStyle(styleRef) { alignment = UnityEngine.TextAnchor.UpperRight });
}
private (string, string) parseDrawString(string drawString) {
string[] lines = drawString.Split('\n');
int lineCount = lines.Length;
bool doLoop = true;
while(doLoop) {
if(string.IsNullOrWhiteSpace(lines[lineCount - 1])) {
lineCount--;
}
else {
doLoop = false;
}
}
if(!gs.distributeEnabled) {
if(gs.leftSide) {
return (drawString, "");
}
else {
string rightSide = "";
for(int i = 1; i < lineCount; i++) {
rightSide += lines[i] + "\n";
}
return (lines[0], rightSide);
}
}
int division = gs.evenSplit ? (int)Math.Ceiling((double)lineCount / 2) + (gs.leftSide ? 0 : 1) : Math.Min((int)gs.maxMods,lineCount);
string firstHalf = "";
string secondHalf = "";
for(int i = 1; i < division; i++) {
firstHalf += lines[i] + "\n";
}
for(int i = division; i < lineCount; i++) {
secondHalf += lines[i] + "\n";
}
if(gs.leftSide)
return ($"{lines[0]}\n{firstHalf}", secondHalf);
return ($"{lines[0]}\n{secondHalf}", firstHalf);
}
public MenuScreen GetMenuScreen(MenuScreen modListMenu, ModToggleDelegates? modtoggledelegates) {
MenuRef ??= new Menu(
name: "Better Mod List",
elements: new Element[] {
new HorizontalOption(
name: "Side",
description: "",
values: new string[] { "Left\n", "Right\n" },
applySetting: index => {
gs.leftSide = index == 0;
isUpdatingGUI = true;
},
loadSetting: () => gs.leftSide ? 0 : 1
),
new HorizontalOption(
name: "Distribution",
description: "",
values: new string[] { "Fill to max\n", "Even split\n", "Disabled\n" },
applySetting: index => {
gs.distributeEnabled = index != 2;
gs.evenSplit = index == 1;
isUpdatingGUI = true;
},
loadSetting: () => gs.distributeEnabled ? gs.evenSplit ? 1 : 0 : 2),
new CustomSlider(
name: "Max length",
storeValue: val => {
gs.maxMods = (int)val;
isUpdatingGUI = true;
},
loadValue: () => (int)gs.maxMods,
minValue: 1,
maxValue: 100,
wholeNumbers: true
),
new CustomSlider(
name: "Font size",
storeValue: val => {
gs.fontSize = (int)val;
},
loadValue: () => (int)gs.fontSize,
minValue: 8,
maxValue: 32,
wholeNumbers: true
)
}
);
return MenuRef.GetMenuScreen(modListMenu);
}
public bool ToggleButtonInsideMenu {
get;
}
public void OnLoadGlobal(GlobalSettings s) {
gs = s;
}
public GlobalSettings OnSaveGlobal() {
return gs;
}
}
public class GlobalSettings {
public bool distributeEnabled = true;
public float maxMods = 50;
public bool evenSplit = false;
public bool leftSide = true;
public int fontSize = 13;
}
}