Changeset 402 for binary-improvements2/MapRendering/src/MapRenderer.cs
- Timestamp:
- Jan 27, 2023, 7:28:00 PM (22 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/MapRendering/src/MapRenderer.cs
r391 r402 5 5 using System.Text; 6 6 using System.Threading; 7 using AllocsFixes.FileCache;8 using AllocsFixes.JSON;9 7 using UnityEngine; 10 8 using UnityEngine.Profiling; 9 using Utf8Json; 10 using Webserver.FileCache; 11 11 using Object = UnityEngine.Object; 12 12 … … 27 27 28 28 private MapRenderer () { 29 Constants.MapDirectory = GameIO.GetSaveGameDir () + "/map"; 30 31 lock (lockObject) { 32 if (!LoadMapInfo ()) { 33 WriteMapInfo (); 34 } 29 Constants.MapDirectory = $"{GameIO.GetSaveGameDir ()}/map"; 30 31 if (!LoadMapInfo ()) { 32 WriteMapInfo (); 35 33 } 36 34 … … 47 45 public static MapRenderer Instance => instance ??= new MapRenderer (); 48 46 49 public static MapTileCache GetTileCache () {47 public static AbstractCache GetTileCache () { 50 48 return Instance.cache; 51 49 } … … 95 93 } 96 94 } catch (Exception e) { 97 Log.Out ( "Exception in MapRendering.RenderSingleChunk(): " + e);95 Log.Out ($"Exception in MapRendering.RenderSingleChunk(): {e}"); 98 96 } 99 97 }, _chunk); … … 161 159 } 162 160 } catch (Exception e) { 163 Log.Out ( "Exception: " + e);161 Log.Out ($"Exception: {e}"); 164 162 } 165 163 } … … 177 175 if (fullMapTexture != null) { 178 176 byte[] array = fullMapTexture.EncodeToPNG (); 179 File.WriteAllBytes ( Constants.MapDirectory + "/map.png", array);177 File.WriteAllBytes ($"{Constants.MapDirectory}/map.png", array); 180 178 Object.Destroy (fullMapTexture); 181 179 } … … 183 181 renderingFullMap = false; 184 182 185 Log.Out ( "Generating map took: " + microStopwatch.ElapsedMilliseconds + "ms");186 Log.Out ( "World extent: " + minPos + " - " + maxPos);183 Log.Out ($"Generating map took: {microStopwatch.ElapsedMilliseconds} ms"); 184 Log.Out ($"World extent: {minPos} - {maxPos}"); 187 185 } 188 186 … … 307 305 308 306 private void WriteMapInfo () { 309 JsonObject mapInfo = new JsonObject (); 310 mapInfo.Add ("blockSize", new JsonNumber (Constants.MapBlockSize)); 311 mapInfo.Add ("maxZoom", new JsonNumber (Constants.Zoomlevels - 1)); 307 JsonWriter writer = new JsonWriter (); 308 writer.WriteBeginObject (); 309 310 writer.WriteString ("blockSize"); 311 writer.WriteNameSeparator (); 312 writer.WriteInt32 (Constants.MapBlockSize); 313 314 writer.WriteValueSeparator (); 315 writer.WriteString ("maxZoom"); 316 writer.WriteNameSeparator (); 317 writer.WriteInt32 (Constants.Zoomlevels - 1); 318 319 writer.WriteEndObject (); 312 320 313 321 Directory.CreateDirectory (Constants.MapDirectory); 314 File.WriteAll Text (Constants.MapDirectory + "/mapinfo.json", mapInfo.ToString (), Encoding.UTF8);322 File.WriteAllBytes ($"{Constants.MapDirectory}/mapinfo.json", writer.ToUtf8ByteArray ()); 315 323 } 316 324 317 325 private bool LoadMapInfo () { 318 if (!File.Exists ( Constants.MapDirectory + "/mapinfo.json")) {326 if (!File.Exists ($"{Constants.MapDirectory}/mapinfo.json")) { 319 327 return false; 320 328 } 321 329 322 string json = File.ReadAllText ( Constants.MapDirectory + "/mapinfo.json", Encoding.UTF8);330 string json = File.ReadAllText ($"{Constants.MapDirectory}/mapinfo.json", Encoding.UTF8); 323 331 try { 324 JsonNode node = Parser.Parse (json); 325 if (node is JsonObject jo) { 326 if (jo.ContainsKey ("blockSize")) { 327 Constants.MapBlockSize = ((JsonNumber) jo ["blockSize"]).GetInt (); 328 } 329 330 if (jo.ContainsKey ("maxZoom")) { 331 Constants.Zoomlevels = ((JsonNumber) jo ["maxZoom"]).GetInt () + 1; 332 } 333 334 return true; 335 } 336 } catch (MalformedJsonException e) { 337 Log.Out ("Exception in LoadMapInfo: " + e); 338 } catch (InvalidCastException e) { 339 Log.Out ("Exception in LoadMapInfo: " + e); 332 IDictionary<string,object> inputJson = JsonSerializer.Deserialize<IDictionary<string, object>> (json); 333 334 if (inputJson.TryGetValue ("blockSize", out object fieldNode) && fieldNode is double value) { 335 Constants.MapBlockSize = (int)value; 336 } 337 338 if (inputJson.TryGetValue ("maxZoom", out fieldNode) && fieldNode is double value2) { 339 Constants.Zoomlevels = (int)value2 + 1; 340 } 341 342 return true; 343 } catch (Exception e) { 344 Log.Out ($"Exception in LoadMapInfo: {e}"); 340 345 } 341 346
Note:
See TracChangeset
for help on using the changeset viewer.