Changeset 143 for binary-improvements/7dtd-server-fixes
- Timestamp:
- Aug 30, 2014, 3:47:59 PM (10 years ago)
- Location:
- binary-improvements/7dtd-server-fixes/src
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/7dtd-server-fixes/src/MapRendering/Constants.cs
r130 r143 7 7 public const int MAP_BLOCK_SIZE = 128; 8 8 public const int MAP_CHUNK_SIZE = 16; 9 public const int MAP_REGION_SIZE = 512; 9 10 public const int MAP_BLOCK_TO_CHUNK_DIV = MAP_BLOCK_SIZE / MAP_CHUNK_SIZE; 11 public const int MAP_REGION_TO_CHUNK_DIV = MAP_REGION_SIZE / MAP_CHUNK_SIZE; 10 12 public const int ZOOMLEVELS = 5; 11 13 public static string MAP_DIRECTORY = string.Empty; -
binary-improvements/7dtd-server-fixes/src/MapRendering/MapRenderBlockBuffer.cs
r142 r143 29 29 } 30 30 31 public voidLoadBlock (Vector2i block)31 public bool LoadBlock (Vector2i block) 32 32 { 33 bool res = false; 33 34 Monitor.Enter (blockMap); 34 35 try { … … 37 38 Directory.CreateDirectory (folder); 38 39 if (!fileName.Equals (currentBlockMap)) { 40 res = true; 39 41 SaveBlock(); 40 42 loadTextureFromFile (fileName); … … 44 46 Monitor.Exit (blockMap); 45 47 } 48 return res; 46 49 } 47 50 -
binary-improvements/7dtd-server-fixes/src/MapRendering/MapRendering.cs
r140 r143 20 20 } 21 21 22 private RegionFileManager rfm;23 private Texture2D fullMapTexture = null;24 22 private MapRenderBlockBuffer[] zoomLevelBuffers = new MapRenderBlockBuffer[Constants.ZOOMLEVELS]; 25 private Color[] chunkColors = new Color[Constants.MAP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE];23 private Dictionary<Vector2i, Color[]> dirtyChunks = new Dictionary<Vector2i, Color[]> (); 26 24 private System.Timers.Timer chunkSaveTimer = new System.Timers.Timer (500); 27 25 private bool renderingFullMap = false; 28 26 public static bool renderingEnabled = true; 27 private MicroStopwatch msw = new MicroStopwatch (); 29 28 30 29 private MapRendering () … … 37 36 38 37 chunkSaveTimer.AutoReset = false; 39 chunkSaveTimer.Elapsed += new System.Timers.ElapsedEventHandler ( SaveAllBlockMaps);38 chunkSaveTimer.Elapsed += new System.Timers.ElapsedEventHandler (RenderDirtyChunks); 40 39 } 41 40 … … 47 46 try { 48 47 if (!Instance.renderingFullMap) { 49 Chunk c = (Chunk)o; 50 Vector3i cPos = c.GetWorldPos (); 51 Vector2i cPos2 = new Vector2i (cPos.x / Constants.MAP_CHUNK_SIZE, cPos.z / Constants.MAP_CHUNK_SIZE); 52 Instance.RenderChunk (c, cPos2); 53 Instance.chunkSaveTimer.Stop (); 54 Instance.chunkSaveTimer.Start (); 48 Monitor.Enter (Instance.zoomLevelBuffers); 49 try { 50 Chunk c = (Chunk)o; 51 Vector3i cPos = c.GetWorldPos (); 52 Vector2i cPos2 = new Vector2i (cPos.x / Constants.MAP_CHUNK_SIZE, cPos.z / Constants.MAP_CHUNK_SIZE); 53 54 ushort[] mapColors = c.GetMapColors (); 55 if (mapColors != null) { 56 Color[] realColors = new Color[Constants.MAP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE]; 57 for (int i_colors = 0; i_colors < mapColors.Length; i_colors++) { 58 realColors [i_colors] = shortColorToColor (mapColors [i_colors]); 59 } 60 Instance.dirtyChunks [cPos2] = realColors; 61 Log.Out ("Add Dirty: " + cPos2); 62 Instance.chunkSaveTimer.Stop (); 63 Instance.chunkSaveTimer.Start (); 64 } 65 } finally { 66 Monitor.Exit (Instance.zoomLevelBuffers); 67 } 55 68 } 56 69 } catch (Exception e) { … … 66 79 67 80 string regionSaveDir = StaticDirectories.GetSaveGameRegionDir (); 68 rfm = new RegionFileManager (regionSaveDir, regionSaveDir, 1, false); 81 RegionFileManager rfm = new RegionFileManager (regionSaveDir, regionSaveDir, 0, false); 82 Texture2D fullMapTexture = null; 69 83 70 84 if (Directory.Exists (Constants.MAP_DIRECTORY)) … … 76 90 Vector2i minPos, maxPos; 77 91 int widthChunks, heightChunks, widthPix, heightPix; 78 getWorldExtent ( out minChunk, out maxChunk, out minPos, out maxPos, out widthChunks, out heightChunks, out widthPix, out heightPix);92 getWorldExtent (rfm, out minChunk, out maxChunk, out minPos, out maxPos, out widthChunks, out heightChunks, out widthPix, out heightPix); 79 93 80 94 Log.Out (String.Format ("RenderMap: min: {0}, max: {1}, minPos: {2}, maxPos: {3}, w/h: {4}/{5}, wP/hP: {6}/{7}", … … 88 102 fullMapTexture = new Texture2D (widthPix, heightPix); 89 103 90 Vector2i curFullMapPos; 91 Vector2i curChunkPos; 92 for (curFullMapPos.x = 0; curFullMapPos.x < widthPix; curFullMapPos.x += Constants.MAP_CHUNK_SIZE) { 93 for (curFullMapPos.y = 0; curFullMapPos.y < heightPix; curFullMapPos.y += Constants.MAP_CHUNK_SIZE) { 94 curChunkPos.x = (curFullMapPos.x / Constants.MAP_CHUNK_SIZE) + minChunk.x; 95 curChunkPos.y = (curFullMapPos.y / Constants.MAP_CHUNK_SIZE) + minChunk.y; 96 97 RenderChunk (curChunkPos, curFullMapPos); 104 Monitor.Enter (Instance.zoomLevelBuffers); 105 try { 106 Vector2i curFullMapPos; 107 Vector2i curChunkPos; 108 for (curFullMapPos.x = 0; curFullMapPos.x < widthPix; curFullMapPos.x += Constants.MAP_CHUNK_SIZE) { 109 for (curFullMapPos.y = 0; curFullMapPos.y < heightPix; curFullMapPos.y += Constants.MAP_CHUNK_SIZE) { 110 curChunkPos.x = (curFullMapPos.x / Constants.MAP_CHUNK_SIZE) + minChunk.x; 111 curChunkPos.y = (curFullMapPos.y / Constants.MAP_CHUNK_SIZE) + minChunk.y; 112 113 try { 114 long chunkKey = WorldChunkCache.MakeChunkKey (curChunkPos.x, curChunkPos.y); 115 if (rfm.ContainsChunkSync (chunkKey)) { 116 Chunk c = rfm.GetChunkSync (chunkKey); 117 ushort[] mapColors = c.GetMapColors (); 118 if (mapColors != null) { 119 Color[] realColors = new Color[Constants.MAP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE]; 120 for (int i_colors = 0; i_colors < mapColors.Length; i_colors++) { 121 realColors [i_colors] = shortColorToColor (mapColors [i_colors]); 122 } 123 dirtyChunks [curChunkPos] = realColors; 124 if (fullMapTexture != null) 125 fullMapTexture.SetPixels (curFullMapPos.x, curFullMapPos.y, Constants.MAP_CHUNK_SIZE, Constants.MAP_CHUNK_SIZE, realColors); 126 } 127 } 128 } catch (Exception e) { 129 Log.Out ("Exception: " + e); 130 } 131 } 132 Log.Out (String.Format ("RenderMap: {0}/{1} ({2}%)", curFullMapPos.x, widthPix, (int)((float)curFullMapPos.x / widthPix * 100))); 98 133 } 99 Log.Out (String.Format ("RenderMap: {0}/{1} ({2}%)", curFullMapPos.x, widthPix, (int)((float)curFullMapPos.x / widthPix * 100))); 100 } 101 SaveAllBlockMaps (null, null); 102 103 rfm = null; 134 } finally { 135 Monitor.Exit (Instance.zoomLevelBuffers); 136 } 137 int totalDirtyCount = dirtyChunks.Count; 138 Log.Out (String.Format ("Rendering chunks: {0}/{1} ({2}%)", totalDirtyCount - dirtyChunks.Count, totalDirtyCount, (int)((float)(totalDirtyCount - dirtyChunks.Count) / totalDirtyCount * 100))); 139 while (dirtyChunks.Count > 0) { 140 RenderDirtyChunks (null, null); 141 Log.Out (String.Format ("Rendering chunks: {0}/{1} ({2}%)", totalDirtyCount - dirtyChunks.Count, totalDirtyCount, (int)((float)(totalDirtyCount - dirtyChunks.Count) / totalDirtyCount * 100))); 142 } 104 143 105 144 if (fullMapTexture != null) { … … 116 155 } 117 156 118 private int saveCount = 0;119 private long renderCount = 0;120 121 157 private void SaveAllBlockMaps (object source, System.Timers.ElapsedEventArgs e) 158 { 159 for (int i = 0; i < Constants.ZOOMLEVELS; i++) { 160 zoomLevelBuffers [i].SaveBlock (); 161 } 162 } 163 164 private void RenderDirtyChunks (object source, System.Timers.ElapsedEventArgs e) 122 165 { 123 166 Monitor.Enter (zoomLevelBuffers); 124 167 try { 125 Log.Out ("------- SaveAllBlockMaps " + ++saveCount + " - " + renderCount); 126 for (int i = 0; i < Constants.ZOOMLEVELS; i++) { 127 zoomLevelBuffers [i].SaveBlock (); 128 } 129 } finally { 130 Monitor.Exit (zoomLevelBuffers); 131 } 132 } 133 134 private bool RenderChunk (Vector2i chunkPos, Vector2i fullMapPos = default(Vector2i)) 135 { 136 long chunkKey = WorldChunkCache.MakeChunkKey (chunkPos.x, chunkPos.y); 137 if (rfm.ContainsChunkSync (chunkKey)) { 138 Chunk chunk = rfm.GetChunkSync (chunkKey); 139 return RenderChunk (chunk, chunkPos, fullMapPos); 140 } 141 return false; 142 } 143 144 private bool RenderChunk (Chunk chunk, Vector2i chunkPos, Vector2i fullMapPos = default(Vector2i)) 145 { 146 Monitor.Enter (zoomLevelBuffers); 147 try { 148 renderCount++; 149 ushort[] mapColors = chunk.GetMapColors (); 150 if (mapColors != null) { 168 msw.ResetAndRestart (); 169 170 if (dirtyChunks.Count > 0) { 171 List<Vector2i> keys = new List<Vector2i> (dirtyChunks.Keys); 172 List<Vector2i> chunksDone = new List<Vector2i> (); 173 174 Vector2i chunkPos = keys [0]; 175 chunksDone.Add (chunkPos); 176 177 //Log.Out ("Start Dirty: " + chunkPos); 178 151 179 Vector2i block, blockOffset; 152 180 getBlockNumber (chunkPos, out block, out blockOffset, Constants.MAP_BLOCK_TO_CHUNK_DIV, Constants.MAP_CHUNK_SIZE); 153 181 154 for (int i_colors = 0; i_colors < mapColors.Length; i_colors++) { 155 chunkColors [i_colors] = shortColorToColor (mapColors [i_colors]); 182 zoomLevelBuffers [Constants.ZOOMLEVELS - 1].LoadBlock (block); 183 184 Vector2i v_block, v_blockOffset; 185 foreach (Vector2i v in keys) { 186 getBlockNumber (v, out v_block, out v_blockOffset, Constants.MAP_BLOCK_TO_CHUNK_DIV, Constants.MAP_CHUNK_SIZE); 187 if (v_block.Equals (block)) { 188 //Log.Out ("Dirty: " + v + " render: true"); 189 chunksDone.Add (v); 190 zoomLevelBuffers [Constants.ZOOMLEVELS - 1].SetPart (v_blockOffset, Constants.MAP_CHUNK_SIZE, dirtyChunks [v]); 191 } else { 192 //Log.Out ("Dirty: " + v + " render: false"); 193 } 156 194 } 157 195 158 zoomLevelBuffers [Constants.ZOOMLEVELS - 1].LoadBlock (block); 159 zoomLevelBuffers [Constants.ZOOMLEVELS - 1].SetPart (blockOffset, Constants.MAP_CHUNK_SIZE, chunkColors); 160 161 if (renderingFullMap && fullMapTexture != null) 162 fullMapTexture.SetPixels (fullMapPos.x, fullMapPos.y, Constants.MAP_CHUNK_SIZE, Constants.MAP_CHUNK_SIZE, chunkColors); 196 foreach (Vector2i v in chunksDone) 197 dirtyChunks.Remove (v); 163 198 164 199 RenderZoomLevel (Constants.ZOOMLEVELS - 1, block); 165 200 166 return true;201 SaveAllBlockMaps (null, null); 167 202 } 168 return false; 203 204 if (e != null) 205 if (dirtyChunks.Count > 0) 206 Instance.chunkSaveTimer.Start (); 169 207 } finally { 170 208 Monitor.Exit (zoomLevelBuffers); … … 193 231 } 194 232 195 private void getWorldExtent ( out Vector2i minChunk, out Vector2i maxChunk,233 private void getWorldExtent (RegionFileManager rfm, out Vector2i minChunk, out Vector2i maxChunk, 196 234 out Vector2i minPos, out Vector2i maxPos, 197 235 out int widthChunks, out int heightChunks, … … 236 274 } 237 275 238 private Color32 shortColorToColor32 (ushort col) 239 { 240 return new Color32 ((byte)((float)(col >> 10 & 31) / 31f * 255f), (byte)((float)(col >> 5 & 31) / 31f * 255f), (byte)((float)(col & 31) / 31f * 255f), 255); 241 } 242 243 private Color shortColorToColor (ushort col) 276 private static Color shortColorToColor (ushort col) 244 277 { 245 278 return new Color (((float)(col >> 10 & 31) / 31f), ((float)(col >> 5 & 31) / 31f), ((float)(col & 31) / 31f), 255); -
binary-improvements/7dtd-server-fixes/src/PersistentData/KnownPlayers.cs
r142 r143 4 4 namespace AllocsFixes.PersistentData 5 5 { 6 [Serializable ()]7 public class KnownPlayers : ISerializable6 [Serializable] 7 public class KnownPlayers 8 8 { 9 9 private int entityId; … … 16 16 public KnownPlayers (SerializationInfo info, StreamingContext ctxt) 17 17 { 18 this.entityId = info.GetInt32 ("entityId");19 this.name = info.GetString ("name");18 this.entityId = info.GetInt32 ("entityId"); 19 this.name = info.GetString ("name"); 20 20 } 21 22 public void GetObjectData (SerializationInfo info, StreamingContext context) 23 { 24 info.AddValue("name", this.name); 25 } 26 } 21 } 27 22 } 28 23 -
binary-improvements/7dtd-server-fixes/src/PersistentData/PersistentContainer.cs
r142 r143 7 7 namespace AllocsFixes.PersistentData 8 8 { 9 [Serializable ()]10 public class PersistentContainer : ISerializable9 [Serializable] 10 public class PersistentContainer 11 11 { 12 12 public Dictionary<string, KnownPlayers> players = new Dictionary<string, KnownPlayers> (); … … 49 49 } 50 50 51 public PersistentContainer (SerializationInfo info, StreamingContext ctxt)52 {53 this.players = (Dictionary<string, KnownPlayers>)info.GetValue ("players", typeof(Dictionary<string, KnownPlayers>));54 }55 56 public void GetObjectData (SerializationInfo info, StreamingContext context)57 {58 info.AddValue ("players", this.players);59 }60 61 51 } 62 52 }
Note:
See TracChangeset
for help on using the changeset viewer.