2 using System.Collections.Generic;
6 using XmlWriterSettings = System.Xml.XmlWriterSettings;
11 public static class CreatureMetrics
13 private const string path =
"creature_metrics.xml";
18 public static HashSet<Identifier> RecentlyEncountered {
get;
private set; } =
new HashSet<Identifier>();
19 public static HashSet<Identifier> Encountered {
get;
private set; } =
new HashSet<Identifier>();
20 public static HashSet<Identifier> Unlocked {
get;
private set; } =
new HashSet<Identifier>();
21 public static HashSet<Identifier> Killed {
get;
private set; } =
new HashSet<Identifier>();
22 public static bool IsInitialized {
get;
private set; }
23 public static bool UnlockAll {
get;
set; }
25 public static void Init()
28 if (File.Exists(path))
35 private static void Load()
37 XDocument doc = XMLExtensions.TryLoadXml(path);
38 XElement? root = doc?.Root;
41 DebugConsole.AddWarning($
"Failed to load creature metrics from {path}!");
44 UnlockAll = root.GetAttributeBool(nameof(UnlockAll), UnlockAll);
45 Unlocked =
new HashSet<Identifier>(root.GetAttributeIdentifierArray(nameof(Unlocked), Array.Empty<Identifier>()));
46 Encountered =
new HashSet<Identifier>(root.GetAttributeIdentifierArray(nameof(Encountered), Array.Empty<Identifier>()));
47 Killed =
new HashSet<Identifier>(root.GetAttributeIdentifierArray(nameof(Killed), Array.Empty<Identifier>()));
51 public static void Save()
55 throw new Exception(
"Creature Metrics not yet initialized!");
58 XDocument configDoc =
new XDocument();
59 XElement root =
new XElement(
"CreatureMetrics");
61 root.SetAttributeValue(nameof(UnlockAll), UnlockAll);
62 root.SetAttributeValue(nameof(Unlocked),
string.Join(
",", Unlocked).Trim().ToLowerInvariant());
63 root.SetAttributeValue(nameof(Encountered),
string.Join(
",", Encountered).Trim().ToLowerInvariant());
64 root.SetAttributeValue(nameof(Killed),
string.Join(
",", Killed).Trim().ToLowerInvariant());
65 configDoc.SaveSafe(path);
66 XmlWriterSettings settings =
new XmlWriterSettings
69 OmitXmlDeclaration =
true,
70 NewLineOnAttributes =
true
75 configDoc.WriteTo(writer);
80 DebugConsole.ThrowError(
"Saving creature metrics failed.", e);
81 GameAnalyticsManager.AddErrorEventOnce(
"CreatureMetrics.Save:SaveFailed", GameAnalyticsManager.ErrorSeverity.Error,
82 "Saving creature metrics failed.\n" + e.Message +
"\n" + e.StackTrace.CleanupStackTrace());
86 public static void RecordKill(Identifier species)
88 AddEncounter(species);
89 if (!Killed.Contains(species))
95 public static void AddEncounter(Identifier species)
97 if (species == CharacterPrefab.HumanSpeciesName) {
return; }
98 if (Encountered.Contains(species)) {
return; }
99 Encountered.Add(species);
100 RecentlyEncountered.Add(species);
101 UnlockInEditor(species);
104 private static IEnumerable<CharacterFile>? vanillaCharacters;
105 public static void UnlockInEditor(Identifier species)
107 if (species == CharacterPrefab.HumanSpeciesName) {
return; }
108 if (Unlocked.Contains(species)) {
return; }
109 vanillaCharacters ??= GameMain.VanillaContent.GetFiles<CharacterFile>();
110 var contentFile = CharacterPrefab.FindBySpeciesName(species);
111 if (contentFile ==
null) {
return; }
112 if (!vanillaCharacters.Contains(contentFile.ContentFile))
117 Unlocked.Add(species);
120 private static void SyncSets()
124 foreach (var species
in Killed)
126 Encountered.Add(species);
128 foreach (var species
in Encountered)
130 Unlocked.Add(species);
static XmlWriter Create(string path, System.Xml.XmlWriterSettings settings)