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 Identifier Group { get; private set; }
38 
39  public bool MatchesSpeciesNameOrGroup(Identifier speciesNameOrGroup) => Identifier == speciesNameOrGroup || Group == speciesNameOrGroup;
40 
41  public void InheritFrom(CharacterPrefab parent)
42  {
44  ParseConfigElement();
45  }
46 
47  private void ParseConfigElement()
48  {
49  var headsElement = ConfigElement.GetChildElement("Heads");
50  var varsElement = ConfigElement.GetChildElement("Vars");
51  var menuCategoryElement = ConfigElement.GetChildElement("MenuCategory");
52  var pronounsElement = ConfigElement.GetChildElement("Pronouns");
53 
54  HasCharacterInfo = headsElement != null || ConfigElement.GetAttributeBool(nameof(HasCharacterInfo), false);
55  if (HasCharacterInfo)
56  {
57  CharacterInfoPrefab = new CharacterInfoPrefab(this, headsElement, varsElement, menuCategoryElement, pronounsElement);
58  }
60  }
61 
62  private readonly ContentXElement originalElement;
63  public ContentXElement ConfigElement { get; private set; }
64 
65  public CharacterInfoPrefab CharacterInfoPrefab { get; private set; }
66 
67  public static IEnumerable<ContentXElement> ConfigElements => Prefabs.Select(p => p.ConfigElement);
68 
69  public static readonly Identifier HumanSpeciesName = "human".ToIdentifier();
70  public static readonly Identifier HumanGroup = "human".ToIdentifier();
71 
74 
75  public static CharacterPrefab FindBySpeciesName(Identifier speciesName)
76  {
77  if (!Prefabs.ContainsKey(speciesName)) { return null; }
78  return Prefabs[speciesName];
79  }
80 
81  public static CharacterPrefab FindByFilePath(string filePath)
82  {
83  return Prefabs.Find(p => p.ContentFile.Path == filePath);
84  }
85 
86  public static CharacterPrefab Find(Predicate<CharacterPrefab> predicate)
87  {
88  return Prefabs.Find(predicate);
89  }
90 
91  public CharacterPrefab(ContentXElement mainElement, CharacterFile file) : base(file, ParseName(mainElement, file))
92  {
93  originalElement = mainElement;
94  ConfigElement = mainElement;
95  VariantOf = mainElement.VariantOf();
96 
97  ParseConfigElement();
98  }
99 
100  public static Identifier ParseName(XElement element, CharacterFile file)
101  {
102  string name = element.GetAttributeString("name", null);
103  if (!string.IsNullOrEmpty(name))
104  {
105  DebugConsole.NewMessage($"Error in {file.Path}: 'name' is deprecated! Use 'speciesname' instead.", Color.Orange);
106  }
107  else
108  {
109  name = element.GetAttributeString("speciesname", string.Empty);
110  }
111  return new Identifier(name);
112  }
113 
114  public static bool CheckSpeciesName(XElement mainElement, CharacterFile file, out Identifier name)
115  {
116  name = ParseName(mainElement, file);
117  if (name == Identifier.Empty)
118  {
119  DebugConsole.ThrowError($"No species name defined for: {file.Path}",
120  contentPackage: file.ContentPackage);
121  return false;
122  }
123  return true;
124  }
125  }
126 }
static void RemoveByPrefab(CharacterPrefab prefab)
Contains character data that should be editable in the character editor.
static XElement CreateVariantXml(ContentXElement variantXML, ContentXElement 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
bool MatchesSpeciesNameOrGroup(Identifier speciesNameOrGroup)
static readonly Identifier HumanSpeciesName
readonly ContentPackage ContentPackage
Definition: ContentFile.cs:136
ContentPackage? ContentPackage
ContentXElement? GetChildElement(string name)
bool GetAttributeBool(string key, bool def)
Identifier GetAttributeIdentifier(string key, string 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....