OSDN Git Service

Merge branch 'Split_renderers' into vgd
[automap/automap.git] / Automap / Subsystems / AutomapSystem.cs
index 880fe90..20af359 100644 (file)
@@ -45,7 +45,6 @@ namespace Automap
                private ColumnsMetadata chunkTopMetadata;
                private PointsOfInterest POIs = new PointsOfInterest();
                private EntitiesOfInterest EOIs = new EntitiesOfInterest();
-               private string jsonPreBuilt;
 
                internal Dictionary<int, BlockDesignator> BlockID_Designators { get; private set; }
                internal Dictionary<AssetLocation, EntityDesignator> Entity_Designators { get; private set; }
@@ -64,7 +63,6 @@ namespace Automap
 
                public static string AutomapStatusEventKey = @"AutomapStatus";
                public static string AutomapCommandEventKey = @"AutomapCommand";
-               PersistedConfiguration cachedConfiguration;
 
                public AutomapSystem(ICoreClientAPI clientAPI, ILogger logger, PersistedConfiguration config)
                {
@@ -193,10 +191,18 @@ namespace Automap
                                                if (chunkTopMetadata.Contains(mostActiveCol.Key))
                                                {
                                                        chunkMeta = chunkTopMetadata[mostActiveCol.Key];
+#if DEBUG
+                                                       Logger.VerboseDebug("Loaded chunk {0}", mostActiveCol.Key);
+                                                       //Console.WriteLine($"Load {mostActiveCol.Key}");
+#endif
                                                }
                                                else
                                                {
                                                        chunkMeta = CreateColumnMetadata(mostActiveCol, mapChunk);
+#if DEBUG
+                                                       Logger.VerboseDebug("Created chunk {0}", mostActiveCol.Key);
+                                                       //Console.WriteLine($"Created chunk {mostActiveCol.Key}");
+#endif
                                                }
 
                                                ProcessChunkBlocks(mostActiveCol.Key, mapChunk, ref chunkMeta);
@@ -206,10 +212,9 @@ namespace Automap
 
                                                if (updatedPixels > 0)
                                                {
-
-                                                       #if DEBUG
+#if DEBUG
                                                        Logger.VerboseDebug("Wrote chunk shard: ({0}) - Edits#:{1}, Pixels#:{2}", mostActiveCol.Key, mostActiveCol.Value, updatedPixels);
-                                                       #endif
+#endif
                                                        updatedChunks++;
                                                        chunkTopMetadata.Update(chunkMeta);
                                                        columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
@@ -256,7 +261,7 @@ namespace Automap
                        finally
                        {
                                Logger.VerboseDebug("Thread '{0}' executing finally block.", Thread.CurrentThread.Name);
-                               PersistPointsData( );
+                               PersistPointsData();
                        }
                }
 
@@ -366,7 +371,7 @@ namespace Automap
 
                private ColumnMeta CreateColumnMetadata(KeyValuePair<Vec2i, uint> mostActiveCol, IMapChunk mapChunk)
                {
-                       ColumnMeta data = new ColumnMeta(mostActiveCol.Key.Copy(), (byte) chunkSize);
+                       ColumnMeta data = new ColumnMeta(mostActiveCol.Key.Copy(), ClientAPI, (byte) chunkSize);
                        BlockPos equivBP = new BlockPos(mostActiveCol.Key.X * chunkSize,
                                                                                        mapChunk.YMax,
                                                                                        mostActiveCol.Key.Y * chunkSize);
@@ -385,37 +390,46 @@ namespace Automap
                {
                        var worldmapDir = new DirectoryInfo(path);
 
-                       if (worldmapDir.Exists)
+                       if (!worldmapDir.Exists)
                        {
+#if DEBUG
+                               Logger.VerboseDebug("Could not open world map directory");
+#endif
+                               return;
+                       }
+                       var shardFiles = worldmapDir.GetFiles(chunkFile_filter);
 
-                               var shardFiles = worldmapDir.GetFiles(chunkFile_filter);
+                       if (shardFiles.Length > 0)
+                       {
+#if DEBUG
+                               Logger.VerboseDebug("Metadata reloading from {0} shards", shardFiles.Length);
+#endif
 
-                               if (shardFiles.Length > 0)
+                               foreach (var shardFile in shardFiles)
                                {
-                                       #if DEBUG
-                                       Logger.VerboseDebug("Metadata reloading from {0} shards", shardFiles.Length);
-                                       #endif
-                                        
-                                       foreach (var shardFile in shardFiles) {
 
                                        if (shardFile.Length < 1024) continue;
                                        var result = chunkShardRegex.Match(shardFile.Name);
-                                       if (result.Success) {
-                                       int X_chunk_pos = int.Parse(result.Groups["X"].Value);
-                                       int Z_chunk_pos = int.Parse(result.Groups["Z"].Value);
+                                       if (result.Success)
+                                       {
+                                               int X_chunk_pos = int.Parse(result.Groups["X"].Value);
+                                               int Z_chunk_pos = int.Parse(result.Groups["Z"].Value);
 
-                                       try 
+                                               try
                                                {
-                                               using (var fileStream = shardFile.OpenRead( )) {
-
-                                               PngReader pngRead = new PngReader(fileStream);
-                                               pngRead.ReadSkippingAllRows( );
-                                               pngRead.End( );
-                                               //Parse PNG chunks for METADATA in shard
-                                               PngMetadataChunk metadataFromPng = pngRead.GetChunksList( ).GetById1(PngMetadataChunk.ID) as PngMetadataChunk;
+                                                       using (var fileStream = shardFile.OpenRead())
+                                                       {
 
-                                               chunkTopMetadata.Add(metadataFromPng.ChunkMetadata);
-                                               }
+                                                               PngReader pngRead = new PngReader(fileStream);
+                                                               pngRead.ReadSkippingAllRows();
+                                                               pngRead.End();
+                                                               //Parse PNG chunks for METADATA in shard
+                                                               PngMetadataChunk metadataFromPng = pngRead.GetChunksList().GetById1(PngMetadataChunk.ID) as PngMetadataChunk;
+                                                               var column = metadataFromPng.ChunkMetadata;
+                                                               if (column.PrettyLocation == null)
+                                                                       column = column.Reload(ClientAPI);
+                                                               chunkTopMetadata.Add(column);
+                                                       }
 
                                                }
                                                catch (PngjException someEx)
@@ -423,43 +437,37 @@ namespace Automap
                                                        Logger.Error("PNG Corruption file '{0}' - Reason: {1}", shardFile.Name, someEx);
                                                        continue;
                                                }
-                                               }
-
                                        }
+
                                }
+                       }
 
-                               //POI and EOI raw dump files ~ reload em!
-                               //var poiRawFile = File.
-                               string poiPath = Path.Combine(path, poiFileName);
-                               string eoiPath = Path.Combine(path, eoiFileName);
+                       //POI and EOI raw dump files ~ reload em!
+                       //var poiRawFile = File.
+                       string poiPath = Path.Combine(path, poiFileName);
+                       string eoiPath = Path.Combine(path, eoiFileName);
 
-                               if (File.Exists(poiPath)) {
-                                       using (var poiFile = File.OpenRead(poiPath)) {
+                       if (File.Exists(poiPath))
+                       {
+                               using (var poiFile = File.OpenRead(poiPath))
+                               {
                                        this.POIs = Serializer.Deserialize<PointsOfInterest>(poiFile);
                                        Logger.VerboseDebug("Reloaded {0} POIs from file.", this.POIs.Count);
-                                       }
                                }
+                       }
 
-                               if (File.Exists(eoiPath)) {
-                                       using (var eoiFile = File.OpenRead(eoiPath)) {
+                       if (File.Exists(eoiPath))
+                       {
+                               using (var eoiFile = File.OpenRead(eoiPath))
+                               {
                                        this.EOIs = Serializer.Deserialize<EntitiesOfInterest>(eoiFile);
                                        Logger.VerboseDebug("Reloaded {0} EOIs from file.", this.EOIs.Count);
-                                       }
                                }
-
-                       }
-                       else
-                       {
-                               #if DEBUG
-                               Logger.VerboseDebug("Could not open world map directory");
-                               #endif
                        }
 
-
-
                }
 
-               private PngWriter SetupPngImage(Vec2i coord, ColumnMeta metadata)
+               private PngWriter SetupPngImage(Vec2i coord, ref ColumnMeta metadata)
                {
                        ImageInfo imageInf = new ImageInfo(chunkSize, chunkSize, 8, false);
 
@@ -477,7 +485,7 @@ namespace Automap
                                ChunkMetadata = metadata
                        };
                        pngWriter.GetChunksList().Queue(pngChunkMeta);
-                       pngWriter.CompLevel = 9;// 9 is the maximum compression
+                       pngWriter.CompLevel = 5;// 9 is the maximum compression but thats too high for the small benefit it gives
                        pngWriter.CompressionStrategy = Hjg.Pngcs.Zlib.EDeflateCompressStrategy.Huffman;
 
                        return pngWriter;
@@ -495,7 +503,7 @@ namespace Automap
                        int targetChunkY = mapChunk.YMax / chunkSize;//Surface ... 
                        for (; targetChunkY > 0; targetChunkY--)
                        {
-                       WorldChunk chunkData = ClientAPI.World.BlockAccessor.GetChunk(key.X, targetChunkY, key.Y) as WorldChunk;
+                               WorldChunk chunkData = ClientAPI.World.BlockAccessor.GetChunk(key.X, targetChunkY, key.Y) as WorldChunk;
 
                        if (chunkData == null || chunkData.BlockEntities == null) {
                        #if DEBUG
@@ -508,9 +516,9 @@ namespace Automap
                                /*************** Chunk Entities Scanning *********************/
                                if (chunkData.BlockEntities != null && chunkData.BlockEntities.Length > 0)
                                {
-                               #if DEBUG
+#if DEBUG
                                        Logger.VerboseDebug("Surface@ {0} = BlockEntities: {1}", key, chunkData.BlockEntities.Length);
-                               #endif
+#endif
 
                                        foreach (var blockEnt in chunkData.BlockEntities)
                                        {
@@ -580,9 +588,9 @@ namespace Automap
                        foreach (var loadedEntity in ClientAPI.World.LoadedEntities.ToArray())
                        {
 
-                               #if DEBUG
+#if DEBUG
                                //Logger.VerboseDebug($"ENTITY: ({loadedEntity.Value.Code}) = #{loadedEntity.Value.EntityId} {loadedEntity.Value.State} {loadedEntity.Value.LocalPos}    <<<<<<<<<<<<");
-                               #endif
+#endif
 
                                var dMatch = Entity_Designators.SingleOrDefault(se => se.Key.Equals(loadedEntity.Value.Code));
                                if (dMatch.Value != null)
@@ -621,11 +629,12 @@ namespace Automap
                        {
                                switch (cmdData.State)
                                {
-                                       case CommandType.Run:                                                                                           
+                                       case CommandType.Run:
                                        case CommandType.Stop:
-                                               if (CurrentState != cmdData.State) {
-                                               CurrentState = cmdData.State;
-                                               AwakenCartographer(0.0f);
+                                               if (CurrentState != cmdData.State)
+                                               {
+                                                       CurrentState = cmdData.State;
+                                                       AwakenCartographer(0.0f);
                                                }
                                                break;