using System; using System.Diagnostics; 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(@"(?[\d]+)_(?[\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]; switch (command) { case @"--heightmap": Process_ShardData( ); break; case @"--shards": Scan_ShardData( ); break; case @"--points": Scan_PointsData( ); break; case @"--oneshard": Scan_OneShard( ); break; default: Console.WriteLine("Unrecognized Command: {0}", command); break; } } 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; } } } } private static void Scan_PointsData( ) { try { var eoiFile = new FileInfo(Path.Combine(mapPath, @"eoi_binary")); var poiFile = new FileInfo(Path.Combine(mapPath, @"poi_binary")); uint entities = 0, points = 0; if (eoiFile.Exists) { using (var eoiStream = eoiFile.OpenRead( )) { EntitiesOfInterest eoiData = Serializer.Deserialize(eoiStream); foreach (var entry in eoiData) { Console.WriteLine("#{0}, [{1}], '{2}', {3}", entry.EntityId, entry.Location, entry.Name, entry.Timestamp.ToUniversalTime( ) ); entities++; } Console.WriteLine("Entities Of Interest: {0}", entities); } if (poiFile.Exists) { using (var poiStream = poiFile.OpenRead( )) { PointsOfInterest poiData = Serializer.Deserialize(poiStream); foreach (var entry in poiData) { Console.WriteLine("[{0}], {1}, {2}, {3}", entry.Location, entry.Name, entry.Destination, entry.Timestamp.ToUniversalTime( ) ); points++; } } Console.WriteLine("Points Of Interest: {0}", points); } } } catch (Exception uhOh) { Console.WriteLine(uhOh); } } private static void Scan_ShardData( ) { var shardsDir = new DirectoryInfo(Path.Combine(mapPath, _chunkPath)); ulong count = 0,errors = 0, flat = 0; 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) { Console.WriteLine("File: '{0}' too small to be valid; skipping!", shardFile.FullName); errors++; 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; Console.Write("X{0,6:D} Y{1,6:D} Age:{2:N1} ", columnData.Location.X, columnData.Location.Y, columnData.ChunkAge.TotalDays); Console.Write("YMax:{0:D3} ChkS:{1} Air:{2,7:D} NotAir:{3,7:D} ", columnData.YMax,columnData.ChunkSize , columnData.AirBlocks, columnData.NonAirBlocks ); if (columnData.HeightMap != null) { Console.Write("(Heights [{0}x{1}] ", columnData.HeightMap.GetLength(0), columnData.HeightMap.GetLength(1)); ushort lowest = ushort.MaxValue, highest = 0; ulong sum = 0; foreach (var hmEntry in columnData.HeightMap) { lowest = Math.Min(lowest, hmEntry); highest = Math.Max(highest, hmEntry); sum += hmEntry; } Console.Write("Max:{0,3}, Min:{1,3}, ", highest, lowest); if (sum > 0) Console.Write("Avg:{0:F1})", ( float )sum / (columnData.ChunkSize * columnData.ChunkSize)); Console.WriteLine( ); /*------ROCK RATIOs mini-table----------*/ if (columnData.RockRatio != null && columnData.RockRatio.Count > 0) { Console.Write("Ratios({0,2:D})[",columnData.RockRatio.Count); foreach (var rock in columnData.RockRatio) { Console.Write("ID:{0,5:D} x{1,4:D}, ", rock.Key, rock.Value); } Console.Write(" ]\n"); } if ( sum == 0 || columnData.YMax == 0) flat++; } else { flat++; } } } catch (PngjException someEx) { Console.WriteLine("PNG Corruption file '{0}' - Reason: {1}", shardFile.Name, someEx); errors++; continue; } catch (ProtoException protoEx) { Console.WriteLine("ProtoBuf invalid! file:'{0}' - Reason: {1}", shardFile.Name, protoEx); errors++; continue; } count++; } } Console.WriteLine("Scanned {0} files, {1} errors, {2} FLAT entries", count, errors, flat); } private static void Scan_OneShard( ) { //--oneshard ~/ApplicationData/vintagestory/Maps/World_1316328588/Chunks/9363_9379.png var oneChunkFile = new FileInfo(mapPath); if (oneChunkFile.Exists) { try { using (var fileStream = oneChunkFile.OpenRead( )) { PngReader pngRead = new PngReader(fileStream); pngRead.ReadSkippingAllRows( ); //Parse PNG chunks for METADATA in shard PngMetadataChunk metadataFromPng = pngRead.GetChunksList( ).GetById1(PngMetadataChunk.ID) as PngMetadataChunk; ColumnMeta columnData = metadataFromPng.ChunkMetadata; var metadata = pngRead.GetMetadata( ); var pngWriteTime = metadata.GetTime( ); var chunkX = metadata.GetTxtForKey(@"Chunk_X"); var chunkY = metadata.GetTxtForKey(@"Chunk_Y"); var pixelSize = metadata.GetTxtForKey(@"PxSz"); var gameDate = metadata.GetTxtForKey(@"GameDY"); var dateBlob = pngWriteTime.GetYMDHMS( ); /* return new int[] { this.year, this.mon, this.day, this.hour, this.min, this.sec */ Console.WriteLine($"PNG-Timestamp: Y{dateBlob[0] - 456960} M{dateBlob[1]} D{dateBlob[2]} H{dateBlob[3]} M {dateBlob[4]} S{dateBlob[5]} Chunk: X {chunkX} Y {chunkY} PixelSize:{pixelSize} Game-Date: {gameDate}"); pngRead.End( ); } } catch (Exception darn) { Debug.Write("Oops! File causes: {0}", darn.ToString( )); } } } } }