OSDN Git Service

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