Changeset 187 for binary-improvements
- Timestamp:
- Sep 10, 2014, 8:09:28 PM (10 years ago)
- Location:
- binary-improvements
- Files:
-
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/7dtd-server-fixes/7dtd-server-fixes.csproj
r182 r187 120 120 <Compile Include="src\NetConnections\Servers\Web\API\GetLandClaims.cs" /> 121 121 <Compile Include="src\BlockingQueue.cs" /> 122 <Compile Include="src\JSON\Parser.cs" /> 123 <Compile Include="src\JSON\JSONNull.cs" /> 124 <Compile Include="src\JSON\MalformedJSONException.cs" /> 122 125 </ItemGroup> 123 126 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> … … 132 135 <Folder Include="src\PersistentData\" /> 133 136 <Folder Include="src\NetConnections\Servers\Web\API\" /> 137 <Folder Include="src\JSON\Parser\" /> 134 138 </ItemGroup> 135 139 </Project> -
binary-improvements/7dtd-server-fixes/src/JSON/JSONArray.cs
r154 r187 9 9 private List<JSONNode> nodes = new List<JSONNode> (); 10 10 11 public JSONNode this [int index] { 12 get { return nodes [index]; } 13 set { nodes [index] = value; } 14 } 15 16 public int Count { 17 get { return nodes.Count; } 18 } 19 11 20 public void Add (JSONNode node) 12 21 { 13 nodes.Add (node);22 nodes.Add (node); 14 23 } 15 24 16 public override string ToString ( )25 public override string ToString (bool prettyPrint = false, int currentLevel = 0) 17 26 { 18 27 StringBuilder sb = new StringBuilder ("["); 28 if (prettyPrint) 29 sb.Append ('\n'); 19 30 foreach (JSONNode n in nodes) { 20 sb.Append (n.ToString ()); 31 if (prettyPrint) 32 sb.Append (new String ('\t', currentLevel + 1)); 33 sb.Append (n.ToString (prettyPrint, currentLevel + 1)); 21 34 sb.Append (","); 35 if (prettyPrint) 36 sb.Append ('\n'); 22 37 } 23 38 if (sb.Length > 1) 24 sb.Remove (sb.Length - 1, 1); 39 sb.Remove (sb.Length - (prettyPrint ? 2 : 1), 1); 40 if (prettyPrint) 41 sb.Append (new String ('\t', currentLevel)); 25 42 sb.Append ("]"); 26 43 return sb.ToString (); 44 } 45 46 public static JSONArray Parse (string json, ref int offset) 47 { 48 //Log.Out ("ParseArray enter (" + offset + ")"); 49 JSONArray arr = new JSONArray (); 50 51 bool nextElemAllowed = true; 52 offset++; 53 while (true) { 54 Parser.SkipWhitespace (json, ref offset); 55 56 switch (json [offset]) { 57 case ',': 58 if (!nextElemAllowed) { 59 nextElemAllowed = true; 60 offset++; 61 } else 62 throw new MalformedJSONException ("Could not parse array, found a comma without a value first"); 63 break; 64 case ']': 65 offset++; 66 //Log.Out ("JSON:Parsed Array: " + arr.ToString ()); 67 return arr; 68 default: 69 arr.Add (Parser.ParseInternal (json, ref offset)); 70 nextElemAllowed = false; 71 break; 72 } 73 } 27 74 } 28 75 -
binary-improvements/7dtd-server-fixes/src/JSON/JSONBoolean.cs
r154 r187 12 12 } 13 13 14 public override string ToString () 14 public bool GetBool () 15 { 16 return value; 17 } 18 19 public override string ToString (bool prettyPrint = false, int currentLevel = 0) 15 20 { 16 21 return value.ToString (System.Globalization.CultureInfo.InvariantCulture).ToLower (); 22 } 23 24 public static JSONBoolean Parse (string json, ref int offset) 25 { 26 //Log.Out ("ParseBool enter (" + offset + ")"); 27 28 if (json.Substring (offset, 4).Equals ("true")) { 29 //Log.Out ("JSON:Parsed Bool: true"); 30 offset += 4; 31 return new JSONBoolean (true); 32 } else if (json.Substring (offset, 5).Equals ("false")) { 33 //Log.Out ("JSON:Parsed Bool: false"); 34 offset += 5; 35 return new JSONBoolean (false); 36 } else { 37 throw new MalformedJSONException ("No valid boolean found"); 38 } 17 39 } 18 40 -
binary-improvements/7dtd-server-fixes/src/JSON/JSONNode.cs
r160 r187 5 5 public abstract class JSONNode 6 6 { 7 public abstract override string ToString();7 public abstract string ToString(bool prettyPrint = false, int currentLevel = 0); 8 8 } 9 9 } -
binary-improvements/7dtd-server-fixes/src/JSON/JSONNumber.cs
r154 r187 1 1 using System; 2 using System.Text; 2 3 3 4 namespace AllocsFixes.JSON … … 5 6 public class JSONNumber : JSONNode 6 7 { 7 private floatvalue;8 private double value; 8 9 9 public JSONNumber ( floatvalue)10 public JSONNumber (double value) 10 11 { 11 12 this.value = value; 12 13 } 13 14 14 public override string ToString () 15 public double GetDouble () 16 { 17 return value; 18 } 19 20 public int GetInt () 21 { 22 return (int)Math.Round(value); 23 } 24 25 public override string ToString (bool prettyPrint = false, int currentLevel = 0) 15 26 { 16 27 return value.ToString (System.Globalization.CultureInfo.InvariantCulture); 28 } 29 30 public static JSONNumber Parse (string json, ref int offset) 31 { 32 //Log.Out ("ParseNumber enter (" + offset + ")"); 33 StringBuilder sbNum = new StringBuilder (); 34 StringBuilder sbExp = null; 35 bool hasDec = false; 36 bool hasExp = false; 37 while (offset < json.Length) { 38 if (json [offset] >= '0' && json [offset] <= '9') { 39 if (hasExp) 40 sbExp.Append (json [offset]); 41 else 42 sbNum.Append (json [offset]); 43 } else if (json [offset] == '.') { 44 if (hasExp) { 45 throw new MalformedJSONException ("Decimal separator in exponent"); 46 } else { 47 if (hasDec) 48 throw new MalformedJSONException ("Multiple decimal separators in number found"); 49 else if (sbNum.Length == 0) { 50 throw new MalformedJSONException ("No leading digits before decimal separator found"); 51 } else { 52 sbNum.Append ('.'); 53 hasDec = true; 54 } 55 } 56 } else if (json [offset] == '-') { 57 if (hasExp) { 58 if (sbExp.Length > 0) 59 throw new MalformedJSONException ("Negative sign in exponent after digits"); 60 else 61 sbExp.Append (json [offset]); 62 } else { 63 if (sbNum.Length > 0) 64 throw new MalformedJSONException ("Negative sign in mantissa after digits"); 65 else 66 sbNum.Append (json [offset]); 67 } 68 } else if (json [offset] == 'e' || json [offset] == 'E') { 69 if (hasExp) 70 throw new MalformedJSONException ("Multiple exponential markers in number found"); 71 else if (sbNum.Length == 0) { 72 throw new MalformedJSONException ("No leading digits before exponential marker found"); 73 } else { 74 sbExp = new StringBuilder (); 75 hasExp = true; 76 } 77 } else if (json [offset] == '+') { 78 if (hasExp) { 79 if (sbExp.Length > 0) 80 throw new MalformedJSONException ("Positive sign in exponent after digits"); 81 else 82 sbExp.Append (json [offset]); 83 } else { 84 throw new MalformedJSONException ("Positive sign in mantissa found"); 85 } 86 } else { 87 double number; 88 if (!double.TryParse (sbNum.ToString (), out number)) { 89 throw new MalformedJSONException ("Mantissa is not a valid decimal (\"" + sbNum.ToString () + "\")"); 90 } 91 92 if (hasExp) { 93 int exp; 94 if (!int.TryParse (sbExp.ToString (), out exp)) { 95 throw new MalformedJSONException ("Exponent is not a valid integer (\"" + sbExp.ToString () + "\")"); 96 } 97 98 number = number * Math.Pow (10, exp); 99 } 100 101 //Log.Out ("JSON:Parsed Number: " + number.ToString ()); 102 return new JSONNumber (number); 103 } 104 offset++; 105 } 106 throw new MalformedJSONException ("End of JSON reached before parsing number finished"); 17 107 } 18 108 -
binary-improvements/7dtd-server-fixes/src/JSON/JSONObject.cs
r154 r187 9 9 private Dictionary<string, JSONNode> nodes = new Dictionary<string, JSONNode> (); 10 10 11 public JSONNode this [string name] { 12 get { return nodes [name]; } 13 set { nodes [name] = value; } 14 } 15 16 public int Count { 17 get { return nodes.Count; } 18 } 19 20 public List<string> Keys { 21 get { return new List<string> (nodes.Keys); } 22 } 23 24 public bool ContainsKey (string name) 25 { 26 return nodes.ContainsKey (name); 27 } 28 11 29 public void Add (string name, JSONNode node) 12 30 { … … 14 32 } 15 33 16 public override string ToString ( )34 public override string ToString (bool prettyPrint = false, int currentLevel = 0) 17 35 { 18 36 StringBuilder sb = new StringBuilder ("{"); 37 if (prettyPrint) 38 sb.Append ('\n'); 19 39 foreach (KeyValuePair<string, JSONNode> kvp in nodes) { 40 if (prettyPrint) 41 sb.Append (new String ('\t', currentLevel + 1)); 20 42 sb.Append (String.Format ("\"{0}\":", kvp.Key)); 21 sb.Append (kvp.Value.ToString ()); 43 if (prettyPrint) 44 sb.Append (" "); 45 sb.Append (kvp.Value.ToString (prettyPrint, currentLevel + 1)); 22 46 sb.Append (","); 47 if (prettyPrint) 48 sb.Append ('\n'); 23 49 } 24 50 if (sb.Length > 1) 25 sb.Remove (sb.Length - 1, 1); 51 sb.Remove (sb.Length - (prettyPrint ? 2 : 1), 1); 52 if (prettyPrint) 53 sb.Append (new String ('\t', currentLevel)); 26 54 sb.Append ("}"); 27 55 return sb.ToString (); 56 } 57 58 public static JSONObject Parse (string json, ref int offset) 59 { 60 //Log.Out ("ParseObject enter (" + offset + ")"); 61 JSONObject obj = new JSONObject (); 62 63 bool nextElemAllowed = true; 64 offset++; 65 while (true) { 66 Parser.SkipWhitespace (json, ref offset); 67 switch (json [offset]) { 68 case '"': 69 if (nextElemAllowed) { 70 JSONString key = JSONString.Parse (json, ref offset); 71 Parser.SkipWhitespace (json, ref offset); 72 if (json [offset] != ':') { 73 throw new MalformedJSONException ("Could not parse object, missing colon (\":\") after key"); 74 } 75 offset++; 76 JSONNode val = Parser.ParseInternal (json, ref offset); 77 obj.Add (key.GetString (), val); 78 nextElemAllowed = false; 79 } else { 80 throw new MalformedJSONException ("Could not parse object, found new key without a separating comma"); 81 } 82 break; 83 case ',': 84 if (!nextElemAllowed) { 85 nextElemAllowed = true; 86 offset++; 87 } else 88 throw new MalformedJSONException ("Could not parse object, found a comma without a key/value pair first"); 89 break; 90 case '}': 91 offset++; 92 //Log.Out ("JSON:Parsed Object: " + obj.ToString ()); 93 return obj; 94 } 95 } 28 96 } 29 97 -
binary-improvements/7dtd-server-fixes/src/JSON/JSONString.cs
r162 r187 13 13 } 14 14 15 public override string ToString () 15 public string GetString () 16 { 17 return value; 18 } 19 20 public override string ToString (bool prettyPrint = false, int currentLevel = 0) 16 21 { 17 22 if (value == null || value.Length == 0) { … … 61 66 } 62 67 68 public static JSONString Parse (string json, ref int offset) 69 { 70 //Log.Out ("ParseString enter (" + offset + ")"); 71 StringBuilder sb = new StringBuilder (); 72 offset++; 73 while (offset < json.Length) { 74 switch (json [offset]) { 75 case '\\': 76 offset++; 77 switch (json [offset]) { 78 case '\\': 79 case '"': 80 case '/': 81 sb.Append (json [offset]); 82 break; 83 case 'b': 84 sb.Append ('\b'); 85 break; 86 case 't': 87 sb.Append ('\t'); 88 break; 89 case 'n': 90 sb.Append ('\n'); 91 break; 92 case 'f': 93 sb.Append ('\f'); 94 break; 95 case 'r': 96 sb.Append ('\r'); 97 break; 98 default: 99 sb.Append (json [offset]); 100 break; 101 } 102 offset++; 103 break; 104 case '"': 105 offset++; 106 //Log.Out ("JSON:Parsed String: " + sb.ToString ()); 107 return new JSONString (sb.ToString ()); 108 default: 109 sb.Append (json [offset]); 110 offset++; 111 break; 112 } 113 } 114 throw new MalformedJSONException ("End of JSON reached before parsing string finished"); 115 } 116 63 117 } 64 118 } -
binary-improvements/7dtd-server-fixes/src/MapRendering/Constants.cs
r144 r187 3 3 namespace AllocsFixes.MapRendering 4 4 { 5 public staticclass Constants5 public class Constants 6 6 { 7 public constint MAP_BLOCK_SIZE = 128;8 public constint MAP_CHUNK_SIZE = 16;9 public constint MAP_REGION_SIZE = 512;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;12 public constint ZOOMLEVELS = 5;7 public static int MAP_BLOCK_SIZE = 128; 8 public static int MAP_CHUNK_SIZE = 16; 9 public static int MAP_REGION_SIZE = 512; 10 public static int MAP_BLOCK_TO_CHUNK_DIV { get { return MAP_BLOCK_SIZE / MAP_CHUNK_SIZE; } } 11 public static int MAP_REGION_TO_CHUNK_DIV { get { return MAP_REGION_SIZE / MAP_CHUNK_SIZE; } } 12 public static int ZOOMLEVELS = 5; 13 13 public static string MAP_DIRECTORY = string.Empty; 14 14 } -
binary-improvements/7dtd-server-fixes/src/MapRendering/MapRendering.cs
r168 r187 1 using AllocsFixes.JSON; 1 2 using System; 2 3 using System.Collections.Generic; 3 4 using System.IO; 5 using System.Text; 4 6 using System.Threading; 5 7 using UnityEngine; … … 20 22 } 21 23 22 private MapRenderBlockBuffer[] zoomLevelBuffers = new MapRenderBlockBuffer[Constants.ZOOMLEVELS];24 private MapRenderBlockBuffer[] zoomLevelBuffers; 23 25 private Dictionary<Vector2i, Color[]> dirtyChunks = new Dictionary<Vector2i, Color[]> (); 24 26 private System.Timers.Timer chunkSaveTimer = new System.Timers.Timer (500); … … 31 33 Constants.MAP_DIRECTORY = StaticDirectories.GetSaveGameDir () + "/map"; 32 34 35 if (File.Exists (Constants.MAP_DIRECTORY + "/mapinfo.json")) { 36 LoadMapInfo (); 37 } else { 38 WriteMapInfo (); 39 } 40 41 zoomLevelBuffers = new MapRenderBlockBuffer[Constants.ZOOMLEVELS]; 33 42 for (int i = 0; i < Constants.ZOOMLEVELS; i++) { 34 43 zoomLevelBuffers [i] = new MapRenderBlockBuffer (i); … … 100 109 } 101 110 102 if (Directory.Exists (Constants.MAP_DIRECTORY)) 111 if (Directory.Exists (Constants.MAP_DIRECTORY)) { 103 112 Directory.Delete (Constants.MAP_DIRECTORY, true); 113 } 114 WriteMapInfo (); 104 115 105 116 renderingFullMap = true; … … 239 250 } 240 251 252 private void WriteMapInfo () 253 { 254 JSONObject mapInfo = new JSONObject (); 255 mapInfo.Add ("blockSize", new JSONNumber (Constants.MAP_BLOCK_SIZE)); 256 mapInfo.Add ("maxZoom", new JSONNumber (Constants.ZOOMLEVELS - 1)); 257 258 Directory.CreateDirectory (Constants.MAP_DIRECTORY); 259 File.WriteAllText (Constants.MAP_DIRECTORY + "/mapinfo.json", mapInfo.ToString (), Encoding.UTF8); 260 } 261 262 private void LoadMapInfo () 263 { 264 if (File.Exists (Constants.MAP_DIRECTORY + "/mapinfo.json")) { 265 string json = File.ReadAllText (Constants.MAP_DIRECTORY + "/mapinfo.json", Encoding.UTF8); 266 JSONNode node = Parser.Parse (json); 267 if (node is JSONObject) { 268 JSONObject jo = (JSONObject)node; 269 if (jo.ContainsKey ("blockSize")) 270 Constants.MAP_BLOCK_SIZE = ((JSONNumber)jo ["blockSize"]).GetInt (); 271 if (jo.ContainsKey ("maxZoom")) 272 Constants.ZOOMLEVELS = ((JSONNumber)jo ["maxZoom"]).GetInt () + 1; 273 } 274 } 275 } 276 241 277 private void getWorldExtent (RegionFileManager rfm, out Vector2i minChunk, out Vector2i maxChunk, 242 278 out Vector2i minPos, out Vector2i maxPos, -
binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Telnet/Telnet.cs
r184 r187 9 9 public class Telnet : IServer 10 10 { 11 private const int MAX_LOGIN_ATTEMPTS = 10; 12 private const int BLOCK_TIME_SECONDS = 10; 13 14 private class LoginAttempts 15 { 16 private int count = 0; 17 private DateTime lastAttempt = new DateTime (0); 18 19 public bool LogAttempt () 20 { 21 lastAttempt = DateTime.Now; 22 count++; 23 return count < MAX_LOGIN_ATTEMPTS; 24 } 25 26 public bool IsBanned () 27 { 28 if ((DateTime.Now - lastAttempt).TotalSeconds > BLOCK_TIME_SECONDS) 29 count = 0; 30 return count >= MAX_LOGIN_ATTEMPTS; 31 } 32 } 33 11 34 private TcpListener listener = null; 12 35 private bool authEnabled = false; 13 36 private List<TelnetConnection> connections = new List<TelnetConnection> (); 37 private Dictionary<int, LoginAttempts> loginAttemptsPerIP = new Dictionary<int, LoginAttempts> (); 14 38 15 39 public Telnet () … … 39 63 } 40 64 41 public void FailedLogins ()65 public bool RegisterFailedLogin (int addressHash) 42 66 { 67 lock (loginAttemptsPerIP) { 68 LoginAttempts la = loginAttemptsPerIP [addressHash]; 69 return la.LogAttempt (); 70 } 43 71 } 44 72 … … 46 74 { 47 75 if (listener.Server.IsBound) { 48 TelnetConnection c = new TelnetConnection (this, listener.EndAcceptTcpClient (asyncResult), authEnabled); 49 connections.Add (c); 76 TcpClient client = listener.EndAcceptTcpClient (asyncResult); 77 78 EndPoint endpoint = client.Client.RemoteEndPoint; 79 int addressHash = -1; 80 if (endpoint is IPEndPoint) { 81 addressHash = ((IPEndPoint)endpoint).Address.GetHashCode (); 82 //Log.Out ("Hash: " + endpointAddressHash); 83 } else { 84 Log.Out ("EndPoint is not an IPEndPoint but: " + endpoint.GetType ().ToString ()); 85 } 86 87 lock (loginAttemptsPerIP) { 88 LoginAttempts la = null; 89 if (loginAttemptsPerIP.ContainsKey(addressHash)) 90 la = loginAttemptsPerIP [addressHash]; 91 if (la == null) { 92 la = new LoginAttempts (); 93 loginAttemptsPerIP [addressHash] = la; 94 } 95 if (!la.IsBanned ()) { 96 TelnetConnection con = new TelnetConnection (this, client, authEnabled); 97 connections.Add (con); 98 } else { 99 client.Close (); 100 Log.Out ("Telnet connection not accepted for too many login attempts: " + endpoint); 101 } 102 } 50 103 listener.BeginAcceptTcpClient (new AsyncCallback (AcceptClient), null); 51 104 } -
binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Telnet/TelnetConnection.cs
r185 r187 15 15 private readonly Thread receiveThread = null; 16 16 private readonly Thread sendThread = null; 17 private int authTries = 0;18 17 private bool authenticated = false; 19 18 private readonly bool authEnabled; … … 91 90 LoginMessage (); 92 91 } else { 93 authTries++; 94 // TODO: check if IP has more login attempts by now 95 if (authTries < 3) { 92 if (owner.RegisterFailedLogin(endpointAddressHash)) { 96 93 WriteLine ("Password incorrect, please enter password:"); 97 94 } else { 98 95 WriteLine ("Too many failed login attempts!"); 99 96 Thread.Sleep(100); 100 //TODO: owner.FailedLogins();101 97 Close (); 102 98 Log.Out ("Telnet connection closed for too many login attempts: " + endpoint); -
binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Web/MimeType.cs
r133 r187 209 209 {".jpg", "image/jpeg"}, 210 210 {".js", "application/x-javascript"}, 211 {".json", "application/json"}, 211 212 {".jsx", "text/jscript"}, 212 213 {".jsxbin", "text/plain"}, -
binary-improvements/bin/Release/7dtd-server-fixes_version.txt
r185 r187 1 Version: 0.93.536 5.395581 Version: 0.93.5366.36263
Note:
See TracChangeset
for help on using the changeset viewer.