Changeset 402 for binary-improvements2/MapRendering/src
- Timestamp:
- Jan 27, 2023, 7:28:00 PM (22 months ago)
- Location:
- binary-improvements2/MapRendering/src
- Files:
-
- 4 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/MapRendering/src/Commands/EnableRendering.cs
r391 r402 15 15 public override void Execute (List<string> _params, CommandSenderInfo _senderInfo) { 16 16 if (_params.Count != 1) { 17 SdtdConsole.Instance.Output ( "Current state: " + MapRenderer.renderingEnabled);17 SdtdConsole.Instance.Output ($"Current state: {MapRenderer.renderingEnabled}"); 18 18 return; 19 19 } 20 20 21 21 MapRenderer.renderingEnabled = _params [0].Equals ("1"); 22 SdtdConsole.Instance.Output ( "Set live map rendering to " + _params [0].Equals ("1"));22 SdtdConsole.Instance.Output ($"Set live map rendering to {_params [0].Equals ("1")}"); 23 23 } 24 24 } -
binary-improvements2/MapRendering/src/MapRenderBlockBuffer.cs
r391 r402 1 1 using System; 2 2 using System.IO; 3 using AllocsFixes.FileCache;4 3 using Unity.Collections; 5 4 using UnityEngine; … … 21 20 zoomLevel = _level; 22 21 cache = _cache; 23 folderBase = Constants.MapDirectory + "/" + zoomLevel + "/";22 folderBase = $"{Constants.MapDirectory}/{zoomLevel}/"; 24 23 25 24 { … … 52 51 saveTextureToFile (); 53 52 } catch (Exception e) { 54 Log.Warning ( "Exception in MapRenderBlockBuffer.SaveBlock(): " + e);53 Log.Warning ($"Exception in MapRenderBlockBuffer.SaveBlock(): {e}"); 55 54 } 56 55 Profiler.EndSample (); … … 64 63 string folder; 65 64 if (currentBlockMapPos.x != _block.x) { 66 folder = folderBase + _block.x + '/';65 folder = $"{folderBase}{_block.x}/"; 67 66 68 67 Profiler.BeginSample ("LoadBlock.Directory"); … … 73 72 } 74 73 75 string fileName = folder + _block.y + ".png";74 string fileName = $"{folder}{_block.y}.png"; 76 75 Profiler.EndSample (); 77 76 … … 105 104 public Color32[] GetHalfScaled () { 106 105 Profiler.BeginSample ("HalfScaled.ResizeBuffer"); 107 zoomBuffer.Re size (Constants.MapBlockSize, Constants.MapBlockSize);106 zoomBuffer.Reinitialize (Constants.MapBlockSize, Constants.MapBlockSize); 108 107 Profiler.EndSample (); 109 108 … … 156 155 Profiler.BeginSample ("HalfScaledNative.ResizeBuffer"); 157 156 if (zoomBuffer.format != blockMap.format || zoomBuffer.height != Constants.MapBlockSize / 2 || zoomBuffer.width != Constants.MapBlockSize / 2) { 158 zoomBuffer.Re size (Constants.MapBlockSize / 2, Constants.MapBlockSize / 2, blockMap.format, false);157 zoomBuffer.Reinitialize (Constants.MapBlockSize / 2, Constants.MapBlockSize / 2, blockMap.format, false); 159 158 } 160 159 Profiler.EndSample (); … … 206 205 207 206 if (array != null) { 208 Log.Error ( "Map image tile " + _fileName + "has been corrupted, recreating tile");207 Log.Error ($"Map image tile {_fileName} has been corrupted, recreating tile"); 209 208 } 210 209 211 210 if (blockMap.format != Constants.DefaultTextureFormat || blockMap.height != Constants.MapBlockSize || 212 211 blockMap.width != Constants.MapBlockSize) { 213 blockMap.Re size (Constants.MapBlockSize, Constants.MapBlockSize, Constants.DefaultTextureFormat,212 blockMap.Reinitialize (Constants.MapBlockSize, Constants.MapBlockSize, Constants.DefaultTextureFormat, 214 213 false); 215 214 } -
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 -
binary-improvements2/MapRendering/src/MapTileCache.cs
r399 r402 1 1 using System; 2 using System.Diagnostics.CodeAnalysis; 2 3 using System.IO; 3 4 using UnityEngine; 4 5 using UnityEngine.Profiling; 6 using Webserver.FileCache; 5 7 using Object = UnityEngine.Object; 6 8 7 namespace AllocsFixes.FileCache{9 namespace MapRendering { 8 10 // Special "cache" for map tile folder as both map rendering and webserver access files in there. 9 11 // Only map rendering tiles are cached. Writing is done by WriteThrough. … … 25 27 } 26 28 29 // SetZoomCount only called before processing happens in MapRenderer.ctor, no locking required 30 [SuppressMessage ("ReSharper", "InconsistentlySynchronizedField")] 27 31 public void SetZoomCount (int _count) { 28 32 cache = new CurrentZoomFile[_count]; … … 55 59 } 56 60 } catch (Exception e) { 57 Log.Warning ( "Error in MapTileCache.LoadTile: " + e);61 Log.Warning ($"Error in MapTileCache.LoadTile: {e}"); 58 62 } 59 63 … … 81 85 } 82 86 } catch (Exception e) { 83 Log.Warning ( "Error in MapTileCache.SaveTile: " + e);87 Log.Warning ($"Error in MapTileCache.SaveTile: {e}"); 84 88 } 85 89 } … … 92 96 } 93 97 } catch (Exception e) { 94 Log.Warning ( "Error in MapTileCache.ResetTile: " + e);98 Log.Warning ($"Error in MapTileCache.ResetTile: {e}"); 95 99 } 96 100 } … … 108 112 } 109 113 } catch (Exception e) { 110 Log.Warning ( "Error in MapTileCache.GetFileContent: " + e);114 Log.Warning ($"Error in MapTileCache.GetFileContent: {e}"); 111 115 } 112 116 … … 119 123 120 124 private static byte[] ReadAllBytes (string _path) { 121 using (FileStream fileStream = new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096)) { 122 int bytesRead = 0; 123 int bytesLeft = (int) fileStream.Length; 124 byte[] result = new byte[bytesLeft]; 125 while (bytesLeft > 0) { 126 int readThisTime = fileStream.Read (result, bytesRead, bytesLeft); 127 if (readThisTime == 0) { 128 throw new IOException ("Unexpected end of stream"); 129 } 130 131 bytesRead += readThisTime; 132 bytesLeft -= readThisTime; 125 using FileStream fileStream = new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096); 126 127 int bytesRead = 0; 128 int bytesLeft = (int) fileStream.Length; 129 byte[] result = new byte[bytesLeft]; 130 while (bytesLeft > 0) { 131 int readThisTime = fileStream.Read (result, bytesRead, bytesLeft); 132 if (readThisTime == 0) { 133 throw new IOException ("Unexpected end of stream"); 133 134 } 134 135 135 return result; 136 bytesRead += readThisTime; 137 bytesLeft -= readThisTime; 136 138 } 139 140 return result; 137 141 } 138 142 -
binary-improvements2/MapRendering/src/ModApi.cs
r391 r402 1 1 using JetBrains.Annotations; 2 using Webserver; 3 using Webserver.UrlHandlers; 2 4 3 5 namespace MapRendering { … … 7 9 ModEvents.GameShutdown.RegisterHandler (GameShutdown); 8 10 ModEvents.CalcChunkColorsDone.RegisterHandler (CalcChunkColorsDone); 11 12 Web.ServerInitialized += _web => { 13 _web.RegisterPathHandler ("/map/", new StaticHandler ( 14 $"{GameIO.GetSaveGameDir ()}/map", 15 MapRenderer.GetTileCache (), 16 false, 17 "web.map") 18 ); 19 }; 9 20 } 10 21
Note:
See TracChangeset
for help on using the changeset viewer.