Client LuaCsForBarotrauma
AfflictionsFile.cs
1 using Microsoft.Xna.Framework;
2 using System;
3 using System.Collections.Generic;
4 using System.Collections.Immutable;
5 using System.Linq;
6 using System.Reflection;
7 using System.Xml.Linq;
9 
10 namespace Barotrauma
11 {
12  [RequiredByCorePackage]
14  {
15  private readonly static ImmutableHashSet<Type> afflictionTypes;
16  static AfflictionsFile()
17  {
18  afflictionTypes = ReflectionUtils.GetDerivedNonAbstract<Affliction>()
19  .ToImmutableHashSet();
20  }
21 
22  public AfflictionsFile(ContentPackage contentPackage, ContentPath path) : base(contentPackage, path) { }
23 
24  private void ParseElement(ContentXElement element, bool overriding)
25  {
26  Identifier elementName = element.NameAsIdentifier();
27  if (element.IsOverride())
28  {
29  element.Elements().ForEach(s => ParseElement(s, overriding: true));
30  }
31  else if (elementName == "Afflictions")
32  {
33  element.Elements().ForEach(s => ParseElement(s, overriding: overriding));
34  }
35  else if (elementName == "cprsettings")
36  {
37  var cprSettings = new CPRSettings(element, this);
38  CPRSettings.Prefabs.Add(cprSettings, overriding);
39  }
40  else if (elementName == "damageoverlay")
41  {
42 #if CLIENT
43  var damageOverlay = new CharacterHealth.DamageOverlayPrefab(element, this);
44  CharacterHealth.DamageOverlayPrefab.Prefabs.Add(damageOverlay, overriding);
45 #endif
46  }
47  else
48  {
49  Identifier identifier = element.GetAttributeIdentifier("identifier", Identifier.Empty);
50  if (identifier.IsEmpty)
51  {
52  DebugConsole.ThrowError(
53  $"No identifier defined for the affliction '{elementName}' in file '{Path}'",
54  contentPackage: element?.ContentPackage);
55  return;
56  }
57 
58  if (AfflictionPrefab.Prefabs.ContainsKey(identifier))
59  {
60  if (overriding)
61  {
62  DebugConsole.NewMessage(
63  $"Overriding an affliction or a buff with the identifier '{identifier}' using the file '{Path}'",
64  Color.MediumPurple);
65  }
66  else
67  {
68  DebugConsole.ThrowError(
69  $"Duplicate affliction: '{identifier}' defined in {elementName} of '{Path}'",
70  contentPackage: element?.ContentPackage);
71  return;
72  }
73  }
74 
75  var type = afflictionTypes.FirstOrDefault(t =>
76  t.Name == elementName
77  || t.Name == $"Affliction{elementName}".ToIdentifier())
78  ?? typeof(Affliction);
79  var prefab = CreatePrefab(element, type);
80  AfflictionPrefab.Prefabs.Add(prefab, overriding);
81  }
82  }
83 
84  public override void LoadFile()
85  {
86  XDocument doc = XMLExtensions.TryLoadXml(Path);
87  if (doc?.Root is null) { return; }
88  ParseElement(doc.Root.FromPackage(ContentPackage), overriding: false);
89  }
90 
91  private AfflictionPrefab CreatePrefab(ContentXElement element, Type type)
92  {
93  if (type == typeof(AfflictionHusk)) { return new AfflictionPrefabHusk(element, this, type); }
94  return new AfflictionPrefab(element, this, type);
95  }
96 
97  public override void UnloadFile()
98  {
99 #if CLIENT
100  CharacterHealth.DamageOverlayPrefab.Prefabs.RemoveByFile(this);
101 #endif
102  CPRSettings.Prefabs.RemoveByFile(this);
103  AfflictionPrefab.Prefabs.RemoveByFile(this);
104  }
105 
106  public override void Sort()
107  {
108 #if CLIENT
110 #endif
111  CPRSettings.Prefabs.Sort();
112  AfflictionPrefab.Prefabs.SortAll();
113  }
114  }
115 }
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)