Client LuaCsForBarotrauma
DecalManager.cs
2 using Microsoft.Xna.Framework;
3 using System;
4 using System.Linq;
5 using System.Xml.Linq;
6 
7 namespace Barotrauma
8 {
9  public class GrimeSprite : Prefab
10  {
11  public GrimeSprite(Sprite spr, DecalsFile file, int indexInFile) : base(file, $"{nameof(GrimeSprite)}{indexInFile}".ToIdentifier())
12  {
13  Sprite = spr;
14  IndexInFile = indexInFile;
15  }
16 
17  public readonly int IndexInFile;
18 
19  public Sprite Sprite { get; private set; }
20 
21  public override void Dispose()
22  {
23  Sprite?.Remove(); Sprite = null;
24  }
25  }
26 
27  static class DecalManager
28  {
29  public static readonly PrefabCollection<DecalPrefab> Prefabs = new PrefabCollection<DecalPrefab>();
30 
31  public static int GrimeSpriteCount { get; private set; } = 0;
32 
33  public static readonly PrefabCollection<GrimeSprite> GrimeSprites = new PrefabCollection<GrimeSprite>(
34  onAdd: (sprite, b) => GrimeSpriteCount = Math.Max(GrimeSpriteCount, sprite.IndexInFile + 1),
35  onRemove: (s) =>
36  GrimeSpriteCount = GrimeSprites.AllPrefabs
37  .SelectMany(kvp => kvp.Value)
38  .Where(p => p != s).Select(p => p.IndexInFile + 1).MaxOrNull() ?? 0,
39  onSort: null, onAddOverrideFile: null, onRemoveOverrideFile: null);
40 
41  public static void LoadFromFile(DecalsFile configFile)
42  {
43  XDocument doc = XMLExtensions.TryLoadXml(configFile.Path);
44  if (doc == null) { return; }
45 
46  bool allowOverriding = false;
47  var mainElement = doc.Root.FromPackage(configFile.ContentPackage);
48  if (doc.Root.IsOverride())
49  {
50  mainElement = mainElement.FirstElement();
51  allowOverriding = true;
52  }
53 
54  int grimeIndex = 0;
55  foreach (var sourceElement in mainElement.Elements())
56  {
57  var element = sourceElement.IsOverride() ? sourceElement.FirstElement() : sourceElement;
58  bool isOverride = allowOverriding || sourceElement.IsOverride();
59  string name = element.Name.ToString().ToLowerInvariant();
60 
61  switch (name)
62  {
63  case "grime":
64  GrimeSprites.Add(new GrimeSprite(new Sprite(element), configFile, grimeIndex), isOverride);
65  grimeIndex++;
66  break;
67  default:
68  var prefab = new DecalPrefab(element, configFile);
69  Prefabs.Add(prefab, isOverride);
70  break;
71  }
72  }
73  }
74 
75  public static void RemoveByFile(DecalsFile configFile)
76  {
77  Prefabs.RemoveByFile(configFile);
78  GrimeSprites.RemoveByFile(configFile);
79  }
80 
81  public static void SortAll()
82  {
83  Prefabs.SortAll();
84  GrimeSprites.SortAll();
85  }
86 
87  public static Decal CreateDecal(string decalName, float scale, Vector2 worldPosition, Hull hull, int? spriteIndex = null)
88  {
89  string lowerCaseDecalName = decalName.ToLowerInvariant();
90  if (!Prefabs.ContainsKey(lowerCaseDecalName))
91  {
92  DebugConsole.ThrowError("Decal prefab " + decalName + " not found!");
93  return null;
94  }
95 
96  DecalPrefab prefab = Prefabs[lowerCaseDecalName];
97 
98  return new Decal(prefab, scale, worldPosition, hull, spriteIndex);
99  }
100  }
101 }
readonly int IndexInFile
Definition: DecalManager.cs:17
GrimeSprite(Sprite spr, DecalsFile file, int indexInFile)
Definition: DecalManager.cs:11
override void Dispose()
Definition: DecalManager.cs:21