Skip to content

Commit

Permalink
0.11.2 support; optimize and clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiswkearney committed Aug 12, 2018
1 parent cdce651 commit 8e7d61b
Showing 1 changed file with 71 additions and 52 deletions.
123 changes: 71 additions & 52 deletions Custom Menu Text/CustomMenuTextPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]> 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;
Expand All @@ -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<string[]> entriesInFile = new List<string[]>();
List<string> currentEntry = new List<string>();
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<string[]> readFromFile(string relPath)
{
List<string[]> entriesInFile = new List<string[]>();

// 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<string> currentEntry = new List<string>();
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;
}

/// <summary>
Expand All @@ -109,7 +123,7 @@ private void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1)
/// <param name="lines">
/// The text to display, separated by lines (from top to bottom).
/// </param>
public void setText(string[] lines)
public static void setText(string[] lines)
{
TextMeshPro wasB = GameObject.Find("B").GetComponent<TextMeshPro>();
TextMeshPro wasE = GameObject.Find("E").GetComponent<TextMeshPro>();
Expand Down Expand Up @@ -178,6 +192,11 @@ public void setText(string[] lines)
}
}

public void reloadFile()
{
allEntries = readFromFile(FILE_PATH);
}

public void OnApplicationQuit()
{
SceneManager.activeSceneChanged -= SceneManagerOnActiveSceneChanged;
Expand Down

0 comments on commit 8e7d61b

Please sign in to comment.