forked from headassbtw/CustomMenuText
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPlugin.cs
More file actions
executable file
·437 lines (382 loc) · 17.2 KB
/
Plugin.cs
File metadata and controls
executable file
·437 lines (382 loc) · 17.2 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
using IPA;
using IPA.Config;
using IPA.Config.Stores;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
using IPALogger = IPA.Logging.Logger;
using TMPro;
using System.IO;
using System.Text;
using IPA.Utilities;
using System.Reflection;
using System.Threading.Tasks;
using BeatSaberMarkupLanguage.Util;
using HarmonyLib;
using IPA.Loader;
namespace CustomMenuText
{
[Plugin(RuntimeOptions.SingleStartInit)]
public class Plugin
{
public static int selection_type = 0;
public static int choice = 0;
private static GameObject _defaultLogo;
public static GameObject defaultLogo
{
get
{
if (_defaultLogo != null)
{
return _defaultLogo;
}
else
{
try
{
Transform gltrans1 = null;
Transform gltrans2 = null;
GameObject _menuManager = GameObject.FindObjectOfType<MenuEnvironmentManager>().gameObject;
foreach (Transform logoTransform in _menuManager.GetComponentInChildren<Transform>(true).gameObject.GetComponentsInChildren<Transform>(true))
{
if (logoTransform.name == "Logo")
{
_defaultLogo = logoTransform.gameObject;
} else if (logoTransform.name.Contains("GlowLines"))
{
if (gltrans1 == null)
{
gltrans1 = logoTransform;
} else
{
gltrans2 = logoTransform;
}
}
}
gltrans1.SetParent(_defaultLogo.transform, true);
gltrans2.SetParent(_defaultLogo.transform, true);
return _defaultLogo;
} catch
{
Plugin.Log.Warn("Logo not found, creating empty object.");
return new GameObject();
}
}
}
set
{
_defaultLogo = value;
}
}
internal static Plugin Instance { get; private set; }
internal static IPALogger Log { get; private set; }
internal static Harmony harmony;
[Init]
/// <summary>
/// Called when the plugin is first loaded by IPA (either when the game starts or when the plugin is enabled if it starts disabled).
/// [Init] methods that use a Constructor or called before regular methods like InitWithConfig.
/// Only use [Init] with one Constructor.
/// </summary>
public void Init(IPALogger logger)
{
Instance = this;
Log = logger;
harmony = new Harmony("Saeraphinx.custommenutext");
Log.Info("CustomMenuText initialized.");
}
public static Color defaultMainColor = Color.red;
public static Color defaultBottomColor = new Color(0, 0.659f, 1);
public static Color diMainColor = Color.red;
public static Color diBottomColor = new Color(0, 0.659f, 1);
public static Color MainColor = Color.red;
public static Color BottomColor = new Color(0, 0.659f, 1);
#region BSIPA Config
//Uncomment to use BSIPA's config
[Init]
public void InitWithConfig(Config conf)
{
Configuration.PluginConfig.Instance = conf.Generated<Configuration.PluginConfig>();
Log.Debug("Config loaded");
selection_type = Configuration.PluginConfig.Instance.SelectionType;
choice = Configuration.PluginConfig.Instance.SelectedTextEntry;
}
#endregion
public static Plugin instance;
// path to the file to load text from
private const string FILE_PATH = "/UserData/CustomMenuText.txt";
public static string IMG_PATH = null;
public static string FONT_PATH = null;
// prefab to instantiate when creating the TextMeshPros
public static GameObject textPrefab;
// used if we can't load any custom entries
public static readonly string[] DEFAULT_TEXT = { "BEAT", "SABER" };
public static readonly string[] EMPTY = { "", "" };
public static string[] CURRENT_TEXT = { "FUCKIN", "BEES" };
// Logo Top Pos : 0.63, 18.61, 26.1
// Logo Bottom Pos : 0.63, 14, 26.1
public static Vector3 DefTopPos = new Vector3(0.63f, 18.61f, 26.1f);
public static Vector3 DefBotPos = new Vector3(0.63f, 14f, 26.1f);
public static bool initalFunctionsFinished = false;
// caches entries loaded from the file so we don't need to do IO every time the menu loads
public static List<string[]> allEntries = null;
public string Name => "Custom Menu Text";
public string Version => "4.0.4";
// Store the text objects so when we leave the menu and come back, we aren't creating a bunch of them
public static TextMeshPro mainText;
public static TextMeshPro bottomText; // BOTTOM TEXT
public System.Random random;
public static GameObject MetallicaLogoObject;
[OnDisable]
public void OnDisable()
{
harmony.UnpatchSelf();
}
[OnStart]
async public void OnApplicationStart()
{
if (!Directory.Exists(Path.Combine(UnityGame.UserDataPath, "CustomMenuText", "Cache")))
Directory.CreateDirectory(Path.Combine(UnityGame.UserDataPath, "CustomMenuText", "Cache"));
IMG_PATH = Path.Combine(UnityGame.UserDataPath, "CustomMenuText", "Images") + "\\";
FONT_PATH = Path.Combine(UnityGame.UserDataPath, "CustomMenuText", "Fonts") + "\\";
InitializeImageFolder();
choice = Configuration.PluginConfig.Instance.SelectedTextEntry;
SceneManager.activeSceneChanged += SceneManagerOnActiveSceneChanged;
harmony.PatchAll(Assembly.GetExecutingAssembly());
instance = this;
FontManager.FirstTimeFontLoad();
await MainMenuAwaiter.WaitForMainMenuAsync();
MainMenuAwaiter.MainMenuInitializing += Views.UICreator.CreateMenu;
Views.UICreator.CreateMenu();
reloadFile();
MetallicaLogoObject = GameObject.Find("MetallicaLogo");
MetallicaLogoObject.transform.gameObject.SetActive(!Configuration.PluginConfig.Instance.DisableMetallicaLogo);
}
void InitializeImageFolder()
{
if (!Directory.Exists(Path.Combine(UnityGame.UserDataPath, "CustomMenuText") + "\\"))
Directory.CreateDirectory(Path.Combine(UnityGame.UserDataPath, "CustomMenuText") + "\\");
if (!Directory.Exists(FONT_PATH))
Directory.CreateDirectory(FONT_PATH);
//if (!Directory.Exists(IMG_PATH))
// Directory.CreateDirectory(IMG_PATH);
}
public void YeetUpTheText()
{
MainColor = defaultMainColor;
BottomColor = defaultBottomColor;
if (Configuration.PluginConfig.Instance.SelectionType == 0)
{
setText(EMPTY);
defaultLogo.SetActive(true);
}
else
{
switch (Configuration.PluginConfig.Instance.SelectionType)
{
case 1:
//random
pickRandomEntry();
break;
case 2:
//pre-chosen
try
{
setText(allEntries[choice]);
}
catch (Exception e)
{
Plugin.Log.Critical(e);
}
break;
}
defaultLogo.SetActive(false);
}
}
public void ApplyFont()
{
Plugin.replaceLogo();
YeetUpTheText();
}
private void SceneManagerOnActiveSceneChanged(Scene arg0, Scene arg1)
{
if (arg1.name.Contains("Menu")) // Only run in menu scene
{
TextInit();
YeetUpTheText();
}
}
public void TextInit()
{
//if (Configuration.PluginConfig.Instance.OnlyInMainMenu) GameObject.Find("CustomMenuText")?.transform.SetParent(defaultLogo.transform.parent.transform, true);
//if (Configuration.PluginConfig.Instance.OnlyInMainMenu) GameObject.Find("CustomMenuText-Bot")?.transform.SetParent(defaultLogo.transform.parent.transform, true);
GameObject.Find("CustomMenuText")?.transform.SetParent(null, true);
GameObject.Find("CustomMenuText-Bot")?.transform.SetParent(null, true);
if (mainText != null) mainText.color = MainColor;
if (bottomText != null) bottomText.color = BottomColor;
if (allEntries == null)
{
reloadFile();
}
if (allEntries.Count == 0)
{
Log.Notice("File found, but it contained no entries! Leaving original logo intact.");
}
else
{
YeetUpTheText();
}
}
/// <summary>
/// Chooses a random entry from the current config and sets the menu text to that entry.
/// Warning: Only call this function from the main menu scene!
/// </summary>
public void pickRandomEntry()
{
// Choose an entry randomly
// Unity's random seems to give biased results
// int entryPicked = UnityEngine.Random.Range(0, entriesInFile.Count);
// using System.Random instead
if (random == null) random = new System.Random();
int entryPicked = random.Next(allEntries.Count);
// Set the text
setText(allEntries[entryPicked]);
}
/// <summary>
/// Replaces the logo in the main menu (which is an image and not text
/// as of game version 0.12.0) with an editable TextMeshPro-based
/// version. Performs only the necessary steps (if the logo has already
/// been replaced, restores the text's position and color to default
/// instead).
/// Warning: Only call this function from the main menu scene!
///
/// Code generously donated by Kyle1413; edited some by Arti
/// </summary>
///
public static void replaceLogo()
{
// Since 0.13.0, we have to create our TextMeshPros differently! You can't change the font at runtime, so we load a prefab with the right font from an AssetBundle. This has the side effect of allowing for custom fonts, an oft-requested feature.
// Logo Top Pos : 0.63, 18.61, 26.1
// Logo Bottom Pos : 0, 14, 26.1
if(!textPrefab)
textPrefab = FontManager.loadPrefab("NeonTubes");
if (mainText == null) mainText = GameObject.Find("CustomMenuText")?.GetComponent<TextMeshPro>();
if (mainText == null)
{
GameObject textObj = GameObject.Instantiate(textPrefab);
textObj.name = "CustomMenuText";
textObj.SetActive(false);
mainText = textObj.GetComponent<TextMeshPro>();
mainText.alignment = TextAlignmentOptions.Center;
mainText.fontSize = 12;
mainText.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 2f);
mainText.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 2f);
mainText.richText = true;
textObj.transform.localScale *= 3.7f;
mainText.overflowMode = TextOverflowModes.Overflow;
mainText.enableWordWrapping = false;
textObj.SetActive(true);
//if (Configuration.PluginConfig.Instance.OnlyInMainMenu) textObj.transform.SetParent(defaultLogo.transform.parent.transform, true);
}
mainText.rectTransform.position = DefTopPos;
mainText.color = MainColor;
mainText.text = "BEAT";
mainText.font = FontManager.Fonts[Configuration.PluginConfig.Instance.Font];
if (bottomText == null) bottomText = GameObject.Find("CustomMenuText-Bot")?.GetComponent<TextMeshPro>();
if (bottomText == null)
{
GameObject textObj2 = GameObject.Instantiate(textPrefab);
textObj2.name = "CustomMenuText-Bot";
textObj2.SetActive(false);
bottomText = textObj2.GetComponent<TextMeshPro>();
bottomText.alignment = TextAlignmentOptions.Center;
bottomText.fontSize = 12;
bottomText.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 2f);
bottomText.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 2f);
bottomText.richText = true;
textObj2.transform.localScale *= 3.7f;
bottomText.overflowMode = TextOverflowModes.Overflow;
bottomText.enableWordWrapping = false;
textObj2.SetActive(true);
//if(Configuration.PluginConfig.Instance.OnlyInMainMenu) textObj2.transform.SetParent(defaultLogo.transform.parent.transform, true);
}
bottomText.rectTransform.position = DefBotPos;
bottomText.color = BottomColor;
bottomText.text = "SABER";
bottomText.font = FontManager.Fonts[Configuration.PluginConfig.Instance.Font];
// Destroy Default Logo
//if (defaultLogo != null) defaultLogo.SetActive(false);
}
/// <summary>
/// Sets the text in the main menu (which normally reads BEAT SABER) to
/// the text of your choice. TextMeshPro formatting can be used here.
/// Additionally:
/// - If the text is exactly 2 lines long, the first line will be
/// displayed in red, and the second will be displayed in blue.
/// Warning: Only call this function from the main menu scene!
/// </summary>
/// <param name="lines">
/// The text to display, separated by lines (from top to bottom).
/// </param>
public static void setText(string[] lines)
{
// Set up the replacement logo
if(!mainText)
replaceLogo();
CURRENT_TEXT = lines;
if (lines.Length == 2)
{
mainText.color = MainColor;
mainText.transform.position = DefTopPos;
mainText.text = lines[0];
bottomText.color = BottomColor;
bottomText.transform.position = DefBotPos;
bottomText.text = lines[1];
}
else
{
// Hide the bottom line entirely; we're just going to use the main one
bottomText.text = "";
// Center the text vertically (halfway between the original positions)
Vector3 newPos = mainText.transform.position;
newPos.y = (newPos.y + bottomText.transform.position.y) / 2;
mainText.transform.position = newPos;
// Set text color to white by default (users can change it with formatting anyway)
mainText.color = Color.white;
// Set the text
mainText.text = String.Join("\n", lines);
}
Plugin.Log.Info("Text set to: \"" + mainText.text + "\" Bottom Text: \"" + bottomText.text + "\"");
}
public void reloadFile()
{
allEntries?.Clear();
allEntries = FileUtils.readFromFile(FILE_PATH);
Configuration.PluginConfig.Instance.SelectionType = selection_type;
Configuration.PluginConfig.Instance.SelectedTextEntry = choice;
}
[OnExit]
public void OnApplicationQuit()
{
Configuration.PluginConfig.Instance.SelectionType = selection_type;
Configuration.PluginConfig.Instance.SelectedTextEntry = choice;
try
{
SceneManager.activeSceneChanged -= SceneManagerOnActiveSceneChanged;
}
catch (Exception) { }
harmony.UnpatchSelf();
string cachePath = Path.Combine(UnityGame.UserDataPath, "CustomMenuText", "Cache");
if (Directory.Exists(cachePath))
{
foreach (var st in Directory.GetFiles(cachePath))
{
File.Delete(st);
}
Directory.Delete(cachePath);
}
}
}
}