OSDN Git Service

W.I.P. P.O.C. Shard Post Processor Skeliton
[automap/automap.git] / ShardProcessor / Program.cs
1 using System;
2 using System.IO;
3 using System.Text.RegularExpressions;
4
5 using Automap;
6
7 using Hjg.Pngcs;
8 using Hjg.Pngcs.Chunks;
9
10 using ProtoBuf;
11
12 namespace ShardProcessor
13 {       
14         class MainClass
15         {
16                 //private ILogger Logger { get; set; }
17                 const string chunkFile_filter = @"*_*.png";
18                 static Regex chunkShardRegex = new Regex(@"(?<X>[\d]+)_(?<Z>[\d]+)\.png", RegexOptions.Singleline);
19                 static string mapPath;
20                 internal const string _chunkPath = @"Chunks";
21
22                 /* TODO:
23                         -Process existing PNGs: Report/Dump contents of Chunk Metadata, as per current version
24                         -Grayscale Heightmap extraction from P.Buf heightmap from shards
25                         -Extract contents of game's SQLLite map DB, INTO Automap type shards...
26                         -Other stuff? chunk fixing / validation?
27                 */
28                 public static void Main(string[ ] args)
29                 {
30                 Console.WriteLine("AUTOMAP Offline Shard processor v0.1");
31                 //Called once - thus it can only be in a static constructor.
32                 PngChunk.FactoryRegister(PngMetadataChunk.ID, typeof(PngMetadataChunk));
33
34                 ArgsDecoder(args);
35
36                 }
37
38                 private static void ArgsDecoder(string[ ] args)
39                 {
40                 //#1 Path to maps '~/ApplicationData/vintagestory/Map/World_1234567890
41                 mapPath = args[1];
42
43                 //#0 Command: Heightmaps (Generation from existing shard data)
44                 string command = args[0];
45
46                 if (command == "--heightmap") {
47                 Process_ShardData( );
48                 }
49
50                 }
51
52                 private static void Process_ShardData( )
53                 {
54                 var shardsDir = new DirectoryInfo( Path.Combine(mapPath , _chunkPath));
55                                         
56                 var shardFiles = shardsDir.GetFiles(chunkFile_filter);
57
58                 if (shardFiles.Length > 0) {
59                 #if DEBUG
60                 //Logger.VerboseDebug("Metadata reloading from {0} shards", shardFiles.Length);
61                 #endif
62
63                 foreach (var shardFile in shardFiles) {
64
65                 if (shardFile.Length < 1024) continue;
66                 var result = chunkShardRegex.Match(shardFile.Name);
67                 if (!result.Success) continue;
68
69                 int X_chunk_pos = int.Parse(result.Groups["X"].Value);
70                 int Z_chunk_pos = int.Parse(result.Groups["Z"].Value);
71
72                 try {
73                 using (var fileStream = shardFile.OpenRead( )) {
74
75                 PngReader pngRead = new PngReader(fileStream);
76                 pngRead.ReadSkippingAllRows( );
77                 pngRead.End( );
78                 //Parse PNG chunks for METADATA in shard
79                 PngMetadataChunk metadataFromPng = pngRead.GetChunksList( ).GetById1(PngMetadataChunk.ID) as PngMetadataChunk;
80                 ColumnMeta columnData = metadataFromPng.ChunkMetadata;
81                 //columnData.HeightMap //Should be sane Heightmap...
82
83                 
84
85                 }
86
87                 } catch (PngjException someEx) {
88                 //Logger.Error("PNG Corruption file '{0}' - Reason: {1}", shardFile.Name, someEx);
89                 continue;
90                 } catch (ProtoException protoEx) {
91                 //Logger.Error("ProtoBuf invalid! file:'{0}' - Reason: {1}", shardFile.Name, protoEx);
92                 continue;
93                 }
94                 }
95                 }
96
97                 
98                 }
99
100         }
101 }