Option to define multiple compatible content packages in submarine files, added extension methods for parsing string, float and int arrays from XML elements

This commit is contained in:
Joonas Rikkonen
2018-02-06 10:18:39 +02:00
parent ec302e099b
commit 633dc1ff1e
6 changed files with 118 additions and 16 deletions
@@ -88,7 +88,7 @@ namespace Barotrauma
public int RecommendedCrewSizeMin = 1, RecommendedCrewSizeMax = 2;
public string RecommendedCrewExperience;
public string ContentPackage;
public HashSet<string> CompatibleContentPackages = new HashSet<string>();
//properties ----------------------------------------------------
@@ -286,7 +286,11 @@ namespace Barotrauma
RecommendedCrewSizeMin = doc.Root.GetAttributeInt("recommendedcrewsizemin", 0);
RecommendedCrewSizeMax = doc.Root.GetAttributeInt("recommendedcrewsizemax", 0);
RecommendedCrewExperience = doc.Root.GetAttributeString("recommendedcrewexperience", "Unknown");
ContentPackage = doc.Root.GetAttributeString("contentpackage", "Unknown");
string[] contentPackageNames = doc.Root.GetAttributeStringArray("compatiblecontentpackages", new string[0]);
foreach (string contentPackageName in contentPackageNames)
{
CompatibleContentPackages.Add(contentPackageName);
}
#if CLIENT
string previewImageData = doc.Root.GetAttributeString("previewimage", "");
@@ -1201,7 +1205,7 @@ namespace Barotrauma
element.Add(new XAttribute("recommendedcrewsizemin", RecommendedCrewSizeMin));
element.Add(new XAttribute("recommendedcrewsizemax", RecommendedCrewSizeMax));
element.Add(new XAttribute("recommendedcrewexperience", RecommendedCrewExperience ?? ""));
element.Add(new XAttribute("contentpackage", ContentPackage ?? ""));
element.Add(new XAttribute("compatiblecontentpackages", string.Join(", ", CompatibleContentPackages)));
foreach (MapEntity e in MapEntity.mapEntityList)
{
@@ -75,6 +75,25 @@ namespace Barotrauma
return String.IsNullOrEmpty(value) ? defaultValue : value;
}
public static string[] GetAttributeStringArray(this XElement element, string name, string[] defaultValue, bool trim = true)
{
if (element?.Attribute(name) == null) return defaultValue;
string stringValue = element.Attribute(name).Value;
if (string.IsNullOrEmpty(stringValue)) return defaultValue;
string[] splitValue = stringValue.Split(',');
if (trim)
{
for (int i = 0; i < splitValue.Length; i++)
{
splitValue[i] = splitValue[i].Trim();
}
}
return splitValue;
}
public static float GetAttributeFloat(this XElement element, float defaultValue, params string[] matchingAttributeName)
{
if (element == null) return defaultValue;
@@ -143,6 +162,31 @@ namespace Barotrauma
return val;
}
public static float[] GetAttributeFloatArray(this XElement element, string name, float[] defaultValue)
{
if (element?.Attribute(name) == null) return defaultValue;
string stringValue = element.Attribute(name).Value;
if (string.IsNullOrEmpty(stringValue)) return defaultValue;
string[] splitValue = stringValue.Split(',');
float[] floatValue = new float[splitValue.Length];
for (int i = 0; i < splitValue.Length; i++)
{
try
{
float val = Single.Parse(splitValue[i], CultureInfo.InvariantCulture);
floatValue[i] = val;
}
catch (Exception e)
{
DebugConsole.ThrowError("Error in " + element + "! ", e);
}
}
return floatValue;
}
public static int GetAttributeInt(this XElement element, string name, int defaultValue)
{
if (element?.Attribute(name) == null) return defaultValue;
@@ -161,6 +205,31 @@ namespace Barotrauma
return val;
}
public static int[] GetAttributeIntArray(this XElement element, string name, int[] defaultValue)
{
if (element?.Attribute(name) == null) return defaultValue;
string stringValue = element.Attribute(name).Value;
if (string.IsNullOrEmpty(stringValue)) return defaultValue;
string[] splitValue = stringValue.Split(',');
int[] intValue = new int[splitValue.Length];
for (int i = 0; i < splitValue.Length; i++)
{
try
{
int val = Int32.Parse(splitValue[i]);
intValue[i] = val;
}
catch (Exception e)
{
DebugConsole.ThrowError("Error in " + element + "! ", e);
}
}
return intValue;
}
public static bool GetAttributeBool(this XElement element, string name, bool defaultValue)
{
if (element?.Attribute(name) == null) return defaultValue;