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

@ -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", " ");

View File

@ -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);

View File

@ -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.");

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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];
}

View File

@ -40,7 +40,7 @@ namespace MCGalaxy.Drawing
/// <example> "level example1", "file example2" </example>
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; } }

View File

@ -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<string> cfg) {

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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);
}
}
}