Unstable 0.17.0.0

This commit is contained in:
Markus Isberg
2022-02-26 02:43:01 +09:00
parent a83f375681
commit 3974067915
913 changed files with 32472 additions and 32364 deletions
@@ -10,7 +10,7 @@ namespace Barotrauma
{
public CampaignMode Campaign { get; }
private readonly Dictionary<string, object> data = new Dictionary<string, object>();
private readonly Dictionary<Identifier, object> data = new Dictionary<Identifier, object>();
public CampaignMetadata(CampaignMode campaign)
{
@@ -21,15 +21,15 @@ namespace Barotrauma
{
Campaign = campaign;
foreach (XElement subElement in element.Elements())
foreach (var subElement in element.Elements())
{
if (string.Equals(subElement.Name.ToString(), "data", StringComparison.InvariantCultureIgnoreCase))
{
string identifier = subElement.GetAttributeString("key", string.Empty).ToLowerInvariant();
Identifier identifier = subElement.GetAttributeIdentifier("key", Identifier.Empty);
string value = subElement.GetAttributeString("value", string.Empty);
string valueType = subElement.GetAttributeString("type", string.Empty);
if (string.IsNullOrWhiteSpace(identifier) || string.IsNullOrWhiteSpace(value) || string.IsNullOrWhiteSpace(valueType))
if (identifier.IsEmpty || string.IsNullOrWhiteSpace(value) || string.IsNullOrWhiteSpace(valueType))
{
DebugConsole.ThrowError("Unable to load value because one or more of the required attributes are empty.\n" +
$"key: \"{identifier}\", value: \"{value}\", type: \"{valueType}\"");
@@ -43,28 +43,20 @@ namespace Barotrauma
DebugConsole.ThrowError($"Type for {identifier} not found ({valueType}).");
continue;
}
if (type == typeof(float))
else if (type == typeof(Identifier))
{
if (!float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out float floatValue))
{
DebugConsole.ThrowError($"Error in campaign metadata: could not parse \"{value}\" as a float.");
continue;
}
data.Add(identifier, floatValue);
data.Add(identifier, value.ToIdentifier());
}
else
{
data.Add(identifier, Convert.ChangeType(value, type));
data.Add(identifier, Convert.ChangeType(value, type, NumberFormatInfo.InvariantInfo));
}
}
}
}
public void SetValue(string identifier, object value)
public void SetValue(Identifier identifier, object value)
{
identifier = identifier.ToLowerInvariant();
DebugConsole.Log($"Set the value \"{identifier}\" to {value}");
if (!data.ContainsKey(identifier))
@@ -76,33 +68,32 @@ namespace Barotrauma
data[identifier] = value;
}
public float GetFloat(string identifier, float? defaultValue = null)
public float GetFloat(Identifier identifier, float? defaultValue = null)
{
return (float)GetTypeOrDefault(identifier, typeof(float), defaultValue ?? 0f);
}
public int GetInt(string identifier, int? defaultValue = null)
public int GetInt(Identifier identifier, int? defaultValue = null)
{
return (int)GetTypeOrDefault(identifier, typeof(int), defaultValue ?? 0);
}
public bool GetBoolean(string identifier, bool? defaultValue = null)
public bool GetBoolean(Identifier identifier, bool? defaultValue = null)
{
return (bool)GetTypeOrDefault(identifier, typeof(bool), defaultValue ?? false);
}
public string GetString(string identifier, string? defaultValue = null)
public string GetString(Identifier identifier, string? defaultValue = null)
{
return (string)GetTypeOrDefault(identifier, typeof(string), defaultValue ?? string.Empty);
}
public bool HasKey(string identifier)
public bool HasKey(Identifier identifier)
{
identifier = identifier.ToLowerInvariant();
return data.ContainsKey(identifier);
}
private object GetTypeOrDefault(string identifier, Type type, object defaultValue)
private object GetTypeOrDefault(Identifier identifier, Type type, object defaultValue)
{
object? value = GetValue(identifier);
@@ -122,7 +113,7 @@ namespace Barotrauma
return defaultValue;
}
public object? GetValue(string identifier)
public object? GetValue(Identifier identifier)
{
return data.ContainsKey(identifier) ? data[identifier] : null;
}
@@ -133,16 +124,16 @@ namespace Barotrauma
foreach (var (key, value) in data)
{
string valueStr = value?.ToString() ?? "";
if (value?.GetType() == typeof(float))
string valueStr = value.ToString() ?? throw new NullReferenceException();
if (value is float f)
{
valueStr = ((float)value).ToString("G", CultureInfo.InvariantCulture);
valueStr = f.ToString("G", CultureInfo.InvariantCulture);
}
element.Add(new XElement("Data",
new XAttribute("key", key),
new XAttribute("value", valueStr),
new XAttribute("type", value?.GetType())));
new XAttribute("type", value.GetType())));
}
#if DEBUG
DebugConsole.Log(element.ToString());