From f0f8ce9ab9daf5d13bf79bb0c0a049ff3312673c Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 22 Dec 2023 20:24:28 +1100 Subject: [PATCH] More work towards parsing/serialising integers using invariant culture --- MCGalaxy/Commands/Information/CmdRankInfo.cs | 8 ++++---- MCGalaxy/Commands/Overseer.cs | 11 +++++------ MCGalaxy/Commands/other/CmdTimer.cs | 7 ++++--- MCGalaxy/Config/NumberAttributes.cs | 7 +++++++ MCGalaxy/Config/OtherAttributes.cs | 14 +++++++++----- MCGalaxy/Database/ColumnDesc.cs | 2 +- MCGalaxy/Drawing/CopyState.cs | 2 +- MCGalaxy/Economy/Item.cs | 5 +++-- MCGalaxy/Levels/LevelConfig.cs | 2 +- MCGalaxy/Player/Group.cs | 2 +- MCGalaxy/util/NumberUtils.cs | 8 ++++++++ 11 files changed, 44 insertions(+), 24 deletions(-) diff --git a/MCGalaxy/Commands/Information/CmdRankInfo.cs b/MCGalaxy/Commands/Information/CmdRankInfo.cs index 0d32cda47..c8194e733 100644 --- a/MCGalaxy/Commands/Information/CmdRankInfo.cs +++ b/MCGalaxy/Commands/Information/CmdRankInfo.cs @@ -52,9 +52,9 @@ namespace MCGalaxy.Commands.Info int offset; if (args.Length <= 6) { - delta = DateTime.UtcNow - long.Parse(args[2]).FromUnixTime(); + delta = DateTime.UtcNow - long.Parse(args[2]).FromUnixTime(); newRank = args[3]; oldRank = args[4]; - offset = 5; + offset = 5; } else { // Backwards compatibility with old format int min = NumberUtils.ParseInt32(args[2]); @@ -63,9 +63,9 @@ namespace MCGalaxy.Commands.Info int month = NumberUtils.ParseInt32(args[5]); int year = NumberUtils.ParseInt32(args[6]); - delta = DateTime.Now - new DateTime(year, month, day, hour, min, 0); + delta = DateTime.Now - new DateTime(year, month, day, hour, min, 0); newRank = args[7]; oldRank = args[8]; - offset = 9; + offset = 9; } string reason = args.Length <= offset ? "(no reason given)" : args[offset].Replace("%20", " "); diff --git a/MCGalaxy/Commands/Overseer.cs b/MCGalaxy/Commands/Overseer.cs index f2e35e817..a20b0af29 100644 --- a/MCGalaxy/Commands/Overseer.cs +++ b/MCGalaxy/Commands/Overseer.cs @@ -256,11 +256,10 @@ namespace MCGalaxy.Commands.World { ); static void HandleMapPhysics(Player p, string message) { - if (message == "0" || message == "1" || message == "2" || message == "3" || message == "4" || message == "5") { - CmdPhysics.SetPhysics(p.level, int.Parse(message)); - } else { - p.Message("Accepted numbers are: 0, 1, 2, 3, 4 or 5"); - } + int level = 0; + if (!CommandParser.GetInt(p, message, "Physics level", ref level, 0, 5)) return; + + CmdPhysics.SetPhysics(p.level, level); } static void HandleMapAdd(Player p, string message) { @@ -272,7 +271,7 @@ namespace MCGalaxy.Commands.World { if (level == null) return; string[] bits = message.SplitSpaces(); - if (message.Length == 0) message = "128 128 128"; + if (message.Length == 0) message = "128 128 128"; else if (bits.Length < 3) message = "128 128 128 " + message; string[] genArgs = (level + " " + message.TrimEnd()).SplitSpaces(6); diff --git a/MCGalaxy/Commands/other/CmdTimer.cs b/MCGalaxy/Commands/other/CmdTimer.cs index e6c325733..a93053881 100644 --- a/MCGalaxy/Commands/other/CmdTimer.cs +++ b/MCGalaxy/Commands/other/CmdTimer.cs @@ -31,8 +31,9 @@ namespace MCGalaxy.Commands.Misc { int TotalTime = 0; try { - TotalTime = int.Parse(message.SplitSpaces()[0]); - message = message.Substring(message.IndexOf(' ') + 1); + string[] bits = message.SplitSpaces(2); + TotalTime = int.Parse(bits[0]); + message = bits[1]; } catch { @@ -44,7 +45,7 @@ namespace MCGalaxy.Commands.Misc { TimerArgs args = new TimerArgs(); args.Message = message; args.Repeats = (int)(TotalTime / 5) + 1; - args.Player = p; + args.Player = p; p.cmdTimer = true; p.level.Message("Timer lasting for " + TotalTime + " seconds has started."); diff --git a/MCGalaxy/Config/NumberAttributes.cs b/MCGalaxy/Config/NumberAttributes.cs index f0bfe8c8d..3d83a7656 100644 --- a/MCGalaxy/Config/NumberAttributes.cs +++ b/MCGalaxy/Config/NumberAttributes.cs @@ -43,6 +43,12 @@ namespace MCGalaxy.Config } return value; } + + public override string Serialise(object value) { + if (value is int) return NumberUtils.StringifyInt((int)value); + + return base.Serialise(value); + } } public sealed class ConfigIntAttribute : ConfigIntegerAttribute @@ -120,6 +126,7 @@ namespace MCGalaxy.Config public override string Serialise(object value) { if (value is float) return NumberUtils.StringifyDouble((float)value); if (value is double) return NumberUtils.StringifyDouble((double)value); + return base.Serialise(value); } } diff --git a/MCGalaxy/Config/OtherAttributes.cs b/MCGalaxy/Config/OtherAttributes.cs index f6123fad8..19346ec29 100644 --- a/MCGalaxy/Config/OtherAttributes.cs +++ b/MCGalaxy/Config/OtherAttributes.cs @@ -42,7 +42,8 @@ namespace MCGalaxy.Config { } } - public sealed class ConfigPermAttribute : ConfigAttribute { + public sealed class ConfigPermAttribute : ConfigAttribute + { LevelPermission defPerm; public ConfigPermAttribute(string name, string section, LevelPermission def) @@ -69,11 +70,12 @@ namespace MCGalaxy.Config { public override string Serialise(object value) { LevelPermission perm = (LevelPermission)value; - return ((sbyte)perm).ToString(); + return NumberUtils.StringifyInt((sbyte)perm); } } - public sealed class ConfigEnumAttribute : ConfigAttribute { + public sealed class ConfigEnumAttribute : ConfigAttribute + { object defValue; Type enumType; @@ -93,7 +95,8 @@ namespace MCGalaxy.Config { } } - public sealed class ConfigVec3Attribute : ConfigAttribute { + public sealed class ConfigVec3Attribute : ConfigAttribute + { public ConfigVec3Attribute(string name, string section) : base(name, section) { } public override object Parse(string raw) { @@ -109,7 +112,8 @@ namespace MCGalaxy.Config { } } - public sealed class ConfigBoolArrayAttribute : ConfigAttribute { + public sealed class ConfigBoolArrayAttribute : ConfigAttribute + { bool defValue; int minCount; diff --git a/MCGalaxy/Database/ColumnDesc.cs b/MCGalaxy/Database/ColumnDesc.cs index 9111a82ad..d2d027fbd 100644 --- a/MCGalaxy/Database/ColumnDesc.cs +++ b/MCGalaxy/Database/ColumnDesc.cs @@ -46,7 +46,7 @@ namespace MCGalaxy.SQL } public string FormatType() { - if (Type == ColumnType.Char) return "CHAR(" + MaxLength + ")"; + if (Type == ColumnType.Char) return "CHAR(" + MaxLength + ")"; if (Type == ColumnType.VarChar) return "VARCHAR(" + MaxLength + ")"; return colTypes[(int)Type]; } diff --git a/MCGalaxy/Drawing/CopyState.cs b/MCGalaxy/Drawing/CopyState.cs index b4a54b710..0f4aaef87 100644 --- a/MCGalaxy/Drawing/CopyState.cs +++ b/MCGalaxy/Drawing/CopyState.cs @@ -40,7 +40,7 @@ namespace MCGalaxy.Drawing /// "level example1", "file example2" public string CopySource; - internal int OppositeOriginX { get { return OriginX == X ? X + Width - 1 : X; } } + internal int OppositeOriginX { get { return OriginX == X ? X + Width - 1 : X; } } internal int OppositeOriginY { get { return OriginY == Y ? Y + Height - 1 : Y; } } internal int OppositeOriginZ { get { return OriginZ == Z ? Z + Length - 1 : Z; } } diff --git a/MCGalaxy/Economy/Item.cs b/MCGalaxy/Economy/Item.cs index c60351e78..f0838ed3d 100644 --- a/MCGalaxy/Economy/Item.cs +++ b/MCGalaxy/Economy/Item.cs @@ -125,8 +125,9 @@ namespace MCGalaxy.Eco public int Price = 100; public override void Parse(string prop, string value) { - if (prop.CaselessEq("price")) - Price = int.Parse(value); + if (prop.CaselessEq("price")) { + Price = NumberUtils.ParseInt32(value); + } } public override void Serialise(List cfg) { diff --git a/MCGalaxy/Levels/LevelConfig.cs b/MCGalaxy/Levels/LevelConfig.cs index 3f757885c..b8e3b4103 100644 --- a/MCGalaxy/Levels/LevelConfig.cs +++ b/MCGalaxy/Levels/LevelConfig.cs @@ -49,7 +49,7 @@ namespace MCGalaxy if (num == -1) return "-1.0"; if (num == EnvConfig.ENV_USE_DEFAULT) num = -1; - return num.ToString(); + return NumberUtils.StringifyInt(num); } } diff --git a/MCGalaxy/Player/Group.cs b/MCGalaxy/Player/Group.cs index 62e5ecdb7..ab7663a18 100644 --- a/MCGalaxy/Player/Group.cs +++ b/MCGalaxy/Player/Group.cs @@ -147,7 +147,7 @@ namespace MCGalaxy if (value == null) return defPerm; sbyte perm; - if (sbyte.TryParse(value, out perm)) + if (NumberUtils.TryParseInt8(value, out perm)) return (LevelPermission)perm; Group grp = Find(value); diff --git a/MCGalaxy/util/NumberUtils.cs b/MCGalaxy/util/NumberUtils.cs index 2c0ca76d5..1796ed68f 100644 --- a/MCGalaxy/util/NumberUtils.cs +++ b/MCGalaxy/util/NumberUtils.cs @@ -52,6 +52,10 @@ namespace MCGalaxy return value.ToString(CultureInfo.InvariantCulture); } + public static string StringifyInt(int value) { + return value.ToString(CultureInfo.InvariantCulture); + } + // Some languages don't have - as the negative sign symbol public static bool TryParseInt32(string s, out int result) { @@ -61,5 +65,9 @@ namespace MCGalaxy public static int ParseInt32(string s) { return int.Parse(s, INTEGER_STYLE, NumberFormatInfo.InvariantInfo); } + + public static bool TryParseInt8(string s, out sbyte result) { + return sbyte.TryParse(s, INTEGER_STYLE, NumberFormatInfo.InvariantInfo, out result); + } } }