More work towards parsing/serialising integers using invariant culture

This commit is contained in:
UnknownShadow200 2023-12-22 20:24:28 +11:00
parent c2efe55058
commit f0f8ce9ab9
11 changed files with 44 additions and 24 deletions

View File

@ -256,11 +256,10 @@ namespace MCGalaxy.Commands.World {
); );
static void HandleMapPhysics(Player p, string message) { static void HandleMapPhysics(Player p, string message) {
if (message == "0" || message == "1" || message == "2" || message == "3" || message == "4" || message == "5") { int level = 0;
CmdPhysics.SetPhysics(p.level, int.Parse(message)); if (!CommandParser.GetInt(p, message, "Physics level", ref level, 0, 5)) return;
} else {
p.Message("Accepted numbers are: 0, 1, 2, 3, 4 or 5"); CmdPhysics.SetPhysics(p.level, level);
}
} }
static void HandleMapAdd(Player p, string message) { static void HandleMapAdd(Player p, string message) {

View File

@ -31,8 +31,9 @@ namespace MCGalaxy.Commands.Misc {
int TotalTime = 0; int TotalTime = 0;
try try
{ {
TotalTime = int.Parse(message.SplitSpaces()[0]); string[] bits = message.SplitSpaces(2);
message = message.Substring(message.IndexOf(' ') + 1); TotalTime = int.Parse(bits[0]);
message = bits[1];
} }
catch catch
{ {

View File

@ -43,6 +43,12 @@ namespace MCGalaxy.Config
} }
return value; 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 public sealed class ConfigIntAttribute : ConfigIntegerAttribute
@ -120,6 +126,7 @@ namespace MCGalaxy.Config
public override string Serialise(object value) { public override string Serialise(object value) {
if (value is float) return NumberUtils.StringifyDouble((float)value); if (value is float) return NumberUtils.StringifyDouble((float)value);
if (value is double) return NumberUtils.StringifyDouble((double)value); if (value is double) return NumberUtils.StringifyDouble((double)value);
return base.Serialise(value); return base.Serialise(value);
} }
} }

View File

@ -42,7 +42,8 @@ namespace MCGalaxy.Config {
} }
} }
public sealed class ConfigPermAttribute : ConfigAttribute { public sealed class ConfigPermAttribute : ConfigAttribute
{
LevelPermission defPerm; LevelPermission defPerm;
public ConfigPermAttribute(string name, string section, LevelPermission def) public ConfigPermAttribute(string name, string section, LevelPermission def)
@ -69,11 +70,12 @@ namespace MCGalaxy.Config {
public override string Serialise(object value) { public override string Serialise(object value) {
LevelPermission perm = (LevelPermission)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; object defValue;
Type enumType; 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 ConfigVec3Attribute(string name, string section) : base(name, section) { }
public override object Parse(string raw) { 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; bool defValue;
int minCount; int minCount;

View File

@ -125,8 +125,9 @@ namespace MCGalaxy.Eco
public int Price = 100; public int Price = 100;
public override void Parse(string prop, string value) { public override void Parse(string prop, string value) {
if (prop.CaselessEq("price")) if (prop.CaselessEq("price")) {
Price = int.Parse(value); Price = NumberUtils.ParseInt32(value);
}
} }
public override void Serialise(List<string> cfg) { public override void Serialise(List<string> cfg) {

View File

@ -49,7 +49,7 @@ namespace MCGalaxy
if (num == -1) return "-1.0"; if (num == -1) return "-1.0";
if (num == EnvConfig.ENV_USE_DEFAULT) num = -1; if (num == EnvConfig.ENV_USE_DEFAULT) num = -1;
return num.ToString(); return NumberUtils.StringifyInt(num);
} }
} }

View File

@ -147,7 +147,7 @@ namespace MCGalaxy
if (value == null) return defPerm; if (value == null) return defPerm;
sbyte perm; sbyte perm;
if (sbyte.TryParse(value, out perm)) if (NumberUtils.TryParseInt8(value, out perm))
return (LevelPermission)perm; return (LevelPermission)perm;
Group grp = Find(value); Group grp = Find(value);

View File

@ -52,6 +52,10 @@ namespace MCGalaxy
return value.ToString(CultureInfo.InvariantCulture); 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 // Some languages don't have - as the negative sign symbol
public static bool TryParseInt32(string s, out int result) { public static bool TryParseInt32(string s, out int result) {
@ -61,5 +65,9 @@ namespace MCGalaxy
public static int ParseInt32(string s) { public static int ParseInt32(string s) {
return int.Parse(s, INTEGER_STYLE, NumberFormatInfo.InvariantInfo); 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);
}
} }
} }