IContentFileRegistrar
public interface IContentFileRegistrar : IPluginService
Interface that allows registering custom XML content in your mod.
Remarks
The methods are instance methods. The examples assume an instance has been retrieved and stored, e.g.:
public static readonly IContentFileRegistrar ContentFileRegistrar = PluginServiceProvider.GetService<IContentFileRegistrar>();
Members
| Member | Description |
|---|---|
RegisterContentFile<T>() |
Registers a content file type to be recognized by the game. The class should have the name “%NAME%File” where %NAME% is the name of the content type, and it should preferably inherit GenericPrefabFile<T>. |
Methods
RegisterContentFile
void RegisterContentFile<T>() where T : ContentFile
Registers a content file type to be recognized by the game. The class should have the name “%NAME%File” where %NAME% is the name of the content type, and it should preferably inherit GenericPrefabFile<T>.
| Type parameter | Description |
|---|---|
T |
Implementation of ContentFile to register. |
Example
Barotrauma will recognize this example content type with the name “MyContent” due to the class name and alternatively as “FooBar” due to the attribute.
The content file implementation handles parsing the XML documents defined in filelist.xml and passing the found elements to the prefab class.
[Barotrauma.AlternativeContentTypeNames("FooBar")]
public class MyContentFile : Barotrauma.GenericPrefabFile<MyPrefab>
{
public MyContentFile(ContentPackage contentPackage, ContentPath path) : base(contentPackage, path) { }
protected override bool MatchesSingular(Identifier identifier) => identifier == "MyPrefab";
protected override bool MatchesPlural(Identifier identifier) => identifier == "MyPrefabs";
protected override MyPrefab CreatePrefab(ContentXElement element) => new(this, element);
protected override PrefabCollection<MyPrefab> Prefabs => MyPrefab.Prefabs;
}
An instance of the prefab class is created for each matching XML element found in the content file.
public class MyPrefab : Barotrauma.Prefab
{
public static readonly PrefabCollection<MyPrefab> Prefabs = new();
public int MyProperty { get; set; }
public MyPrefab(ContentFile file, ContentXElement element) : base(file, element.GetAttributeIdentifier("identifier", Identifier.Empty))
{
MyProperty = element.GetAttributeInt("myproperty", 0);
}
public override void Dispose() { }
}
Plugin init:
ContentFileRegistrar.RegisterContentFile<MyContentFile>();