2 using System.Collections.Generic;
3 using System.Diagnostics;
6 using System.Threading;
7 using System.Threading.Tasks;
10 using Hjg.Pngcs.Chunks;
14 public class Snapshotter
16 public readonly int chunkSize;
17 public string fileName;
18 public string chunkPath;
19 public ColumnsMetadata cols;
20 //TODO: Refactor - so Edges are set at construction time, as ColumnsMetadata is async updating in real time!
21 public int NorthEdge => cols.North_mostChunk;
22 public int WestEdge => cols.West_mostChunk;
23 public int Width => (cols.East_mostChunk - WestEdge + 1);
24 public int Height => (cols.South_mostChunk - NorthEdge + 1);
27 public Snapshotter(string path, ColumnsMetadata cols, int chunkSize, int worldSeed)
29 this.fileName = Path.Combine(path, $"snapshot_{worldSeed}_{DateTime.UtcNow:u}.png");
30 this.chunkPath = Path.Combine(path, AutomapSystem._chunkPath);
32 this.chunkSize = chunkSize;
36 /// takes a snapshot. this should be called from an extra thread.
38 /// <param name="path">path to the map dir</param>
39 /// <param name="chunkPath">name of the chunks dir part thing</param>
40 /// <param name="cols"></param>
41 /// <param name="chunkSize"></param>
42 public async void Take()
44 var t = new Stopwatch();
47 Console.WriteLine("snapshot started");
49 ImageInfo info = new ImageInfo(Width * chunkSize, Height * chunkSize, 8, false);
50 PngWriter snapWriter = FileHelper.CreatePngWriter(fileName, info, true);
51 PngMetadata meta = snapWriter.GetMetadata( );
53 var transparencyChunk = meta.CreateTRNSChunk( );
54 transparencyChunk.SetRGB(0, 0, 0);//CHECK: This is pure black.
55 meta.SetText("Northmost", NorthEdge.ToString("D"));
56 meta.SetText("Eastmost", WestEdge.ToString("D"));
57 meta.SetText("Width", Width.ToString("D"));
58 meta.SetText("Height", Height.ToString("D"));
63 Red: 2 bytes, range 0 .. (2^bitdepth)-1
64 Green: 2 bytes, range 0 .. (2^bitdepth)-1
65 Blue: 2 bytes, range 0 .. (2^bitdepth)-1
69 snapWriter.CompLevel = 5;
70 snapWriter.CompressionStrategy = Hjg.Pngcs.Zlib.EDeflateCompressStrategy.Filtered;
76 group ch by ch.Location.Y into g
79 // that sorts things in ascending order so we can only create (chunkSize) lines at once
80 int row = 0, lastPosY = 0, posY = 0;
81 foreach (var chunkGroup in orderedList)
83 // oh god here we go...
84 posY = chunkGroup.Key - NorthEdge;
85 var delta = posY - lastPosY;
88 // if there is more than 1 blank step then the gap needs to be filled.
89 for (int i = 1; i < delta; i++)
91 byte[] blankLine = new byte[info.BytesPerRow];
92 for (int j = 0; j < chunkSize; j++)
93 snapWriter.WriteRowByte(blankLine, row++);
96 var inGroup = (await ReadAllInGroup(chunkGroup)).ToArray();
98 for (int r = 0; r < chunkSize; r++)
100 var line = new byte[info.BytesPerRow];
101 for (int chunk = 0; chunk < inGroup.Length; chunk++)
103 var posX = inGroup[chunk].Key * chunkSize * info.BytesPixel;
104 inGroup[chunk].Value?[r].CopyTo(line, posX);
106 snapWriter.WriteRowByte(line, row++);
109 snapWriter.ShouldCloseStream = true;
116 Console.WriteLine("Snapshot exception!");
118 Console.WriteLine($"snapshot finished in {t.ElapsedMilliseconds}");
121 private async Task<Dictionary<int, byte[][]>> ReadAllInGroup(IGrouping<int, ColumnMeta> group)
123 var taskGroup = new Dictionary<int, byte[][]>(group.Count());
124 foreach (var shardMeta in group)
126 var shardPath = Path.Combine(chunkPath, $"{shardMeta.Location.X}_{shardMeta.Location.Y}.png");
127 taskGroup.Add(shardMeta.Location.X - WestEdge, await ReadNoThrow(shardPath));
132 private async Task<byte[][]> ReadNoThrow(string readPath)
136 return await Task.Run(() => FileHelper.CreatePngReader(readPath).ReadRowsByte().ScanlinesB);
140 Console.WriteLine(e.Message);
142 } // do nothing on error