From c76975a1cd65eff40a64e85eb95139b774fe8c13 Mon Sep 17 00:00:00 2001 From: melchior Date: Thu, 13 Feb 2020 21:52:29 -0500 Subject: [PATCH] W.I.P. VII: Cmd events mostly Protobufferized, JSON Metadata file now generated....(but not validated) --- Automap/Automap.csproj | 1 + Automap/Data/CommandData.cs | 62 ++++++++++++++++++++++++++----- Automap/Subsystems/AutomapGUIDialog.cs | 9 +++-- Automap/Subsystems/AutomapSystem.cs | 68 +++++++++++++++++++++++++++++++++- 4 files changed, 126 insertions(+), 14 deletions(-) diff --git a/Automap/Automap.csproj b/Automap/Automap.csproj index ea1c568..6042ebb 100644 --- a/Automap/Automap.csproj +++ b/Automap/Automap.csproj @@ -69,6 +69,7 @@ VS_libs\Pngcs.dll + diff --git a/Automap/Data/CommandData.cs b/Automap/Data/CommandData.cs index ccce2df..c87c358 100644 --- a/Automap/Data/CommandData.cs +++ b/Automap/Data/CommandData.cs @@ -1,35 +1,71 @@ using System; +using System.Collections; +using System.Collections.Generic; +using System.Drawing; using System.IO; +using ProtoBuf; + using Vintagestory.API.Common; using Vintagestory.API.Datastructures; namespace Automap { + [ProtoContract(SkipConstructor = true)] + //[ProtoInclude(5,typeof(DelegateState))] public class CommandData : IAttribute { + [ProtoMember(1)] public RunState State { get; set; }//Run , Stop, SingleSnapshot -> Stop - //POI Delegate list {enabled/Disable} + [ProtoMember(2)] + private List DelegatesFlags; + + //POI Delegate list {enabled/Disable}, color? //Other params...? Tick rate? //Choose : Renderer(s) - public CommandData( RunState assumeState) + public CommandData( RunState assumeState, bool[] theseDelegates) { State = assumeState; + DelegatesFlags = new List(theseDelegates.Length); + foreach (var df in theseDelegates) { + DelegatesFlags.Add(new DelegateState( ) { + Enabled = df, + AlternateColor = null, + }); + } + } + + public CommandData(RunState assumeState) + { + State = assumeState;//Never RUN. + + DelegatesFlags = new List(); + } public void FromBytes(BinaryReader stream) { - State = ( RunState )stream.ReadByte( ); + var temp = ProtoBuf.Serializer.Deserialize(stream.BaseStream); + this.State = temp.State; + this.DelegatesFlags = temp.DelegatesFlags; + + } + + public void ToBytes(BinaryWriter stream) + { + ProtoBuf.Serializer.Serialize(stream.BaseStream, this); } + + public int GetAttributeId( ) { return 12346; @@ -40,15 +76,10 @@ namespace Automap return this; } - public void ToBytes(BinaryWriter stream) - { - stream.Write(( byte )State); - - } public string ToJsonToken( ) { - return $"New-State:{State}, "; + return $"New-State:{State}, Delegates# {DelegatesFlags.Count} "; } public bool Equals(IWorldAccessor worldForResolve, IAttribute attr) @@ -63,5 +94,18 @@ namespace Automap return false; } } + + [ProtoContract] + internal struct DelegateState + { + [ProtoMember(1)] + public bool Enabled; + + [ProtoMember(2)] + public Color? AlternateColor; + + + + } } diff --git a/Automap/Subsystems/AutomapGUIDialog.cs b/Automap/Subsystems/AutomapGUIDialog.cs index 1692997..898c6a0 100644 --- a/Automap/Subsystems/AutomapGUIDialog.cs +++ b/Automap/Subsystems/AutomapGUIDialog.cs @@ -89,16 +89,19 @@ namespace Automap { Logger.VerboseDebug("Dialog Changed; [ Automap Enabled: {0} ]", toggle); var statusText = this.SingleComposer.GetDynamicText(_statusTextKey); - statusText.SetNewText($"State: {(toggle? "Run": "Halt" )}, Total: ?, Nulls: 0 " ); + statusText.SetNewText($"State: {(toggle? "Run": "Halt" )}, Total: {totalShards}, Nulls: {voidShards} " ); + + CommandData cmd; if (toggle) { dashTickHandle = capi.Event.RegisterGameTickListener(UpdateDashDisplay, 6001); + cmd = new CommandData(toggle ? RunState.Run : RunState.Stop, new bool[ ] { true, true, true, true, true }); } else { capi.Event.UnregisterGameTickListener(dashTickHandle); + cmd = new CommandData(toggle ? RunState.Run : RunState.Stop); } - - CommandData cmd = new CommandData(toggle ? RunState.Run : RunState.Stop); + capi.Event.PushEvent(AutomapSystem.AutomapCommandEventKey, cmd); } diff --git a/Automap/Subsystems/AutomapSystem.cs b/Automap/Subsystems/AutomapSystem.cs index 104680c..6dfa240 100644 --- a/Automap/Subsystems/AutomapSystem.cs +++ b/Automap/Subsystems/AutomapSystem.cs @@ -185,6 +185,7 @@ namespace Automap //What about chunk updates themselves; a update bitmap isn't kept... updatedChunksTotal += updatedChunks; GenerateMapHTML( ); + GenerateJSONMetadata( ); updatedChunks = 0; } @@ -258,7 +259,7 @@ namespace Automap } - //TODO: Convert to RAZOR model + private void GenerateMapHTML( ) { string mapFilename = Path.Combine(path, "Automap.html"); @@ -460,6 +461,48 @@ namespace Automap Logger.VerboseDebug("Generated HTML map"); } + /// + /// Generates the JSON Metadata. (in MAP object format ) + /// + private void GenerateJSONMetadata( ) + { + string jsonFilename = Path.Combine(path, "Metadata.js"); + + StreamWriter jsonWriter = new StreamWriter(jsonFilename, false, Encoding.UTF8); + using (jsonWriter) + { + jsonWriter.WriteLine("var worldSeedNum = {0};", ClientAPI.World.Seed); + jsonWriter.WriteLine("var genTime = new Date('{0}');", DateTimeOffset.UtcNow.ToString("O")); + jsonWriter.WriteLine("var chunkSize = {0};", chunkSize); + jsonWriter.WriteLine("var northMostChunk ={0};", chunkTopMetadata.North_mostChunk); + jsonWriter.WriteLine("var southMostChunk ={0};", chunkTopMetadata.South_mostChunk); + jsonWriter.WriteLine("var eastMostChunk ={0};", chunkTopMetadata.East_mostChunk); + jsonWriter.WriteLine("var westMostChunk ={0};", chunkTopMetadata.West_mostChunk); + //MAP object format - [key, value]: key is "x_y" + jsonWriter.Write("let shardsMetadata = new Map(["); + foreach (var shard in chunkTopMetadata) + { + jsonWriter.Write("['{0}_{1}',", shard.Location.X, shard.Location.Y); + jsonWriter.Write("{"); + jsonWriter.Write("ChunkAge = '{0}',", shard.ChunkAge);//World age - relative? or last edit ?? + jsonWriter.Write("Temperature = {0},", shard.Temperature.ToString("F1")); + jsonWriter.Write("YMax = {0},", shard.YMax); + jsonWriter.Write("Fertility = {0},", shard.Fertility.ToString("F1")); + jsonWriter.Write("ForestDensity = {0},", shard.ForestDensity.ToString("F1")); + jsonWriter.Write("Rainfall = {0},", shard.Rainfall.ToString("F1")); + jsonWriter.Write("ShrubDensity = {0},", shard.ShrubDensity.ToString("F1")); + jsonWriter.Write("AirBlocks = {0},", shard.AirBlocks); + jsonWriter.Write("NonAirBlocks = {0},", shard.NonAirBlocks); + //TODO: Heightmap + //TODO: Rock-ratio + jsonWriter.Write("}],"); + //TODO: POIs + } + jsonWriter.Write("]);\n\n"); + jsonWriter.Flush( ); + } + + } private ColumnMeta CreateColumnMetadata(KeyValuePair mostActiveCol, IMapChunk mapChunk) @@ -655,7 +698,28 @@ namespace Automap Logger.VerboseDebug("MsgBus RX: AutomapCommandMsg: {0}", data.ToJsonToken() ); CommandData cmdData = data as CommandData; - //TODO: Support snapshot mode + + + if (CurrentState != RunState.Snapshot) { + switch (cmdData.State) { + case RunState.Run: + CurrentState = cmdData.State; + AwakenCartographer(0.0f); + break; + + case RunState.Stop: + CurrentState = cmdData.State; + break; + + case RunState.Snapshot: + CurrentState = RunState.Stop; + //Snapshot starts a second thread/process... + + break; + } + + } + if (CurrentState != cmdData.State) { CurrentState = cmdData.State; AwakenCartographer(0.0f); -- 2.11.0