Converted the GetAttribute methods in the ToolBox class to extension methods

This commit is contained in:
Joonas Rikkonen
2017-10-04 18:38:40 +03:00
parent 6c7c97a875
commit 1ff2054ca8
99 changed files with 854 additions and 875 deletions
@@ -85,9 +85,9 @@ namespace Barotrauma
{
DecompressToDirectory(filePath, TempPath, null);
XDocument doc = ToolBox.TryLoadXml(Path.Combine(TempPath, "gamesession.xml"));
XDocument doc = XMLExtensions.TryLoadXml(Path.Combine(TempPath, "gamesession.xml"));
string subPath = Path.Combine(TempPath, ToolBox.GetAttributeString(doc.Root, "submarine", "")) + ".sub";
string subPath = Path.Combine(TempPath, doc.Root.GetAttributeString("submarine", "")) + ".sub";
Submarine selectedSub = new Submarine(subPath, "");
GameMain.GameSession = new GameSession(selectedSub, filePath, doc);
}
@@ -95,7 +95,7 @@ namespace Barotrauma
public static void LoadGame(string filePath, GameSession gameSession)
{
DecompressToDirectory(filePath, TempPath, null);
XDocument doc = ToolBox.TryLoadXml(Path.Combine(TempPath, "gamesession.xml"));
XDocument doc = XMLExtensions.TryLoadXml(Path.Combine(TempPath, "gamesession.xml"));
gameSession.Load(doc.Root);
}
@@ -112,7 +112,7 @@ namespace Barotrauma
return null;
}
return ToolBox.TryLoadXml(Path.Combine(tempPath, "gamesession.xml"));
return XMLExtensions.TryLoadXml(Path.Combine(tempPath, "gamesession.xml"));
}
public static void DeleteSave(string filePath)
@@ -1,12 +1,8 @@
using Lidgren.Network;
using Microsoft.Xna.Framework;
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace Barotrauma
{
@@ -27,6 +23,7 @@ namespace Barotrauma
public static partial class ToolBox
{
public static bool IsProperFilenameCase(string filename)
{
char[] delimiters = { '/','\\' };
@@ -70,339 +67,6 @@ namespace Barotrauma
return true;
}
public static XDocument TryLoadXml(string filePath)
{
XDocument doc;
try
{
IsProperFilenameCase(filePath);
doc = XDocument.Load(filePath);
}
catch (Exception e)
{
DebugConsole.ThrowError("Couldn't load xml document \""+filePath+"\"!", e);
return null;
}
if (doc.Root == null) return null;
return doc;
}
/*public static SpriteFont TryLoadFont(string file, Microsoft.Xna.Framework.Content.ContentManager contentManager)
{
SpriteFont font = null;
try
{
font = contentManager.Load<SpriteFont>(file);
}
catch (Exception e)
{
DebugConsole.ThrowError("Loading font \""+file+"\" failed", e);
}
return font;
}*/
public static object GetAttributeObject(XElement element, string name)
{
if (element == null || element.Attribute(name) == null) return null;
return GetAttributeObject(element.Attribute(name));
}
public static object GetAttributeObject(XAttribute attribute)
{
if (attribute == null) return null;
return ParseToObject(attribute.Value.ToString());
}
public static object ParseToObject(string value)
{
float floatVal;
int intVal;
if (value.ToString().Contains(".") && float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out floatVal))
{
return floatVal;
}
else if (int.TryParse(value, out intVal))
{
return intVal;
}
else
{
string lowerTrimmedVal = value.ToLowerInvariant().Trim();
if (lowerTrimmedVal == "true")
{
return true;
}
else if (lowerTrimmedVal == "false")
{
return false;
}
else
{
return value;
}
}
}
public static string GetAttributeString(XElement element, string name, string defaultValue)
{
if (element == null || element.Attribute(name) == null) return defaultValue;
return GetAttributeString(element.Attribute(name), defaultValue);
}
public static string GetAttributeString(XAttribute attribute, string defaultValue)
{
string value = attribute.Value;
if (String.IsNullOrEmpty(value)) return defaultValue;
return value;
}
public static float GetAttributeFloat(XElement element, float defaultValue, params string[] matchingAttributeName)
{
if (element == null) return defaultValue;
foreach (string name in matchingAttributeName)
{
if (element.Attribute(name) == null) continue;
float val = defaultValue;
try
{
if (!float.TryParse(element.Attribute(name).Value, NumberStyles.Float, CultureInfo.InvariantCulture, out val))
{
continue;
}
}
catch (Exception e)
{
DebugConsole.ThrowError("Error in "+element+"!", e);
continue;
}
return val;
}
return defaultValue;
}
public static float GetAttributeFloat(XElement element, string name, float defaultValue)
{
if (element == null || element.Attribute(name) == null) return defaultValue;
float val = defaultValue;
try
{
if (!float.TryParse(element.Attribute(name).Value, NumberStyles.Float, CultureInfo.InvariantCulture, out val))
{
return defaultValue;
}
}
catch (Exception e)
{
DebugConsole.ThrowError("Error in "+element+"!", e);
}
return val;
}
public static float GetAttributeFloat(XAttribute attribute, float defaultValue)
{
if (attribute == null) return defaultValue;
float val = defaultValue;
try
{
val = float.Parse(attribute.Value, CultureInfo.InvariantCulture);
}
catch (Exception e)
{
DebugConsole.ThrowError("Error in " + attribute + "! ", e);
}
return val;
}
public static int GetAttributeInt(XElement element, string name, int defaultValue)
{
if (element == null || element.Attribute(name) == null) return defaultValue;
int val = defaultValue;
try
{
val = int.Parse(element.Attribute(name).Value);
}
catch (Exception e)
{
DebugConsole.ThrowError("Error in " + element + "! ", e);
}
return val;
}
public static bool GetAttributeBool(XElement element, string name, bool defaultValue)
{
if (element == null || element.Attribute(name) == null) return defaultValue;
return GetAttributeBool(element.Attribute(name), defaultValue);
}
public static bool GetAttributeBool(XAttribute attribute, bool defaultValue)
{
if (attribute == null) return defaultValue;
string val = attribute.Value.ToLowerInvariant().Trim();
if (val == "true")
{
return true;
}
else if (val == "false")
{
return false;
}
else
{
DebugConsole.ThrowError("Error in " + attribute.Value.ToString() + "! \"" + val + "\" is not a valid boolean value");
return false;
}
}
public static Vector2 GetAttributeVector2(XElement element, string name, Vector2 defaultValue)
{
if (element == null || element.Attribute(name) == null) return defaultValue;
string val = element.Attribute(name).Value;
return ParseToVector2(val);
}
public static Vector3 GetAttributeVector3(XElement element, string name, Vector3 defaultValue)
{
if (element == null || element.Attribute(name) == null) return defaultValue;
string val = element.Attribute(name).Value;
return ParseToVector3(val);
}
public static Vector4 GetAttributeVector4(XElement element, string name, Vector4 defaultValue)
{
if (element == null || element.Attribute(name) == null) return defaultValue;
string val = element.Attribute(name).Value;
return ParseToVector4(val);
}
public static string ElementInnerText(this XElement el)
{
StringBuilder str = new StringBuilder();
foreach (XNode element in el.DescendantNodes().Where(x => x.NodeType == XmlNodeType.Text))
{
str.Append(element.ToString());
}
return str.ToString();
}
public static Vector2 ParseToVector2(string stringVector2, bool errorMessages = true)
{
string[] components = stringVector2.Split(',');
Vector2 vector = Vector2.Zero;
if (components.Length!=2)
{
if (!errorMessages) return vector;
DebugConsole.ThrowError("Failed to parse the string \""+stringVector2+"\" to Vector2");
return vector;
}
float.TryParse(components[0], NumberStyles.Any, CultureInfo.InvariantCulture, out vector.X);
float.TryParse(components[1], NumberStyles.Any, CultureInfo.InvariantCulture, out vector.Y);
return vector;
}
public static string Vector2ToString(Vector2 vector)
{
return vector.X.ToString("G", CultureInfo.InvariantCulture) + "," + vector.Y.ToString("G", CultureInfo.InvariantCulture);
}
public static Vector3 ParseToVector3(string stringVector3, bool errorMessages = true)
{
string[] components = stringVector3.Split(',');
Vector3 vector = Vector3.Zero;
if (components.Length!=3)
{
if (!errorMessages) return vector;
DebugConsole.ThrowError("Failed to parse the string \""+stringVector3+"\" to Vector3");
return vector;
}
float.TryParse(components[0], NumberStyles.Any, CultureInfo.InvariantCulture, out vector.X);
float.TryParse(components[1], NumberStyles.Any, CultureInfo.InvariantCulture, out vector.Y);
float.TryParse(components[2], NumberStyles.Any, CultureInfo.InvariantCulture, out vector.Z);
return vector;
}
public static Vector4 ParseToVector4(string stringVector4, bool errorMessages = true)
{
string[] components = stringVector4.Split(',');
Vector4 vector = Vector4.Zero;
if (components.Length < 3)
{
if (errorMessages) DebugConsole.ThrowError("Failed to parse the string \"" + stringVector4 + "\" to Vector4");
return vector;
}
float.TryParse(components[0], NumberStyles.Float, CultureInfo.InvariantCulture, out vector.X);
float.TryParse(components[1], NumberStyles.Float, CultureInfo.InvariantCulture, out vector.Y);
float.TryParse(components[2], NumberStyles.Float, CultureInfo.InvariantCulture, out vector.Z);
if (components.Length>3)
float.TryParse(components[3], NumberStyles.Float, CultureInfo.InvariantCulture, out vector.W);
return vector;
}
public static string Vector4ToString(Vector4 vector, string format = "G")
{
return vector.X.ToString(format, CultureInfo.InvariantCulture) + "," +
vector.Y.ToString(format, CultureInfo.InvariantCulture) + "," +
vector.Z.ToString(format, CultureInfo.InvariantCulture) + "," +
vector.W.ToString(format, CultureInfo.InvariantCulture);
}
public static float[] ParseArrayToFloat(string[] stringArray)
{
if (stringArray == null || stringArray.Length == 0) return null;
float[] floatArray = new float[stringArray.Length];
for (int i = 0; i<floatArray.Length; i++)
{
floatArray[i]=0.0f;
float.TryParse(stringArray[i], NumberStyles.Float, CultureInfo.InvariantCulture, out floatArray[i]);
}
return floatArray;
}
public static string LimitString(string str, int maxCharacters)
{
if (str == null || maxCharacters < 0) return null;
@@ -50,7 +50,7 @@ namespace Barotrauma
foreach (XElement file in fileListElement.Elements())
{
string filePath = ToolBox.GetAttributeString(file, "path", "");
string filePath = file.GetAttributeString("path", "");
fileList.Add(filePath);
}
@@ -71,7 +71,7 @@ namespace Barotrauma
foreach (XElement file in fileList.Elements())
{
string filePath = ToolBox.GetAttributeString(file, "path", "");
string filePath = file.GetAttributeString("path", "");
if (!File.Exists(filePath))
{
@@ -79,7 +79,7 @@ namespace Barotrauma
continue;
}
string md5 = ToolBox.GetAttributeString(file, "md5", "");
string md5 = file.GetAttributeString("md5", "");
if (GetFileMd5Hash(filePath) != md5)
{
@@ -0,0 +1,314 @@
using System;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using Microsoft.Xna.Framework;
namespace Barotrauma
{
public static class XMLExtensions
{
public static XDocument TryLoadXml(string filePath)
{
XDocument doc;
try
{
ToolBox.IsProperFilenameCase(filePath);
doc = XDocument.Load(filePath);
}
catch (Exception e)
{
DebugConsole.ThrowError("Couldn't load xml document \"" + filePath + "\"!", e);
return null;
}
if (doc.Root == null) return null;
return doc;
}
public static object GetAttributeObject(XAttribute attribute)
{
if (attribute == null) return null;
return ParseToObject(attribute.Value.ToString());
}
public static object ParseToObject(string value)
{
float floatVal;
int intVal;
if (value.Contains(".") && Single.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out floatVal))
{
return floatVal;
}
if (Int32.TryParse(value, out intVal))
{
return intVal;
}
string lowerTrimmedVal = value.ToLowerInvariant().Trim();
if (lowerTrimmedVal == "true")
{
return true;
}
if (lowerTrimmedVal == "false")
{
return false;
}
return value;
}
public static string GetAttributeString(this XElement element, string name, string defaultValue)
{
if (element?.Attribute(name) == null) return defaultValue;
return GetAttributeString(element.Attribute(name), defaultValue);
}
private static string GetAttributeString(XAttribute attribute, string defaultValue)
{
string value = attribute.Value;
return String.IsNullOrEmpty(value) ? defaultValue : value;
}
public static float GetAttributeFloat(this XElement element, float defaultValue, params string[] matchingAttributeName)
{
if (element == null) return defaultValue;
foreach (string name in matchingAttributeName)
{
if (element.Attribute(name) == null) continue;
float val;
try
{
if (!Single.TryParse(element.Attribute(name).Value, NumberStyles.Float, CultureInfo.InvariantCulture, out val))
{
continue;
}
}
catch (Exception e)
{
DebugConsole.ThrowError("Error in " + element + "!", e);
continue;
}
return val;
}
return defaultValue;
}
public static float GetAttributeFloat(this XElement element, string name, float defaultValue)
{
if (element?.Attribute(name) == null) return defaultValue;
float val = defaultValue;
try
{
if (!Single.TryParse(element.Attribute(name).Value, NumberStyles.Float, CultureInfo.InvariantCulture, out val))
{
return defaultValue;
}
}
catch (Exception e)
{
DebugConsole.ThrowError("Error in " + element + "!", e);
}
return val;
}
public static float GetAttributeFloat(this XAttribute attribute, float defaultValue)
{
if (attribute == null) return defaultValue;
float val = defaultValue;
try
{
val = Single.Parse(attribute.Value, CultureInfo.InvariantCulture);
}
catch (Exception e)
{
DebugConsole.ThrowError("Error in " + attribute + "! ", e);
}
return val;
}
public static int GetAttributeInt(this XElement element, string name, int defaultValue)
{
if (element?.Attribute(name) == null) return defaultValue;
int val = defaultValue;
try
{
val = Int32.Parse(element.Attribute(name).Value);
}
catch (Exception e)
{
DebugConsole.ThrowError("Error in " + element + "! ", e);
}
return val;
}
public static bool GetAttributeBool(this XElement element, string name, bool defaultValue)
{
if (element?.Attribute(name) == null) return defaultValue;
return element.Attribute(name).GetAttributeBool(defaultValue);
}
public static bool GetAttributeBool(this XAttribute attribute, bool defaultValue)
{
if (attribute == null) return defaultValue;
string val = attribute.Value.ToLowerInvariant().Trim();
if (val == "true")
{
return true;
}
if (val == "false")
{
return false;
}
DebugConsole.ThrowError("Error in " + attribute.Value.ToString() + "! \"" + val + "\" is not a valid boolean value");
return false;
}
public static Vector2 GetAttributeVector2(this XElement element, string name, Vector2 defaultValue)
{
if (element?.Attribute(name) == null) return defaultValue;
string val = element.Attribute(name).Value;
return ParseToVector2(val);
}
public static Vector3 GetAttributeVector3(this XElement element, string name, Vector3 defaultValue)
{
if (element == null || element.Attribute(name) == null) return defaultValue;
string val = element.Attribute(name).Value;
return ParseToVector3(val);
}
public static Vector4 GetAttributeVector4(this XElement element, string name, Vector4 defaultValue)
{
if (element == null || element.Attribute(name) == null) return defaultValue;
string val = element.Attribute(name).Value;
return ParseToVector4(val);
}
public static string ElementInnerText(this XElement el)
{
StringBuilder str = new StringBuilder();
foreach (XNode element in el.DescendantNodes().Where(x => x.NodeType == XmlNodeType.Text))
{
str.Append(element.ToString());
}
return str.ToString();
}
public static Vector2 ParseToVector2(string stringVector2, bool errorMessages = true)
{
string[] components = stringVector2.Split(',');
Vector2 vector = Vector2.Zero;
if (components.Length != 2)
{
if (!errorMessages) return vector;
DebugConsole.ThrowError("Failed to parse the string \"" + stringVector2 + "\" to Vector2");
return vector;
}
Single.TryParse(components[0], NumberStyles.Any, CultureInfo.InvariantCulture, out vector.X);
Single.TryParse(components[1], NumberStyles.Any, CultureInfo.InvariantCulture, out vector.Y);
return vector;
}
public static string Vector2ToString(Vector2 vector)
{
return vector.X.ToString("G", CultureInfo.InvariantCulture) + "," + vector.Y.ToString("G", CultureInfo.InvariantCulture);
}
public static Vector3 ParseToVector3(string stringVector3, bool errorMessages = true)
{
string[] components = stringVector3.Split(',');
Vector3 vector = Vector3.Zero;
if (components.Length != 3)
{
if (!errorMessages) return vector;
DebugConsole.ThrowError("Failed to parse the string \"" + stringVector3 + "\" to Vector3");
return vector;
}
Single.TryParse(components[0], NumberStyles.Any, CultureInfo.InvariantCulture, out vector.X);
Single.TryParse(components[1], NumberStyles.Any, CultureInfo.InvariantCulture, out vector.Y);
Single.TryParse(components[2], NumberStyles.Any, CultureInfo.InvariantCulture, out vector.Z);
return vector;
}
public static Vector4 ParseToVector4(string stringVector4, bool errorMessages = true)
{
string[] components = stringVector4.Split(',');
Vector4 vector = Vector4.Zero;
if (components.Length < 3)
{
if (errorMessages) DebugConsole.ThrowError("Failed to parse the string \"" + stringVector4 + "\" to Vector4");
return vector;
}
Single.TryParse(components[0], NumberStyles.Float, CultureInfo.InvariantCulture, out vector.X);
Single.TryParse(components[1], NumberStyles.Float, CultureInfo.InvariantCulture, out vector.Y);
Single.TryParse(components[2], NumberStyles.Float, CultureInfo.InvariantCulture, out vector.Z);
if (components.Length > 3)
Single.TryParse(components[3], NumberStyles.Float, CultureInfo.InvariantCulture, out vector.W);
return vector;
}
public static string Vector4ToString(Vector4 vector, string format = "G")
{
return vector.X.ToString(format, CultureInfo.InvariantCulture) + "," +
vector.Y.ToString(format, CultureInfo.InvariantCulture) + "," +
vector.Z.ToString(format, CultureInfo.InvariantCulture) + "," +
vector.W.ToString(format, CultureInfo.InvariantCulture);
}
public static float[] ParseArrayToFloat(string[] stringArray)
{
if (stringArray == null || stringArray.Length == 0) return null;
float[] floatArray = new float[stringArray.Length];
for (int i = 0; i < floatArray.Length; i++)
{
floatArray[i] = 0.0f;
Single.TryParse(stringArray[i], NumberStyles.Float, CultureInfo.InvariantCulture, out floatArray[i]);
}
return floatArray;
}
}
}