From c7503c541ee0a083de1e3ffd7da67054e23b70df Mon Sep 17 00:00:00 2001 From: melchior Date: Sun, 9 Feb 2020 21:09:14 -0500 Subject: [PATCH] W.I.P. VI: Automap Commanded by local event bus --- Automap/Automap.csproj | 2 + Automap/Data/CommandData.cs | 67 ++++++++++++++++++++++++++++++++++ Automap/Data/RunState.cs | 11 ++++++ Automap/Data/StatusData.cs | 13 ++++--- Automap/Subsystems/AutomapGUIDialog.cs | 25 +++++++------ Automap/Subsystems/AutomapSystem.cs | 39 ++++++++++++++++---- 6 files changed, 132 insertions(+), 25 deletions(-) create mode 100644 Automap/Data/CommandData.cs create mode 100644 Automap/Data/RunState.cs diff --git a/Automap/Automap.csproj b/Automap/Automap.csproj index 985f16c..ea1c568 100644 --- a/Automap/Automap.csproj +++ b/Automap/Automap.csproj @@ -87,6 +87,8 @@ + + diff --git a/Automap/Data/CommandData.cs b/Automap/Data/CommandData.cs new file mode 100644 index 0000000..ccce2df --- /dev/null +++ b/Automap/Data/CommandData.cs @@ -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 index 0000000..55311b3 --- /dev/null +++ b/Automap/Data/RunState.cs @@ -0,0 +1,11 @@ +using System; +namespace Automap +{ + public enum RunState : byte + { + Stop = 0x00, + Run = 0x01, + Snapshot = 0x02, + } +} + diff --git a/Automap/Data/StatusData.cs b/Automap/Data/StatusData.cs index 24f7344..a877ef4 100644 --- a/Automap/Data/StatusData.cs +++ b/Automap/Data/StatusData.cs @@ -10,22 +10,23 @@ namespace Automap 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; + CurrentState = currently; } - - public void FromBytes(BinaryReader stream) { TotalUpdates = stream.ReadUInt32( ); VoidChunks = stream.ReadUInt32( ); Delta = stream.ReadUInt32( ); + CurrentState = ( RunState )stream.ReadByte( ); } public int GetAttributeId( ) @@ -43,6 +44,7 @@ namespace Automap stream.Write(TotalUpdates); stream.Write(VoidChunks); stream.Write(Delta); + stream.Write((byte)CurrentState); } public string ToJsonToken( ) @@ -56,7 +58,8 @@ namespace Automap 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; diff --git a/Automap/Subsystems/AutomapGUIDialog.cs b/Automap/Subsystems/AutomapGUIDialog.cs index 007e6d8..1692997 100644 --- a/Automap/Subsystems/AutomapGUIDialog.cs +++ b/Automap/Subsystems/AutomapGUIDialog.cs @@ -13,7 +13,6 @@ namespace Automap private const string _statusTextKey = @"txtStatus"; private ILogger Logger; - private AutomapSystem _automapSystem; private long dashTickHandle; @@ -25,13 +24,15 @@ namespace Automap } private uint totalShards, voidShards, changesThisTick; + private RunState lastState; + public AutomapGUIDialog(ICoreClientAPI capi,AutomapSystem ams) : base(capi) { - _automapSystem = ams; + 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); - ElementBounds toggleBounds = textBounds.CopyOffsetedSibling(3, 26, 5, 2); + ElementBounds toggleBounds = textBounds.CopyOffsetedSibling(3, 64, 5, 2); 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; @@ -83,13 +84,12 @@ namespace Automap /// Toggle Automap from/to RUN state /// /// The toggle. - /// T1. + /// Run. internal void RunToggle(bool toggle) { - _automapSystem.Enabled = toggle; 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); @@ -98,24 +98,25 @@ namespace Automap 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) { - Logger.VerboseDebug("MsgBus - AutomapStatusMsg"); + Logger.VerboseDebug("MsgBus RX: AutomapStatusMsg"); 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); - statusText.SetNewText($"Running: {this._automapSystem.Enabled}, Total: {totalShards}, Delta: {changesThisTick} Nulls: {voidShards} "); + statusText.SetNewText($"State: {lastState}, Total: {totalShards}, Delta: {changesThisTick} Nulls: {voidShards} "); } diff --git a/Automap/Subsystems/AutomapSystem.cs b/Automap/Subsystems/AutomapSystem.cs index 2c6b0b3..104680c 100644 --- a/Automap/Subsystems/AutomapSystem.cs +++ b/Automap/Subsystems/AutomapSystem.cs @@ -17,6 +17,7 @@ using Vintagestory.API.Client; using Vintagestory.API.Common; using Vintagestory.API.Common.Entities; using Vintagestory.API.Config; +using Vintagestory.API.Datastructures; using Vintagestory.API.MathTools; using Vintagestory.Common; @@ -44,7 +45,7 @@ namespace Automap internal Dictionary Entity_Designators { get; private set; } internal Dictionary 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; @@ -53,7 +54,8 @@ namespace Automap 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) @@ -65,6 +67,10 @@ namespace Automap //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) { - 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 @@ -116,9 +122,9 @@ namespace Automap //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) { - 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( ) @@ -499,6 +505,7 @@ namespace Automap //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( ); @@ -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 + } } \ No newline at end of file -- 2.11.0