Client LuaCsForBarotrauma
AfflictionsFile.cs
2 using Microsoft.Xna.Framework;
3 using System;
4 using System.Collections.Immutable;
5 using System.Linq;
6 using System.Xml.Linq;
7 
8 namespace Barotrauma
9 {
10  [RequiredByCorePackage]
12  {
13  private readonly static ImmutableHashSet<Type> afflictionTypes;
14  static AfflictionsFile()
15  {
16  afflictionTypes = ReflectionUtils.GetDerivedNonAbstract<Affliction>()
17  .ToImmutableHashSet();
18  }
19 
20  public AfflictionsFile(ContentPackage contentPackage, ContentPath path) : base(contentPackage, path) { }
21 
22  private void ParseElement(ContentXElement element, bool overriding)
23  {
24  Identifier elementName = element.NameAsIdentifier();
25  if (element.IsOverride())
26  {
27  element.Elements().ForEach(s => ParseElement(s, overriding: true));
28  }
29  else if (elementName == "Afflictions")
30  {
31  element.Elements().ForEach(s => ParseElement(s, overriding: overriding));
32  }
33  else if (elementName == "cprsettings")
34  {
35  var cprSettings = new CPRSettings(element, this);
36  CPRSettings.Prefabs.Add(cprSettings, overriding);
37  }
38  else if (elementName == "damageoverlay")
39  {
40 #if CLIENT
41  var damageOverlay = new CharacterHealth.DamageOverlayPrefab(element, this);
42  CharacterHealth.DamageOverlayPrefab.Prefabs.Add(damageOverlay, overriding);
43 #endif
44  }
45  else
46  {
47  Identifier identifier = element.GetAttributeIdentifier("identifier", Identifier.Empty);
48  if (identifier.IsEmpty)
49  {
50  DebugConsole.ThrowError(
51  $"No identifier defined for the affliction '{elementName}' in file '{Path}'",
52  contentPackage: element?.ContentPackage);
53  return;
54  }
55 
56  if (AfflictionPrefab.Prefabs.TryGet(identifier, out var existingAffliction))
57  {
58  if (overriding)
59  {
60  DebugConsole.NewMessage(
61  $"Overriding an affliction or a buff with the identifier '{identifier}' using the version in '{element.ContentPackage.Name}'",
62  Color.MediumPurple);
63  }
64  else
65  {
66  DebugConsole.ThrowError(
67  $"Duplicate affliction: '{identifier}' defined in {element.ContentPackage.Name} is already defined in the previously loaded content package {existingAffliction.ContentPackage.Name}."+
68  $" You may need to adjust the mod load order to make sure {element.ContentPackage.Name} is loaded first.",
69  contentPackage: element?.ContentPackage);
70  return;
71  }
72  }
73 
74  var type = afflictionTypes.FirstOrDefault(t =>
75  t.Name == elementName
76  || t.Name == $"Affliction{elementName}".ToIdentifier())
77  ?? typeof(Affliction);
78  var prefab = CreatePrefab(element, type);
79  AfflictionPrefab.Prefabs.Add(prefab, overriding);
80  }
81  }
82 
83  public override void LoadFile()
84  {
85  XDocument doc = XMLExtensions.TryLoadXml(Path);
86  if (doc?.Root is null) { return; }
87  ParseElement(doc.Root.FromPackage(ContentPackage), overriding: false);
88  }
89 
90  private AfflictionPrefab CreatePrefab(ContentXElement element, Type type)
91  {
92  if (type == typeof(AfflictionHusk)) { return new AfflictionPrefabHusk(element, this, type); }
93  return new AfflictionPrefab(element, this, type);
94  }
95 
96  public override void UnloadFile()
97  {
98 #if CLIENT
99  CharacterHealth.DamageOverlayPrefab.Prefabs.RemoveByFile(this);
100 #endif
101  CPRSettings.Prefabs.RemoveByFile(this);
102  AfflictionPrefab.Prefabs.RemoveByFile(this);
103  }
104 
105  public override void Sort()
106  {
107 #if CLIENT
109 #endif
110  CPRSettings.Prefabs.Sort();
111  AfflictionPrefab.Prefabs.SortAll();
112  }
113  }
114 }
A special affliction type that gradually makes the character turn into another type of character....
AfflictionPrefab is a prefab that defines a type of affliction that can be applied to a character....
static readonly PrefabCollection< AfflictionPrefab > Prefabs
AfflictionPrefabHusk is a special type of affliction that has added functionality for husk infection.
AfflictionsFile(ContentPackage contentPackage, ContentPath path)
static readonly PrefabSelector< CPRSettings > Prefabs
Base class for content file types, which are loaded from filelist.xml via reflection....
Definition: ContentFile.cs:23
readonly ContentPackage ContentPackage
Definition: ContentFile.cs:136
Identifier NameAsIdentifier()
IEnumerable< ContentXElement > Elements()
Identifier GetAttributeIdentifier(string key, string def)