using System.Collections.Generic; using System.Collections.Immutable; using System.IO; using System.Xml.Linq; namespace Barotrauma { internal static class CampaignModePresets { public static readonly ImmutableArray List; public static readonly ImmutableDictionary Definitions; private static readonly string fileListPath = Path.Combine("Data", "campaignsettings.xml"); static CampaignModePresets() { if (!File.Exists(fileListPath) || !(XMLExtensions.TryLoadXml(fileListPath)?.Root is { } docRoot)) { List = ImmutableArray.Empty; return; } List list = new List(); Dictionary definitions = new Dictionary(); foreach (XElement element in docRoot.Elements()) { Identifier name = element.NameAsIdentifier(); if (name == CampaignSettings.LowerCaseSaveElementName) { list.Add(new CampaignSettings(element)); } else if (name == nameof(CampaignSettingDefinitions)) { foreach (XElement subElement in element.Elements()) { definitions.Add(subElement.NameAsIdentifier(), new CampaignSettingDefinitions(subElement)); } } } List = list.ToImmutableArray(); Definitions = definitions.ToImmutableDictionary(); } } internal readonly struct CampaignSettingDefinitions { // Definitely not the best way to do this private readonly ImmutableDictionary> values; public CampaignSettingDefinitions(XElement element) { var definitions = new Dictionary>(); foreach (XAttribute attribute in element.Attributes()) { Identifier name = attribute.NameAsIdentifier(); if (attribute.Value.Contains('.')) { definitions.Add(name, element.GetAttributeFloat(name.Value, 0)); } else { definitions.Add(name, element.GetAttributeInt(name.Value, 0)); } } values = definitions.ToImmutableDictionary(); } public float GetFloat(Identifier identifier) { float range = 0; if (!values.TryGetValue(identifier, out Either value) || !value.TryGet(out range)) { DebugConsole.ThrowError($"CampaignSettings: Can't find value for {identifier}"); } return range; } public int GetInt(Identifier identifier) { int integer = 0; if (!values.TryGetValue(identifier, out Either value) || !value.TryGet(out integer)) { DebugConsole.ThrowError($"CampaignSettings: Can't find value for {identifier}"); } return integer; } } }