From ec3438c8c02c57ac0241b388851e659b1cc0fc65 Mon Sep 17 00:00:00 2001 From: melchior Date: Sat, 7 Nov 2020 15:15:59 -0500 Subject: [PATCH] W.I.P. P.O.C. Shard Post Processor Skeliton --- ShardProcessor/Program.cs | 86 +++++++++++++++++++++++++++++++++++- ShardProcessor/ShardProcessor.csproj | 28 ++++++++++++ 2 files changed, 112 insertions(+), 2 deletions(-) diff --git a/ShardProcessor/Program.cs b/ShardProcessor/Program.cs index e9e5b6c..0d2f1e1 100644 --- a/ShardProcessor/Program.cs +++ b/ShardProcessor/Program.cs @@ -1,19 +1,101 @@ 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(@"(?[\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]; + + 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; + } + } + } + + + } + } } diff --git a/ShardProcessor/ShardProcessor.csproj b/ShardProcessor/ShardProcessor.csproj index ba17e0f..2d9462d 100644 --- a/ShardProcessor/ShardProcessor.csproj +++ b/ShardProcessor/ShardProcessor.csproj @@ -18,6 +18,7 @@ prompt 4 true + --heightmap ~/ApplicationData/vintagestory/Maps/World_19781215 true @@ -28,6 +29,33 @@ + + ..\Automap\VS_libs\protobuf-net.dll + + + ..\Automap\VS_libs\Pngcs.dll + + + ..\Automap\VS_libs\Newtonsoft.Json.dll + + + ..\Automap\bin\Debug\Automap.dll + + + ..\Automap\VS_libs\VintagestoryAPI.dll + + + ..\Automap\VS_libs\VintagestoryLib.dll + + + ..\Automap\VS_libs\VSEssentials.dll + + + ..\Automap\VS_libs\VSCreativeMod.dll + + + ..\Automap\VS_libs\VSSurvivalMod.dll + -- 2.11.0