Client LuaCsForBarotrauma
CharacterPrefab.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Xml.Linq;
5 using Microsoft.Xna.Framework;
6 
7 namespace Barotrauma
8 {
10  {
12 
13  public override void Dispose()
14  {
16  }
17 
18  public string Name => Identifier.Value;
19  public Identifier VariantOf { get; }
20  public CharacterPrefab ParentPrefab { get; set; }
21 
23  {
24  if (!VariantOf.IsEmpty)
25  {
26  speciesName = VariantOf;
27  if (ParentPrefab is { VariantOf.IsEmpty: false } parentPrefab)
28  {
29  speciesName = parentPrefab.GetBaseCharacterSpeciesName(speciesName);
30  }
31  }
32  return speciesName;
33  }
34 
35  public bool HasCharacterInfo { get; private set; }
36 
37  public void InheritFrom(CharacterPrefab parent)
38  {
40  ParseConfigElement();
41  }
42 
43  private void ParseConfigElement()
44  {
45  var headsElement = ConfigElement.GetChildElement("Heads");
46  var varsElement = ConfigElement.GetChildElement("Vars");
47  var menuCategoryElement = ConfigElement.GetChildElement("MenuCategory");
48  var pronounsElement = ConfigElement.GetChildElement("Pronouns");
49 
50  HasCharacterInfo = headsElement != null || ConfigElement.GetAttributeBool(nameof(HasCharacterInfo), false);
51  if (HasCharacterInfo)
52  {
53  CharacterInfoPrefab = new CharacterInfoPrefab(this, headsElement, varsElement, menuCategoryElement, pronounsElement);
54  }
55  }
56 
57  private readonly XElement originalElement;
58  public ContentXElement ConfigElement { get; private set; }
59 
60  public CharacterInfoPrefab CharacterInfoPrefab { get; private set; }
61 
62  public static IEnumerable<ContentXElement> ConfigElements => Prefabs.Select(p => p.ConfigElement);
63 
64  public static readonly Identifier HumanSpeciesName = "human".ToIdentifier();
65  public static readonly Identifier HumanGroup = "human".ToIdentifier();
66 
69 
70  public static CharacterPrefab FindBySpeciesName(Identifier speciesName)
71  {
72  if (!Prefabs.ContainsKey(speciesName)) { return null; }
73  return Prefabs[speciesName];
74  }
75 
76  public static CharacterPrefab FindByFilePath(string filePath)
77  {
78  return Prefabs.Find(p => p.ContentFile.Path == filePath);
79  }
80 
81  public static CharacterPrefab Find(Predicate<CharacterPrefab> predicate)
82  {
83  return Prefabs.Find(predicate);
84  }
85 
86  public CharacterPrefab(ContentXElement mainElement, CharacterFile file) : base(file, ParseName(mainElement, file))
87  {
88  originalElement = mainElement;
89  ConfigElement = mainElement;
90  VariantOf = mainElement.VariantOf();
91 
92  ParseConfigElement();
93  }
94 
95  public static Identifier ParseName(XElement element, CharacterFile file)
96  {
97  string name = element.GetAttributeString("name", null);
98  if (!string.IsNullOrEmpty(name))
99  {
100  DebugConsole.NewMessage($"Error in {file.Path}: 'name' is deprecated! Use 'speciesname' instead.", Color.Orange);
101  }
102  else
103  {
104  name = element.GetAttributeString("speciesname", string.Empty);
105  }
106  return new Identifier(name);
107  }
108 
109  public static bool CheckSpeciesName(XElement mainElement, CharacterFile file, out Identifier name)
110  {
111  name = ParseName(mainElement, file);
112  if (name == Identifier.Empty)
113  {
114  DebugConsole.ThrowError($"No species name defined for: {file.Path}",
115  contentPackage: file.ContentPackage);
116  return false;
117  }
118  return true;
119  }
120  }
121 }
static void RemoveByPrefab(CharacterPrefab prefab)
Contains character data that should be editable in the character editor.
static XElement CreateVariantXml(XElement variantXML, XElement baseXML)
static bool CheckSpeciesName(XElement mainElement, CharacterFile file, out Identifier name)
static readonly Identifier HumanGroup
static IEnumerable< ContentXElement > ConfigElements
static CharacterPrefab FindByFilePath(string filePath)
static CharacterFile HumanConfigFile
void InheritFrom(CharacterPrefab parent)
CharacterPrefab(ContentXElement mainElement, CharacterFile file)
static CharacterPrefab Find(Predicate< CharacterPrefab > predicate)
Identifier GetBaseCharacterSpeciesName(Identifier speciesName)
static CharacterPrefab FindBySpeciesName(Identifier speciesName)
CharacterInfoPrefab CharacterInfoPrefab
ContentXElement ConfigElement
static Identifier ParseName(XElement element, CharacterFile file)
CharacterPrefab ParentPrefab
static readonly PrefabCollection< CharacterPrefab > Prefabs
static readonly Identifier HumanSpeciesName
readonly ContentPackage ContentPackage
Definition: ContentFile.cs:136
ContentPackage? ContentPackage
ContentXElement? GetChildElement(string name)
bool GetAttributeBool(string key, bool def)
readonly ContentFile ContentFile
Definition: Prefab.cs:35
readonly Identifier Identifier
Definition: Prefab.cs:34
Prefab that has a property serves as a deterministic hash of a prefab's identifier....