OSDN Git Service

1st attempt to fix Protocol buffer corruption issue
[automap/automap.git] / Automap / Subsystems / AutomapSystem.cs
1 using System;
2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
4 using System.IO;
5 using System.Linq;
6 using System.Text;
7 using System.Text.RegularExpressions;
8 using System.Threading;
9
10 using Hjg.Pngcs;
11 using Hjg.Pngcs.Chunks;
12
13 using ProtoBuf;
14
15 using Vintagestory.API.Client;
16 using Vintagestory.API.Common;
17 using Vintagestory.API.Config;
18 using Vintagestory.API.Datastructures;
19 using Vintagestory.API.MathTools;
20 using Vintagestory.Common;
21
22 namespace Automap
23 {
24         public class AutomapSystem
25         {
26                 private Thread cartographer_thread;
27                 private Thread snapshotThread;
28                 private Snapshotter snapshot;
29                 private ICoreClientAPI ClientAPI { get; set; }
30                 private ILogger Logger { get; set; }
31                 private IChunkRenderer ChunkRenderer { get; set; }
32                 private JsonGenerator JsonGenerator { get; set; }
33
34                 internal const string _mapPath = @"Maps";
35                 internal const string _chunkPath = @"Chunks";
36                 private const string _domain = @"automap";
37                 private const string chunkFile_filter = @"*_*.png";
38                 private const string poiFileName = @"poi_binary";
39                 private const string eoiFileName = @"eoi_binary";
40                 private const string pointsTsvFileName = @"points_of_interest.tsv";
41                 private static Regex chunkShardRegex = new Regex(@"(?<X>[\d]+)_(?<Z>[\d]+)\.png", RegexOptions.Singleline);
42
43                 private ConcurrentDictionary<Vec2i, uint> columnCounter = new ConcurrentDictionary<Vec2i, uint>(3, 150);
44                 private ColumnsMetadata chunkTopMetadata;
45                 private PointsOfInterest POIs = new PointsOfInterest();
46                 private EntitiesOfInterest EOIs = new EntitiesOfInterest();
47
48                 internal Dictionary<int, BlockDesignator> BlockID_Designators { get; private set; }
49                 internal Dictionary<AssetLocation, EntityDesignator> Entity_Designators { get; private set; }
50                 internal Dictionary<int, string> RockIdCodes { get; private set; }
51
52                 internal CommandType CurrentState { get; set; }
53                 //Run status, Chunks processed, stats, center of map....
54                 private uint nullChunkCount, nullMapCount, updatedChunksTotal;
55                 private Vec2i startChunkColumn;
56
57                 private readonly int chunkSize;
58                 private string path;
59                 private IAsset staticMap;
60                 private PersistedConfiguration configuration;
61
62
63                 public static string AutomapStatusEventKey = @"AutomapStatus";
64                 public static string AutomapCommandEventKey = @"AutomapCommand";
65
66                 public AutomapSystem(ICoreClientAPI clientAPI, ILogger logger, PersistedConfiguration config)
67                 {
68                         this.ClientAPI = clientAPI;
69                         this.Logger = logger;
70                         chunkSize = ClientAPI.World.BlockAccessor.ChunkSize;
71                         ClientAPI.Event.LevelFinalize += EngageAutomap;
72                         configuration = config;
73
74                         //TODO:Choose which one from GUI 
75                         this.ChunkRenderer = new StandardRenderer(clientAPI, logger);
76
77                         //Listen on bus for commands
78                         ClientAPI.Event.RegisterEventBusListener(CommandListener, 1.0, AutomapSystem.AutomapCommandEventKey);
79
80
81                         if (configuration.Autostart)
82                         {
83                                 CurrentState = CommandType.Run;
84                                 Logger.Debug("Autostart is Enabled.");
85                         }
86
87                 }
88
89
90                 #region Internals
91                 private void EngageAutomap()
92                 {
93                         path = ClientAPI.GetOrCreateDataPath(_mapPath);
94                         path = ClientAPI.GetOrCreateDataPath(Path.Combine(path, "World_" + ClientAPI.World.Seed));//Add name of World too...'ServerApi.WorldManager.CurrentWorldName'
95                         ClientAPI.GetOrCreateDataPath(Path.Combine(path, _chunkPath));
96                                                   
97                         JsonGenerator = new JsonGenerator(ClientAPI, Logger, path);
98
99                         string mapFilename = Path.Combine(path, "automap.html");
100                         StreamWriter outputText = new StreamWriter(File.Open(mapFilename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite));
101
102                         staticMap = ClientAPI.World.AssetManager.Get(new AssetLocation(_domain, "config/automap.html"));
103                         outputText.Write(staticMap.ToText());
104                         outputText.Flush();
105
106                         Prefill_POI_Designators();
107                         startChunkColumn = new Vec2i((ClientAPI.World.Player.Entity.Pos.AsBlockPos.X / chunkSize), (ClientAPI.World.Player.Entity.Pos.AsBlockPos.Z / chunkSize));
108                         chunkTopMetadata = new ColumnsMetadata(startChunkColumn);
109                         Logger.Notification("AUTOMAP Start {0}", startChunkColumn);
110                         Reload_Metadata();
111
112                         ClientAPI.Event.ChunkDirty += ChunkAChanging;
113
114                         cartographer_thread = new Thread(Cartographer)
115                         {
116                                 Name = "Cartographer",
117                                 Priority = ThreadPriority.Lowest,
118                                 IsBackground = true
119                         };
120
121                         snapshot = new Snapshotter(path, chunkTopMetadata, chunkSize,ClientAPI.World.Seed );
122                         snapshotThread = new Thread(Snap)
123                         {
124                                 Name = "Snapshot",
125                                 Priority = ThreadPriority.Lowest,
126                                 IsBackground = true
127                         };
128
129                         ClientAPI.Event.RegisterGameTickListener(AwakenCartographer, 6000);
130                 }
131
132                 private void ChunkAChanging(Vec3i chunkCoord, IWorldChunk chunk, EnumChunkDirtyReason reason)
133                 {
134                         Vec2i topPosition = new Vec2i(chunkCoord.X, chunkCoord.Z);
135
136                         columnCounter.AddOrUpdate(topPosition, 1, (key, colAct) => colAct + 1);
137                 }
138
139                 private void AwakenCartographer(float delayed)
140                 {
141
142                         if (CurrentState == CommandType.Run && (ClientAPI.IsGamePaused != false || ClientAPI.IsShuttingDown != true))
143                         {
144 #if DEBUG
145                                 Logger.VerboseDebug("Cartographer re-trigger from [{0}]", cartographer_thread.ThreadState);
146 #endif
147
148                                 if (cartographer_thread.ThreadState.HasFlag(ThreadState.Unstarted))
149                                 {
150                                         cartographer_thread.Start();
151                                 }
152                                 else if (cartographer_thread.ThreadState.HasFlag(ThreadState.WaitSleepJoin))
153                                 {
154                                         //Time to (re)write chunk shards
155                                         cartographer_thread.Interrupt();
156                                 }
157                                 //#if DEBUG
158                                 //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})");
159                                 //#endif
160                         }
161                         else if (CurrentState == CommandType.Snapshot)
162                         {
163                                 if (snapshotThread.ThreadState.HasFlag(ThreadState.Unstarted))
164                                 {
165                                         snapshotThread.Start();
166                                 } else if (snapshotThread.ThreadState.HasFlag(ThreadState.WaitSleepJoin))
167                                 {
168                                         snapshotThread.Interrupt();
169                                 }
170                         }
171
172                 }
173
174
175                 private void Cartographer()
176                 {
177                         wake:
178                         Logger.VerboseDebug("Cartographer thread awoken");
179
180                         try
181                         {
182                                 uint ejectedItem = 0;
183                                 uint updatedChunks = 0;
184                                 uint updatedPixels = 0;
185
186                                 //-- Should dodge enumerator changing underfoot....at a cost.
187                                 if (!columnCounter.IsEmpty)
188                                 {
189                                         var tempSet = columnCounter.ToArray().OrderByDescending(kvp => kvp.Value);
190                                         UpdateEntityMetadata();
191
192                                         foreach (var mostActiveCol in tempSet)
193                                         {
194                                                 var mapChunk = ClientAPI.World.BlockAccessor.GetMapChunk(mostActiveCol.Key);
195
196                                                 if (mapChunk == null)
197                                                 {
198                                                         Logger.Warning("SKIP CHUNK: ({0}) - Map Chunk NULL!", mostActiveCol.Key);
199                                                         nullMapCount++;
200                                                         columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
201                                                         continue;
202                                                 }
203
204                                                 ColumnMeta chunkMeta;
205                                                 if (chunkTopMetadata.Contains(mostActiveCol.Key))
206                                                 {
207                                                         chunkMeta = chunkTopMetadata[mostActiveCol.Key];
208 #if DEBUG
209                                                         Logger.VerboseDebug("Loaded chunk {0}", mostActiveCol.Key);
210 #endif
211                                                 }
212                                                 else
213                                                 {
214                                                         chunkMeta = CreateColumnMetadata(mostActiveCol, mapChunk);
215 #if DEBUG
216                                                         Logger.VerboseDebug("Created chunk {0}", mostActiveCol.Key);
217 #endif
218                                                 }
219                                                 ProcessChunkBlocks(mostActiveCol.Key, mapChunk, ref chunkMeta);
220
221                                                 PngWriter pngWriter = SetupPngImage(mostActiveCol.Key, ref chunkMeta);
222                                                 ChunkRenderer.GenerateChunkPngShard(mostActiveCol.Key, mapChunk, chunkMeta, pngWriter, out updatedPixels);
223
224                                                 if (updatedPixels > 0)
225                                                 {
226 #if DEBUG
227                                                         Logger.VerboseDebug("Wrote chunk shard: ({0}) - Edits#:{1}, Pixels#:{2}", mostActiveCol.Key, mostActiveCol.Value, updatedPixels);
228 #endif
229                                                         updatedChunks++;
230                                                         chunkTopMetadata.Update(chunkMeta);
231                                                         columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
232                                                 }
233                                                 else
234                                                 {
235                                                         columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
236 #if DEBUG
237                                                         Logger.VerboseDebug("Un-painted chunk: ({0}) ", mostActiveCol.Key);
238 #endif
239                                                 }
240                                         }
241                                         //Cleanup persisted Metadata...
242                                         chunkTopMetadata.ClearMetadata();
243                                 }
244
245                                 UpdateStatus(this.updatedChunksTotal, this.nullChunkCount, updatedChunks);
246
247                                 if (updatedChunks > 0)
248                                 {
249                                         //What about chunk updates themselves; a update bitmap isn't kept...
250                                         updatedChunksTotal += updatedChunks;
251                                         JsonGenerator.GenerateJSONMetadata(chunkTopMetadata, startChunkColumn, POIs, EOIs, RockIdCodes);
252                                         updatedChunks = 0;
253                                 }
254
255                                 //Then sleep until interupted again, and repeat
256 #if DEBUG
257                                 Logger.VerboseDebug("Thread '{0}' about to sleep indefinitely.", Thread.CurrentThread.Name);
258 #endif
259                                 Thread.Sleep(Timeout.Infinite);
260
261                         }
262                         catch (ThreadInterruptedException)
263                         {
264
265 #if DEBUG
266                                 Logger.VerboseDebug("Thread '{0}' interupted [awoken]", Thread.CurrentThread.Name);
267 #endif
268                                 goto wake;
269
270                         }
271                         catch (ThreadAbortException)
272                         {
273 #if DEBUG
274                                 Logger.VerboseDebug("Thread '{0}' aborted.", Thread.CurrentThread.Name);
275 #endif
276                         }
277                         finally
278                         {
279 #if DEBUG
280                                 Logger.VerboseDebug("Thread '{0}' executing finally block.", Thread.CurrentThread.Name);
281 #endif
282                                 PersistPointsData();
283                         }
284                 }
285
286                 private void Snap()
287                 {
288                         snapshotTake:
289 #if DEBUG
290                         Logger.VerboseDebug("Snapshot started");
291 #endif
292                         try
293                         {
294                                 snapshot.Take();
295 #if DEBUG
296                                 Logger.VerboseDebug("Snapshot sleeping");
297 #endif
298                                 CurrentState = CommandType.Run;
299                                 Thread.Sleep(Timeout.Infinite);
300                         }
301                         catch (ThreadInterruptedException)
302                         {
303 #if DEBUG
304                                 Logger.VerboseDebug("Snapshot intertupted");
305 #endif
306                                 goto snapshotTake;
307                         }
308                 }
309
310                 private void UpdateStatus(uint totalUpdates, uint voidChunks, uint delta)
311                 {
312                         StatusData updateData = new StatusData(totalUpdates, voidChunks, delta, CommandType.Run);
313
314                         this.ClientAPI.Event.PushEvent(AutomapStatusEventKey, updateData);
315                 }
316
317                 private void Prefill_POI_Designators()
318                 {
319
320                         this.BlockID_Designators = new Dictionary<int, BlockDesignator>();
321                         this.Entity_Designators = new Dictionary<AssetLocation, EntityDesignator>();
322                         this.RockIdCodes = Helpers.ArbitrarytBlockIdHunter(ClientAPI, new AssetLocation(GlobalConstants.DefaultDomain, "rock-"), EnumBlockMaterial.Stone);
323
324                         //Add special marker types for BlockID's of "Interest", overwrite colour, and method
325
326                         Reload_POI_Designators();
327                 }
328
329                 private void Reload_POI_Designators()
330                 {
331                         Logger.VerboseDebug("Connecting {0} Configured Block-Designators", configuration.BlockDesignators.Count);
332                         foreach (var designator in configuration.BlockDesignators)
333                         {
334                                 var blockIDs = Helpers.ArbitrarytBlockIdHunter(ClientAPI, designator.Pattern, designator.Material);
335                                 if (blockIDs.Count > 0) { Logger.VerboseDebug("Designator {0} has {1} associated blockIDs", designator.ToString(), blockIDs.Count); }
336                                 foreach (var entry in blockIDs)
337                                 {
338                                         BlockID_Designators.Add(entry.Key, designator);
339                                 }
340                         }
341                         this.ChunkRenderer.BlockID_Designators = BlockID_Designators;
342
343
344                         Logger.VerboseDebug("Connecting {0} Configured Entity-Designators", configuration.EntityDesignators.Count);
345                         foreach (var designator in configuration.EntityDesignators)
346                         {
347                                 //Get Variants first, from EntityTypes...better be populated!
348                                 var matched = ClientAPI.World.EntityTypes.FindAll(entp => entp.Code.BeginsWith(designator.Pattern.Domain, designator.Pattern.Path));
349
350                                 foreach (var match in matched)
351                                 {
352                                         Logger.VerboseDebug("Linked Entity: {0} Designator: {1}", match.Code, designator);
353                                         this.Entity_Designators.Add(match.Code, designator);
354                                 }
355
356                                 //EntityProperties props = ClientAPI.World.GetEntityType(designator.Pattern);
357                         }
358
359
360                 }
361
362
363
364                 /// <summary>
365                 /// Store Points/Entity of Interest
366                 /// </summary>
367                 private void PersistPointsData()
368                 {
369                         //POI and EOI raw dump files ~ WRITE em!
370                         //var poiRawFile = File.
371                         string poiPath = Path.Combine(path, poiFileName);
372                         string eoiPath = Path.Combine(path, eoiFileName);
373
374                         if (this.POIs.Count > 0)
375                         {
376                                 using (var poiFile = File.OpenWrite(poiPath))
377                                 {
378                                         Serializer.Serialize<PointsOfInterest>(poiFile, this.POIs);
379                                 }
380                         }
381
382                         if (this.EOIs.Count > 0)
383                         {
384                                 using (var eoiFile = File.OpenWrite(eoiPath))
385                                 {
386                                         Serializer.Serialize<EntitiesOfInterest>(eoiFile, this.EOIs);
387                                 }
388                         }
389
390                         //Create Easy to Parse TSV file for tool/human use....
391                         string pointsTsvPath = Path.Combine(path, pointsTsvFileName);
392
393                         using (var tsvWriter = new StreamWriter(pointsTsvPath, false, Encoding.UTF8))
394                         {
395                                 tsvWriter.WriteLine("Name\tDescription\tLocation\tTime\tDestination");
396                                 foreach (var point in this.POIs)
397                                 {
398                                         tsvWriter.Write(point.Name + "\t");
399                                         var notes = point.Notes
400                                                 .Replace("\n", "\\n")
401                                                 .Replace("\t", "\\t")
402                                                 .Replace("\\", "\\\\");
403                                         tsvWriter.Write(notes + "\t");
404                                         tsvWriter.Write(point.Location.PrettyCoords(ClientAPI) + "\t");
405                                         tsvWriter.Write(point.Timestamp.ToString("u") + "\t");
406                                         tsvWriter.Write((point.Destination != null ? point.Destination.PrettyCoords(ClientAPI) : "---") +"\t");
407                                         tsvWriter.WriteLine();
408                                 }
409                                 foreach (var entity in this.EOIs)
410                                 {
411                                         tsvWriter.Write(entity.Name + "\t");
412                                         var notes = entity.Notes
413                                                 .Replace("\n", "\\n")
414                                                 .Replace("\t", "\\t")
415                                                 .Replace("\\", "\\\\");
416                                         tsvWriter.Write(notes + "\t");
417                                         tsvWriter.Write(entity.Location.PrettyCoords(ClientAPI) + "\t");
418                                         tsvWriter.Write(entity.Timestamp.ToString("u") + "\t");
419                                         tsvWriter.Write("n/a\t");
420                                         tsvWriter.WriteLine();
421                                 }
422                                 tsvWriter.WriteLine();
423                                 tsvWriter.Flush();
424                         }
425
426                 }
427
428                 private ColumnMeta CreateColumnMetadata(KeyValuePair<Vec2i, uint> mostActiveCol, IMapChunk mapChunk)
429                 {
430                         ColumnMeta data = new ColumnMeta(mostActiveCol.Key.Copy(), ClientAPI, (byte) chunkSize);
431                         BlockPos equivBP = new BlockPos(mostActiveCol.Key.X * chunkSize,
432                                                                                         mapChunk.YMax,
433                                                                                         mostActiveCol.Key.Y * chunkSize);
434
435                         var climate = ClientAPI.World.BlockAccessor.GetClimateAt(equivBP);
436                         data.UpdateFieldsFrom(climate, mapChunk, TimeSpan.FromHours(ClientAPI.World.Calendar.TotalHours));
437
438                         return data;
439                 }
440
441                 /// <summary>
442                 /// Reload chunk bounds from chunk shards
443                 /// </summary>
444                 /// <returns>The metadata.</returns>
445                 private void Reload_Metadata()
446                 {
447                         var shardsDir = new DirectoryInfo( Path.Combine(path, _chunkPath) );
448
449                         if (!shardsDir.Exists)
450                         {
451                                 #if DEBUG
452                                 Logger.VerboseDebug("Could not open world map (shards) directory");
453                                 #endif
454                                 return;
455                         }
456                         var shardFiles = shardsDir.GetFiles(chunkFile_filter);
457
458                         if (shardFiles.Length > 0)
459                         {
460                                 #if DEBUG
461                                 Logger.VerboseDebug("Metadata reloading from {0} shards", shardFiles.Length);
462                                 #endif
463
464                                 foreach (var shardFile in shardFiles)
465                                 {
466
467                                         if (shardFile.Length < 1024) continue;
468                                         var result = chunkShardRegex.Match(shardFile.Name);
469                                         if (!result.Success) continue;
470
471                                         int X_chunk_pos = int.Parse(result.Groups["X"].Value);
472                                         int Z_chunk_pos = int.Parse(result.Groups["Z"].Value);
473
474                                         try
475                                         {
476                                                 using (var fileStream = shardFile.OpenRead())
477                                                 {
478
479                                                         PngReader pngRead = new PngReader(fileStream);
480                                                         pngRead.ReadSkippingAllRows();
481                                                         pngRead.End();
482                                                         //Parse PNG chunks for METADATA in shard
483                                                         PngMetadataChunk metadataFromPng = pngRead.GetChunksList().GetById1(PngMetadataChunk.ID) as PngMetadataChunk;
484                                                         var column = metadataFromPng.ChunkMetadata;
485                                                         if (column.PrettyLocation == null)
486                                                                 column = column.Reload(ClientAPI);
487                                                         chunkTopMetadata.Add(column);
488                                                 }
489
490                                         }
491                                         catch (PngjException someEx)
492                                         {
493                                                 Logger.Error("PNG Corruption file '{0}' - Reason: {1}", shardFile.Name, someEx);
494                                                 continue;
495                                         }
496                                         catch (ProtoException protoEx) 
497                                         {
498                                                 Logger.Error("ProtoBuf invalid! file:'{0}' - Reason: {1}", shardFile.Name, protoEx);
499                                                 continue;
500                                         }
501                                 }
502                         }
503
504                         //POI and EOI raw dump files ~ reload em!
505                         //var poiRawFile = File.
506                         string poiPath = Path.Combine(path, poiFileName);
507                         string eoiPath = Path.Combine(path, eoiFileName);
508
509                         if (File.Exists(poiPath))
510                         {
511                                 using (var poiFile = File.OpenRead(poiPath))
512                                 {
513                                         this.POIs = Serializer.Deserialize<PointsOfInterest>(poiFile);
514                                         Logger.VerboseDebug("Reloaded {0} POIs from file.", this.POIs.Count);
515                                 }
516                         }
517
518                         if (File.Exists(eoiPath))
519                         {
520                                 using (var eoiFile = File.OpenRead(eoiPath))
521                                 {
522                                         this.EOIs = Serializer.Deserialize<EntitiesOfInterest>(eoiFile);
523                                         Logger.VerboseDebug("Reloaded {0} EOIs from file.", this.EOIs.Count);
524                                 }
525                         }
526
527                 }
528
529                 private PngWriter SetupPngImage(Vec2i coord, ref ColumnMeta metadata)
530                 {
531                         ImageInfo imageInf = new ImageInfo(chunkSize, chunkSize, 8, false);
532
533                         string filename = $"{coord.X}_{coord.Y}.png";
534                         filename = Path.Combine(path, _chunkPath ,filename);
535
536                         PngWriter pngWriter = FileHelper.CreatePngWriter(filename, imageInf, true);
537                         PngMetadata meta = pngWriter.GetMetadata();
538                         meta.SetTimeNow();
539                         meta.SetText("Chunk_X", coord.X.ToString("D"));
540                         meta.SetText("Chunk_Y", coord.Y.ToString("D"));
541                         //Setup specialized meta-data PNG chunks here...
542                         PngMetadataChunk pngChunkMeta = new PngMetadataChunk(pngWriter.ImgInfo)
543                         {
544                                 ChunkMetadata = metadata
545                         };
546                         pngWriter.GetChunksList().Queue(pngChunkMeta);
547                         pngWriter.CompLevel = 5;// 9 is the maximum compression but thats too high for the small benefit it gives
548                         pngWriter.CompressionStrategy = Hjg.Pngcs.Zlib.EDeflateCompressStrategy.Huffman;
549
550                         return pngWriter;
551                 }
552
553                 /// <summary>
554                 /// Does the heavy lifting of Scanning columns of chunks - scans for BlockEntity, creates Heightmap and stats...
555                 /// </summary>
556                 /// <param name="key">Chunk Coordinate</param>
557                 /// <param name="mapChunk">Map chunk.</param>
558                 /// <param name="chunkMeta">Chunk metadata</param>
559                 private void ProcessChunkBlocks(Vec2i key, IMapChunk mapChunk, ref ColumnMeta chunkMeta)
560                 {
561
562                         int targetChunkY = mapChunk.YMax / chunkSize;//Surface ... 
563                         for (; targetChunkY > 0; targetChunkY--)
564                         {
565                                 WorldChunk chunkData = ClientAPI.World.BlockAccessor.GetChunk(key.X, targetChunkY, key.Y) as WorldChunk;
566
567                                 if (chunkData == null || chunkData.BlockEntities == null)
568                                 {
569 #if DEBUG
570                                         Logger.VerboseDebug("Chunk null or empty X{0} Y{1} Z{2}", key.X, targetChunkY, key.Y);
571 #endif
572                                         nullChunkCount++;
573                                         continue;
574                                 }
575
576                                 /*************** Chunk Entities Scanning *********************/
577                                 if (chunkData.BlockEntities != null && chunkData.BlockEntities.Count > 0)
578                                 {
579 #if DEBUG
580                                         Logger.VerboseDebug("Surface@ {0} = BlockEntities: {1}", key, chunkData.BlockEntities.Count);
581 #endif
582
583                                         foreach (var blockEnt in chunkData.BlockEntities)
584                                         {
585                                                 if (blockEnt.Value != null && blockEnt.Value.Block != null && BlockID_Designators.ContainsKey(blockEnt.Value.Block.BlockId))
586                                                 {
587                                                         var designator = BlockID_Designators[blockEnt.Value.Block.BlockId];
588                                                         designator.SpecialAction(ClientAPI, POIs, blockEnt.Value.Pos.Copy(), blockEnt.Value.Block);
589                                                 }
590                                         }
591                                 }
592                                 /********************* Chunk/Column BLOCKs scanning ****************/
593                                 //Heightmap, Stats, block tally
594                                 chunkData.Unpack();
595
596                                 //int X_index, Y_index, Z_index;
597
598                                 //Ensure ChunkData Metadata fields arn't null...due to being tossed out
599                                 //if (chunkMeta.HeightMap == null) { chunkMeta.HeightMap = new ushort[chunkSize, chunkSize]; }
600                                 //if (chunkMeta.RockRatio == null) { chunkMeta.RockRatio = new Dictionary<int, uint>(10); }
601
602                                 //for (Y_index = 0; Y_index < chunkSize - 1; Y_index++)
603                                 //{
604                                 //      for (Z_index = 0; Z_index < chunkSize - 1; Z_index++)
605                                 //      {
606                                 //              for (X_index = 0; X_index < chunkSize - 1; X_index++)
607                                 //              {
608                                 //                      /* Encode packed indicie
609                                 //                      (y * chunksize + z) * chunksize + x
610                                 //                      */
611                                 //                      var indicie = Helpers.ChunkBlockIndicie16(X_index, Y_index, Z_index);
612                                 //                      int aBlockId = chunkData.Blocks[indicie];
613
614                                 //                      if (aBlockId == 0)
615                                 //                      {//Air
616                                 //                              chunkMeta.AirBlocks++;
617                                 //                              continue;
618                                 //                      }
619
620                                 //                      if (RockIdCodes.ContainsKey(aBlockId))
621                                 //                      {
622                                 //                              if (chunkMeta.RockRatio.ContainsKey(aBlockId))
623                                 //                                      chunkMeta.RockRatio[aBlockId]++;
624                                 //                              else
625                                 //                                      chunkMeta.RockRatio.Add(aBlockId, 1);
626                                 //                      }
627
628                                 //                      chunkMeta.NonAirBlocks++;
629
630                                 //                      //Heightmap 
631                                 //                      //if (chunkMeta.HeightMap[X_index, Z_index] == 0)
632                                 //                      //{
633
634                                 //                      //      chunkMeta.HeightMap[X_index, Z_index] = (ushort) (Y_index + (targetChunkY * chunkSize));
635                                 //                      //}
636                                 //              }
637                                 //      }
638
639                                 //}
640
641                         }
642                 }
643
644                 private void UpdateEntityMetadata()
645                 {
646                         Logger.Debug("Presently {0} Entities", ClientAPI.World.LoadedEntities.Count);
647                         //Mabey scan only for 'new' entities by tracking ID in set?
648                         foreach (var loadedEntity in ClientAPI.World.LoadedEntities.ToArray())
649                         {
650
651 #if DEBUG
652                                 //Logger.VerboseDebug($"ENTITY: ({loadedEntity.Value.Code}) = #{loadedEntity.Value.EntityId} {loadedEntity.Value.State} {loadedEntity.Value.LocalPos}    <<<<<<<<<<<<");
653 #endif
654
655                                 var dMatch = Entity_Designators.SingleOrDefault(se => se.Key.Equals(loadedEntity.Value.Code));
656                                 if (dMatch.Value != null)
657                                 {
658                                         dMatch.Value.SpecialAction(ClientAPI, this.EOIs, loadedEntity.Value.Pos.AsBlockPos.Copy(), loadedEntity.Value);
659                                 }
660
661                         }
662
663
664                 }
665
666                 private void AddNote(string notation)
667                 {
668                         var playerNodePoi = new PointOfInterest()
669                         {
670                                 Name = "Note",
671                                 Location = ClientAPI.World.Player.Entity.Pos.AsBlockPos.Copy(),
672                                 Notes = notation,
673                                 Timestamp = DateTime.UtcNow,
674                         };
675
676                         this.POIs.AddReplace(playerNodePoi);
677                 }
678
679
680
681                 private void CommandListener(string eventName, ref EnumHandling handling, IAttribute data)
682                 {
683                         //Logger.VerboseDebug("MsgBus RX: AutomapCommandMsg: {0}", data.ToJsonToken());
684
685                         CommandData cmdData = data as CommandData;
686
687                         switch (cmdData.State)
688                         {
689                                 case CommandType.Run:
690                                 case CommandType.Stop:
691                                 case CommandType.Snapshot:
692                                         if (CurrentState != cmdData.State)
693                                         {
694                                                 CurrentState = cmdData.State;
695                                                 AwakenCartographer(0.0f);
696                                         }
697                                         break;
698
699                                 case CommandType.Notation:
700                                         //Add to POI list where player location
701                                         AddNote(cmdData.Notation);
702                                         break;
703                         }
704 #if DEBUG
705                         ClientAPI.TriggerChatMessage($"Automap commanded to: {cmdData.State} ");
706 #endif
707                 }
708                 #endregion
709
710         }
711
712 }