OSDN Git Service

W.I.P. VI: Automap Commanded by local event bus
authormelchior <melchior@users.osdn.me>
Mon, 10 Feb 2020 02:09:14 +0000 (21:09 -0500)
committermelchior <melchior@users.osdn.me>
Mon, 10 Feb 2020 02:09:14 +0000 (21:09 -0500)
Automap/Automap.csproj
Automap/Data/CommandData.cs [new file with mode: 0644]
Automap/Data/RunState.cs [new file with mode: 0644]
Automap/Data/StatusData.cs
Automap/Subsystems/AutomapGUIDialog.cs
Automap/Subsystems/AutomapSystem.cs

index 985f16c..ea1c568 100644 (file)
@@ -87,6 +87,8 @@
     <Compile Include="Data\EntitiesOfInterest.cs" />
     <Compile Include="Data\EntityDesignator.cs" />
     <Compile Include="Data\StatusData.cs" />
     <Compile Include="Data\EntitiesOfInterest.cs" />
     <Compile Include="Data\EntityDesignator.cs" />
     <Compile Include="Data\StatusData.cs" />
+    <Compile Include="Data\CommandData.cs" />
+    <Compile Include="Data\RunState.cs" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="VS_libs\" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="VS_libs\" />
diff --git a/Automap/Data/CommandData.cs b/Automap/Data/CommandData.cs
new file mode 100644 (file)
index 0000000..ccce2df
--- /dev/null
@@ -0,0 +1,67 @@
+using System;
+using System.IO;
+
+using Vintagestory.API.Common;
+using Vintagestory.API.Datastructures;
+
+namespace Automap
+{
+       public class CommandData : IAttribute
+       {
+               public RunState State { get; set; }//Run , Stop, SingleSnapshot -> Stop
+
+               //POI Delegate list {enabled/Disable}
+               //Other params...? Tick rate?
+               //Choose : Renderer(s)
+
+
+               public CommandData( RunState assumeState)
+               {
+               State = assumeState;
+
+               }
+
+
+
+               public void FromBytes(BinaryReader stream)
+               {
+               State = ( RunState )stream.ReadByte( );
+               
+
+               }
+
+               public int GetAttributeId( )
+               {
+               return 12346;
+               }
+
+               public object GetValue( )
+               {
+               return this;
+               }
+
+               public void ToBytes(BinaryWriter stream)
+               {
+               stream.Write(( byte )State);
+               
+               }
+
+               public string ToJsonToken( )
+               {
+               return $"New-State:{State},  ";
+               }
+
+               public bool Equals(IWorldAccessor worldForResolve, IAttribute attr)
+               {
+               var other = attr.GetValue( ) as CommandData;
+
+                       if (this.State == other.State )
+                       {
+                       return true;
+                       }
+
+               return false;
+               }
+       }
+}
+
diff --git a/Automap/Data/RunState.cs b/Automap/Data/RunState.cs
new file mode 100644 (file)
index 0000000..55311b3
--- /dev/null
@@ -0,0 +1,11 @@
+using System;
+namespace Automap
+{
+       public enum RunState : byte
+       {
+               Stop = 0x00,
+               Run = 0x01,
+               Snapshot = 0x02,
+       }
+}
+
index 24f7344..a877ef4 100644 (file)
@@ -10,22 +10,23 @@ namespace Automap
                public uint TotalUpdates { get; set; }
                public uint VoidChunks { get; set; }
                public uint Delta { get; set; }
                public uint TotalUpdates { get; set; }
                public uint VoidChunks { get; set; }
                public uint Delta { get; set; }
+               public uint Max_N, Max_E, Max_S, Max_W;
+               public RunState CurrentState { get; set; }
 
 
-
-               public StatusData(uint totalUpdates, uint voidChunks, uint delta  )
+               public StatusData(uint totalUpdates, uint voidChunks, uint delta, RunState currently  )
                {
                TotalUpdates = totalUpdates;
                VoidChunks = voidChunks;
                Delta = delta;
                {
                TotalUpdates = totalUpdates;
                VoidChunks = voidChunks;
                Delta = delta;
+               CurrentState = currently;
                }
 
                }
 
-
-
                public void FromBytes(BinaryReader stream)
                {
                TotalUpdates = stream.ReadUInt32( );
                VoidChunks = stream.ReadUInt32( );
                Delta = stream.ReadUInt32( );
                public void FromBytes(BinaryReader stream)
                {
                TotalUpdates = stream.ReadUInt32( );
                VoidChunks = stream.ReadUInt32( );
                Delta = stream.ReadUInt32( );
+               CurrentState = ( RunState )stream.ReadByte( );
                }
 
                public int GetAttributeId( )
                }
 
                public int GetAttributeId( )
@@ -43,6 +44,7 @@ namespace Automap
                stream.Write(TotalUpdates);
                stream.Write(VoidChunks);
                stream.Write(Delta);
                stream.Write(TotalUpdates);
                stream.Write(VoidChunks);
                stream.Write(Delta);
+               stream.Write((byte)CurrentState);
                }
 
                public string ToJsonToken( )
                }
 
                public string ToJsonToken( )
@@ -56,7 +58,8 @@ namespace Automap
 
                if (this.TotalUpdates == other.TotalUpdates &&
                        this.VoidChunks == other.VoidChunks &&
 
                if (this.TotalUpdates == other.TotalUpdates &&
                        this.VoidChunks == other.VoidChunks &&
-                       this.Delta == other.Delta) {
+                       this.Delta == other.Delta &&
+                       this.CurrentState == other.CurrentState) {
                return true;
                }
                return false;
                return true;
                }
                return false;
index 007e6d8..1692997 100644 (file)
@@ -13,7 +13,6 @@ namespace Automap
                private const string _statusTextKey = @"txtStatus";
 
                private ILogger Logger;
                private const string _statusTextKey = @"txtStatus";
 
                private ILogger Logger;
-               private AutomapSystem _automapSystem;
 
                private long dashTickHandle;
 
 
                private long dashTickHandle;
 
@@ -25,13 +24,15 @@ namespace Automap
                }
 
                private uint totalShards, voidShards, changesThisTick;
                }
 
                private uint totalShards, voidShards, changesThisTick;
+               private RunState lastState;
+
 
                public AutomapGUIDialog(ICoreClientAPI capi,AutomapSystem ams) : base(capi)
        {
 
                public AutomapGUIDialog(ICoreClientAPI capi,AutomapSystem ams) : base(capi)
        {
-               _automapSystem = ams;
+
                Logger = capi.Logger;
                SetupDialog( );
                Logger = capi.Logger;
                SetupDialog( );
-               capi.Event.RegisterEventBusListener(AutomapStatusMsg, 1.0D, AutomapSystem.automapStatusEventKey);
+               capi.Event.RegisterEventBusListener(AutomapStatusMsg, 1.0D, AutomapSystem.AutomapStatusEventKey);
                
                }
 
                
                }
 
@@ -47,11 +48,11 @@ namespace Automap
                bgBounds.BothSizing = ElementSizing.FitToChildren;
                bgBounds.WithChildren(textBounds);
 
                bgBounds.BothSizing = ElementSizing.FitToChildren;
                bgBounds.WithChildren(textBounds);
 
-               ElementBounds toggleBounds = textBounds.CopyOffsetedSibling(3, 26, 5, 2);
+               ElementBounds toggleBounds = textBounds.CopyOffsetedSibling(3, 64, 5, 2);
                toggleBounds.fixedHeight = 24;
                toggleBounds.fixedWidth = 64;
 
                toggleBounds.fixedHeight = 24;
                toggleBounds.fixedWidth = 64;
 
-               ElementBounds txtStatusBounds = textBounds.CopyOffsetedSibling(0, 64, 2, 4);
+               ElementBounds txtStatusBounds = textBounds.CopyOffsetedSibling(0, 26, 2, 4);
                txtStatusBounds.fixedHeight = 16;
                txtStatusBounds.percentWidth = 1;
 
                txtStatusBounds.fixedHeight = 16;
                txtStatusBounds.percentWidth = 1;
 
@@ -83,13 +84,12 @@ namespace Automap
                /// Toggle Automap from/to RUN state
                /// </summary>
                /// <returns>The toggle.</returns>
                /// Toggle Automap from/to RUN state
                /// </summary>
                /// <returns>The toggle.</returns>
-               /// <param name="t1">T1.</param>
+               /// <param name="toggle">Run.</param>
                internal void RunToggle(bool toggle)
                {
                internal void RunToggle(bool toggle)
                {
-               _automapSystem.Enabled = toggle;
                Logger.VerboseDebug("Dialog Changed; [ Automap Enabled: {0} ]", toggle);
                var statusText = this.SingleComposer.GetDynamicText(_statusTextKey);
                Logger.VerboseDebug("Dialog Changed; [ Automap Enabled: {0} ]", toggle);
                var statusText = this.SingleComposer.GetDynamicText(_statusTextKey);
-                       statusText.SetNewText($"Running: {this._automapSystem.Enabled}, Total: ?, Nulls: 0 " );
+               statusText.SetNewText($"State: {(toggle? "Run": "Halt" )}, Total: ?, Nulls: 0 " );
 
                if (toggle) {
                dashTickHandle = capi.Event.RegisterGameTickListener(UpdateDashDisplay, 6001);
 
                if (toggle) {
                dashTickHandle = capi.Event.RegisterGameTickListener(UpdateDashDisplay, 6001);
@@ -98,24 +98,25 @@ namespace Automap
                capi.Event.UnregisterGameTickListener(dashTickHandle);
                }
 
                capi.Event.UnregisterGameTickListener(dashTickHandle);
                }
 
-
+               CommandData cmd = new CommandData(toggle ? RunState.Run : RunState.Stop);
+               capi.Event.PushEvent(AutomapSystem.AutomapCommandEventKey, cmd);
                }
 
 
                private void AutomapStatusMsg(string eventName, ref EnumHandling handling, IAttribute data)
                {
                }
 
 
                private void AutomapStatusMsg(string eventName, ref EnumHandling handling, IAttribute data)
                {
-               Logger.VerboseDebug("MsgBus - AutomapStatusMsg");
+               Logger.VerboseDebug("MsgBus RX: AutomapStatusMsg");
                StatusData realData = data as StatusData;
                totalShards = realData.TotalUpdates;
                voidShards = realData.VoidChunks;
                changesThisTick = realData.Delta;
                StatusData realData = data as StatusData;
                totalShards = realData.TotalUpdates;
                voidShards = realData.VoidChunks;
                changesThisTick = realData.Delta;
-                               
+               lastState = realData.CurrentState;
                }
 
                private void UpdateDashDisplay(float delay )
                {
                var statusText = this.SingleComposer.GetDynamicText(_statusTextKey);
                }
 
                private void UpdateDashDisplay(float delay )
                {
                var statusText = this.SingleComposer.GetDynamicText(_statusTextKey);
-               statusText.SetNewText($"Running: {this._automapSystem.Enabled}, Total: {totalShards}, Delta: {changesThisTick} Nulls: {voidShards} ");
+               statusText.SetNewText($"State: {lastState}, Total: {totalShards}, Delta: {changesThisTick} Nulls: {voidShards} ");
 
 
                }
 
 
                }
index 2c6b0b3..104680c 100644 (file)
@@ -17,6 +17,7 @@ using Vintagestory.API.Client;
 using Vintagestory.API.Common;
 using Vintagestory.API.Common.Entities;
 using Vintagestory.API.Config;
 using Vintagestory.API.Common;
 using Vintagestory.API.Common.Entities;
 using Vintagestory.API.Config;
+using Vintagestory.API.Datastructures;
 using Vintagestory.API.MathTools;
 using Vintagestory.Common;
 
 using Vintagestory.API.MathTools;
 using Vintagestory.Common;
 
@@ -44,7 +45,7 @@ namespace Automap
                internal Dictionary<AssetLocation, EntityDesignator> Entity_Designators { get; private set; }
                internal Dictionary<int, string> RockIdCodes { get; private set; }
 
                internal Dictionary<AssetLocation, EntityDesignator> Entity_Designators { get; private set; }
                internal Dictionary<int, string> RockIdCodes { get; private set; }
 
-               internal bool Enabled { get; set; }
+               internal RunState CurrentState { get; set; }
                //Run status, Chunks processed, stats, center of map....
                private uint nullChunkCount, updatedChunksTotal;
                private Vec2i startChunkColumn;
                //Run status, Chunks processed, stats, center of map....
                private uint nullChunkCount, updatedChunksTotal;
                private Vec2i startChunkColumn;
@@ -53,7 +54,8 @@ namespace Automap
                private string path;
                private IAsset stylesFile;
 
                private string path;
                private IAsset stylesFile;
 
-               public static string automapStatusEventKey = @"AutomapStatus";
+               public static string AutomapStatusEventKey = @"AutomapStatus";
+               public static string AutomapCommandEventKey = @"AutomapCommand";
 
 
                public AutomapSystem(ICoreClientAPI clientAPI, ILogger logger)
 
 
                public AutomapSystem(ICoreClientAPI clientAPI, ILogger logger)
@@ -65,6 +67,10 @@ namespace Automap
 
                //TODO:Choose which one from GUI 
                this.ChunkRenderer = new StandardRenderer(clientAPI, logger);
 
                //TODO:Choose which one from GUI 
                this.ChunkRenderer = new StandardRenderer(clientAPI, logger);
+
+               //Listen on bus for commands
+               ClientAPI.Event.RegisterEventBusListener(CommandListener, 1.0, AutomapSystem.AutomapCommandEventKey);
+
                }
 
 
                }
 
 
@@ -104,7 +110,7 @@ namespace Automap
                private void AwakenCartographer(float delayed)
                {
 
                private void AwakenCartographer(float delayed)
                {
 
-               if (Enabled && (ClientAPI.IsGamePaused != false || ClientAPI.IsShuttingDown != true)) {
+               if (CurrentState == RunState.Run && (ClientAPI.IsGamePaused != false || ClientAPI.IsShuttingDown != true)) {
                #if DEBUG
                Logger.VerboseDebug("Cartographer re-trigger from [{0}]", cartographer_thread.ThreadState);
                #endif
                #if DEBUG
                Logger.VerboseDebug("Cartographer re-trigger from [{0}]", cartographer_thread.ThreadState);
                #endif
@@ -116,9 +122,9 @@ namespace Automap
                //Time to (re)write chunk shards
                cartographer_thread.Interrupt( );
                }
                //Time to (re)write chunk shards
                cartographer_thread.Interrupt( );
                }
-               #if DEBUG
-               ClientAPI.TriggerChatMessage($"Automap {updatedChunksTotal} Updates - MAX (N:{chunkTopMetadata.North_mostChunk},S:{chunkTopMetadata.South_mostChunk},E:{chunkTopMetadata.East_mostChunk}, W:{chunkTopMetadata.West_mostChunk} - TOTAL: {chunkTopMetadata.Count})");
-               #endif
+               //#if DEBUG
+               //ClientAPI.TriggerChatMessage($"Automap {updatedChunksTotal} Updates - MAX (N:{chunkTopMetadata.North_mostChunk},S:{chunkTopMetadata.South_mostChunk},E:{chunkTopMetadata.East_mostChunk}, W:{chunkTopMetadata.West_mostChunk} - TOTAL: {chunkTopMetadata.Count})");
+               //#endif
                }
 
                }
                }
 
                }
@@ -203,9 +209,9 @@ namespace Automap
 
                private void UpdateStatus( uint totalUpdates, uint voidChunks,  uint delta)
                {
 
                private void UpdateStatus( uint totalUpdates, uint voidChunks,  uint delta)
                {
-               StatusData updateData = new StatusData(totalUpdates, voidChunks, delta);
+               StatusData updateData = new StatusData(totalUpdates, voidChunks, delta, RunState.Run);
 
 
-               this.ClientAPI.Event.PushEvent(automapStatusEventKey, updateData);
+               this.ClientAPI.Event.PushEvent(AutomapStatusEventKey, updateData);
                }
 
                private void Prefill_POI_Designators( )
                }
 
                private void Prefill_POI_Designators( )
@@ -499,6 +505,7 @@ namespace Automap
                //Parse PNG chunks for METADATA in shard
                using (var fileStream = shardFile.OpenRead( ))
                {
                //Parse PNG chunks for METADATA in shard
                using (var fileStream = shardFile.OpenRead( ))
                {
+               //TODO: Add corrupted PNG Exception handing HERE !
                PngReader pngRead = new PngReader(fileStream );
                pngRead.ReadSkippingAllRows( );
                pngRead.End( );
                PngReader pngRead = new PngReader(fileStream );
                pngRead.ReadSkippingAllRows( );
                pngRead.End( );
@@ -643,12 +650,28 @@ namespace Automap
                }
 
 
                }
 
 
+               private void CommandListener(string eventName, ref EnumHandling handling, IAttribute data)
+               {
+               Logger.VerboseDebug("MsgBus RX: AutomapCommandMsg: {0}", data.ToJsonToken() );
+
+               CommandData cmdData = data as CommandData;
+               //TODO: Support snapshot mode
+               if (CurrentState != cmdData.State) {
+               CurrentState = cmdData.State;           
+               AwakenCartographer(0.0f);
 
 
+               #if DEBUG
+               ClientAPI.TriggerChatMessage($"Automap commanded to: {cmdData.State} ");
+               #endif
+               }
+               
+               }
 
 
                #endregion
 
 
 
 
                #endregion
 
 
+
        }
 
 }
\ No newline at end of file
        }
 
 }
\ No newline at end of file