Client LuaCsForBarotrauma
CharacterFile.cs
2 using Microsoft.Xna.Framework;
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Xml.Linq;
7 
8 namespace Barotrauma
9 {
10  [RequiredByCorePackage]
11  sealed class CharacterFile : ContentFile
12  {
13  public CharacterFile(ContentPackage contentPackage, ContentPath path) : base(contentPackage, path) { }
14 
15  public override void LoadFile()
16  {
17  ClearCaches();
18  XDocument doc = XMLExtensions.TryLoadXml(Path);
19  if (doc == null)
20  {
21  DebugConsole.ThrowError($"Loading character file failed: {Path}", contentPackage: ContentPackage);
22  return;
23  }
24  if (CharacterPrefab.Prefabs.AllPrefabs.Any(kvp => kvp.Value.Any(cf => cf?.ContentFile == this)))
25  {
26  DebugConsole.ThrowError($"Duplicate path: {Path}", contentPackage: ContentPackage);
27  return;
28  }
29  var mainElement = doc.Root.FromPackage(ContentPackage);
30  bool isOverride = mainElement.IsOverride();
31  if (isOverride) { mainElement = mainElement.FirstElement(); }
32  if (!CharacterPrefab.CheckSpeciesName(mainElement, this, out Identifier n)) { return; }
33  var prefab = new CharacterPrefab(mainElement, this);
34  CharacterPrefab.Prefabs.Add(prefab, isOverride);
35  }
36 
37  public override void UnloadFile()
38  {
39  CharacterPrefab.Prefabs.RemoveByFile(this);
40  ClearCaches();
41  }
42 
43  private static void ClearCaches()
44  {
45  // Clear the caches to get rid of any overrides.
46  // Variants should have their own params instances, but let's keep it simple and play safe.
49  }
50 
51  public override void Sort()
52  {
53  CharacterPrefab.Prefabs.SortAll();
54  }
55 
56  public override void Preload(Action<Sprite> addPreloadedSprite)
57  {
58 #if CLIENT
59  CharacterPrefab characterPrefab = CharacterPrefab.FindByFilePath(Path.Value);
60  if (characterPrefab?.ConfigElement == null)
61  {
62  throw new Exception($"Failed to load the character config file from {Path}!");
63  }
64  var mainElement = characterPrefab.ConfigElement;
65  mainElement.GetChildElements("sound").ForEach(e => RoundSound.Load(e));
66  if (!CharacterPrefab.CheckSpeciesName(mainElement, this, out Identifier speciesName)) { return; }
67  bool humanoid = mainElement.GetAttributeBool("humanoid", false);
68  RagdollParams ragdollParams;
69  try
70  {
71  if (humanoid)
72  {
73  ragdollParams = RagdollParams.GetDefaultRagdollParams<HumanRagdollParams>(speciesName, mainElement, ContentPackage);
74  }
75  else
76  {
77  ragdollParams = RagdollParams.GetDefaultRagdollParams<FishRagdollParams>(speciesName, mainElement, ContentPackage);
78  }
79  }
80  catch (Exception e)
81  {
82  DebugConsole.ThrowError($"Failed to preload a ragdoll file for the character \"{characterPrefab.Name}\"", e,
83  contentPackage: characterPrefab.ContentPackage);
84  return;
85  }
86 
87  if (ragdollParams != null)
88  {
89  HashSet<ContentPath> texturePaths = new HashSet<ContentPath>();
90  AddTexturePath(ragdollParams.Texture);
91  foreach (RagdollParams.LimbParams limb in ragdollParams.Limbs)
92  {
93  AddTexturePath(limb.normalSpriteParams?.Texture);
94  AddTexturePath(limb.deformSpriteParams?.Texture);
95  AddTexturePath(limb.damagedSpriteParams?.Texture);
96  foreach (var decorativeSprite in limb.decorativeSpriteParams)
97  {
98  AddTexturePath(decorativeSprite.Texture);
99  }
100  }
101  foreach (ContentPath texturePath in texturePaths)
102  {
103  addPreloadedSprite(new Sprite(texturePath.Value, Vector2.Zero));
104  }
105 
106  void AddTexturePath(string path)
107  {
108  if (string.IsNullOrEmpty(path)) { return; }
109  texturePaths.Add(ContentPath.FromRaw(characterPrefab.ContentPackage, ragdollParams.Texture));
110  }
111  }
112 #endif
113  }
114  }
115 }
override void Sort()
override void UnloadFile()
override void Preload(Action< Sprite > addPreloadedSprite)
CharacterFile(ContentPackage contentPackage, ContentPath path)
override void LoadFile()
static bool CheckSpeciesName(XElement mainElement, CharacterFile file, out Identifier name)
static CharacterPrefab FindByFilePath(string filePath)
ContentXElement ConfigElement
static readonly PrefabCollection< CharacterPrefab > 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
static ContentPath FromRaw(string? rawValue)
string???????????? Value
Definition: ContentPath.cs:27
IEnumerable< ContentXElement > GetChildElements(string name)
ContentPackage? ContentPackage
Definition: Prefab.cs:37
static void ClearCache()
List< LimbParams > Limbs
static ? RoundSound Load(ContentXElement element, bool stream=false)
Definition: RoundSound.cs:61