1 | using System;
|
---|
2 | using System.IO;
|
---|
3 | using AllocsFixes.FileCache;
|
---|
4 | using Unity.Collections;
|
---|
5 | using UnityEngine;
|
---|
6 | using UnityEngine.Profiling;
|
---|
7 |
|
---|
8 | namespace MapRendering {
|
---|
9 | public class MapRenderBlockBuffer {
|
---|
10 | private readonly Texture2D blockMap = new Texture2D (Constants.MapBlockSize, Constants.MapBlockSize, Constants.DefaultTextureFormat, false);
|
---|
11 | private readonly MapTileCache cache;
|
---|
12 | private readonly NativeArray<int> emptyImageData;
|
---|
13 | private readonly Texture2D zoomBuffer = new Texture2D (Constants.MapBlockSize / 2, Constants.MapBlockSize / 2, Constants.DefaultTextureFormat, false);
|
---|
14 | private readonly int zoomLevel;
|
---|
15 | private readonly string folderBase;
|
---|
16 |
|
---|
17 | private Vector2i currentBlockMapPos = new Vector2i (int.MinValue, int.MinValue);
|
---|
18 | private string currentBlockMapFolder = string.Empty;
|
---|
19 |
|
---|
20 | public MapRenderBlockBuffer (int _level, MapTileCache _cache) {
|
---|
21 | zoomLevel = _level;
|
---|
22 | cache = _cache;
|
---|
23 | folderBase = Constants.MapDirectory + "/" + zoomLevel + "/";
|
---|
24 |
|
---|
25 | {
|
---|
26 | // Initialize empty tile data
|
---|
27 | Color nullColor = new Color (0, 0, 0, 0);
|
---|
28 | for (int x = 0; x < Constants.MapBlockSize; x++) {
|
---|
29 | for (int y = 0; y < Constants.MapBlockSize; y++) {
|
---|
30 | blockMap.SetPixel (x, y, nullColor);
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | NativeArray<int> blockMapData = blockMap.GetRawTextureData<int> ();
|
---|
35 | emptyImageData = new NativeArray<int> (blockMapData.Length, Allocator.Persistent,
|
---|
36 | NativeArrayOptions.UninitializedMemory);
|
---|
37 | blockMapData.CopyTo (emptyImageData);
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | public TextureFormat FormatSelf => blockMap.format;
|
---|
42 |
|
---|
43 | public void ResetBlock () {
|
---|
44 | currentBlockMapFolder = string.Empty;
|
---|
45 | currentBlockMapPos = new Vector2i (int.MinValue, int.MinValue);
|
---|
46 | cache.ResetTile (zoomLevel);
|
---|
47 | }
|
---|
48 |
|
---|
49 | public void SaveBlock () {
|
---|
50 | Profiler.BeginSample ("SaveBlock");
|
---|
51 | try {
|
---|
52 | saveTextureToFile ();
|
---|
53 | } catch (Exception e) {
|
---|
54 | Log.Warning ("Exception in MapRenderBlockBuffer.SaveBlock(): " + e);
|
---|
55 | }
|
---|
56 | Profiler.EndSample ();
|
---|
57 | }
|
---|
58 |
|
---|
59 | public bool LoadBlock (Vector2i _block) {
|
---|
60 | Profiler.BeginSample ("LoadBlock");
|
---|
61 | lock (blockMap) {
|
---|
62 | if (currentBlockMapPos != _block) {
|
---|
63 | Profiler.BeginSample ("LoadBlock.Strings");
|
---|
64 | string folder;
|
---|
65 | if (currentBlockMapPos.x != _block.x) {
|
---|
66 | folder = folderBase + _block.x + '/';
|
---|
67 |
|
---|
68 | Profiler.BeginSample ("LoadBlock.Directory");
|
---|
69 | Directory.CreateDirectory (folder);
|
---|
70 | Profiler.EndSample ();
|
---|
71 | } else {
|
---|
72 | folder = currentBlockMapFolder;
|
---|
73 | }
|
---|
74 |
|
---|
75 | string fileName = folder + _block.y + ".png";
|
---|
76 | Profiler.EndSample ();
|
---|
77 |
|
---|
78 | SaveBlock ();
|
---|
79 | loadTextureFromFile (fileName);
|
---|
80 |
|
---|
81 | currentBlockMapFolder = folder;
|
---|
82 | currentBlockMapPos = _block;
|
---|
83 |
|
---|
84 | Profiler.EndSample ();
|
---|
85 | return true;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | Profiler.EndSample ();
|
---|
90 | return false;
|
---|
91 | }
|
---|
92 |
|
---|
93 | public void SetPart (Vector2i _offset, int _partSize, Color32[] _pixels) {
|
---|
94 | if (_offset.x + _partSize > Constants.MapBlockSize || _offset.y + _partSize > Constants.MapBlockSize) {
|
---|
95 | Log.Error (
|
---|
96 | $"MapBlockBuffer[{zoomLevel}].SetPart ({_offset}, {_partSize}, {_pixels.Length}) has blockMap.size ({Constants.MapBlockSize}/{Constants.MapBlockSize})");
|
---|
97 | return;
|
---|
98 | }
|
---|
99 |
|
---|
100 | Profiler.BeginSample ("SetPart");
|
---|
101 | blockMap.SetPixels32 (_offset.x, _offset.y, _partSize, _partSize, _pixels);
|
---|
102 | Profiler.EndSample ();
|
---|
103 | }
|
---|
104 |
|
---|
105 | public Color32[] GetHalfScaled () {
|
---|
106 | Profiler.BeginSample ("HalfScaled.ResizeBuffer");
|
---|
107 | zoomBuffer.Resize (Constants.MapBlockSize, Constants.MapBlockSize);
|
---|
108 | Profiler.EndSample ();
|
---|
109 |
|
---|
110 | Profiler.BeginSample ("HalfScaled.CopyPixels");
|
---|
111 | if (blockMap.format == zoomBuffer.format) {
|
---|
112 | Profiler.BeginSample ("Native");
|
---|
113 | NativeArray<byte> dataSrc = blockMap.GetRawTextureData<byte> ();
|
---|
114 | NativeArray<byte> dataZoom = zoomBuffer.GetRawTextureData<byte> ();
|
---|
115 | dataSrc.CopyTo (dataZoom);
|
---|
116 | Profiler.EndSample ();
|
---|
117 | } else {
|
---|
118 | Profiler.BeginSample ("GetSetPixels");
|
---|
119 | zoomBuffer.SetPixels32 (blockMap.GetPixels32 ());
|
---|
120 | Profiler.EndSample ();
|
---|
121 | }
|
---|
122 | Profiler.EndSample ();
|
---|
123 |
|
---|
124 | Profiler.BeginSample ("HalfScaled.Scale");
|
---|
125 | TextureScale.Point (zoomBuffer, Constants.MapBlockSize / 2, Constants.MapBlockSize / 2);
|
---|
126 | Profiler.EndSample ();
|
---|
127 |
|
---|
128 | Profiler.BeginSample ("HalfScaled.Return");
|
---|
129 | Color32[] result = zoomBuffer.GetPixels32 ();
|
---|
130 | Profiler.EndSample ();
|
---|
131 |
|
---|
132 | return result;
|
---|
133 | }
|
---|
134 |
|
---|
135 | public void SetPartNative (Vector2i _offset, int _partSize, NativeArray<int> _pixels) {
|
---|
136 | if (_offset.x + _partSize > Constants.MapBlockSize || _offset.y + _partSize > Constants.MapBlockSize) {
|
---|
137 | Log.Error (
|
---|
138 | $"MapBlockBuffer[{zoomLevel}].SetPart ({_offset}, {_partSize}, {_pixels.Length}) has blockMap.size ({Constants.MapBlockSize}/{Constants.MapBlockSize})");
|
---|
139 | return;
|
---|
140 | }
|
---|
141 |
|
---|
142 | Profiler.BeginSample ("SetPartNative");
|
---|
143 | NativeArray<int> destData = blockMap.GetRawTextureData<int> ();
|
---|
144 |
|
---|
145 | for (int y = 0; y < _partSize; y++) {
|
---|
146 | int srcLineStartIdx = _partSize * y;
|
---|
147 | int destLineStartIdx = blockMap.width * (_offset.y + y) + _offset.x;
|
---|
148 | for (int x = 0; x < _partSize; x++) {
|
---|
149 | destData [destLineStartIdx + x] = _pixels [srcLineStartIdx + x];
|
---|
150 | }
|
---|
151 | }
|
---|
152 | Profiler.EndSample ();
|
---|
153 | }
|
---|
154 |
|
---|
155 | public NativeArray<int> GetHalfScaledNative () {
|
---|
156 | Profiler.BeginSample ("HalfScaledNative.ResizeBuffer");
|
---|
157 | if (zoomBuffer.format != blockMap.format || zoomBuffer.height != Constants.MapBlockSize / 2 || zoomBuffer.width != Constants.MapBlockSize / 2) {
|
---|
158 | zoomBuffer.Resize (Constants.MapBlockSize / 2, Constants.MapBlockSize / 2, blockMap.format, false);
|
---|
159 | }
|
---|
160 | Profiler.EndSample ();
|
---|
161 |
|
---|
162 | Profiler.BeginSample ("HalfScaledNative.Scale");
|
---|
163 | ScaleNative (blockMap, zoomBuffer);
|
---|
164 | Profiler.EndSample ();
|
---|
165 |
|
---|
166 | return zoomBuffer.GetRawTextureData<int> ();
|
---|
167 | }
|
---|
168 |
|
---|
169 | private static void ScaleNative (Texture2D _sourceTex, Texture2D _targetTex) {
|
---|
170 | NativeArray<int> srcData = _sourceTex.GetRawTextureData<int> ();
|
---|
171 | NativeArray<int> targetData = _targetTex.GetRawTextureData<int> ();
|
---|
172 |
|
---|
173 | int oldWidth = _sourceTex.width;
|
---|
174 | int oldHeight = _sourceTex.height;
|
---|
175 | int newWidth = _targetTex.width;
|
---|
176 | int newHeight = _targetTex.height;
|
---|
177 |
|
---|
178 | float ratioX = (float) oldWidth / newWidth;
|
---|
179 | float ratioY = (float) oldHeight / newHeight;
|
---|
180 |
|
---|
181 | for (int y = 0; y < newHeight; y++) {
|
---|
182 | int oldLineStart = (int) (ratioY * y) * oldWidth;
|
---|
183 | int newLineStart = y * newWidth;
|
---|
184 | for (int x = 0; x < newWidth; x++) {
|
---|
185 | targetData [newLineStart + x] = srcData [(int) (oldLineStart + ratioX * x)];
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | private void loadTextureFromFile (string _fileName) {
|
---|
191 | Profiler.BeginSample ("LoadTexture");
|
---|
192 |
|
---|
193 | Profiler.BeginSample ("LoadFile");
|
---|
194 | byte[] array = cache.LoadTile (zoomLevel, _fileName);
|
---|
195 | Profiler.EndSample ();
|
---|
196 |
|
---|
197 | Profiler.BeginSample ("LoadImage");
|
---|
198 | if (array != null && blockMap.LoadImage (array) && blockMap.height == Constants.MapBlockSize &&
|
---|
199 | blockMap.width == Constants.MapBlockSize) {
|
---|
200 | Profiler.EndSample ();
|
---|
201 |
|
---|
202 | Profiler.EndSample ();
|
---|
203 | return;
|
---|
204 | }
|
---|
205 | Profiler.EndSample ();
|
---|
206 |
|
---|
207 | if (array != null) {
|
---|
208 | Log.Error ("Map image tile " + _fileName + " has been corrupted, recreating tile");
|
---|
209 | }
|
---|
210 |
|
---|
211 | if (blockMap.format != Constants.DefaultTextureFormat || blockMap.height != Constants.MapBlockSize ||
|
---|
212 | blockMap.width != Constants.MapBlockSize) {
|
---|
213 | blockMap.Resize (Constants.MapBlockSize, Constants.MapBlockSize, Constants.DefaultTextureFormat,
|
---|
214 | false);
|
---|
215 | }
|
---|
216 |
|
---|
217 | blockMap.LoadRawTextureData (emptyImageData);
|
---|
218 |
|
---|
219 | Profiler.EndSample ();
|
---|
220 | }
|
---|
221 |
|
---|
222 | private void saveTextureToFile () {
|
---|
223 | Profiler.BeginSample ("EncodePNG");
|
---|
224 | byte[] array = blockMap.EncodeToPNG ();
|
---|
225 | Profiler.EndSample ();
|
---|
226 |
|
---|
227 | cache.SaveTile (zoomLevel, array);
|
---|
228 | }
|
---|
229 | }
|
---|
230 | }
|
---|