14 KiB
Barotrauma C# modding guide
Introduction
C# modding is part of the Lua For Barotrauma mod and requires the package Cs For Barotrauma to be turned on (steam workshop), in some cases the package is not needed and a prompt in game will be shown to enable it automatically.
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.
Setting Up Assembly Mod
Part 1: General Project Setup
- Download Visual Studio. You may use another version of VS, or another IDE like JetBrains Rider. However, it must support
.NET 6as a platform/SDK target and there is no guarantee that the project will function as expected, although there should be little issue.
- Important: You must have
Visual Studio Build Tools2019 or later installed with the Individual Component underSDKs, libraries, and frameworksnamedVisual Studio SDK Build Tools Coreinstalled.
- Git Clone or Download the following:
- Refs.zip: This contains all references used in the project. Choose the one from the latest release on the Github repo.
- VSProjectSkeleton: This contains the skeleton project that we will be using as the basis for this tutorial. Most things that need to be setup have already been done.
- If you do not have Git Tools installed, it is recommended that you get familiar with them as it makes life easier and is required for Part 2 (optional). Download and install it.
- Extract the VS Project Skeleton to a folder that you are working out of.
-- IMPORTANT: This folder should NOT be in your Barotrauma/Luatruama local mods folder! - Copy the contents of the Refs.zip into the
/Refs/folder in the VS Skeleton Project. Your project should then have the below structure.
./MyModName.sln
./.gitignore
./README.txt
./Build.props
=== Assets Here ===
./Assets
----/Content/ <<=== Your Non-CSharp Content Goes Here (including Lua, XML)!
Ex:----/Lua/...
Ex:----/Items/...
----/filelist.xml
----/RunConfig.xml
=== Client CSharp Code Here ===
./ClientProject
----/ClientSource/...
----/LinuxClient.csproj
----/OSXClient.csproj
----/WindowsClient.csproj
=== Server CSharp Code Here ===
./ServerProject
----/ServerSource/...
----/LinuxServer.csproj
----/OSXServer.csproj
----/WindowsServer.csproj
=== Shared CSharp Code Here ===
./SharedProject
----/SharedSource/...
./Refs
----/Linux/Barotrauma.dll
----/Linux/DedicatedServer.dll
----/Windows/Barotrauma.dll
----/Windows/DedicatedServer.dll
----/OSX/Barotrauma.dll
----/OSX/DedicatedServer.dll
----/0Harmony.dll
----/Farseer.NetStandard.dll
----/Lidgren.NetStandard.dll
----/Mono.Cecil.dll
----/MonoGame.Framework.Linux.NetStandard.dll
----/MonoGame.Framework.MacOS.NetStandard.dll
----/MonoGame.Framework.Windows.NetStandard.dll
----/MonoMod.Common.dll
----/MoonSharp.Interpreter.dll
----/XNATypes.dll
- Rename the Skeleton Project folder to the name of your mod.
- Open up the Solution file (
.sln) from the Skeleton Project in Visual Studio. Just verify that there are not errors related to "Unable to find Reference", this means that all assemblies in/Refshave be found successfully. - Rename the
MyModName.slnfile to the name of your mod. IE:ModdingToolkit.sln. - Open the
.slnSolution file. In your IDE, you will need to do the following for all.csprojfiles:
- Assets.csproj
- LinuxClient.csproj
- LinuxServer.csproj
- OSXClient.csproj
- OSXServer.csproj
- WindowsClient.csproj
- WindowsServer.csproj
- A. Open the Project Configuration for the
.csprojfile by right-clicking and selectingProperties. - B. Change the
AssemblyNameto the name of your mod WITHOUT SPACES OR SPECIAL CHARACTERS. Example:ModdingToolkit - C. Change the
Root Namespaceto either your mod's Assembly Name or mod's short version without spaces in plain English. Example:ModdingToolkit.
Part 2 (Optional, Recommended): Setting up a debug build of LuaCsForBarotrauma
This will give us a debug build of LuaCsForBarotrauma. A debug build gives us access to many tools as well as the DEBUG symbol for writing test/print code that will not be run in the release version.
- Clone the LuaCsForBarotrauma repository to your local drive. If you do not have Git Tools installed, please download and install it.
- Important: When cloning, use the command
git clone --recurse-submodules --remote-submodules https://github.com/evilfactory/LuaCsForBarotrauma.git. This will download the submodules automatically.
- Open up the LuaCs Solution in your IDE based on your Operating System, one of:
- WindowsSolution.sln
- MacSolution.sln
- LinuxSolution.sln
-- NOTE: This tutorial assumes that you are using the WindowsSolution.sln. For other platforms, the naming of files may be slightly different (MacXXX, LinuxXXX, where 'XXX' is either "Client" or "Server").
-
In the Project Settings for
WindowsClientandWindowsServer, you want to change thePlatform TargetfromAny CPUtox64. This is necessary for OpenAL code to build successfully. -
Build the whole Solution (
Build -> Build Solution). This will create the necessary dependencies from libraries and make sure that there are no errors at this point.\ -
For both the
WindowsClientandWindowsServerprojects, set their output/build type toDEBUG(should be in a drop-down menu at the top next to a green play button).\ -
Select
Build Solution. This will generate Debug builds for use. This debug build will now exist in./Barotrauma/bin/DebugXXX/net6.0/whereXXXis based on your Solution choice. IE../Barotrauma/bin/DebugWindows/net6.0. -
Make the local folder in Barotrauma/LocalMods/ for your mod:
- A. Navigate into the Client Debug Build folder (or game folder if you are coming from Part 3, Step 3) and then into
/LocalMods. - B. CREATE a folder with the name of your mod. Ideally, it should match the name of your
.slnfile with the extension. - C. COPY the path to this directory (with your mod) and save it in a sticky note or .txt file. We will be using it shortly.
Part 3: Setting up MSBuild to Copy Files into Local Mods.
You will have to do the following for the Build.props file found in the main project directory:
-
Open the file in your Text Editor or IDE of choice.
-
Locate the line
ModDeployDir, IE:
<PropertyGroup>
<!-- IMPORTANT: Should point to <Barotrauma_Install>\LocalMods\<MyModName>\ -->
<!-- IMPORTANT: ModDeplyDir Path must end with '\' -->
<ModDeployDir>..\LUATRAMA_DEBUG_LOCALMODS_MYMODDIR\</ModDeployDir>
<!-- IMPORTANT: Avoid the use of special (IE: / : ; , \ < > ?) (periods "." are good) and non-english characters in the AssemblyName.-->
<AssemblyName>MyModName</AssemblyName>
</PropertyGroup>
- Change the
ModDeployDirvalue from..\LUATRAMA_DEBUG_LOCALMODS_MYMODDIR\to the path to your mod's folder in the/LocalModsfolder for LuaCsForBarotrauma fromPart 2, Step 7.
- IMPORTANT: The value must end with
/or\. IE:\LocalMods\ModdingToolkit\. - Note: If you did not complete Part 2, then complete only Part 2, Step 7 but use your vanilla Barotrauma installation location instead.
- Note: If you do not want either the
Clientplugin or theServerplugin (IE. Client-side or Server-side ONLY mod), then you must delete all projects that end withServer. Example:LinuxServer,WindowsServer. Warning, this is generally irreversible and will require you to setup new Cs Projects if you want a client in the future.
- Replace
MyModNamewith a valid assembly name, this should be similar to your mod name but does not need to match. This name should:
- Not include spaces.
- Not include special characters except periods ( . ), which are allowed.
- Use english characters.
- Recommended: Follow the convention of
ModNameorPackageName.LibraryNamefor libraries, IE:ModdingToolkit.GUI.
Part 4: Adding Launch Configurations for Debug-Mode Barotrauma Client and Server to the IDE.
This part makes life very easy and gives you access to Edit and Continue. It'll let you Debug your build as you go and launch Barotrauma from the IDE. Note that this is IDE-specific so please follow the links for your IDE while doing the below:
-- Make two launch options;
- A. One should target
Barotrauma.exeorBarotrauma.dll, name itClient. - B. The other should target
DedicatedServer.exeorDedicatedServer.dlland name itServer.
Follow the link;
- For Jetbrains Rider IDE (what I use).
- For Visual Studio 2022 IDE.
(Optional) Part 5: Adding Script Dependencies
This part is optional and only to be used if your mod relies on the assemblies/code from another mod to run/compile. This will allow you to add Script/Code ContentPackages as dependencies to make sure they load first.
- Open
<ProjectDirectory>/Assets/RunConfig.cfgit should look like this:
<?xml version="1.0" encoding="utf-8"?>
<RunConfig>
<!--Options: "None" / "Forced" (always) / "Standard" (when enabled)-->
<Server>Standard</Server>
<Client>Standard</Client>
</RunConfig>
- Add a
Dependenciessection/node/element:
<?xml version="1.0" encoding="utf-8"?>
<RunConfig>
<!--Options: "None" / "Forced" (always) / "Standard" (when enabled)-->
<Server>Standard</Server>
<Client>Standard</Client>
<Dependencies>
</Dependencies>
</RunConfig>
- Add the STEAMID or Name of your dependencies surrounded by
<Dependency></Dependency>elements as follows:
<PackageName>Name</PackageName>for name matching<SteamWorkshopId>000000</SteamWorkshopId>and replace zeroes with the dependency's workshop steam id.
<?xml version="1.0" encoding="utf-8"?>
<RunConfig>
<!--Options: "None" / "Forced" (always) / "Standard" (when enabled)-->
<Server>Standard</Server>
<Client>Standard</Client>
<Dependencies>
<Dependency>
<!-- You do not need both of these, the SteamWorkshopId is usually enough -->
<SteamWorkshopId><!--ID goes here--></SteamWorkshopId>
<PackageName><!--Name goes here--></PackageName>
</Dependency>
<Dependency> <!-- Dependency 2 Example -->
<PackageName>modding toolkit</PackageName>
</Dependency>
</Dependencies>
</RunConfig>
And you're good to go!
Setting Up Simple Mod
The main star of the show is Barotrauma.ACsMod class. It is what most of your mods will use to hook game methods, and execute custom code. If you are planning on creating new components it's possible to just inherit from ItemComponent.
Important features
- Mods have full access to Harmony to allow you to perform any patches you wish.
- 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
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>Standard</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>