@@ -14,10 +14,14 @@ public class CustomMenuTextPlugin : IPlugin
1414 // path to the file to load text from
1515 private const string FILE_PATH = "/UserData/CustomMenuText.txt" ;
1616
17+ // used if we can't load any custom entries
1718 public static readonly string [ ] DEFAULT_TEXT = { "BEAT" , "SABER" } ;
1819
20+ // caches entries loaded from the file so we don't need to do IO every time the menu loads
21+ public static List < string [ ] > allEntries = null ;
22+
1923 public string Name => "Custom Menu Text" ;
20- public string Version => "2.0 .0" ;
24+ public string Version => "2.1 .0" ;
2125 public void OnApplicationStart ( )
2226 {
2327 SceneManager . activeSceneChanged += SceneManagerOnActiveSceneChanged ;
@@ -30,70 +34,80 @@ private void SceneManagerOnActiveSceneChanged(Scene arg0, Scene arg1)
3034
3135 private void SceneManager_sceneLoaded ( Scene arg0 , LoadSceneMode arg1 )
3236 {
33- if ( arg0 . buildIndex == 1 ) // Menu scene
37+ if ( arg0 . name == "Menu" ) // Only run in menu scene
3438 {
35- // Look for the custom text file
36- string gameDirectory = Environment . CurrentDirectory ;
37- gameDirectory = gameDirectory . Replace ( '\\ ' , '/' ) ;
38- if ( File . Exists ( gameDirectory + FILE_PATH ) )
39+ if ( allEntries == null )
40+ {
41+ reloadFile ( ) ;
42+ }
43+ if ( allEntries . Count == 0 )
3944 {
40- var linesInFile = File . ReadLines ( gameDirectory + FILE_PATH ) ;
45+ setText ( DEFAULT_TEXT ) ;
46+ }
47+ else
48+ {
49+ // Choose an entry randomly
4150
42- // Strip comments (all lines beginning with #)
43- linesInFile = linesInFile . Where ( s => s == "" || s [ 0 ] != '#' ) ;
51+ // Unity's random seems to give biased results
52+ // int entryPicked = UnityEngine.Random.Range(0, entriesInFile.Count);
53+ // using System.Random instead
54+ System . Random r = new System . Random ( ) ;
55+ int entryPicked = r . Next ( allEntries . Count ) ;
4456
45- // Collect entries, splitting on empty lines
46- List < string [ ] > entriesInFile = new List < string [ ] > ( ) ;
47- List < string > currentEntry = new List < string > ( ) ;
48- foreach ( string line in linesInFile )
49- {
50- if ( line == "" )
51- {
52- entriesInFile . Add ( currentEntry . ToArray ( ) ) ;
53- currentEntry . Clear ( ) ;
54- }
55- else
56- {
57- currentEntry . Add ( line ) ;
58- }
59- }
60- if ( currentEntry . Count != 0 )
61- {
62- // in case the last entry doesn't end in a newline
63- entriesInFile . Add ( currentEntry . ToArray ( ) ) ;
64- }
57+ // Set the text
58+ setText ( allEntries [ entryPicked ] ) ;
59+ }
60+ }
61+ }
62+
63+ public static List < string [ ] > readFromFile ( string relPath )
64+ {
65+ List < string [ ] > entriesInFile = new List < string [ ] > ( ) ;
66+
67+ // Look for the custom text file
68+ string gameDirectory = Environment . CurrentDirectory ;
69+ gameDirectory = gameDirectory . Replace ( '\\ ' , '/' ) ;
70+ if ( File . Exists ( gameDirectory + FILE_PATH ) )
71+ {
72+ var linesInFile = File . ReadLines ( gameDirectory + FILE_PATH ) ;
73+
74+ // Strip comments (all lines beginning with #)
75+ linesInFile = linesInFile . Where ( s => s == "" || s [ 0 ] != '#' ) ;
6576
66- if ( entriesInFile . Count == 0 )
77+ // Collect entries, splitting on empty lines
78+ List < string > currentEntry = new List < string > ( ) ;
79+ foreach ( string line in linesInFile )
80+ {
81+ if ( line == "" )
6782 {
68- // No entries; warn and default to BEAT SABER
69- Console . WriteLine ( "[CustomMenuText] File found, but it contained no entries!" ) ;
70- setText ( DEFAULT_TEXT ) ;
83+ entriesInFile . Add ( currentEntry . ToArray ( ) ) ;
84+ currentEntry . Clear ( ) ;
7185 }
7286 else
7387 {
74- // Choose an entry randomly
75-
76- // Unity's random seems to give biased results
77- // int entryPicked = UnityEngine.Random.Range(0, entriesInFile.Count);
78- // using System.Random instead
79- System . Random r = new System . Random ( ) ;
80- int entryPicked = r . Next ( entriesInFile . Count ) ;
81-
82- // Set the text
83- setText ( entriesInFile [ entryPicked ] ) ;
88+ currentEntry . Add ( line ) ;
8489 }
8590 }
86- else
91+ if ( currentEntry . Count != 0 )
8792 {
88- // No custom text file found!
89- // Print an error in the console
90- Console . WriteLine ( "[CustomMenuText] No custom text file found!" ) ;
91- Console . WriteLine ( "Make sure the file is in the UserData folder and named CustomMenuText.txt!" ) ;
92-
93- // Default to BEAT SABER
94- setText ( DEFAULT_TEXT ) ;
93+ // in case the last entry doesn't end in a newline
94+ entriesInFile . Add ( currentEntry . ToArray ( ) ) ;
9595 }
96+ if ( entriesInFile . Count == 0 )
97+ {
98+ // No entries; warn and continue
99+ Console . WriteLine ( "[CustomMenuText] File found, but it contained no entries!" ) ;
100+ }
101+ }
102+ else
103+ {
104+ // No custom text file found!
105+ // Print an error in the console
106+ Console . WriteLine ( "[CustomMenuText] No custom text file found!" ) ;
107+ Console . WriteLine ( "Make sure the file is in the UserData folder and named CustomMenuText.txt!" ) ;
96108 }
109+
110+ return entriesInFile ;
97111 }
98112
99113 /// <summary>
@@ -109,7 +123,7 @@ private void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1)
109123 /// <param name="lines">
110124 /// The text to display, separated by lines (from top to bottom).
111125 /// </param>
112- public void setText ( string [ ] lines )
126+ public static void setText ( string [ ] lines )
113127 {
114128 TextMeshPro wasB = GameObject . Find ( "B" ) . GetComponent < TextMeshPro > ( ) ;
115129 TextMeshPro wasE = GameObject . Find ( "E" ) . GetComponent < TextMeshPro > ( ) ;
@@ -178,6 +192,11 @@ public void setText(string[] lines)
178192 }
179193 }
180194
195+ public void reloadFile ( )
196+ {
197+ allEntries = readFromFile ( FILE_PATH ) ;
198+ }
199+
181200 public void OnApplicationQuit ( )
182201 {
183202 SceneManager . activeSceneChanged -= SceneManagerOnActiveSceneChanged ;
0 commit comments