3 using System.Collections.Generic;
4 using System.Collections.Immutable;
7 using System.Reflection;
12 [AttributeUsage(AttributeTargets.Class, Inherited =
true)]
30 public readonly ImmutableHashSet<Identifier>
Names;
31 private readonly MethodInfo? contentPathMutator;
45 HashSet<Identifier> names =
new HashSet<Identifier> { type.Name.RemoveFromEnd(
"File").ToIdentifier() };
48 names.UnionWith(altNames);
51 Names = names.ToImmutableHashSet();
55 => (
ContentPath?)contentPathMutator?.Invoke(
null,
new object[] { path })
62 public readonly
static ImmutableHashSet<TypeInfo>
Types;
67 .ToImmutableHashSet();
72 Identifier elemName = contentFileElement.NameAsIdentifier();
73 if (elemName ==
"TraitorMissions")
77 DebugConsole.AddWarning(
78 $
"The content type \"TraitorMission\" in content package \"{package.Name}\" is no longer supported." +
79 $
" Traitor missions should be implemented using the scripted event system and the content type TraitorEvents.",
92 Identifier elemName = element.NameAsIdentifier();
93 var type =
Types.FirstOrDefault(t => t.Names.Contains(elemName));
94 var filePath = element.GetAttributeContentPath(
"file", contentPackage);
97 return fail($
"Invalid content type \"{elemName}\"");
101 return fail($
"No content path defined for file of type \"{elemName}\"");
104 using var errorCatcher = DebugConsole.ErrorCatcher.Create();
107 filePath = type.MutateContentPath(filePath);
108 if (!File.Exists(filePath.FullPath))
110 return fail($
"Failed to load file \"{filePath}\" of type \"{elemName}\": file not found.");
113 var file = type.CreateInstance(contentPackage, filePath);
114 if (file is
null) {
return fail($
"Content type {type.Type.Name} is not implemented correctly"); }
116 if (errorCatcher.Errors.Any())
119 $
"Errors were issued to the debug console when loading \"{filePath}\" of type \"{elemName}\"");
125 return fail($
"Failed to load file \"{filePath}\" of type \"{elemName}\": {e.Message}", e);
143 public virtual void Preload(Action<Sprite> addPreloadedSprite) { }
readonly ImmutableHashSet< Identifier > Names
readonly? ImmutableHashSet< Type > AlternativeTypes
readonly ImmutableHashSet< Identifier > Names
readonly bool NotSyncedInMultiplayer
ContentPath MutateContentPath(ContentPath path)
ContentFile? CreateInstance(ContentPackage contentPackage, ContentPath path)
readonly bool RequiredByCorePackage
Base class for content file types, which are loaded from filelist.xml via reflection....
static Result< ContentFile, ContentPackage.LoadError > CreateFromXElement(ContentPackage contentPackage, XElement element)
static bool IsLegacyContentType(XElement contentFileElement, ContentPackage package, bool logWarning)
static readonly ImmutableHashSet< TypeInfo > Types
readonly ContentPath Path
abstract void UnloadFile()
readonly ContentPackage ContentPackage
virtual Md5Hash CalculateHash()
virtual void Preload(Action< Sprite > addPreloadedSprite)
ContentFile(ContentPackage contentPackage, ContentPath path)
readonly record struct LoadError(string Message, Exception? Exception)
static Md5Hash CalculateForFile(string path, StringHashOptions options)