From fddfa31939d5bff636bb695a2c6d901be7ddeb77 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sun, 23 Jul 2017 19:36:15 +0100
Subject: [PATCH] Fix the saving system. It works!

---
 Nibriboard/NibriboardServer.cs               |  2 +-
 Nibriboard/RippleSpace/Chunk.cs              |  1 +
 Nibriboard/RippleSpace/Plane.cs              | 18 +++++++++++-------
 Nibriboard/RippleSpace/RippleSpaceManager.cs |  5 +++--
 Nibriboard/Utilities/CalcPaths.cs            |  6 +++---
 5 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/Nibriboard/NibriboardServer.cs b/Nibriboard/NibriboardServer.cs
index 3571228..d435d5c 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.tar.gz" };
+		private RippleSpaceManager planeManager = new RippleSpaceManager() { SourceFilename = "./test.ripplespace.zip" };
 
 		private readonly CancellationTokenSource clientManagerCanceller = new CancellationTokenSource();
 		private NibriClientManager clientManager;
diff --git a/Nibriboard/RippleSpace/Chunk.cs b/Nibriboard/RippleSpace/Chunk.cs
index 63f1559..a5bdae8 100644
--- a/Nibriboard/RippleSpace/Chunk.cs
+++ b/Nibriboard/RippleSpace/Chunk.cs
@@ -235,6 +235,7 @@ namespace Nibriboard.RippleSpace
 		public async Task SaveTo(StreamWriter destination)
 		{
 			await destination.WriteLineAsync(JsonConvert.SerializeObject(this));
+			destination.Close();
 		}
 
 		public void OnDeserialization(object sender)
diff --git a/Nibriboard/RippleSpace/Plane.cs b/Nibriboard/RippleSpace/Plane.cs
index 7ed0672..9559a33 100644
--- a/Nibriboard/RippleSpace/Plane.cs
+++ b/Nibriboard/RippleSpace/Plane.cs
@@ -256,24 +256,28 @@ namespace Nibriboard.RippleSpace
 				string chunkDestinationFilename = CalcPaths.ChunkFilepath(StorageDirectory, loadedChunkItem.Key);
 				Directory.CreateDirectory(Path.GetDirectoryName(chunkDestinationFilename));
 				// Ask the chunk to save itself
-				using(StreamWriter chunkDestination = new StreamWriter(chunkDestinationFilename))
-				{
-					chunkSavers.Add(loadedChunkItem.Value.SaveTo(chunkDestination));
-				}
+				StreamWriter chunkDestination = new StreamWriter(chunkDestinationFilename);
+				chunkSavers.Add(loadedChunkItem.Value.SaveTo(chunkDestination));
 			}
 			await Task.WhenAll(chunkSavers);
 
 			// Pack the chunks into an nplane file
-			WriterOptions packingOptions = new WriterOptions(CompressionType.GZip);
+			WriterOptions packingOptions = new WriterOptions(CompressionType.Deflate);
 
-			IEnumerable<string> chunkFiles = Directory.GetFiles(StorageDirectory);
-			using(IWriter packer = WriterFactory.Open(destination, ArchiveType.Tar, packingOptions))
+			IEnumerable<string> chunkFiles = Directory.GetFiles(StorageDirectory.TrimEnd("/".ToCharArray()));
+			using(IWriter packer = WriterFactory.Open(destination, ArchiveType.Zip, packingOptions))
 			{
 				foreach(string nextChunkFile in chunkFiles)
 				{
+					Console.WriteLine("[Command/Save] Packing {0} as {1}",
+						nextChunkFile, 
+						$"{Name}/{Path.GetFileName(nextChunkFile)}"
+					);
 					packer.Write($"{Name}/{Path.GetFileName(nextChunkFile)}", nextChunkFile);
 				}
 			}
+			destination.Flush();
+			destination.Close();
 		}
 
 		/// <summary>
diff --git a/Nibriboard/RippleSpace/RippleSpaceManager.cs b/Nibriboard/RippleSpace/RippleSpaceManager.cs
index d6551f6..32572b6 100644
--- a/Nibriboard/RippleSpace/RippleSpaceManager.cs
+++ b/Nibriboard/RippleSpace/RippleSpaceManager.cs
@@ -137,15 +137,16 @@ namespace Nibriboard.RippleSpace
 
 			// Pack the planes into the ripplespace archive
 			Stream destination = File.OpenWrite(SourceFilename);
-			string[] planeFiles = Directory.GetFiles(UnpackedDirectory, "*.nplane.tar.gz", SearchOption.TopDirectoryOnly);
+			string[] planeFiles = Directory.GetFiles(UnpackedDirectory, "*.nplane.zip", SearchOption.TopDirectoryOnly);
 
-			using(IWriter rippleSpacePacker = WriterFactory.Open(destination, ArchiveType.Tar, new WriterOptions(CompressionType.GZip)))
+			using(IWriter rippleSpacePacker = WriterFactory.Open(destination, ArchiveType.Zip, new WriterOptions(CompressionType.Deflate)))
 			{
 				foreach(string planeFilename in planeFiles)
 				{
 					rippleSpacePacker.Write(Path.GetFileName(planeFilename), planeFilename);
 				}
 			}
+			destination.Close();
 		}
 
 		public async Task<RippleSpaceManager> FromFile(string filename)
diff --git a/Nibriboard/Utilities/CalcPaths.cs b/Nibriboard/Utilities/CalcPaths.cs
index 0bc8126..44ae3de 100644
--- a/Nibriboard/Utilities/CalcPaths.cs
+++ b/Nibriboard/Utilities/CalcPaths.cs
@@ -13,7 +13,7 @@ namespace Nibriboard.Utilities
 		/// <returns>The directory to which a plane should unpack it's data to.</returns>
 		public static string UnpackedPlaneDir(string unpackingRoot, string planeName)
 		{
-			string result = $"{unpackingRoot}/Planes/{planeName}/";
+			string result = $"{unpackingRoot}Planes/{planeName}/";
 			return result;
 		}
 
@@ -35,13 +35,13 @@ namespace Nibriboard.Utilities
 		/// <returns>The path to the packed plane file.</returns>
 		public static string UnpackedPlaneFile(string unpackingDir, string planeName)
 		{
-			return $"{unpackingDir}/{planeName}.nplane.tar.gz";
+			return $"{unpackingDir}{planeName}.nplane.zip";
 		}
 
 
 		public static string ChunkFilepath(string planeStorageDirectory, ChunkReference chunkRef)
 		{
-			return $"{planeStorageDirectory}/{chunkRef.AsFilename()}";
+			return $"{planeStorageDirectory}{chunkRef.AsFilename()}";
 		}
 	}
 }
