Client LuaCsForBarotrauma
TalentMigration.cs
1 #nullable enable
2 
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 
7 namespace Barotrauma
8 {
9  internal abstract class TalentMigration
10  {
11  private readonly Version version;
12 
13  private delegate TalentMigration TalentMigrationCtor(Version version, ContentXElement element);
14 
15  private static readonly Dictionary<Identifier, TalentMigrationCtor> migrationTemplates =
16  new()
17  {
18  [new Identifier("AddStat")] =
19  static (version, element) => new TalentMigrationAddStat(version, element),
20 
21  [new Identifier("UpdateStatIdentifier")] =
22  static (version, element) => new TalentMigrationUpdateStatIdentifier(version, element)
23  };
24 
25  public bool TryApply(Version savedVersion, CharacterInfo info)
26  {
27  if (version <= savedVersion) { return false; }
28  Apply(info);
29  return true;
30  }
31 
32  protected abstract void Apply(CharacterInfo info);
33 
34  protected TalentMigration(Version targetVersion)
35  {
36  version = targetVersion;
37  }
38 
39  public static TalentMigration FromXML(ContentXElement element)
40  {
41  Version? version = XMLExtensions.GetAttributeVersion(element, "version", null);
42 
43  if (version is null)
44  {
45  throw new Exception("Talent migration version not defined.");
46  }
47 
48  Identifier name = element.Name.ToString().ToIdentifier();
49 
50  if (!migrationTemplates.TryGetValue(name, out TalentMigrationCtor? ctor))
51  {
52  throw new Exception($"Unknown talent migration type: {name}.");
53  }
54 
55  return ctor(version, element);
56  }
57  }
58 
62  internal sealed class TalentMigrationAddStat : TalentMigration
63  {
64  [Serialize(StatTypes.None, IsPropertySaveable.Yes)]
65  public StatTypes StatType { get; set; }
66 
67  [Serialize("", IsPropertySaveable.Yes)]
68  public Identifier StatIdentifier { get; set; }
69 
70  [Serialize(0f, IsPropertySaveable.Yes)]
71  public float Value { get; set; }
72 
73  [Serialize(false, IsPropertySaveable.Yes)]
74  public bool RemoveOnDeath { get; set; }
75 
76  public TalentMigrationAddStat(Version targetVersion, ContentXElement element) : base(targetVersion)
77  => SerializableProperty.DeserializeProperties(this, element);
78 
79  protected override void Apply(CharacterInfo info)
80  {
81  info.ChangeSavedStatValue(StatType, Value, StatIdentifier, RemoveOnDeath);
82  }
83  }
84 
88  internal class TalentMigrationUpdateStatIdentifier : TalentMigration
89  {
90  [Serialize("", IsPropertySaveable.Yes, "The old identifier to update.")]
91  public Identifier Old { get; set; }
92 
93  [Serialize("", IsPropertySaveable.Yes, "What to change the old identifier to.")]
94  public Identifier New { get; set; }
95 
96  public TalentMigrationUpdateStatIdentifier(Version targetVersion, ContentXElement element) : base(targetVersion)
97  => SerializableProperty.DeserializeProperties(this, element);
98 
99  protected override void Apply(CharacterInfo info)
100  {
101  foreach (SavedStatValue statValue in info.SavedStatValues.Values.SelectMany(static s => s))
102  {
103  if (statValue.StatIdentifier != Old) { continue; }
104 
105  statValue.StatIdentifier = New;
106  }
107  }
108  }
109 }
StatTypes
StatTypes are used to alter several traits of a character. They are mostly used by talents.
Definition: Enums.cs:195