source: binary-improvements/7dtd-server-fixes/src/MapRendering/MapRendering.cs@ 196

Last change on this file since 196 was 192, checked in by alloc, 10 years ago

fixes

File size: 10.9 KB
RevLine 
[187]1using AllocsFixes.JSON;
[130]2using System;
3using System.Collections.Generic;
4using System.IO;
[187]5using System.Text;
[130]6using System.Threading;
7using UnityEngine;
8
9namespace AllocsFixes.MapRendering
10{
11 public class MapRendering
12 {
13 private static MapRendering instance;
14
15 public static MapRendering Instance {
16 get {
17 if (instance == null) {
18 instance = new MapRendering ();
19 }
20 return instance;
21 }
22 }
23
[192]24 private static object lockObject = new object ();
[187]25 private MapRenderBlockBuffer[] zoomLevelBuffers;
[143]26 private Dictionary<Vector2i, Color[]> dirtyChunks = new Dictionary<Vector2i, Color[]> ();
[132]27 private System.Timers.Timer chunkSaveTimer = new System.Timers.Timer (500);
[130]28 private bool renderingFullMap = false;
[140]29 public static bool renderingEnabled = true;
[143]30 private MicroStopwatch msw = new MicroStopwatch ();
[130]31
32 private MapRendering ()
33 {
34 Constants.MAP_DIRECTORY = StaticDirectories.GetSaveGameDir () + "/map";
35
[192]36 lock (lockObject) {
37 if (!LoadMapInfo ())
38 WriteMapInfo ();
[187]39 }
40
41 zoomLevelBuffers = new MapRenderBlockBuffer[Constants.ZOOMLEVELS];
[130]42 for (int i = 0; i < Constants.ZOOMLEVELS; i++) {
43 zoomLevelBuffers [i] = new MapRenderBlockBuffer (i);
44 }
45
46 chunkSaveTimer.AutoReset = false;
[144]47 chunkSaveTimer.Elapsed += new System.Timers.ElapsedEventHandler (TimedRendering);
[130]48 }
49
50 public static void RenderSingleChunk (Chunk chunk)
51 {
[140]52 if (renderingEnabled) {
53 ThreadPool.QueueUserWorkItem ((o) =>
54 {
55 try {
56 if (!Instance.renderingFullMap) {
[192]57 lock (lockObject) {
[143]58 Chunk c = (Chunk)o;
59 Vector3i cPos = c.GetWorldPos ();
60 Vector2i cPos2 = new Vector2i (cPos.x / Constants.MAP_CHUNK_SIZE, cPos.z / Constants.MAP_CHUNK_SIZE);
61
62 ushort[] mapColors = c.GetMapColors ();
63 if (mapColors != null) {
64 Color[] realColors = new Color[Constants.MAP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE];
65 for (int i_colors = 0; i_colors < mapColors.Length; i_colors++) {
66 realColors [i_colors] = shortColorToColor (mapColors [i_colors]);
67 }
68 Instance.dirtyChunks [cPos2] = realColors;
[156]69 //Log.Out ("Add Dirty: " + cPos2);
[143]70 Instance.chunkSaveTimer.Stop ();
71 Instance.chunkSaveTimer.Start ();
72 }
73 }
[140]74 }
75 } catch (Exception e) {
76 Log.Out ("Exception in MapRendering.RenderSingleChunk(): " + e);
[130]77 }
[140]78 }, chunk);
79 }
[130]80 }
81
82 public void RenderFullMap ()
83 {
84 MicroStopwatch microStopwatch = new MicroStopwatch ();
85
[132]86 string regionSaveDir = StaticDirectories.GetSaveGameRegionDir ();
[143]87 RegionFileManager rfm = new RegionFileManager (regionSaveDir, regionSaveDir, 0, false);
88 Texture2D fullMapTexture = null;
[132]89
[148]90 Vector2i minChunk = default(Vector2i), maxChunk = default(Vector2i);
91 Vector2i minPos = default(Vector2i), maxPos = default(Vector2i);
[130]92 int widthChunks, heightChunks, widthPix, heightPix;
[143]93 getWorldExtent (rfm, out minChunk, out maxChunk, out minPos, out maxPos, out widthChunks, out heightChunks, out widthPix, out heightPix);
[130]94
[135]95 Log.Out (String.Format ("RenderMap: min: {0}, max: {1}, minPos: {2}, maxPos: {3}, w/h: {4}/{5}, wP/hP: {6}/{7}",
96 minChunk.ToString (), maxChunk.ToString (),
97 minPos.ToString (), maxPos.ToString (),
98 widthChunks, heightChunks,
[136]99 widthPix, heightPix)
100 );
[135]101
[192]102 lock (lockObject) {
[168]103 for (int i = 0; i < Constants.ZOOMLEVELS; i++) {
104 zoomLevelBuffers [i].ResetBlock ();
105 }
106
[187]107 if (Directory.Exists (Constants.MAP_DIRECTORY)) {
[168]108 Directory.Delete (Constants.MAP_DIRECTORY, true);
[187]109 }
110 WriteMapInfo ();
[168]111
112 renderingFullMap = true;
113
114 if (widthPix <= 8000 && heightPix <= 8000)
115 fullMapTexture = new Texture2D (widthPix, heightPix);
116
[148]117 Vector2i curFullMapPos = default(Vector2i);
118 Vector2i curChunkPos = default(Vector2i);
[143]119 for (curFullMapPos.x = 0; curFullMapPos.x < widthPix; curFullMapPos.x += Constants.MAP_CHUNK_SIZE) {
120 for (curFullMapPos.y = 0; curFullMapPos.y < heightPix; curFullMapPos.y += Constants.MAP_CHUNK_SIZE) {
121 curChunkPos.x = (curFullMapPos.x / Constants.MAP_CHUNK_SIZE) + minChunk.x;
122 curChunkPos.y = (curFullMapPos.y / Constants.MAP_CHUNK_SIZE) + minChunk.y;
[130]123
[143]124 try {
125 long chunkKey = WorldChunkCache.MakeChunkKey (curChunkPos.x, curChunkPos.y);
126 if (rfm.ContainsChunkSync (chunkKey)) {
127 Chunk c = rfm.GetChunkSync (chunkKey);
128 ushort[] mapColors = c.GetMapColors ();
129 if (mapColors != null) {
130 Color[] realColors = new Color[Constants.MAP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE];
131 for (int i_colors = 0; i_colors < mapColors.Length; i_colors++) {
132 realColors [i_colors] = shortColorToColor (mapColors [i_colors]);
133 }
134 dirtyChunks [curChunkPos] = realColors;
135 if (fullMapTexture != null)
136 fullMapTexture.SetPixels (curFullMapPos.x, curFullMapPos.y, Constants.MAP_CHUNK_SIZE, Constants.MAP_CHUNK_SIZE, realColors);
137 }
138 }
139 } catch (Exception e) {
140 Log.Out ("Exception: " + e);
141 }
142 }
[144]143
144 while (dirtyChunks.Count > 0) {
145 RenderDirtyChunks ();
146 }
147
[143]148 Log.Out (String.Format ("RenderMap: {0}/{1} ({2}%)", curFullMapPos.x, widthPix, (int)((float)curFullMapPos.x / widthPix * 100)));
[130]149 }
150 }
151
[136]152 if (fullMapTexture != null) {
153 byte[] array = fullMapTexture.EncodeToPNG ();
154 File.WriteAllBytes (Constants.MAP_DIRECTORY + "/map.png", array);
155 Texture2D.Destroy (fullMapTexture);
156 fullMapTexture = null;
157 }
[130]158
159 renderingFullMap = false;
160
161 Log.Out ("Generating map took: " + microStopwatch.ElapsedMilliseconds + " ms");
162 Log.Out ("World extent: " + minPos + " - " + maxPos);
163 }
164
165 private void SaveAllBlockMaps (object source, System.Timers.ElapsedEventArgs e)
166 {
[143]167 for (int i = 0; i < Constants.ZOOMLEVELS; i++) {
168 zoomLevelBuffers [i].SaveBlock ();
[130]169 }
170 }
171
[144]172 private void TimedRendering (object source, System.Timers.ElapsedEventArgs e)
[130]173 {
[192]174 lock (lockObject) {
[144]175 RenderDirtyChunks ();
176 if (dirtyChunks.Count > 0)
177 Instance.chunkSaveTimer.Start ();
178 }
179 }
[143]180
[144]181 private void RenderDirtyChunks ()
182 {
183 msw.ResetAndRestart ();
[143]184
[144]185 if (dirtyChunks.Count > 0) {
186 List<Vector2i> keys = new List<Vector2i> (dirtyChunks.Keys);
187 List<Vector2i> chunksDone = new List<Vector2i> ();
[143]188
[144]189 Vector2i chunkPos = keys [0];
190 chunksDone.Add (chunkPos);
[143]191
[144]192 //Log.Out ("Start Dirty: " + chunkPos);
[130]193
[148]194 Vector2i block = default(Vector2i), blockOffset = default(Vector2i);
[144]195 getBlockNumber (chunkPos, out block, out blockOffset, Constants.MAP_BLOCK_TO_CHUNK_DIV, Constants.MAP_CHUNK_SIZE);
[143]196
[144]197 zoomLevelBuffers [Constants.ZOOMLEVELS - 1].LoadBlock (block);
198
[148]199 Vector2i v_block = default(Vector2i), v_blockOffset = default(Vector2i);
[144]200 foreach (Vector2i v in keys) {
201 getBlockNumber (v, out v_block, out v_blockOffset, Constants.MAP_BLOCK_TO_CHUNK_DIV, Constants.MAP_CHUNK_SIZE);
202 if (v_block.Equals (block)) {
203 //Log.Out ("Dirty: " + v + " render: true");
204 chunksDone.Add (v);
205 zoomLevelBuffers [Constants.ZOOMLEVELS - 1].SetPart (v_blockOffset, Constants.MAP_CHUNK_SIZE, dirtyChunks [v]);
206 } else {
207 //Log.Out ("Dirty: " + v + " render: false");
[130]208 }
[144]209 }
[130]210
[144]211 foreach (Vector2i v in chunksDone)
212 dirtyChunks.Remove (v);
[130]213
[144]214 RenderZoomLevel (Constants.ZOOMLEVELS - 1, block);
[130]215
[144]216 SaveAllBlockMaps (null, null);
[130]217 }
218 }
219
220 private void RenderZoomLevel (int level, Vector2i innerBlock)
221 {
222 if (level > 0) {
[148]223 Vector2i block = default(Vector2i), blockOffset = default(Vector2i);
[135]224 getBlockNumber (innerBlock, out block, out blockOffset, 2, Constants.MAP_BLOCK_SIZE / 2);
[130]225
[135]226 zoomLevelBuffers [level - 1].LoadBlock (block);
227 zoomLevelBuffers [level - 1].SetPart (blockOffset, Constants.MAP_BLOCK_SIZE / 2, zoomLevelBuffers [level].GetHalfScaled ());
[130]228
229 RenderZoomLevel (level - 1, block);
230 }
231 }
232
233 private void getBlockNumber (Vector2i innerPos, out Vector2i block, out Vector2i blockOffset, int scaleFactor, int offsetSize)
234 {
[151]235 block = default(Vector2i);
236 blockOffset = default(Vector2i);
[130]237 block.x = ((innerPos.x + 16777216) / scaleFactor) - (16777216 / scaleFactor);
238 block.y = ((innerPos.y + 16777216) / scaleFactor) - (16777216 / scaleFactor);
239 blockOffset.x = ((innerPos.x + 16777216) % scaleFactor) * offsetSize;
240 blockOffset.y = ((innerPos.y + 16777216) % scaleFactor) * offsetSize;
241 }
242
[187]243 private void WriteMapInfo ()
244 {
245 JSONObject mapInfo = new JSONObject ();
246 mapInfo.Add ("blockSize", new JSONNumber (Constants.MAP_BLOCK_SIZE));
247 mapInfo.Add ("maxZoom", new JSONNumber (Constants.ZOOMLEVELS - 1));
248
249 Directory.CreateDirectory (Constants.MAP_DIRECTORY);
250 File.WriteAllText (Constants.MAP_DIRECTORY + "/mapinfo.json", mapInfo.ToString (), Encoding.UTF8);
251 }
252
[192]253 private bool LoadMapInfo ()
[187]254 {
255 if (File.Exists (Constants.MAP_DIRECTORY + "/mapinfo.json")) {
256 string json = File.ReadAllText (Constants.MAP_DIRECTORY + "/mapinfo.json", Encoding.UTF8);
[192]257 try {
258 JSONNode node = Parser.Parse (json);
259 if (node is JSONObject) {
260 JSONObject jo = (JSONObject)node;
261 if (jo.ContainsKey ("blockSize"))
262 Constants.MAP_BLOCK_SIZE = ((JSONNumber)jo ["blockSize"]).GetInt ();
263 if (jo.ContainsKey ("maxZoom"))
264 Constants.ZOOMLEVELS = ((JSONNumber)jo ["maxZoom"]).GetInt () + 1;
265 return true;
266 }
267 } catch (MalformedJSONException e) {
268 Log.Out ("Exception in LoadMapInfo: " + e);
269 } catch (InvalidCastException e) {
270 Log.Out ("Exception in LoadMapInfo: " + e);
[187]271 }
272 }
[192]273 return false;
[187]274 }
275
[143]276 private void getWorldExtent (RegionFileManager rfm, out Vector2i minChunk, out Vector2i maxChunk,
[130]277 out Vector2i minPos, out Vector2i maxPos,
278 out int widthChunks, out int heightChunks,
279 out int widthPix, out int heightPix)
280 {
[151]281 minChunk = default(Vector2i);
282 maxChunk = default(Vector2i);
283 minPos = default(Vector2i);
284 maxPos = default(Vector2i);
285
[130]286 long[] keys = rfm.GetAllChunkKeys ();
287 int minX = Int32.MaxValue;
288 int minY = Int32.MaxValue;
289 int maxX = Int32.MinValue;
290 int maxY = Int32.MinValue;
291 foreach (long key in keys) {
292 int x = WorldChunkCache.extractX (key);
293 int y = WorldChunkCache.extractZ (key);
294
295 if (x < minX)
296 minX = x;
297 if (x > maxX)
298 maxX = x;
299 if (y < minY)
300 minY = y;
301 if (y > maxY)
302 maxY = y;
303 }
304
305 minChunk.x = minX;
306 minChunk.y = minY;
307
308 maxChunk.x = maxX;
309 maxChunk.y = maxY;
310
311 minPos.x = minX * Constants.MAP_CHUNK_SIZE;
312 minPos.y = minY * Constants.MAP_CHUNK_SIZE;
313
314 maxPos.x = maxX * Constants.MAP_CHUNK_SIZE;
315 maxPos.y = maxY * Constants.MAP_CHUNK_SIZE;
316
317 widthChunks = maxX - minX + 1;
318 heightChunks = maxY - minY + 1;
319
320 widthPix = widthChunks * Constants.MAP_CHUNK_SIZE;
321 heightPix = heightChunks * Constants.MAP_CHUNK_SIZE;
322 }
323
[143]324 private static Color shortColorToColor (ushort col)
[130]325 {
326 return new Color (((float)(col >> 10 & 31) / 31f), ((float)(col >> 5 & 31) / 31f), ((float)(col & 31) / 31f), 255);
327 }
328
329 }
[143]330}
Note: See TracBrowser for help on using the repository browser.