OSDN Git Service

W.I.P. VII: Cmd events mostly Protobufferized,
authormelchior <melchior@users.osdn.me>
Fri, 14 Feb 2020 02:52:29 +0000 (21:52 -0500)
committermelchior <melchior@users.osdn.me>
Fri, 14 Feb 2020 02:52:29 +0000 (21:52 -0500)
JSON Metadata file now generated....(but not validated)

Automap/Automap.csproj
Automap/Data/CommandData.cs
Automap/Subsystems/AutomapGUIDialog.cs
Automap/Subsystems/AutomapSystem.cs

index ea1c568..6042ebb 100644 (file)
@@ -69,6 +69,7 @@
     <Reference Include="Pngcs">
       <HintPath>VS_libs\Pngcs.dll</HintPath>
     </Reference>
+    <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
     <Compile Include="AutomapMod.cs" />
index ccce2df..c87c358 100644 (file)
@@ -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<DelegateState> 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<DelegateState>(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<DelegateState>();             
+               }
 
 
                public void FromBytes(BinaryReader stream)
                {
-               State = ( RunState )stream.ReadByte( );
+               var temp = ProtoBuf.Serializer.Deserialize<CommandData>(stream.BaseStream);
+               this.State = temp.State;
+               this.DelegatesFlags = temp.DelegatesFlags;
+
+               }
+
+               public void ToBytes(BinaryWriter stream)
+               {
+               ProtoBuf.Serializer.Serialize<CommandData>(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;
+
+
+
+       }
 }
 
index 1692997..898c6a0 100644 (file)
@@ -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);
                }
 
index 104680c..6dfa240 100644 (file)
@@ -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");
                }
 
+               /// <summary>
+               /// Generates the JSON Metadata. (in MAP object format )
+               /// </summary>
+               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<Vec2i, uint> 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);