OSDN Git Service

Snapshots crash fix V0.1.8
authormelchior <melchior@users.osdn.me>
Fri, 14 Jan 2022 21:24:39 +0000 (16:24 -0500)
committermelchior <melchior@users.osdn.me>
Fri, 14 Jan 2022 21:24:39 +0000 (16:24 -0500)
moved snappies to main thread - now also singular use,
fixes multi-click exception

Automap/Automap.csproj
Automap/Designators/DefaultDesignators.cs
Automap/Subsystems/AutomapSystem.cs
Automap/Subsystems/Snapshot.cs
Automap/modinfo.json

index d864e9d..92fad00 100644 (file)
@@ -7,7 +7,7 @@
     <OutputType>Library</OutputType>
     <RootNamespace>Automap</RootNamespace>
     <AssemblyName>Automap</AssemblyName>
-    <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>            
+    <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>            
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <DebugSymbols>true</DebugSymbols>
index 33b499a..652b5c1 100644 (file)
@@ -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);
index abbfc44..2997ec6 100644 (file)
@@ -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)
+               /// <summary>
+               /// Cartographer Thread 'decider'
+               /// </summary>
+               /// <param name="delayed">called delay offset</param>
+               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;
 
index c997333..31ff0c1 100644 (file)
@@ -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;
+               /// <summary>
+               /// Prepares class to take "snapshot" (single use!)
+               /// </summary>
+               /// <param name="path">path to the map dir</param>
+               /// <param name="cols">Input metadata</param>
+               /// <param name="chunkSizeP">size in blocks of std. chunk</param>
+               /// <param name="worldSeedP">World Seed #</param>
+               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);
                }
 
                /// <summary>
                /// takes a snapshot. this should be called from an extra thread.
                /// </summary>
-               /// <param name="path">path to the map dir</param>
-               /// <param name="chunkPath">name of the chunks dir part thing</param>
-               /// <param name="cols"></param>
-               /// <param name="chunkSize"></param>
                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<Dictionary<int, byte[][]>> ReadAllInGroup(IGrouping<int, ColumnMeta> group)
index 5a0c69e..b135bac 100644 (file)
@@ -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