Skip to content

Commit

Permalink
More work towards parsing/serialising integers using invariant culture
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Dec 22, 2023
1 parent c2efe55 commit f0f8ce9
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 24 deletions.
8 changes: 4 additions & 4 deletions MCGalaxy/Commands/Information/CmdRankInfo.cs
Expand Up @@ -52,9 +52,9 @@ public sealed class CmdRankInfo : Command2
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]);
Expand All @@ -63,9 +63,9 @@ public sealed class CmdRankInfo : Command2
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", " ");

Expand Down
11 changes: 5 additions & 6 deletions MCGalaxy/Commands/Overseer.cs
Expand Up @@ -256,11 +256,10 @@ public static class Overseer {
);

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) {
Expand All @@ -272,7 +271,7 @@ public static class Overseer {
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);

Expand Down
7 changes: 4 additions & 3 deletions MCGalaxy/Commands/other/CmdTimer.cs
Expand Up @@ -31,8 +31,9 @@ public sealed class CmdTimer : Command2 {
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
{
Expand All @@ -44,7 +45,7 @@ public sealed class CmdTimer : Command2 {
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.");
Expand Down
7 changes: 7 additions & 0 deletions MCGalaxy/Config/NumberAttributes.cs
Expand Up @@ -43,6 +43,12 @@ public ConfigIntegerAttribute(string name, string section)
}
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
Expand Down Expand Up @@ -120,6 +126,7 @@ public ConfigRealAttribute(string name, string section)
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);
}
}
Expand Down
14 changes: 9 additions & 5 deletions MCGalaxy/Config/OtherAttributes.cs
Expand Up @@ -42,7 +42,8 @@ public ConfigBoolAttribute(string name, string section, bool def)
}
}

public sealed class ConfigPermAttribute : ConfigAttribute {
public sealed class ConfigPermAttribute : ConfigAttribute
{
LevelPermission defPerm;

public ConfigPermAttribute(string name, string section, LevelPermission def)
Expand All @@ -69,11 +70,12 @@ public ConfigPermAttribute(string name, string section, LevelPermission def)

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;

Expand All @@ -93,7 +95,8 @@ public ConfigEnumAttribute(string name, string section, object def, Type type)
}
}

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) {
Expand All @@ -109,7 +112,8 @@ public sealed class ConfigVec3Attribute : ConfigAttribute {
}
}

public sealed class ConfigBoolArrayAttribute : ConfigAttribute {
public sealed class ConfigBoolArrayAttribute : ConfigAttribute
{
bool defValue;
int minCount;

Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Database/ColumnDesc.cs
Expand Up @@ -46,7 +46,7 @@ public ColumnDesc(string col, ColumnType type, ushort maxLen)
}

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];
}
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Drawing/CopyState.cs
Expand Up @@ -40,7 +40,7 @@ public sealed class CopyState
/// <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; } }

Expand Down
5 changes: 3 additions & 2 deletions MCGalaxy/Economy/Item.cs
Expand Up @@ -125,8 +125,9 @@ public abstract class SimpleItem : Item
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) {
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Levels/LevelConfig.cs
Expand Up @@ -49,7 +49,7 @@ public ConfigEnvIntAttribute(string name, int min, int max)
if (num == -1) return "-1.0";

if (num == EnvConfig.ENV_USE_DEFAULT) num = -1;
return num.ToString();
return NumberUtils.StringifyInt(num);
}
}

Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Player/Group.cs
Expand Up @@ -147,7 +147,7 @@ public sealed class Group
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);
Expand Down
8 changes: 8 additions & 0 deletions MCGalaxy/util/NumberUtils.cs
Expand Up @@ -52,6 +52,10 @@ public static class NumberUtils
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) {
Expand All @@ -61,5 +65,9 @@ public static class NumberUtils
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);
}
}
}

0 comments on commit f0f8ce9

Please sign in to comment.