Client LuaCsForBarotrauma
CampaignModePresets.cs
1 using System.Collections.Generic;
2 using System.Collections.Immutable;
3 using System.IO;
4 using System.Xml.Linq;
5 
6 namespace Barotrauma
7 {
8  internal static class CampaignModePresets
9  {
10  public static readonly ImmutableArray<CampaignSettings> List;
11  private static readonly ImmutableDictionary<Identifier, CampaignSettingDefinitions> definitions;
12 
13  private static readonly string fileListPath = Path.Combine("Data", "campaignsettings.xml");
14 
15  static CampaignModePresets()
16  {
17  if (!File.Exists(fileListPath) || !(XMLExtensions.TryLoadXml(fileListPath)?.Root is { } docRoot))
18  {
19  List = ImmutableArray<CampaignSettings>.Empty;
20  return;
21  }
22 
23  List<CampaignSettings> presetList = new List<CampaignSettings>();
24  Dictionary<Identifier, CampaignSettingDefinitions> tempDefinitions = new Dictionary<Identifier, CampaignSettingDefinitions>();
25 
26  foreach (XElement element in docRoot.Elements())
27  {
28  Identifier name = element.NameAsIdentifier();
29 
30  // The campaign setting presets
31  if (name == CampaignSettings.LowerCaseSaveElementName)
32  {
33  presetList.Add(new CampaignSettings(element));
34  }
35  // All the definitions for the setting value options
36  else if (name == nameof(CampaignSettingDefinitions))
37  {
38  // The single definitions that the settings may refer to (eg. PatdownProbabilityMin)
39  foreach (XElement subElement in element.Elements())
40  {
41  tempDefinitions.Add(subElement.NameAsIdentifier(), new CampaignSettingDefinitions(subElement));
42  }
43  }
44  }
45 
46  List = presetList.ToImmutableArray();
47  definitions = tempDefinitions.ToImmutableDictionary();
48  }
49 
50  public static bool TryGetAttribute(Identifier propertyName, Identifier attributeName, out XAttribute attribute)
51  {
52  attribute = null;
53  if (definitions.TryGetValue(propertyName, out CampaignSettingDefinitions definition))
54  {
55  if (definition.Attributes.TryGetValue(attributeName, out XAttribute att))
56  {
57  attribute = att;
58  return true;
59  }
60  }
61  return false;
62  }
63  }
64 
65  internal readonly struct CampaignSettingDefinitions
66  {
67  public readonly ImmutableDictionary<Identifier, XAttribute> Attributes;
68 
69  public CampaignSettingDefinitions(XElement element)
70  {
71  Attributes = element.Attributes().ToImmutableDictionary(
72  a => a.NameAsIdentifier(),
73  a => a
74  );
75  }
76  }
77 }