3 using System.Collections.Generic;
4 using System.Globalization;
9 internal partial class CampaignMetadata
11 private readonly Dictionary<Identifier, object> data =
new Dictionary<Identifier, object>();
13 public CampaignMetadata()
17 public void Load(XElement element)
20 foreach (var subElement
in element.Elements())
22 if (
string.Equals(subElement.Name.ToString(),
"data", StringComparison.InvariantCultureIgnoreCase))
24 Identifier identifier = subElement.GetAttributeIdentifier(
"key", Identifier.Empty);
25 string value = subElement.GetAttributeString(
"value",
string.Empty);
26 string valueType = subElement.GetAttributeString(
"type",
string.Empty);
28 if (identifier.IsEmpty ||
string.IsNullOrWhiteSpace(value) ||
string.IsNullOrWhiteSpace(valueType))
30 DebugConsole.ThrowError(
"Unable to load value because one or more of the required attributes are empty.\n" +
31 $
"key: \"{identifier}\", value: \"{value}\", type: \"{valueType}\"");
35 Type? type = ReflectionUtils.GetType(valueType);
39 DebugConsole.ThrowError($
"Type for {identifier} not found ({valueType}).");
42 else if (type == typeof(Identifier))
44 data.Add(identifier, value.ToIdentifier());
50 data.Add(identifier, Convert.ChangeType(value, type, NumberFormatInfo.InvariantInfo));
54 DebugConsole.ThrowError($
"Failed to change the type of the value \"{value}\" to {type}.", e);
61 public void SetValue(Identifier identifier,
object value)
63 DebugConsole.Log($
"Set the value \"{identifier}\" to {value}");
65 AchievementManager.OnCampaignMetadataSet(identifier, value, unlockClients:
true);
67 if (!data.ContainsKey(identifier))
69 data.Add(identifier, value);
73 data[identifier] = value;
76 public float GetFloat(Identifier identifier,
float? defaultValue =
null)
78 return (
float)GetTypeOrDefault(identifier, typeof(
float), defaultValue ?? 0f);
81 public int GetInt(Identifier identifier,
int? defaultValue =
null)
83 return (
int)GetTypeOrDefault(identifier, typeof(
int), defaultValue ?? 0);
86 public bool GetBoolean(Identifier identifier,
bool? defaultValue =
null)
88 return (
bool)GetTypeOrDefault(identifier, typeof(
bool), defaultValue ??
false);
91 public string GetString(Identifier identifier,
string? defaultValue =
null)
93 return (
string)GetTypeOrDefault(identifier, typeof(
string), defaultValue ??
string.Empty);
96 public bool HasKey(Identifier identifier)
98 return data.ContainsKey(identifier);
101 private object GetTypeOrDefault(Identifier identifier, Type type,
object defaultValue)
103 object? value = GetValue(identifier);
106 if (value.GetType() == type)
112 DebugConsole.ThrowError($
"Attempted to get value \"{identifier}\" as a {type} but the value is {value.GetType()}.");
118 public object? GetValue(Identifier identifier)
120 return data.ContainsKey(identifier) ? data[identifier] :
null;
123 public void Save(XElement modeElement)
125 XElement element =
new XElement(
"Metadata");
127 foreach (var (key, value) in data)
129 string valueStr = value.ToString() ??
throw new NullReferenceException();
130 if (value is
float f)
132 valueStr = f.ToString(
"G", CultureInfo.InvariantCulture);
135 element.Add(
new XElement(
"Data",
136 new XAttribute(
"key", key),
137 new XAttribute(
"value", valueStr),
138 new XAttribute(
"type", value.GetType().FullName ??
"")));
141 DebugConsole.Log(element.ToString());
143 modeElement.Add(element);