1
0
Fork 0
mirror of https://github.com/sbrl/Nibriboard.git synced 2018-01-10 21:33:49 +00:00

Get loading to work :D Now we've just got a few more chunk loading issues to sort out...

This commit is contained in:
Starbeamrainbowlabs 2017-07-29 13:20:12 +01:00
parent d19135d486
commit 226452cb57
9 changed files with 82 additions and 6 deletions

View file

@ -122,7 +122,9 @@ namespace Nibriboard
await destination.WriteLineAsync(" save Save the ripplespace to disk"); await destination.WriteLineAsync(" save Save the ripplespace to disk");
break; break;
case "save": case "save":
await destination.WriteAsync("Saving ripple space - ");
await planeManager.Save(); await planeManager.Save();
await destination.WriteLineAsync("done.");
break; break;
} }
} }

View file

@ -75,7 +75,7 @@ namespace Nibriboard.RippleSpace
/// The location of this chunk, in chunk-space, on the plane. /// The location of this chunk, in chunk-space, on the plane.
/// </summary> /// </summary>
[JsonProperty] [JsonProperty]
public readonly ChunkReference Location; public ChunkReference Location { get; private set; }
/// <summary> /// <summary>
/// Fired when this chunk is updated. /// Fired when this chunk is updated.
@ -223,6 +223,15 @@ namespace Nibriboard.RippleSpace
{ {
Chunk loadedChunk = JsonConvert.DeserializeObject<Chunk>(await chunkSource.ReadToEndAsync()); Chunk loadedChunk = JsonConvert.DeserializeObject<Chunk>(await chunkSource.ReadToEndAsync());
loadedChunk.plane = plane; loadedChunk.plane = plane;
loadedChunk.Location.Plane = plane;
foreach(DrawnLine line in loadedChunk.lines)
{
foreach(LocationReference point in line.Points)
{
point.Plane = plane;
}
}
loadedChunk.OnChunkUpdate += plane.HandleChunkUpdate; loadedChunk.OnChunkUpdate += plane.HandleChunkUpdate;
return loadedChunk; return loadedChunk;

View file

@ -18,6 +18,14 @@ namespace Nibriboard.RippleSpace
public ChunkReference(Plane inPlane, int inX, int inY) : base(inPlane, inX, inY) public ChunkReference(Plane inPlane, int inX, int inY) : base(inPlane, inX, inY)
{ {
}
/// <summary>
/// Creates a new blank <see cref="Nibriboard.RippleSpace.ChunkReference" />.
/// Don't use this yourself! This is only for Newtonsoft.Json to use when deserialising references.
/// </summary>
public ChunkReference() : base()
{
} }
/// <summary> /// <summary>

View file

@ -1,11 +1,16 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Newtonsoft.Json;
namespace Nibriboard.RippleSpace namespace Nibriboard.RippleSpace
{ {
/// <summary> /// <summary>
/// Represents a line drawn across the plane. /// Represents a line drawn across the plane.
/// </summary> /// </summary>
[Serializable]
[JsonObject(MemberSerialization.OptIn)]
public class DrawnLine public class DrawnLine
{ {
/// <summary> /// <summary>
@ -15,24 +20,29 @@ namespace Nibriboard.RippleSpace
/// line that was split across multiple chunks can still be referenced and /// line that was split across multiple chunks can still be referenced and
/// joined together. /// joined together.
/// </summary> /// </summary>
[JsonProperty]
public readonly string LineId; public readonly string LineId;
/// <summary> /// <summary>
/// The width of the line. /// The width of the line.
/// </summary> /// </summary>
[JsonProperty]
public int Width; public int Width;
/// <summary> /// <summary>
/// The colour of the line. /// The colour of the line.
/// </summary> /// </summary>
[JsonProperty]
public string Colour; public string Colour;
/// <summary> /// <summary>
/// The points that represent the line. /// The points that represent the line.
/// </summary> /// </summary>
[JsonProperty]
public List<LocationReference> Points = new List<LocationReference>(); public List<LocationReference> Points = new List<LocationReference>();
/// <summary> /// <summary>
/// Whether this line spans multiple chunks. /// Whether this line spans multiple chunks.
/// </summary> /// </summary>
[JsonProperty]
public bool SpansMultipleChunks { public bool SpansMultipleChunks {
get { get {
// TODO: Make this more intelligent such that connecting lines // TODO: Make this more intelligent such that connecting lines

View file

@ -24,6 +24,14 @@ namespace Nibriboard.RippleSpace
public LocationReference(Plane inPlane, int inX, int inY) : base(inPlane, inX, inY) public LocationReference(Plane inPlane, int inX, int inY) : base(inPlane, inX, inY)
{ {
}
/// <summary>
/// Creates a new blank <see cref="Nibriboard.RippleSpace.LocationReference" />.
/// Don't use this yourself! This is only for Newtonsoft.Json to use when deserialising references.
/// </summary>
public LocationReference() : base()
{
} }
public override bool Equals(object obj) public override bool Equals(object obj)

View file

@ -67,6 +67,12 @@ namespace Nibriboard.RippleSpace
/// </summary> /// </summary>
protected Dictionary<ChunkReference, Chunk> loadedChunkspace = new Dictionary<ChunkReference, Chunk>(); protected Dictionary<ChunkReference, Chunk> loadedChunkspace = new Dictionary<ChunkReference, Chunk>();
public PlaneInfo Info {
get {
return PlaneInfo.FromPlane(this);
}
}
/// <summary> /// <summary>
/// The number of chunks that this plane currently has laoded into active memory. /// The number of chunks that this plane currently has laoded into active memory.
/// </summary> /// </summary>
@ -261,12 +267,19 @@ namespace Nibriboard.RippleSpace
} }
await Task.WhenAll(chunkSavers); await Task.WhenAll(chunkSavers);
// Pack the chunks into an nplane file // Save the plane information
StreamWriter planeInfoWriter = new StreamWriter(CalcPaths.UnpackedPlaneIndex(StorageDirectory));
await planeInfoWriter.WriteLineAsync(JsonConvert.SerializeObject(Info));
planeInfoWriter.Close();
// Pack the chunks & plane information into an nplane file
WriterOptions packingOptions = new WriterOptions(CompressionType.Deflate); WriterOptions packingOptions = new WriterOptions(CompressionType.Deflate);
IEnumerable<string> chunkFiles = Directory.GetFiles(StorageDirectory.TrimEnd("/".ToCharArray())); IEnumerable<string> chunkFiles = Directory.GetFiles(StorageDirectory.TrimEnd("/".ToCharArray()));
using(IWriter packer = WriterFactory.Open(destination, ArchiveType.Zip, packingOptions)) using(IWriter packer = WriterFactory.Open(destination, ArchiveType.Zip, packingOptions))
{ {
packer.Write("plane-index.json", CalcPaths.UnpackedPlaneIndex(StorageDirectory));
foreach(string nextChunkFile in chunkFiles) foreach(string nextChunkFile in chunkFiles)
{ {
packer.Write($"{Name}/{Path.GetFileName(nextChunkFile)}", nextChunkFile); packer.Write($"{Name}/{Path.GetFileName(nextChunkFile)}", nextChunkFile);
@ -313,6 +326,7 @@ namespace Nibriboard.RippleSpace
using(Stream sourceStream = File.OpenRead(sourceFilename)) using(Stream sourceStream = File.OpenRead(sourceFilename))
using(IReader unpacker = ReaderFactory.Open(sourceStream)) using(IReader unpacker = ReaderFactory.Open(sourceStream))
{ {
Directory.CreateDirectory(targetUnpackingPath);
unpacker.WriteAllToDirectory(targetUnpackingPath); unpacker.WriteAllToDirectory(targetUnpackingPath);
} }

View file

@ -1,7 +1,11 @@
using System; using System;
using Newtonsoft.Json;
namespace Nibriboard.RippleSpace namespace Nibriboard.RippleSpace
{ {
[Serializable]
[JsonObject(MemberSerialization.OptOut)]
public class PlaneInfo public class PlaneInfo
{ {
public string Name { get; set; } public string Name { get; set; }
@ -18,6 +22,12 @@ namespace Nibriboard.RippleSpace
Name = inName; Name = inName;
ChunkSize = inChunkSize; ChunkSize = inChunkSize;
} }
}
public static PlaneInfo FromPlane(Plane plane)
{
PlaneInfo result = new PlaneInfo(plane.Name, plane.ChunkSize);
return result;
}
}
} }

View file

@ -10,7 +10,7 @@ namespace Nibriboard.RippleSpace
[JsonObject(MemberSerialization.OptIn)] [JsonObject(MemberSerialization.OptIn)]
public abstract class Reference : ICloneable public abstract class Reference : ICloneable
{ {
public readonly Plane Plane; public Plane Plane { get; set; }
/// <summary> /// <summary>
/// The name of the plane that this reference is located on. /// The name of the plane that this reference is located on.
@ -40,6 +40,14 @@ namespace Nibriboard.RippleSpace
Plane = inPlane; Plane = inPlane;
X = inX; Y = inY; X = inX; Y = inY;
} }
/// <summary>
/// Creates a new blank <see cref="Nibriboard.RippleSpace.Reference" />.
/// Don't use this yourself! This is only for Newtonsoft.Json to use when deserialising references.
/// </summary>
public Reference()
{
}
public override string ToString() public override string ToString()
{ {

View file

@ -126,8 +126,12 @@ namespace Nibriboard.RippleSpace
// Save the planes to disk // Save the planes to disk
List<Task> planeSavers = new List<Task>(); List<Task> planeSavers = new List<Task>();
StreamWriter indexWriter = new StreamWriter(UnpackedDirectory + "index.list");
foreach(Plane item in Planes) foreach(Plane item in Planes)
{ {
// Add the plane to the index
await indexWriter.WriteLineAsync(item.Name);
// Figure out where the plane should save itself to and create the appropriate directories // Figure out where the plane should save itself to and create the appropriate directories
string planeSavePath = CalcPaths.UnpackedPlaneFile(UnpackedDirectory, item.Name); string planeSavePath = CalcPaths.UnpackedPlaneFile(UnpackedDirectory, item.Name);
Directory.CreateDirectory(Path.GetDirectoryName(planeSavePath)); Directory.CreateDirectory(Path.GetDirectoryName(planeSavePath));
@ -135,14 +139,17 @@ namespace Nibriboard.RippleSpace
// Ask the plane to save to the directory // Ask the plane to save to the directory
planeSavers.Add(item.Save(File.OpenWrite(planeSavePath))); planeSavers.Add(item.Save(File.OpenWrite(planeSavePath)));
} }
indexWriter.Close();
await Task.WhenAll(planeSavers); await Task.WhenAll(planeSavers);
// Pack the planes into the ripplespace archive // Pack the planes into the ripplespace archive
Stream destination = File.OpenWrite(SourceFilename); Stream destination = File.OpenWrite(SourceFilename);
string[] planeFiles = Directory.GetFiles(UnpackedDirectory, "*.nplane.zip", SearchOption.TopDirectoryOnly); string[] planeFiles = Directory.GetFiles(UnpackedDirectory, "*.nplane.zip", SearchOption.TopDirectoryOnly);
using(IWriter rippleSpacePacker = WriterFactory.Open(destination, ArchiveType.Zip, new WriterOptions(CompressionType.Deflate))) using(IWriter rippleSpacePacker = WriterFactory.Open(destination, ArchiveType.Zip, new WriterOptions(CompressionType.Deflate)))
{ {
rippleSpacePacker.Write("index.list", UnpackedDirectory + "index.list");
foreach(string planeFilename in planeFiles) foreach(string planeFilename in planeFiles)
{ {
rippleSpacePacker.Write(Path.GetFileName(planeFilename), planeFilename); rippleSpacePacker.Write(Path.GetFileName(planeFilename), planeFilename);