diff --git a/GUI/Window/Window.cs b/GUI/Window/Window.cs index b5f99d7ce..9248410a5 100644 --- a/GUI/Window/Window.cs +++ b/GUI/Window/Window.cs @@ -80,7 +80,11 @@ namespace MCGalaxy.Gui { void UpdateNotifyIconText() { int playerCount = PlayerInfo.Online.Count; string players = " (" + playerCount + " players)"; - notifyIcon.Text = (ServerConfig.Name + players).Truncate(63); + + // ArgumentException thrown if text length is > 63 + string text = (ServerConfig.Name + players); + if (text.Length > 63) text = text.Substring(0, 63); + notifyIcon.Text = text; } void MakeNotifyIcon() { diff --git a/MCGalaxy/Blocks/BlockPerms.cs b/MCGalaxy/Blocks/BlockPerms.cs index d49a09d80..dbba06573 100644 --- a/MCGalaxy/Blocks/BlockPerms.cs +++ b/MCGalaxy/Blocks/BlockPerms.cs @@ -147,11 +147,11 @@ namespace MCGalaxy.Blocks { } static void LoadVersion2(string[] lines) { - char[] colon = new char[] { ':' }; + string[] args = new string[4]; foreach (string line in lines) { if (line.Length == 0 || line[0] == '#') continue; //Name : Lowest : Disallow : Allow - string[] args = line.Replace(" ", "").Split(colon); + line.Replace(" ", "").FixedSplit(args, ':'); BlockPerms perms = new BlockPerms(); if (Block.Byte(args[0]) == Block.Invalid) continue; @@ -159,8 +159,7 @@ namespace MCGalaxy.Blocks { try { perms.MinRank = (LevelPermission)int.Parse(args[1]); - string disallowRaw = args.Length > 2 ? args[2] : null; - string allowRaw = args.Length > 3 ? args[3] : null; + string disallowRaw = args[2], allowRaw = args[3]; perms.Allowed = CommandPerms.ExpandPerms(allowRaw); perms.Disallowed = CommandPerms.ExpandPerms(disallowRaw); diff --git a/MCGalaxy/Chat/ChatTokens.cs b/MCGalaxy/Chat/ChatTokens.cs index 963d15032..256bedbb3 100644 --- a/MCGalaxy/Chat/ChatTokens.cs +++ b/MCGalaxy/Chat/ChatTokens.cs @@ -116,13 +116,11 @@ namespace MCGalaxy { } string[] lines = tokensFile.GetText(); - char[] colon = null; + string[] parts = new string[2]; foreach (string line in lines) { if (line.StartsWith("//")) continue; - - if (colon == null) colon = new char[] { ':' }; - string[] parts = line.Split(colon, 2); - if (parts.Length != 2) continue; + line.FixedSplit(parts, ':'); + if (parts[1] == null) continue; // not a proper line string key = parts[0].Trim(), value = parts[1].Trim(); if (key.Length == 0) continue; diff --git a/MCGalaxy/Commands/CommandExtraPerms.cs b/MCGalaxy/Commands/CommandExtraPerms.cs index c3f5cac9e..44a99a65a 100644 --- a/MCGalaxy/Commands/CommandExtraPerms.cs +++ b/MCGalaxy/Commands/CommandExtraPerms.cs @@ -139,14 +139,15 @@ namespace MCGalaxy.Commands { } static void LoadCore() { + string[] args = new string[4]; using (StreamReader r = new StreamReader(Paths.CmdExtraPermsFile)) { string line; while ((line = r.ReadLine()) != null) { if (line.Length == 0 || line[0] == '#' || line.IndexOf(':') == -1) continue; try { - string[] parts = line.Split(':'); - LoadExtraPerm(parts); + line.FixedSplit(args, ':'); + LoadExtraPerm(args); } catch (Exception ex) { Logger.Log(LogType.Warning, "Loading an additional command permission failed!!"); Logger.LogError(ex); @@ -155,10 +156,10 @@ namespace MCGalaxy.Commands { } } - static void LoadExtraPerm(string[] parts) { - string cmdName = parts[0]; - int number = int.Parse(parts[1]), minPerm = int.Parse(parts[2]); - string desc = parts.Length > 3 ? parts[3] : ""; + static void LoadExtraPerm(string[] args) { + string cmdName = args[0]; + int number = int.Parse(args[1]), minPerm = int.Parse(args[2]); + string desc = args[3] == null ? "" : args[3]; CommandExtraPerms existing = Find(cmdName, number); if (existing != null) desc = existing.Description; diff --git a/MCGalaxy/Commands/CommandPerms.cs b/MCGalaxy/Commands/CommandPerms.cs index ba12a8e0c..42702d239 100644 --- a/MCGalaxy/Commands/CommandPerms.cs +++ b/MCGalaxy/Commands/CommandPerms.cs @@ -195,16 +195,15 @@ namespace MCGalaxy.Commands { } static void LoadVersion2(string[] lines) { - char[] colon = new char[] { ':' }; + string[] args = new string[4]; foreach (string line in lines) { if (line.Length == 0 || line[0] == '#') continue; //Name : Lowest : Disallow : Allow - string[] args = line.Replace(" ", "").Split(colon); + line.Replace(" ", "").FixedSplit(args, ':'); try { LevelPermission minRank = (LevelPermission)int.Parse(args[1]); - string disallowRaw = args.Length > 2 ? args[2] : null; - string allowRaw = args.Length > 3 ? args[3] : null; + string disallowRaw = args[2], allowRaw = args[3]; List allow = ExpandPerms(allowRaw); List disallow = ExpandPerms(disallowRaw); diff --git a/MCGalaxy/Commands/other/CmdHackRank.cs b/MCGalaxy/Commands/other/CmdHackRank.cs index 2edc8a832..0a8189f1b 100644 --- a/MCGalaxy/Commands/other/CmdHackRank.cs +++ b/MCGalaxy/Commands/other/CmdHackRank.cs @@ -44,7 +44,7 @@ namespace MCGalaxy.Commands.Misc { } void DoFakeRank(Player p, Group newRank) { - p.hackrank = true; + p.hackrank = true; string rankMsg = ModActionCmd.FormatRankChange(p.group, newRank, p.name, "Congratulations!"); Chat.MessageGlobal(rankMsg); diff --git a/MCGalaxy/Database/PlayerDB.cs b/MCGalaxy/Database/PlayerDB.cs index e9ef4337d..440076775 100644 --- a/MCGalaxy/Database/PlayerDB.cs +++ b/MCGalaxy/Database/PlayerDB.cs @@ -41,8 +41,9 @@ namespace MCGalaxy.DB { public static bool Load( Player p ) { if (!File.Exists("players/" + p.name + "DB.txt")) return false; - foreach (string line in File.ReadAllLines( "players/" + p.name + "DB.txt")) { - if (string.IsNullOrEmpty(line) || line[0] == '#') continue; + string[] lines = File.ReadAllLines( "players/" + p.name + "DB.txt"); + foreach (string line in lines) { + if (line.Length == 0 || line[0] == '#') continue; string[] parts = line.Split(trimChars, 2); if (parts.Length < 2) continue; string key = parts[0].Trim(), value = parts[1].Trim(); diff --git a/MCGalaxy/Drawing/Image/ImagePalette.cs b/MCGalaxy/Drawing/Image/ImagePalette.cs index 30df1f12d..64a533277 100644 --- a/MCGalaxy/Drawing/Image/ImagePalette.cs +++ b/MCGalaxy/Drawing/Image/ImagePalette.cs @@ -68,12 +68,13 @@ namespace MCGalaxy.Drawing { string[] lines = File.ReadAllLines(file); List entries = new List(); + string[] parts = new string[5]; foreach (string line in lines) { if (line.StartsWith("#") || line.Length == 0) continue; - string[] parts = line.Split(':'); - if (parts.Length != 4) continue; + line.FixedSplit(parts, ':'); + if (parts[3] == null || parts[4] != null) continue; // not a proper line entries.Add(ParseEntry(parts)); } diff --git a/MCGalaxy/Player/Player.Fields.cs b/MCGalaxy/Player/Player.Fields.cs index acfcef60c..e6af1ca6c 100644 --- a/MCGalaxy/Player/Player.Fields.cs +++ b/MCGalaxy/Player/Player.Fields.cs @@ -28,8 +28,6 @@ namespace MCGalaxy { public enum VoteKickChoice { NotYetVoted, Yes, No } public partial class Player : IDisposable { - - public Dictionary ExtraData = new Dictionary(); internal class PendingItem { public string Name; diff --git a/MCGalaxy/Player/Player.Handlers.cs b/MCGalaxy/Player/Player.Handlers.cs index cd1d8ebf8..e2f685d30 100644 --- a/MCGalaxy/Player/Player.Handlers.cs +++ b/MCGalaxy/Player/Player.Handlers.cs @@ -482,7 +482,7 @@ namespace MCGalaxy { bool FilterChat(ref string text, byte continued) { // handles the /womid client message, which displays the WoM vrersion - if (text.StartsWith("/womid") { + if (text.StartsWith("/womid")) { string version = (text.Length <= 21 ? text.Substring(text.IndexOf(' ') + 1) : text.Substring(7, 15)); UsingWom = true; return true; diff --git a/MCGalaxy/util/Extensions/Extensions.cs b/MCGalaxy/util/Extensions/Extensions.cs index 29902c07d..9a348b766 100644 --- a/MCGalaxy/util/Extensions/Extensions.cs +++ b/MCGalaxy/util/Extensions/Extensions.cs @@ -24,7 +24,7 @@ using System.IO.Compression; using System.Text; namespace MCGalaxy { - + /// Converts an object into a string. public delegate string StringFormatter(T value); @@ -39,10 +39,18 @@ namespace MCGalaxy { return value.Split(trimChars, maxParts); } - public static string Truncate(this string source, int maxLength) { - if (source.Length > maxLength) - source = source.Substring(0, maxLength); - return source; + public static void FixedSplit(this string value, string[] split, char splitter) { + int start = 0, i = 0; + for (; i < split.Length && start <= value.Length; i++) { + int end = value.IndexOf(splitter, start); + if (end == -1) end = value.Length; + + split[i] = value.Substring(start, end - start); + start = end + 1; + } + + // If not enough split strings, set remaining to null + for (; i < split.Length; i++) { split[i] = null; } } public static byte[] GZip(this byte[] bytes) { diff --git a/MCGalaxy/util/ExtrasCollection.cs b/MCGalaxy/util/ExtrasCollection.cs index 6a048f9e4..d75c8ee27 100644 --- a/MCGalaxy/util/ExtrasCollection.cs +++ b/MCGalaxy/util/ExtrasCollection.cs @@ -25,7 +25,7 @@ namespace MCGalaxy { public sealed class ExtrasCollection : Dictionary { /// Returns the value associated with the given key as an object, or null if no value exists for this key - public object Get(string key){ + public object Get(string key) { object value; TryGetValue(key, out value); return value;