3.8 KiB
3.8 KiB
Barotrauma C# modding guide
Introduction
C# modding for Barotrauma is a subset of modding for Lua For Barotrauma mod,
that requires a mod package with the name CsForBarotrauma to be turned on (e.g. steam workshop)
This modding requires strict source structure, but comes with the benefits of being handled natively by game engine, witch removes many hurdles with type casting or similar issues.
Starting a new mod
The main star of the show is Barotrauma::ACsMod class. It is what all your mods will use to hook game methods, and execute custom code.
Important features
- All utility classes can be accessed either by their type (i.e that have name that starts with
LuaCs...) or throughGameMain.LuaCsproperty (refer to class documentation). - All C# code files must be located in
<mod_root>/CSharp/*otherwise they won't be compiled - To configure server / client execution behaviour create
RunConfig.xmlinCSharpdirectory, like is shown below (run types areStandard,ForcedandNone) - Additionally you can specify what code runs where by either filepath or pre-processor statements
- In case of filepath, your files must be located in either
CSharp/Shared/*,CSharp/Server/*orCSharp/Client/*, for shared code, server-side code or client-side code respectively (in any other case, the code is assumed to be shared) - I case of pre-processor, you can use
SERVERorCLIENTdefinitions to separate code into server-side code and client-side code respectively - Some classes (e.g. almost all System.IO) is prohibited to thwart malicious mods, although current white-listing prohibits reflection, so no mod can be considered inherently safe
- In above case one must use
LuaCs...classes to get access to system resources or similar things, if any - If you want for your mod to inherently be turned on client-side when it is enabled, you must create a dummy
.xmlfile.
Code example
A generic C# mod boilerplate:
File-tree:
<mod_root>/
├─ CSharp/
│ ├─ RunConfig.xml
│ ├─ Shared/ExampleMod.cs
│ ├─ Server/ExampleMod.cs
│ └─ Client/ExampleMod.cs
└─ dummyitem.xml
<mod_root>/CSharp/RunConfig.xml
<?xml version="1.0" encoding="utf-8"?>
<RunConfig>
<Server>Standard</Server>
<Client>Forced</Client>
</RunConfig>
<mod_root>/CSharp/Shared/ExampleMod.cs
using System;
using Barotrauma;
namespace ExampleNamespace {
partial class ExampleMod: ACsMod {
public ExampleMod() {
// shared code
...
#if SERVER
InitServer();
#elif CLIENT
InitClient();
#endif
}
public override void Stop() {
// stopping code, e.g. save custom data
#if SERVER
// server-side code
...
#elif CLIENT
// client-side code
...
#endif
}
}
}
<mod_root>/CSharp/Server/ExampleMod.cs
using System;
using Barotrauma;
namespace ExampleNamespace {
partial class ExampleMod {
public void InitServer() {
// server-side initialization
...
}
}
}
<mod_root>/CSharp/Client/ExampleMod.cs
using System;
using Barotrauma;
namespace ExampleNamespace {
partial class ExampleMod {
public void InitClient() {
// client-side initialization
...
}
}
}
<mod_root>/dummyitem.xml
<Items></Items>