From 7faac0f6a865369dc7cc286e4ec105b8d4c9f869 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Tue, 10 Jan 2017 20:36:03 +0000
Subject: [PATCH] Fix build. Set up initial blank embeds.

---
 ...cketClient.cs => NibriWebSocketHandler.cs} |  19 +-
 Nibriboard/Nibriboard.csproj                  |   8 +-
 Nibriboard/NibriboardServer.cs                |   7 +-
 Nibriboard/Program.cs                         |   3 +
 Nibriboard/RippleSpace/Chunk.cs               |  11 +-
 Nibriboard/RippleSpace/ChunkReference.cs      |   4 +-
 Nibriboard/RippleSpace/LocationReference.cs   |   6 +-
 Nibriboard/Utilities/EmbeddedFiles.cs         | 169 +++++++++++++++++-
 8 files changed, 196 insertions(+), 31 deletions(-)
 rename Nibriboard/{NibriWebSocketClient.cs => NibriWebSocketHandler.cs} (72%)

diff --git a/Nibriboard/NibriWebSocketClient.cs b/Nibriboard/NibriWebSocketHandler.cs
similarity index 72%
rename from Nibriboard/NibriWebSocketClient.cs
rename to Nibriboard/NibriWebSocketHandler.cs
index 918e52d..c730af5 100644
--- a/Nibriboard/NibriWebSocketClient.cs
+++ b/Nibriboard/NibriWebSocketHandler.cs
@@ -3,18 +3,16 @@ using IotWeb.Common.Http;
 using System.Threading.Tasks;
 namespace Nibriboard
 {
-	public class NibriWebSocketClient : IWebSocketRequestHandler
+	public class NibriWebSocketHandler : IWebSocketRequestHandler
 	{
-		WebSocket client;
-
-		public NibriWebSocketClient()
+		public NibriWebSocketHandler()
 		{
 		}
 
-		public void Connected(WebSocket socket)
+		public void Connected(WebSocket client)
 		{
-			client = socket;
-			client.DataReceived += async (WebSocket clientSocket, string frame) => {
+			
+			/*client.DataReceived += async (WebSocket clientSocket, string frame) => {
 				try {
 					await onMessage(frame);
 				}
@@ -24,7 +22,7 @@ namespace Nibriboard
 				}
 
 				//Task.Run(async () => await onMessage(frame)).Wait();
-			};
+			};*/
 		}
 
 		/// <summary>
@@ -38,10 +36,5 @@ namespace Nibriboard
 			Log.WriteLine("[Nibriboard/Websocket] Accepting {0} via {1}.", uri, protocol);
 			return true;
 		}
-
-		private async Task onMessage(string frame)
-		{
-
-		}
 	}
 }
diff --git a/Nibriboard/Nibriboard.csproj b/Nibriboard/Nibriboard.csproj
index b6473f0..d20e6d9 100644
--- a/Nibriboard/Nibriboard.csproj
+++ b/Nibriboard/Nibriboard.csproj
@@ -57,15 +57,19 @@
     <Compile Include="RippleSpace\Reference.cs" />
     <Compile Include="Utilities.cs" />
     <Compile Include="NibriboardServer.cs" />
+    <Compile Include="NibriWebSocketHandler.cs" />
     <Compile Include="Log.cs" />
-    <Compile Include="NibriWebSocketClient.cs" />
+    <Compile Include="Utilities\EmbeddedFiles.cs" />
   </ItemGroup>
   <ItemGroup>
-  	<EmbeddedResource Include="Client\" Exclude="node_modules" />
+  	<EmbeddedResource Include="Client\index.html" />
+  	<EmbeddedResource Include="Client\NibriClient.js" />
+  	<EmbeddedResource Include="Client\Nibri.css" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="RippleSpace\" />
     <Folder Include="Client\" />
+    <Folder Include="Utilities\" />
   </ItemGroup>
   <ItemGroup>
     <None Include="packages.config" />
diff --git a/Nibriboard/NibriboardServer.cs b/Nibriboard/NibriboardServer.cs
index cac7a3f..65688de 100644
--- a/Nibriboard/NibriboardServer.cs
+++ b/Nibriboard/NibriboardServer.cs
@@ -28,15 +28,10 @@ namespace Nibriboard
 			);
 			httpServer.AddWebSocketRequestHandler(
 				"/RipplespaceConnection",
-
+				new NibriWebSocketHandler()
 			);
 			httpServer.Start();
 			Log.WriteLine("[NibriboardServer] Started on port {0}", Port);
 		}
-
-		private void Test()
-		{
-			HttpListener.
-		}
 	}
 }
diff --git a/Nibriboard/Program.cs b/Nibriboard/Program.cs
index b218785..da9a953 100644
--- a/Nibriboard/Program.cs
+++ b/Nibriboard/Program.cs
@@ -1,4 +1,6 @@
 ﻿using System;
+using System.Reflection;
+using SBRLUtilities;
 
 namespace Nibriboard
 {
@@ -7,6 +9,7 @@ namespace Nibriboard
 		public static void Main(string[] args)
 		{
 			Console.WriteLine("Hello World!");
+			EmbeddedFiles.WriteResourceList();
 		}
 	}
 }
diff --git a/Nibriboard/RippleSpace/Chunk.cs b/Nibriboard/RippleSpace/Chunk.cs
index a248ef9..0322a0f 100644
--- a/Nibriboard/RippleSpace/Chunk.cs
+++ b/Nibriboard/RippleSpace/Chunk.cs
@@ -13,6 +13,11 @@ namespace Nibriboard.RippleSpace
 	[Serializable]
 	public class Chunk : IEnumerable<DrawnLine>, IDeserializationCallback
 	{
+		/// <summary>
+		/// The plane upon which this chunk is located.
+		/// </summary>
+		private Plane plane;
+
 		/// <summary>
 		/// The lines that this chunk currently contains.
 		/// </summary>
@@ -70,7 +75,7 @@ namespace Nibriboard.RippleSpace
 			get {
 				// If the time we were last accessed + the inactive timer is
 				// still less than the current time, then we're inactive.
-				if (TimeLastAccessed.AddMilliseconds(Plane.InactiveMillisecs) < DateTime.Now)
+				if (TimeLastAccessed.AddMilliseconds(plane.InactiveMillisecs) < DateTime.Now)
 					return false;
 				
 				return true;
@@ -96,7 +101,7 @@ namespace Nibriboard.RippleSpace
 
 		public Chunk(Plane inPlane, int inSize, ChunkReference inLocation)
 		{
-			Plane = inPlane;
+			plane = inPlane;
 			Size = inSize;
 			Location = inLocation;
 		}
@@ -168,7 +173,7 @@ namespace Nibriboard.RippleSpace
 		public static async Task<Chunk> FromStream(Plane plane, Stream chunkSource)
 		{
 			Chunk loadedChunk = await Utilities.DeserialiseBinaryObject<Chunk>(chunkSource);
-			loadedChunk.Plane = plane;
+			loadedChunk.plane = plane;
 
 			return loadedChunk;
 		}
diff --git a/Nibriboard/RippleSpace/ChunkReference.cs b/Nibriboard/RippleSpace/ChunkReference.cs
index b1d54d6..37beff1 100644
--- a/Nibriboard/RippleSpace/ChunkReference.cs
+++ b/Nibriboard/RippleSpace/ChunkReference.cs
@@ -64,8 +64,8 @@ namespace Nibriboard.RippleSpace
 			source = source.Substring("ChunkReference:".Length);
 			source = source.Trim("() \v\t\r\n".ToCharArray());
 
-			int x = source.Substring(0, source.IndexOf(","));
-			int y = source.Substring(source.IndexOf(",") + 1);
+			int x = int.Parse(source.Substring(0, source.IndexOf(",")));
+			int y = int.Parse(source.Substring(source.IndexOf(",") + 1));
 			return new ChunkReference(
 				plane,
 				x,
diff --git a/Nibriboard/RippleSpace/LocationReference.cs b/Nibriboard/RippleSpace/LocationReference.cs
index 61304f5..c1e65ca 100644
--- a/Nibriboard/RippleSpace/LocationReference.cs
+++ b/Nibriboard/RippleSpace/LocationReference.cs
@@ -1,5 +1,7 @@
 ﻿using System;
 using System.Configuration;
+using System.IO;
+
 namespace Nibriboard.RippleSpace
 {
 	/// <summary>
@@ -50,8 +52,8 @@ namespace Nibriboard.RippleSpace
 			source = source.Substring("LocationReference:".Length);
 			source = source.Trim("() \v\t\r\n".ToCharArray());
 
-			int x = source.Substring(0, source.IndexOf(","));
-			int y = source.Substring(source.IndexOf(",") + 1);
+			int x = int.Parse(source.Substring(0, source.IndexOf(",")));
+			int y = int.Parse(source.Substring(source.IndexOf(",") + 1));
 			return new LocationReference(
 				plane,
 				x,
diff --git a/Nibriboard/Utilities/EmbeddedFiles.cs b/Nibriboard/Utilities/EmbeddedFiles.cs
index e81f206..6086b9c 100644
--- a/Nibriboard/Utilities/EmbeddedFiles.cs
+++ b/Nibriboard/Utilities/EmbeddedFiles.cs
@@ -1,10 +1,173 @@
 ﻿using System;
-namespace Nibriboard.Utilities
+using System.Reflection;
+using System.IO;
+using System.Net.Configuration;
+using System.Threading.Tasks;
+using System.Collections.Generic;
+
+namespace SBRLUtilities
 {
-	public class EmbeddedFiles
+	/// <summary>
+	/// A collection of static methods for manipulating embedded resources.
+	/// </summary>
+	/// <description>
+	/// v0.2, by Starbeamrainbowlabs <feedback@starbeamrainbowlabs.com>
+	/// Last updated 8th August 2016.
+	/// Licensed under MPL-2.0.
+	/// 
+	/// Changelog:
+	/// v0.1 (25th July 2016):
+	/// 	- Initial release.
+	/// v0.2 (8th August 2016):
+	/// 	- Changed namespace.
+	/// </description>
+	public static class EmbeddedFiles
 	{
-		public EmbeddedFiles()
+		/// <summary>
+		/// An array of the filenames of all the resources embedded in the calling assembly.
+		/// </summary>
+		/// <value>The resource list.</value>
+		public static string[] ResourceList {
+			get {
+				return Assembly.GetCallingAssembly().GetManifestResourceNames();
+			}
+		}
+
+		public static string GetResourceListText()
 		{
+			StringWriter result = new StringWriter();
+			result.WriteLine("Files embedded in {0}:", Assembly.GetCallingAssembly().GetName().Name);
+			foreach (string filename in ResourceList)
+				result.WriteLine(" - {0}", filename);
+			return result.ToString();
+		}
+		/// <summary>
+		/// Writes a list of embedded resources to the Console's standard output.
+		/// </summary>
+		public static void WriteResourceList()
+		{
+			Console.WriteLine(GetResourceListText());
+		}
+
+		/// <summary>
+		/// Gets a StreamReader attached to the specified embedded resource.
+		/// </summary>
+		/// <param name="filename">The filename of the embedded resource to a StreamReader for.</param>
+		/// <returns>A StreamReader attached to the specified embedded resource.</returns>
+		public static StreamReader GetReader(string filename)
+		{
+			return new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(filename));
+		}
+
+		/// <summary>
+		/// Gets the specified embedded resource's content as a byte array.
+		/// </summary>
+		/// <param name="filename">The filename of the embedded resource to get conteent of.</param>
+		/// <returns>The specified embedded resource's content as a byte array.</returns>
+		public static byte[] ReadAllBytes(string filename)
+		{
+			// Referencing the Result property will block until the async method completes
+			return ReadAllBytesAsync(filename).Result;
+		}
+		/// <summary>
+		/// Gets the specified embedded resource's content as a byte array asynchronously.
+		/// </summary>
+		/// <param name="filename">The filename of the embedded resource to get conteent of.</param>
+		/// <returns>The specified embedded resource's content as a byte array.</returns>
+		public static async Task<byte[]> ReadAllBytesAsync(string filename)
+		{
+			using (Stream resourceStream = Assembly.GetCallingAssembly().GetManifestResourceStream(filename))
+			using (MemoryStream temp = new MemoryStream())
+			{
+				await resourceStream.CopyToAsync(temp);
+				return temp.ToArray();
+			}
+		}
+
+		/// <summary>
+		/// Gets all the text stored in the specified embedded resource.
+		/// </summary>
+		/// <param name="filename">The filename to fetch the content of.</param>
+		/// <returns>All the text stored in the specified embedded resource.</returns>
+		public static string ReadAllText(string filename)
+		{
+			using (StreamReader resourceReader = new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(filename)))
+			{
+				return resourceReader.ReadToEnd();
+			}
+		}
+		/// <summary>
+		/// Gets all the text stored in the specified embedded resource asynchronously.
+		/// </summary>
+		/// <param name="filename">The filename to fetch the content of.</param>
+		/// <returns>All the text stored in the specified embedded resource.</returns>
+		public static async Task<string> ReadAllTextAsync(string filename)
+		{
+			using (StreamReader resourceReader = new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(filename)))
+			{
+				return await resourceReader.ReadToEndAsync();
+			}
+		}
+
+		/// <summary>
+		/// Enumerates the lines of text in the specified embedded resource.
+		/// </summary>
+		/// <param name="filename">The filename of the embedded resource to enumerate.</param>
+		/// <returns>An IEnumerator that enumerates the specified embedded resource.</returns>
+		public static IEnumerator<string> EnumerateLines(string filename)
+		{
+			using (StreamReader resourceReader = new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(filename)))
+			{
+				string nextLine;
+				while ((nextLine = resourceReader.ReadLine()) != null)
+				{
+					yield return nextLine;
+				}
+			}
+		}
+		/// <summary>
+		/// Enumerates the lines of text in the specified embedded resource asynchronously.
+		/// Each successive call returns a task that, when complete, returns the next line of text stored
+		/// in the embedded resource.
+		/// </summary>
+		/// <param name="filename">The filename of the embedded resource to enumerate.</param>
+		/// <returns>An IEnumerator that enumerates the specified embedded resource.</returns>
+		public static IEnumerable<Task<string>> EnumerateLinesAsync(string filename)
+		{
+			using (StreamReader resourceReader = new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(filename)))
+			{
+				while (!resourceReader.EndOfStream)
+				{
+					yield return resourceReader.ReadLineAsync();
+				}
+			}
+		}
+
+		/// <summary>
+		/// Gets all the lines of text in the specified embedded resource.
+		/// You might find EnumerateLines(string filename) more useful depending on your situation.
+		/// </summary>
+		/// <param name="filename">The filename to obtain the lines of text from.</param>
+		/// <returns>A list of lines in the specified embedded resource.</returns>
+		public static List<string> GetAllLines(string filename)
+		{
+			// Referencing the Result property will block until the async method completes
+			return GetAllLinesAsync(filename).Result;
+		}
+		/// <summary>
+		/// Gets all the lines of text in the specified embedded resource asynchronously.
+		/// </summary>
+		/// <param name="filename">The filename to obtain the lines of text from.</param>
+		/// <returns>A list of lines in the specified embedded resource.</returns>
+		public static async Task<List<string>> GetAllLinesAsync(string filename)
+		{
+			List<string> lines = new List<string>();
+			IEnumerable<Task<string>> lineIterator = EnumerateLinesAsync(filename);
+			foreach (Task<string> nextLine in lineIterator)
+			{
+				lines.Add(await nextLine);
+			}
+			return lines;
 		}
 	}
 }
