OSDN Git Service

W.I.P. P.O.C. Shard Post Processor Skeliton
[automap/automap.git] / ShardProcessor / Program.cs
index e9e5b6c..0d2f1e1 100644 (file)
 using System;
+using System.IO;
+using System.Text.RegularExpressions;
+
+using Automap;
+
+using Hjg.Pngcs;
+using Hjg.Pngcs.Chunks;
+
+using ProtoBuf;
 
 namespace ShardProcessor
-{
+{      
        class MainClass
        {
-               /*
+               //private ILogger Logger { get; set; }
+               const string chunkFile_filter = @"*_*.png";
+               static Regex chunkShardRegex = new Regex(@"(?<X>[\d]+)_(?<Z>[\d]+)\.png", RegexOptions.Singleline);
+               static string mapPath;
+               internal const string _chunkPath = @"Chunks";
+
+               /* TODO:
                        -Process existing PNGs: Report/Dump contents of Chunk Metadata, as per current version
+                       -Grayscale Heightmap extraction from P.Buf heightmap from shards
                        -Extract contents of game's SQLLite map DB, INTO Automap type shards...
                        -Other stuff? chunk fixing / validation?
                */
                public static void Main(string[ ] args)
                {
                Console.WriteLine("AUTOMAP Offline Shard processor v0.1");
+               //Called once - thus it can only be in a static constructor.
+               PngChunk.FactoryRegister(PngMetadataChunk.ID, typeof(PngMetadataChunk));
 
+               ArgsDecoder(args);
 
                }
+
+               private static void ArgsDecoder(string[ ] args)
+               {
+               //#1 Path to maps '~/ApplicationData/vintagestory/Map/World_1234567890
+               mapPath = args[1];
+
+               //#0 Command: Heightmaps (Generation from existing shard data)
+               string command = args[0];
+
+               if (command == "--heightmap") {
+               Process_ShardData( );
+               }
+
+               }
+
+               private static void Process_ShardData( )
+               {
+               var shardsDir = new DirectoryInfo( Path.Combine(mapPath , _chunkPath));
+                                       
+               var shardFiles = shardsDir.GetFiles(chunkFile_filter);
+
+               if (shardFiles.Length > 0) {
+               #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) continue;
+
+               int X_chunk_pos = int.Parse(result.Groups["X"].Value);
+               int Z_chunk_pos = int.Parse(result.Groups["Z"].Value);
+
+               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;
+               ColumnMeta columnData = metadataFromPng.ChunkMetadata;
+               //columnData.HeightMap //Should be sane Heightmap...
+
+               
+
+               }
+
+               } catch (PngjException someEx) {
+               //Logger.Error("PNG Corruption file '{0}' - Reason: {1}", shardFile.Name, someEx);
+               continue;
+               } catch (ProtoException protoEx) {
+               //Logger.Error("ProtoBuf invalid! file:'{0}' - Reason: {1}", shardFile.Name, protoEx);
+               continue;
+               }
+               }
+               }
+
+               
+               }
+
        }
 }