From d19135d486ff333bfc4a3d2e317b080a1b201fe5 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sun, 23 Jul 2017 22:02:24 +0100
Subject: [PATCH] Tie in loading to main program

---
 Nibriboard/NibriboardServer.cs               | 12 ++++++--
 Nibriboard/Program.cs                        | 31 +++++++++++++++++++-
 Nibriboard/RippleSpace/RippleSpaceManager.cs |  4 +--
 3 files changed, 42 insertions(+), 5 deletions(-)

diff --git a/Nibriboard/NibriboardServer.cs b/Nibriboard/NibriboardServer.cs
index 56a438a..3853212 100644
--- a/Nibriboard/NibriboardServer.cs
+++ b/Nibriboard/NibriboardServer.cs
@@ -23,7 +23,7 @@ namespace Nibriboard
 		private HttpServer httpServer;
 
 		private ClientSettings clientSettings;
-		private RippleSpaceManager planeManager = new RippleSpaceManager() { SourceFilename = "./test.ripplespace.zip" };
+		private RippleSpaceManager planeManager;
 
 		private readonly CancellationTokenSource clientManagerCanceller = new CancellationTokenSource();
 		private NibriClientManager clientManager;
@@ -31,10 +31,18 @@ namespace Nibriboard
 		public readonly int CommandPort = 31587;
 		public readonly int Port = 31586;
 
-		public NibriboardServer(int inPort = 31586)
+		public NibriboardServer(string pathToRippleSpace, int inPort = 31586)
 		{
 			Port = inPort;
 
+			// Load the specified packed ripple space file if it exists - otherwise save it to disk
+			if(File.Exists(pathToRippleSpace)) {
+				planeManager = RippleSpaceManager.FromFile(pathToRippleSpace).Result;
+			} else {
+				Log.WriteLine("[NibriboardServer] Couldn't find packed ripple space at {0} - creating new ripple space instead.", pathToRippleSpace);
+				planeManager = new RippleSpaceManager() { SourceFilename = pathToRippleSpace };
+			}
+
 			clientSettings = new ClientSettings() {
 				SecureWebSocket = false,
 				WebSocketHost = "192.168.0.56",
diff --git a/Nibriboard/Program.cs b/Nibriboard/Program.cs
index 4289b4d..8127e6a 100644
--- a/Nibriboard/Program.cs
+++ b/Nibriboard/Program.cs
@@ -9,12 +9,41 @@ namespace Nibriboard
 	{
 		public static void Main(string[] args)
 		{
+			string packedRippleSpaceFile = "./default.ripplespace.zip";
+
+			for(int i = 0; i < args.Length; i++)
+			{
+				switch(args[i])
+				{
+					case "-h":
+					case "--help":
+						Console.WriteLine("Nibriboard Server");
+						Console.WriteLine("By Starbeamrainbowlabs");
+						Console.WriteLine();
+						Console.WriteLine("Usage:");
+						Console.WriteLine("    ./Nibriboard.exe [options]");
+						Console.WriteLine();
+						Console.WriteLine("Options:");
+						Console.WriteLine("    -h  --help             Shows this message");
+						Console.WriteLine("    -f  --file [filepath]  Specify the path to the packed ripplespace file to load. Defaults to '{0}'.", packedRippleSpaceFile);
+						Console.WriteLine();
+						return;
+
+					case "-f":
+					case "--file":
+						packedRippleSpaceFile = args[++i];
+						break;
+				}
+			}
+
 			Log.WriteLine("[core] Starting Nibriboard.");
 
 			Log.WriteLine("[core] Detected embedded files: ");
 			EmbeddedFiles.WriteResourceList();
 
-			NibriboardServer server = new NibriboardServer();
+			Log.WriteLine("[core] Loading ripple space from \"{0}\".", packedRippleSpaceFile);
+
+			NibriboardServer server = new NibriboardServer(packedRippleSpaceFile);
 			Task.WaitAll(
 				server.Start(),
 				server.StartCommandListener()
diff --git a/Nibriboard/RippleSpace/RippleSpaceManager.cs b/Nibriboard/RippleSpace/RippleSpaceManager.cs
index 79971d4..23a26f7 100644
--- a/Nibriboard/RippleSpace/RippleSpaceManager.cs
+++ b/Nibriboard/RippleSpace/RippleSpaceManager.cs
@@ -153,7 +153,7 @@ namespace Nibriboard.RippleSpace
 			Log.WriteLine("[Command/Save] Save complete in {0}ms", timer.ElapsedMilliseconds);
 		}
 
-		public async Task<RippleSpaceManager> FromFile(string filename)
+		public static async Task<RippleSpaceManager> FromFile(string filename)
 		{
 			if(!File.Exists(filename))
 				throw new FileNotFoundException($"Error: Couldn't find the packed ripplespace at {filename}");
@@ -165,7 +165,7 @@ namespace Nibriboard.RippleSpace
 			using(IReader rippleSpaceUnpacker = ReaderFactory.Open(packedRippleSpaceStream))
 			{
 				Log.WriteLine($"[Core] Unpacking ripplespace packed with {rippleSpaceUnpacker.ArchiveType} from {filename}.");
-				rippleSpaceUnpacker.WriteAllToDirectory(UnpackedDirectory);
+				rippleSpaceUnpacker.WriteAllToDirectory(rippleSpace.UnpackedDirectory);
 			}
 			Log.WriteLine("[Core] done!");
 
