From: melchior Date: Fri, 14 Jan 2022 21:24:39 +0000 (-0500) Subject: Snapshots crash fix X-Git-Tag: V0.1.8^0 X-Git-Url: http://git.osdn.net/view?p=automap%2Fautomap.git;a=commitdiff_plain;h=6f21853a9ca96dc7cd115f471e296e6f8d3ab655 Snapshots crash fix moved snappies to main thread - now also singular use, fixes multi-click exception --- diff --git a/Automap/Automap.csproj b/Automap/Automap.csproj index d864e9d..92fad00 100644 --- a/Automap/Automap.csproj +++ b/Automap/Automap.csproj @@ -7,7 +7,7 @@ Library Automap Automap - v4.5.2 + v4.6.1 true diff --git a/Automap/Designators/DefaultDesignators.cs b/Automap/Designators/DefaultDesignators.cs index 33b499a..652b5c1 100644 --- a/Automap/Designators/DefaultDesignators.cs +++ b/Automap/Designators/DefaultDesignators.cs @@ -239,9 +239,9 @@ namespace Automap internal static void DecodeTranslocator(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block) { -#if DEBUG + #if DEBUG clientAPI.Logger.VerboseDebug("TRANSLOCATOR Designator Invoked!"); -#endif + #endif //Where to? and from! BlockEntityStaticTranslocator te = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntityStaticTranslocator; @@ -249,9 +249,9 @@ namespace Automap if (te != null) { //FIXME: Delayed rescan ? StringBuilder textTarget = new StringBuilder( ); - //translocatorEntity.GetBlockInfo(clientAPI.World.Player, textTarget); + textTarget.Append(te.FullyRepaired ? "Functional, " : "Broken, "); - textTarget.Append(te.Activated ? "Online, " : "Offline, "); + textTarget.Append(te.Activated ? "Online, " : "Offline, ");//Property hardcoded TRUE ?! textTarget.Append(" Target: [ "); textTarget.Append(te.TargetLocation != null ? "Set ]" : "Invalid ]");//Or ABS coords? textTarget.AppendFormat(", Range ({0} ~ {1})", te.MinTeleporterRangeInBlocks, te.MaxTeleporterRangeInBlocks); diff --git a/Automap/Subsystems/AutomapSystem.cs b/Automap/Subsystems/AutomapSystem.cs index abbfc44..2997ec6 100644 --- a/Automap/Subsystems/AutomapSystem.cs +++ b/Automap/Subsystems/AutomapSystem.cs @@ -24,7 +24,7 @@ namespace Automap public class AutomapSystem { private Thread cartographer_thread; - private Thread snapshotThread; + private Snapshotter snapshot; private ICoreClientAPI ClientAPI { get; set; } private ILogger Logger { get; set; } @@ -84,7 +84,7 @@ namespace Automap if (configuration.Autostart) { CurrentState = CommandType.Run; - Logger.Debug("Autostart is Enabled."); + Logger.Notification("Autostart is Enabled."); } } @@ -121,15 +121,7 @@ namespace Automap IsBackground = true }; - snapshot = new Snapshotter(path, chunkTopMetadata, chunkSize,ClientAPI.World.Seed ); - snapshotThread = new Thread(Snap) - { - Name = "Snapshot", - Priority = ThreadPriority.Lowest, - IsBackground = true - }; - - ClientAPI.Event.RegisterGameTickListener(AwakenCartographer, 6000); + ClientAPI.Event.RegisterGameTickListener(ThreadDecider, 6000); } private void ChunkAChanging(Vec3i chunkCoord, IWorldChunk chunk, EnumChunkDirtyReason reason) @@ -144,14 +136,18 @@ namespace Automap } - private void AwakenCartographer(float delayed) + /// + /// Cartographer Thread 'decider' + /// + /// called delay offset + private void ThreadDecider(float delayed) { if (CurrentState == CommandType.Run && (ClientAPI.IsGamePaused != false || ClientAPI.IsShuttingDown != true)) { -#if DEBUG - Logger.VerboseDebug("Cartographer re-trigger from [{0}]", cartographer_thread.ThreadState); -#endif + #if DEBUG + Logger.VerboseDebug("ThreadDecider re-trigger from [{0}]", cartographer_thread.ThreadState); + #endif if (cartographer_thread.ThreadState.HasFlag(ThreadState.Unstarted)) { @@ -168,15 +164,22 @@ namespace Automap } else if (CurrentState == CommandType.Snapshot) { - if (snapshotThread.ThreadState.HasFlag(ThreadState.Unstarted)) - { - snapshotThread.Start(); - } else if (snapshotThread.ThreadState.HasFlag(ThreadState.WaitSleepJoin)) - { - snapshotThread.Interrupt(); + //Prepare for taking a snopshot + if (snapshot == null) { + snapshot = new Snapshotter(path, chunkTopMetadata, chunkSize, ClientAPI.World.Seed); + #if DEBUG + Logger.VerboseDebug("Starting new Snapshot: {0} Wx{1} Hx{2}", snapshot.fileName, snapshot.Width, snapshot.Height); + #endif + snapshot.Take( ); + } + else if (snapshot != null && snapshot.Finished) { + #if DEBUG + Logger.VerboseDebug("COMPLETED Snapshot: {0} Wx{1} Hx{2}, taking {3}", snapshot.fileName, snapshot.Width, snapshot.Height, snapshot.Timer.Elapsed); + #endif + snapshot = null; + CurrentState = CommandType.Run; } } - } @@ -468,6 +471,7 @@ namespace Automap mdWriter.WriteLine("WorldSeed {0}", ClientAPI.World.Seed); mdWriter.WriteLine("PlayerChunkCoords {0:D} {1:D}", startChunkColumn.X, startChunkColumn.Y); mdWriter.WriteLine("DefaultSpawnPos {0:D} {1:D} {2:D}", ClientAPI.World.DefaultSpawnPosition.AsBlockPos.X,ClientAPI.World.DefaultSpawnPosition.AsBlockPos.Y,ClientAPI.World.DefaultSpawnPosition.AsBlockPos.Z); + //mdWriter.WriteLine("CurrentPlayerSpawn", ClientAPI.World.Player.WorldData.EntityPlayer.); mdWriter.WriteLine("ChunkSize {0}", chunkSize); mdWriter.WriteLine("SeaLevel {0:D}", ClientAPI.World.SeaLevel); mdWriter.WriteLine("WorldSize {0:D} {1:D} {2:D}", ClientAPI.World.BulkBlockAccessor.MapSizeX, ClientAPI.World.BulkBlockAccessor.MapSizeY,ClientAPI.World.BulkBlockAccessor.MapSizeZ); @@ -761,7 +765,7 @@ namespace Automap if (CurrentState != cmdData.State) { CurrentState = cmdData.State; - AwakenCartographer(0.0f); + ThreadDecider(0.0f); } break; diff --git a/Automap/Subsystems/Snapshot.cs b/Automap/Subsystems/Snapshot.cs index c997333..31ff0c1 100644 --- a/Automap/Subsystems/Snapshot.cs +++ b/Automap/Subsystems/Snapshot.cs @@ -13,37 +13,50 @@ namespace Automap { public class Snapshotter { - public readonly int chunkSize; private const string customTimeFormat = @"yyyy.MM.dd.HH.mm.ssZzz"; - public string fileName; - public string chunkPath; - public ColumnsMetadata cols; - //TODO: Refactor - so Edges are set at construction time, as ColumnsMetadata is async updating in real time! - public int NorthEdge => cols.North_mostChunk; - public int WestEdge => cols.West_mostChunk; - public int Width => (cols.East_mostChunk - WestEdge + 1); - public int Height => (cols.South_mostChunk - NorthEdge + 1); - + public readonly int chunkSize; + public readonly int worldSeed; + public readonly string fileName; + public readonly string chunkPath; + public readonly int NorthEdge; + public readonly int WestEdge; + public readonly int Width; + public readonly int Height; + + public ColumnsMetadata colMetadata; + public bool Imaging { get; private set; } + public bool Finished { get; private set; } + public Stopwatch Timer { get; private set;} - public Snapshotter(string path, ColumnsMetadata cols, int chunkSize, int worldSeed) - { - this.fileName = Path.Combine(path, $"snapshot_{worldSeed}_{DateTime.Now.ToString(customTimeFormat)}.png"); - this.chunkPath = Path.Combine(path, AutomapSystem._chunkPath); - this.cols = cols; - this.chunkSize = chunkSize; + /// + /// Prepares class to take "snapshot" (single use!) + /// + /// path to the map dir + /// Input metadata + /// size in blocks of std. chunk + /// World Seed # + public Snapshotter(string path, ColumnsMetadata cols, int chunkSizeP, int worldSeedP) + { + colMetadata = cols; + chunkSize = chunkSizeP; + worldSeed = worldSeedP; + NorthEdge = cols.North_mostChunk; + WestEdge = cols.West_mostChunk; + Width = (cols.East_mostChunk - WestEdge + 1); + Height = (cols.South_mostChunk - NorthEdge + 1); + fileName = Path.Combine(path, $"snapshot_{worldSeed}_{DateTime.Now.ToString(customTimeFormat)}.png"); + chunkPath = Path.Combine(path, AutomapSystem._chunkPath); } /// /// takes a snapshot. this should be called from an extra thread. /// - /// path to the map dir - /// name of the chunks dir part thing - /// - /// public async void Take() { - var t = new Stopwatch(); - t.Start(); + if (Imaging || Finished) return;//Can't take annother photo...this one is started or complete + Imaging = true; + Timer = new Stopwatch(); + Timer.Start(); Debug.WriteLine("snapshot started"); @@ -73,7 +86,7 @@ namespace Automap var orderedList = - from ch in cols + from ch in colMetadata group ch by ch.Location.Y into g orderby g.Key select g; @@ -111,12 +124,15 @@ namespace Automap try { snapWriter.End(); + Timer.Stop( ); + Imaging = false; + Finished = true; } catch (Exception) { Debug.WriteLine("Snapshot exception!"); } - Debug.WriteLine($"snapshot finished in {t.ElapsedMilliseconds}"); + Debug.WriteLine($"snapshot finished in {Timer.ElapsedMilliseconds}"); } private async Task> ReadAllInGroup(IGrouping group) diff --git a/Automap/modinfo.json b/Automap/modinfo.json index 5a0c69e..b135bac 100644 --- a/Automap/modinfo.json +++ b/Automap/modinfo.json @@ -4,10 +4,10 @@ "description" : "Automap; Generates a static HTML5 map dynamically, with P.O.I. Tracking & more.", "authors": ["Melchior","VeryGoodDog"], "contributors":["VeryGoodDog"], - "version": "0.1.7", + "version": "0.1.8", "side":"Client", "dependencies": { - "game": "1.14.10" + "game": "1.16.0" }, "website": "http://automap.osdn.io/" } \ No newline at end of file