Files
LuaCsForBarotraumaEP/Barotrauma/BarotraumaShared/SharedSource/Characters/Talents/TalentPrefab.cs
T
2021-08-26 21:08:21 +09:00

103 lines
3.5 KiB
C#

using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace Barotrauma
{
class TalentPrefab : IPrefab, IDisposable, IHasUintIdentifier
{
public string Identifier { get; private set; }
public string OriginalName => Identifier;
public ContentPackage ContentPackage { get; private set; }
public string FilePath { get; private set; }
public static readonly PrefabCollection<TalentPrefab> TalentPrefabs = new PrefabCollection<TalentPrefab>();
public XElement ConfigElement
{
get;
private set;
}
public TalentPrefab(XElement element, string filePath)
{
FilePath = filePath;
ConfigElement = element;
Identifier = element.GetAttributeString("identifier", "noidentifier");
this.CalculatePrefabUIntIdentifier(TalentPrefabs);
}
private bool disposed = false;
public void Dispose()
{
if (disposed) { return; }
disposed = true;
TalentPrefabs.Remove(this);
}
/// <summary>
/// Unique identifier that's generated by hashing the prefab's string identifier.
/// Used to reduce the amount of bytes needed to write talent data into network messages in multiplayer.
/// </summary>
public uint UIntIdentifier { get; set; }
public static void RemoveByFile(string filePath) => TalentPrefabs.RemoveByFile(filePath);
public static void LoadFromFile(ContentFile file)
{
DebugConsole.Log("Loading talent prefab: " + file.Path);
RemoveByFile(file.Path);
XDocument doc = XMLExtensions.TryLoadXml(file.Path);
if (doc == null) { return; }
var rootElement = doc.Root;
switch (rootElement.Name.ToString().ToLowerInvariant())
{
case "talent":
TalentPrefabs.Add(new TalentPrefab(rootElement, file.Path), false);
break;
case "talents":
foreach (var element in rootElement.Elements())
{
if (element.IsOverride())
{
var itemElement = element.GetChildElement("talent");
if (itemElement != null)
{
TalentPrefabs.Add(new TalentPrefab(rootElement, file.Path), true);
}
else
{
DebugConsole.ThrowError($"Cannot find a talent element from the children of the override element defined in {file.Path}");
}
}
else
{
TalentPrefabs.Add(new TalentPrefab(element, file.Path), false);
}
}
break;
default:
DebugConsole.ThrowError($"Invalid XML root element: '{rootElement.Name.ToString()}' in {file.Path}");
break;
}
}
public static void LoadAll(IEnumerable<ContentFile> files)
{
DebugConsole.Log("Loading talent prefabs: ");
foreach (ContentFile file in files)
{
LoadFromFile(file);
}
}
}
}