new shorthand Exts.(Try)ParseIntegerInvariant

This commit is contained in:
Matthias Mailänder
2014-05-09 08:47:05 +02:00
parent bd55ffc10d
commit 59ace5d01b
21 changed files with 117 additions and 123 deletions

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
@@ -11,6 +11,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Reflection;
@@ -251,6 +252,16 @@ namespace OpenRA
throw new InvalidOperationException("ToBits only accepts up to 32 values.");
return result;
}
public static int ParseIntegerInvariant(string s)
{
return int.Parse(s, NumberStyles.Integer, NumberFormatInfo.InvariantInfo);
}
public static bool TryParseIntegerInvariant(string s, out int i)
{
return int.TryParse(s, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out i);
}
}
public static class Enum<T>

54
OpenRA.Game/FieldLoader.cs Executable file → Normal file
View File

@@ -116,7 +116,7 @@ namespace OpenRA
if (fieldType == typeof(int))
{
int res;
if (int.TryParse(value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out res))
if (Exts.TryParseIntegerInvariant(value, out res))
return res;
return InvalidValueAction(value, fieldType, fieldName);
}
@@ -165,15 +165,15 @@ namespace OpenRA
var parts = value.Split(',');
if (parts.Length == 3)
return Color.FromArgb(
int.Parse(parts[0], NumberStyles.Integer, NumberFormatInfo.InvariantInfo).Clamp(0, 255),
int.Parse(parts[1], NumberStyles.Integer, NumberFormatInfo.InvariantInfo).Clamp(0, 255),
int.Parse(parts[2], NumberStyles.Integer, NumberFormatInfo.InvariantInfo).Clamp(0, 255));
Exts.ParseIntegerInvariant(parts[0]).Clamp(0, 255),
Exts.ParseIntegerInvariant(parts[1]).Clamp(0, 255),
Exts.ParseIntegerInvariant(parts[2]).Clamp(0, 255));
if (parts.Length == 4)
return Color.FromArgb(
int.Parse(parts[0], NumberStyles.Integer, NumberFormatInfo.InvariantInfo).Clamp(0, 255),
int.Parse(parts[1], NumberStyles.Integer, NumberFormatInfo.InvariantInfo).Clamp(0, 255),
int.Parse(parts[2], NumberStyles.Integer, NumberFormatInfo.InvariantInfo).Clamp(0, 255),
int.Parse(parts[3], NumberStyles.Integer, NumberFormatInfo.InvariantInfo).Clamp(0, 255));
Exts.ParseIntegerInvariant(parts[0]).Clamp(0, 255),
Exts.ParseIntegerInvariant(parts[1]).Clamp(0, 255),
Exts.ParseIntegerInvariant(parts[2]).Clamp(0, 255),
Exts.ParseIntegerInvariant(parts[3]).Clamp(0, 255));
return InvalidValueAction(value, fieldType, fieldName);
}
@@ -184,9 +184,9 @@ namespace OpenRA
// Allow old ColorRamp format to be parsed as HSLColor
if (parts.Length == 3 || parts.Length == 4)
return new HSLColor(
(byte)int.Parse(parts[0], NumberStyles.Integer, NumberFormatInfo.InvariantInfo).Clamp(0, 255),
(byte)int.Parse(parts[1], NumberStyles.Integer, NumberFormatInfo.InvariantInfo).Clamp(0, 255),
(byte)int.Parse(parts[2], NumberStyles.Integer, NumberFormatInfo.InvariantInfo).Clamp(0, 255));
(byte)Exts.ParseIntegerInvariant(parts[0]).Clamp(0, 255),
(byte)Exts.ParseIntegerInvariant(parts[1]).Clamp(0, 255),
(byte)Exts.ParseIntegerInvariant(parts[2]).Clamp(0, 255));
return InvalidValueAction(value, fieldType, fieldName);
}
@@ -238,7 +238,7 @@ namespace OpenRA
else if (fieldType == typeof(WAngle))
{
int res;
if (int.TryParse(value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out res))
if (Exts.TryParseIntegerInvariant(value, out res))
return new WAngle(res);
return InvalidValueAction(value, fieldType, fieldName);
}
@@ -249,9 +249,9 @@ namespace OpenRA
if (parts.Length == 3)
{
int rr, rp, ry;
if (int.TryParse(value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out rr)
&& int.TryParse(value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out rp)
&& int.TryParse(value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out ry))
if (Exts.TryParseIntegerInvariant(value, out rr)
&& Exts.TryParseIntegerInvariant(value, out rp)
&& Exts.TryParseIntegerInvariant(value, out ry))
return new WRot(new WAngle(rr), new WAngle(rp), new WAngle(ry));
}
@@ -262,16 +262,16 @@ namespace OpenRA
{
var parts = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
return new CPos(
int.Parse(parts[0], NumberStyles.Integer, NumberFormatInfo.InvariantInfo),
int.Parse(parts[1], NumberStyles.Integer, NumberFormatInfo.InvariantInfo));
Exts.ParseIntegerInvariant(parts[0]),
Exts.ParseIntegerInvariant(parts[1]));
}
else if (fieldType == typeof(CVec))
{
var parts = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
return new CVec(
int.Parse(parts[0], NumberStyles.Integer, NumberFormatInfo.InvariantInfo),
int.Parse(parts[1], NumberStyles.Integer, NumberFormatInfo.InvariantInfo));
Exts.ParseIntegerInvariant(parts[0]),
Exts.ParseIntegerInvariant(parts[1]));
}
else if (fieldType.IsEnum)
@@ -306,16 +306,16 @@ namespace OpenRA
{
var parts = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
return new Size(
int.Parse(parts[0], NumberStyles.Integer, NumberFormatInfo.InvariantInfo),
int.Parse(parts[1], NumberStyles.Integer, NumberFormatInfo.InvariantInfo));
Exts.ParseIntegerInvariant(parts[0]),
Exts.ParseIntegerInvariant(parts[1]));
}
else if (fieldType == typeof(int2))
{
var parts = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
return new int2(
int.Parse(parts[0], NumberStyles.Integer, NumberFormatInfo.InvariantInfo),
int.Parse(parts[1], NumberStyles.Integer, NumberFormatInfo.InvariantInfo));
Exts.ParseIntegerInvariant(parts[0]),
Exts.ParseIntegerInvariant(parts[1]));
}
else if (fieldType == typeof(float2))
@@ -335,10 +335,10 @@ namespace OpenRA
{
var parts = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
return new Rectangle(
int.Parse(parts[0], NumberStyles.Integer, NumberFormatInfo.InvariantInfo),
int.Parse(parts[1], NumberStyles.Integer, NumberFormatInfo.InvariantInfo),
int.Parse(parts[2], NumberStyles.Integer, NumberFormatInfo.InvariantInfo),
int.Parse(parts[3], NumberStyles.Integer, NumberFormatInfo.InvariantInfo));
Exts.ParseIntegerInvariant(parts[0]),
Exts.ParseIntegerInvariant(parts[1]),
Exts.ParseIntegerInvariant(parts[2]),
Exts.ParseIntegerInvariant(parts[3]));
}
else if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(Bits<>))

View File

@@ -142,7 +142,7 @@ namespace OpenRA.FileSystem
// Maybe we were given a raw hash?
uint raw;
if (!uint.TryParse(filename, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out raw))
return null;
return null;
if ("{0:X}".F(raw) == filename && index.ContainsKey(raw))
return raw;

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information
/*
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
@@ -9,7 +9,6 @@
#endregion
using System;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;
using OpenRA.FileSystem;
@@ -42,7 +41,7 @@ namespace OpenRA.Graphics
if (sequences.NodesDict.ContainsKey("ShadowIndex"))
{
Array.Resize(ref shadowIndex, shadowIndex.Length + 1);
int.TryParse(sequences.NodesDict["ShadowIndex"].Value, NumberStyles.Any, NumberFormatInfo.InvariantInfo,
Exts.TryParseIntegerInvariant(sequences.NodesDict["ShadowIndex"].Value,
out shadowIndex[shadowIndex.Length - 1]);
}

View File

@@ -8,7 +8,6 @@
*/
#endregion
using System.Globalization;
using OpenRA.FileFormats;
namespace OpenRA.Graphics
@@ -31,22 +30,22 @@ namespace OpenRA.Graphics
sprites = Game.modData.SpriteLoader.LoadAllSprites(cursorSrc);
var d = info.NodesDict;
start = int.Parse(d["start"].Value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo);
start = Exts.ParseIntegerInvariant(d["start"].Value);
this.palette = palette;
if ((d.ContainsKey("length") && d["length"].Value == "*") || (d.ContainsKey("end") && d["end"].Value == "*"))
length = sprites.Length - start;
else if (d.ContainsKey("length"))
length = int.Parse(d["length"].Value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo);
length = Exts.ParseIntegerInvariant(d["length"].Value);
else if (d.ContainsKey("end"))
length = int.Parse(d["end"].Value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo) - start;
length = Exts.ParseIntegerInvariant(d["end"].Value) - start;
else
length = 1;
if (d.ContainsKey("x"))
int.TryParse(d["x"].Value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out Hotspot.X);
Exts.TryParseIntegerInvariant(d["x"].Value, out Hotspot.X);
if (d.ContainsKey("y"))
int.TryParse(d["y"].Value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out Hotspot.Y);
Exts.TryParseIntegerInvariant(d["y"].Value, out Hotspot.Y);
}
public Sprite GetSprite(int frame)

View File

@@ -9,7 +9,6 @@
#endregion
using System;
using System.Globalization;
using System.Linq;
namespace OpenRA.Graphics
@@ -41,7 +40,7 @@ namespace OpenRA.Graphics
try
{
if (d.ContainsKey("Start"))
Start = int.Parse(d["Start"].Value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo);
Start = Exts.ParseIntegerInvariant(d["Start"].Value);
if (d.ContainsKey("Offset"))
offset = FieldLoader.GetValue<float2>("Offset", d["Offset"].Value);
@@ -59,16 +58,16 @@ namespace OpenRA.Graphics
else if (d["Length"].Value == "*")
Length = sprites.Length - Start;
else
Length = int.Parse(d["Length"].Value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo);
Length = Exts.ParseIntegerInvariant(d["Length"].Value);
if (d.ContainsKey("Stride"))
Stride = int.Parse(d["Stride"].Value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo);
Stride = Exts.ParseIntegerInvariant(d["Stride"].Value);
else
Stride = Length;
if (d.ContainsKey("Facings"))
{
var f = int.Parse(d["Facings"].Value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo);
var f = Exts.ParseIntegerInvariant(d["Facings"].Value);
Facings = Math.Abs(f);
reverseFacings = f < 0;
}
@@ -76,7 +75,7 @@ namespace OpenRA.Graphics
Facings = 1;
if (d.ContainsKey("Tick"))
Tick = int.Parse(d["Tick"].Value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo);
Tick = Exts.ParseIntegerInvariant(d["Tick"].Value);
else
Tick = 40;
@@ -84,10 +83,10 @@ namespace OpenRA.Graphics
transpose = bool.Parse(d["Transpose"].Value);
if (d.ContainsKey("Frames"))
Frames = Array.ConvertAll<string, int>(d["Frames"].Value.Split(','), (s) => int.Parse(s, NumberStyles.Integer, NumberFormatInfo.InvariantInfo));
Frames = Array.ConvertAll<string, int>(d["Frames"].Value.Split(','), Exts.ParseIntegerInvariant);
if (d.ContainsKey("ShadowStart"))
ShadowStart = int.Parse(d["ShadowStart"].Value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo);
ShadowStart = Exts.ParseIntegerInvariant(d["ShadowStart"].Value);
else
ShadowStart = -1;

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information
/*
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
@@ -10,7 +10,6 @@
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using OpenRA.Primitives;
@@ -69,7 +68,7 @@ namespace OpenRA
LobbyDefaults = yaml["LobbyDefaults"];
Fonts = yaml["Fonts"].NodesDict.ToDictionary(x => x.Key,
x => Pair.New(x.Value.NodesDict["Font"].Value,
int.Parse(x.Value.NodesDict["Size"].Value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo)));
Exts.ParseIntegerInvariant(x.Value.NodesDict["Size"].Value)));
if (yaml.ContainsKey("TileSize"))
TileSize = FieldLoader.GetValue<Size>("TileSize", yaml["TileSize"].Value);

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
@@ -11,7 +11,6 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
@@ -203,9 +202,9 @@ namespace OpenRA
var vals = kv.Key.Split(' ');
var loc = vals[1].Split(',');
ret.Add(new SmudgeReference(vals[0], new int2(
int.Parse(loc[0], NumberStyles.Integer, NumberFormatInfo.InvariantInfo),
int.Parse(loc[1], NumberStyles.Integer, NumberFormatInfo.InvariantInfo)),
int.Parse(vals[2], NumberStyles.Integer, NumberFormatInfo.InvariantInfo)));
Exts.ParseIntegerInvariant(loc[0]),
Exts.ParseIntegerInvariant(loc[1])),
Exts.ParseIntegerInvariant(vals[2])));
}
return ret;

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information
/*
* Copyright 2007-2012 The OpenRA Developers (see AUTHORS)
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
@@ -11,7 +11,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
@@ -467,7 +466,7 @@ namespace OpenRA.Server
case "Pong":
{
int pingSent;
if (!int.TryParse(so.Data, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out pingSent))
if (!OpenRA.Exts.TryParseIntegerInvariant(so.Data, out pingSent))
{
Log.Write("server", "Invalid order pong payload: {0}", so.Data);
break;

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
@@ -10,7 +10,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace OpenRA.Support
@@ -37,7 +36,7 @@ namespace OpenRA.Support
case '-': ApplyBinop(s, (x, y) => y - x); break;
case '*': ApplyBinop(s, (x, y) => y * x); break;
case '/': ApplyBinop(s, (x, y) => y / x); break;
default: s.Push(int.Parse(t, NumberStyles.Integer, NumberFormatInfo.InvariantInfo)); break;
default: s.Push(Exts.ParseIntegerInvariant(t)); break;
}
}

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information
/*
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
@@ -9,7 +9,6 @@
#endregion
using System;
using System.Globalization;
using System.Linq;
namespace OpenRA
@@ -57,12 +56,12 @@ namespace OpenRA
switch (components.Length)
{
case 2:
if (!int.TryParse(components[0], NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out cell) ||
!int.TryParse(components[1], NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out subcell))
if (!Exts.TryParseIntegerInvariant(components[0], out cell) ||
!Exts.TryParseIntegerInvariant(components[1], out subcell))
return false;
break;
case 1:
if (!int.TryParse(components[0], NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out subcell))
if (!Exts.TryParseIntegerInvariant(components[0], out subcell))
return false;
break;
default: return false;