Changeset 402 for binary-improvements2/MapRendering
- Timestamp:
- Jan 27, 2023, 7:28:00 PM (22 months ago)
- Location:
- binary-improvements2/MapRendering
- Files:
-
- 6 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/MapRendering/MapRendering.csproj
r392 r402 83 83 <Private>False</Private> 84 84 </Reference> 85 <Reference Include="Utf8Json, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"> 86 <HintPath>..\7dtd-binaries\Utf8Json.dll</HintPath> 87 <Private>False</Private> 88 </Reference> 85 89 </ItemGroup> 86 90 <ItemGroup> … … 88 92 <Compile Include="src\MapRenderBlockBuffer.cs" /> 89 93 <Compile Include="src\MapRenderer.cs" /> 94 <Compile Include="src\MapTileCache.cs" /> 90 95 <Compile Include="src\ModApi.cs" /> 91 96 <Compile Include="src\AssemblyInfo.cs" /> … … 95 100 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> 96 101 <ItemGroup> 97 <ProjectReference Include="..\7dtd-server-fixes\7dtd-server-fixes.csproj">98 <Project>{81DA7F87-1A66-4920-AADA-6EAF1971F8D0}</Project>99 <Name>7dtd-server-fixes</Name>100 <Private>False</Private>101 </ProjectReference>102 </ItemGroup>103 <ItemGroup>104 102 <None Include="ModInfo.xml"> 105 103 <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> 106 104 </None> 107 105 </ItemGroup> 106 <ItemGroup> 107 <ProjectReference Include="..\WebServer\WebServer.csproj"> 108 <Project>{01b5f981-b9fd-4364-8f9e-9127130d2542}</Project> 109 <Name>WebServer</Name> 110 <Private>False</Private> 111 </ProjectReference> 112 </ItemGroup> 108 113 </Project> -
binary-improvements2/MapRendering/ModInfo.xml
r391 r402 1 1 <?xml version="1.0" encoding="UTF-8" ?> 2 2 <xml> 3 <ModInfo> 4 <Name value="TFP_MapRendering" /> 5 <Description value="Render the game map to image map tiles as it is uncovered" /> 6 <Author value="The Fun Pimps LLC" /> 7 <Version value="1" /> 8 <Website value="" /> 9 </ModInfo> 3 <Name value="TFP_MapRendering" /> 4 <DisplayName value="Map Renderer" /> 5 <Description value="Render the game map to image map tiles as it is uncovered" /> 6 <Author value="The Fun Pimps LLC" /> 7 <Version value="21.0" /> 8 <Website value="" /> 10 9 </xml> -
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.