2 using System.Collections.Generic;
3 using System.Diagnostics;
6 using System.Threading;
7 using System.Threading.Tasks;
10 using Hjg.Pngcs.Chunks;
11 using Vintagestory.API.Common;
15 public class Snapshotter
17 public readonly int chunkSize;
18 public string fileName;
19 public string chunkPath;
20 public ColumnsMetadata cols;
21 //TODO: Refactor - so Edges are set at construction time, as ColumnsMetadata is async updating in real time!
22 public int NorthEdge => cols.North_mostChunk;
23 public int WestEdge => cols.West_mostChunk;
24 public int Width => (cols.East_mostChunk - WestEdge + 1);
25 public int Height => (cols.South_mostChunk - NorthEdge + 1);
28 public Snapshotter(string path, ColumnsMetadata cols, int chunkSize, int worldSeed)
30 this.fileName = Path.Combine(path, $"snapshot_{worldSeed}.png");
31 this.chunkPath = Path.Combine(path, AutomapSystem._chunkPath);
33 this.chunkSize = chunkSize;
37 /// takes a snapshot. this should be called from an extra thread.
39 public async void Take()
42 ImageInfo info = new ImageInfo(Width * chunkSize, Height * chunkSize, 8, false);
44 PngWriter snapWriter = FileHelper.CreatePngWriter(fileName, info, true);
46 PngMetadata meta = snapWriter.GetMetadata( );
48 var transparencyChunk = meta.CreateTRNSChunk( );
49 transparencyChunk.SetRGB(0, 0, 0);//CHECK: This is pure black.
50 meta.SetText("Northmost", NorthEdge.ToString("D"));
51 meta.SetText("Eastmost", WestEdge.ToString("D"));
52 meta.SetText("Width", Width.ToString("D"));
53 meta.SetText("Height", Height.ToString("D"));
58 Red: 2 bytes, range 0 .. (2^bitdepth)-1
59 Green: 2 bytes, range 0 .. (2^bitdepth)-1
60 Blue: 2 bytes, range 0 .. (2^bitdepth)-1
64 snapWriter.CompLevel = 5;
65 snapWriter.CompressionStrategy = Hjg.Pngcs.Zlib.EDeflateCompressStrategy.Filtered;
71 group ch by ch.Location.Y into g
74 // that sorts things in ascending order so we can only create (chunkSize) lines at once
75 int row = 0, lastPosY = 0, posY = 0;
76 foreach (var chunkGroup in orderedList)
78 // oh god here we go...
79 posY = chunkGroup.Key - NorthEdge;
80 var delta = posY - lastPosY;
83 // if there is more than 1 blank step then the gap needs to be filled.
84 for (int i = 1; i < delta; i++)
86 byte[] blankLine = new byte[info.BytesPerRow];
87 for (int j = 0; j < chunkSize; j++)
88 snapWriter.WriteRowByte(blankLine, row++);
91 var inGroup = (await ReadAllInGroup(chunkGroup)).ToArray();
93 for (int r = 0; r < chunkSize; r++)
95 var line = new byte[info.BytesPerRow];
96 for (int chunk = 0; chunk < inGroup.Length; chunk++)
98 var posX = inGroup[chunk].Key * chunkSize * info.BytesPixel;
99 inGroup[chunk].Value?[r].CopyTo(line, posX);
101 snapWriter.WriteRowByte(line, row++);
104 snapWriter.ShouldCloseStream = true;
111 Console.WriteLine("Snapshot exception!");
115 private async Task<Dictionary<int, byte[][]>> ReadAllInGroup(IGrouping<int, ColumnMeta> group)
117 var taskGroup = new Dictionary<int, byte[][]>(group.Count());
118 foreach (var shardMeta in group)
120 var shardPath = Path.Combine(chunkPath, $"{shardMeta.Location.X}_{shardMeta.Location.Y}.png");
121 taskGroup.Add(shardMeta.Location.X - WestEdge, await ReadNoThrow(shardPath));
126 private async Task<byte[][]> ReadNoThrow(string readPath)
130 return await Task.Run(() => FileHelper.CreatePngReader(readPath).ReadRowsByte().ScanlinesB);
134 Console.WriteLine(e.Message);
136 } // do nothing on error