From 8e7d61b7b94fbf7b6a48324d9415c11e365a960d Mon Sep 17 00:00:00 2001 From: Arti Kearney Date: Sat, 11 Aug 2018 20:48:24 -0700 Subject: [PATCH] 0.11.2 support; optimize and clean up code --- Custom Menu Text/CustomMenuTextPlugin.cs | 123 +++++++++++++---------- 1 file changed, 71 insertions(+), 52 deletions(-) diff --git a/Custom Menu Text/CustomMenuTextPlugin.cs b/Custom Menu Text/CustomMenuTextPlugin.cs index 267ef90..6f76da4 100644 --- a/Custom Menu Text/CustomMenuTextPlugin.cs +++ b/Custom Menu Text/CustomMenuTextPlugin.cs @@ -14,10 +14,14 @@ public class CustomMenuTextPlugin : IPlugin // path to the file to load text from private const string FILE_PATH = "/UserData/CustomMenuText.txt"; + // used if we can't load any custom entries public static readonly string[] DEFAULT_TEXT = { "BEAT", "SABER" }; + // caches entries loaded from the file so we don't need to do IO every time the menu loads + public static List allEntries = null; + public string Name => "Custom Menu Text"; - public string Version => "2.0.0"; + public string Version => "2.1.0"; public void OnApplicationStart() { SceneManager.activeSceneChanged += SceneManagerOnActiveSceneChanged; @@ -30,70 +34,80 @@ private void SceneManagerOnActiveSceneChanged(Scene arg0, Scene arg1) private void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1) { - if (arg0.buildIndex == 1) // Menu scene + if (arg0.name == "Menu") // Only run in menu scene { - // Look for the custom text file - string gameDirectory = Environment.CurrentDirectory; - gameDirectory = gameDirectory.Replace('\\', '/'); - if (File.Exists(gameDirectory + FILE_PATH)) + if (allEntries == null) + { + reloadFile(); + } + if (allEntries.Count == 0) { - var linesInFile = File.ReadLines(gameDirectory + FILE_PATH); + setText(DEFAULT_TEXT); + } + else + { + // Choose an entry randomly - // Strip comments (all lines beginning with #) - linesInFile = linesInFile.Where(s => s == "" || s[0] != '#'); + // Unity's random seems to give biased results + // int entryPicked = UnityEngine.Random.Range(0, entriesInFile.Count); + // using System.Random instead + System.Random r = new System.Random(); + int entryPicked = r.Next(allEntries.Count); - // Collect entries, splitting on empty lines - List entriesInFile = new List(); - List currentEntry = new List(); - foreach(string line in linesInFile) - { - if (line == "") - { - entriesInFile.Add(currentEntry.ToArray()); - currentEntry.Clear(); - } - else - { - currentEntry.Add(line); - } - } - if (currentEntry.Count != 0) - { - // in case the last entry doesn't end in a newline - entriesInFile.Add(currentEntry.ToArray()); - } + // Set the text + setText(allEntries[entryPicked]); + } + } + } + + public static List readFromFile(string relPath) + { + List entriesInFile = new List(); + + // Look for the custom text file + string gameDirectory = Environment.CurrentDirectory; + gameDirectory = gameDirectory.Replace('\\', '/'); + if (File.Exists(gameDirectory + FILE_PATH)) + { + var linesInFile = File.ReadLines(gameDirectory + FILE_PATH); + + // Strip comments (all lines beginning with #) + linesInFile = linesInFile.Where(s => s == "" || s[0] != '#'); - if (entriesInFile.Count == 0) + // Collect entries, splitting on empty lines + List currentEntry = new List(); + foreach (string line in linesInFile) + { + if (line == "") { - // No entries; warn and default to BEAT SABER - Console.WriteLine("[CustomMenuText] File found, but it contained no entries!"); - setText(DEFAULT_TEXT); + entriesInFile.Add(currentEntry.ToArray()); + currentEntry.Clear(); } else { - // Choose an entry randomly - - // Unity's random seems to give biased results - // int entryPicked = UnityEngine.Random.Range(0, entriesInFile.Count); - // using System.Random instead - System.Random r = new System.Random(); - int entryPicked = r.Next(entriesInFile.Count); - - // Set the text - setText(entriesInFile[entryPicked]); + currentEntry.Add(line); } } - else + if (currentEntry.Count != 0) { - // No custom text file found! - // Print an error in the console - Console.WriteLine("[CustomMenuText] No custom text file found!"); - Console.WriteLine("Make sure the file is in the UserData folder and named CustomMenuText.txt!"); - - // Default to BEAT SABER - setText(DEFAULT_TEXT); + // in case the last entry doesn't end in a newline + entriesInFile.Add(currentEntry.ToArray()); } + if (entriesInFile.Count == 0) + { + // No entries; warn and continue + Console.WriteLine("[CustomMenuText] File found, but it contained no entries!"); + } + } + else + { + // No custom text file found! + // Print an error in the console + Console.WriteLine("[CustomMenuText] No custom text file found!"); + Console.WriteLine("Make sure the file is in the UserData folder and named CustomMenuText.txt!"); } + + return entriesInFile; } /// @@ -109,7 +123,7 @@ private void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1) /// /// The text to display, separated by lines (from top to bottom). /// - public void setText(string[] lines) + public static void setText(string[] lines) { TextMeshPro wasB = GameObject.Find("B").GetComponent(); TextMeshPro wasE = GameObject.Find("E").GetComponent(); @@ -178,6 +192,11 @@ public void setText(string[] lines) } } + public void reloadFile() + { + allEntries = readFromFile(FILE_PATH); + } + public void OnApplicationQuit() { SceneManager.activeSceneChanged -= SceneManagerOnActiveSceneChanged;