Modding:Setting up your Development Environment: Difference between revisions

From Vintage Story Wiki
m (Undo revision 36636 by F (talk))
Tag: Undo
m (fixed a wrong word and typo)
(19 intermediate revisions by 5 users not shown)
Line 1: Line 1:
<languages/><translate>
<languages/>
<!--T:1-->
{{GameVersion|1.19.3}}
{{GameVersion|1.16.4}}
<translate>
__FORCETOC__
 
== New Template ==
If you are using Windows and Visual Studio, it is highly recommended that you instead follow the following tutorials:
 
# [[Modding:Preparing For Code Mods|Preparing For Code Mods]]
# [[Modding:Creating A Code Mod|Creating A Code Mod]]
 
There exists a new template that is significantly easier to setup and work with.


__FORCETOC__
== Software == <!--T:2-->
== Software == <!--T:2-->


<!--T:3-->
<!--T:3-->
When you are ready to begin coding mods for Vintage Story, you probably would want to set up a development environment that simplifies the process of coding and testing your creations. There are several applications that aid you in the development of mods. Here are some suggestions:
When you are ready to begin coding mods for Vintage Story, you probably would want to set up a development environment that simplifies the process of coding and testing your creations.  
VintageStory is developed in C# and since version 1.18.8 we switched to .NET 7.
 
<!--T:101-->
There are several applications that aid you in the development of mods and the following Software is supported by our modding template:


<!--T:4-->
<!--T:4-->
* [https://www.visualstudio.com/en/vs/community/ Visual Studio Community] is a free and very powerful development environment and covered in below guide. This is also the tool the Vintage Story Team uses to build the game. It also has a [https://www.visualstudio.com/vs/mac/ MacOS Version].
* [https://www.visualstudio.com/en/vs/community/ Visual Studio Community] is a free and very powerful development environment made by Microsoft and has probably the best support for working with C# and dotnet which are also developed by Microsoft. This is also the tool the ''Vintage Story'' Team uses to build the game. It also has a [https://www.visualstudio.com/vs/mac/ MacOS Version]. When using it make sure to install the <code>.NET desktop development</code> Workload as well.


* [https://www.jetbrains.com/rider/ Jetbrains Rider] is a freemium and modern development environment with seamless support for Visual Studio projects, keybindings and layouts. It is an enterprise product and some features are locked behind a license (code duplication checking, web dev tools), but the features that you need for VS mod development are all free. The indexing / go to reference features and built-in decompiling features are very powerful, as you are able to click on a method Vintage Story uses and it'll show you the code behind it. Do note that it currently has one bug + workaround documented [https://github.com/anegostudios/vsmodexamples/issues/11 here].
<!--T:88-->
* [https://www.jetbrains.com/rider/ JetBrains Rider] is a paid powerful and user-friendly integrated development environment (IDE) specifically designed for .NET. Since it is made for the .NET it also has very good support for all the features you may need while being fully cross-platform. It is also used to develop the game by the Vintage Story Team.


<!--T:5-->
<!--T:5-->
* [https://code.visualstudio.com/ Visual Studio Code] is very a lightweight version of Visual Studio. Our precious community member [https://twitter.com/koppeh?lang=en copygirl] is using this tool and also [https://github.com/copygirl/howto-example-mod wrote a set up guide on github]
* [https://code.visualstudio.com/ Visual Studio Code] is a free, lightweight and versatile source code editor developed by Microsoft. It supports numerous programming languages and offers a wide range of extensions to enhance its functionality. There are extensions to support C# and thus can be a good choice to start out making mods. It is a lot used by our modding community.


<!--T:6-->
<!--T:102-->
* [https://www.monodevelop.com/ MonoDevelop] is another IDE that is more lightweight than Visual Studio Community.
While you can use any text editor to edit source code files we highly recommend using any of the above tools to make your experience much more manageable.


<!--T:7-->
<!--T:89-->
* [https://notepad-plus-plus.org/ Notepad++] your very basic text editing tool with some extra features. You can code mods in any text editor, really. It does however lack instant context sensitive feedback while programming, which is very helpful. If you choose this way, you can use the game itself to trial and error mods by reloading them while the game is running.
Choose one of the above IDEs or other alternatives. We suggest using '''Visual Studio Community''' for Windows. For Linux you may want to use '''Visual Studio Code''' or '''Rider'''.


== Install Development Tool == <!--T:8-->
<!--T:103-->
Note: If you are on Linux and plan to install Visual Studio Code or Rider using '''Flatpak''' or '''snap''' be aware that there might be some issues with using system tools like .NET 7 or the environment variables. So if possible please use a native installation method if possible.


VintageStory is developed in C#. Choose one of the above IDEs or other alternatives. We suggest using '''Visual Studio Community''' for Windows. For Linux you may want to use '''Visual Studio Code''', '''Rider''' or '''MonoDevelop'''.


After you download an IDE, you can now either get the [https://github.com/anegostudios/vsmodtemplate mod template] project or manually set up a mod project. The mod template offers a convenience tool to set up multiple mod projects and to quickly pack up and have a ready-to-ship mod.
<!--T:104-->
Next, you will need to install the [https://dotnet.microsoft.com/en-us/download/dotnet/7.0 .NET 7 SDK] (Software Development Kit) this will also include the .NET 7 Dekstop Runtime which is needed to run the game since version 1.18.8-rc.1.


=== Installation Guide on Windows / macOS with Visual Studio ===
<!--T:105-->
Verify that the .NET 7 SDK is correctly installed by running the following command in a Terminal:


{| class="wikitable"
<!--T:106-->
|-
''On Windows open the Application Windows PowerShell which is preinstalled on all modern Windows versions (10 / 11).''
! scope="col"| OS
! scope="col"| Repo
! scope="col"| Command
|-
| Windows
| [https://visualstudio.microsoft.com/downloads/ download exe]
| Run downloaded file
|-
| macOS
| [https://visualstudio.microsoft.com/downloads/ download dmg]
| Run downloaded file
|}


=== Installation Guide on Linux / macOS with MonoDevelop === <!--T:10-->
<!--T:107-->
<syntaxhighlight lang=shell>
dotnet --list-sdks
</syntaxhighlight>


You can use the MonoDevelop IDE to make VS mods on Linux. The download instructions are below, choose your distro and follow the link to add the repo, then run the install command.
<!--T:108-->
It should return a list of installed SDK's and should contain a line with <code>7.0.xxx</code>. If that is the case you are good to continue.


{| class="wikitable"
== Setup the Environment == <!--T:109-->
|-
Our Mod template makes use of the <code>VINTAGE_STORY</code> ''environment variable''. An environment variable is a stored value within your operating system that can be easily accessed from any program. In this case, the <code>VINTAGE_STORY</code> variable allows our mod template to easily find our installation folder. The first step for setting up our environment is to create this environment variable to link to our game installation path.
! scope="col"| OS
! scope="col"| Repo
! scope="col"| Command
|-
| Debian 8-10
| [https://www.monodevelop.com/download/#fndtn-download-lin-debian repo]
| <code>sudo apt-get install monodevelop</code>
|-
| Ubuntu 14.04-20.04
| [https://www.monodevelop.com/download/#fndtn-download-lin-ubuntu repo]
| <code>sudo apt-get install monodevelop</code>
|-
| CentOS / RedHat
| [https://www.monodevelop.com/download/#fndtn-download-lin-centos repo]
| <code>yum install monodevelop</code>
|-
| Using Flatpak
| [http://flatpak.org/getting.html Download Flatpak]
| <code>flatpak install --user --from https://download.mono-project.com/repo/monodevelop.flatpakref</code>
|-
| macOS
| [https://www.monodevelop.com/developers/building-monodevelop/ build guide]
| Use Visual Studio or build from source
|-
| Other
| [https://github.com/mono/monodevelop compile from source]
| Follow the instructions to compile from source
|}


=== Installation Guide on Linux / macOS with Visual Studio Code ===
<!--T:110-->
The <code>VINTAGE_STORY</code> environment variable simplifies reusing your Vintage Story game installation path and helps if multiple modders work on the same project to reference the <code>VINTAGE_STORY</code> Environment Variable and have their game installed where ever they want. As well as this, it increases the efficiency of updating any existing mods.


You can use Visual Studio Code to make VS mods on Linux. The download instructions are below, choose your distro and follow the link to add the repo, then run the install command.
=== Windows === <!--T:111-->


{| class="wikitable"
<!--T:112-->
|-
Here you have two options to set the environment variable:
! scope="col"| OS
* Use this short PowerShell script. Open the ''Windows PowerShell'' Application, paste the following into it, and hit ''ENTER.''
! scope="col"| Repo
<syntaxhighlight lang=shell>
! scope="col"| Command
[Environment]::SetEnvironmentVariable("VINTAGE_STORY", "$Env:AppData\Vintagestory", "User")
|-
</syntaxhighlight>
| Debian 8-10
| [https://code.visualstudio.com/download download deb]
| <code>sudo dpkg -i /path/to/vscode/file.deb && sudo apt-get install -f</code>
|-
| Ubuntu 18.04-20.04
| [https://code.visualstudio.com/download download deb]
| <code>sudo apt install /path/to/vscode/file.deb</code>
|-
| CentOS / RedHat / Fedora / SUSE
| [https://code.visualstudio.com/download download rpm]
| <code>rpm -ih /path/to/vscode/file.rpm</code>
|-
| Arch
| [https://aur.archlinux.org/packages/visual-studio-code-bin package]
| <code>yay -S visual-studio-code-bin</code>
|-
| Using Snap
| [https://snapcraft.io/docs/installing-snapd Download <code>snapd</code>]
| <code>sudo snap install code --classic</code>
|-
| macOS
| [https://code.visualstudio.com/download download zip]
| Run downloaded file
|-
| Other
| [https://github.com/microsoft/vscode compile from source]
| Follow the instructions to compile from source
|}


=== Installation Guide on Windows / Linux / macOS with JetBrains Rider ===
<!--T:113-->
Change <code>$Env:AppData\Vintagestory</code> if you used a custom installation path for your game.


You can use Jetbrains Rider to make VS mods on Linux. The download instructions are below, choose your distro and follow the link to add the repo, then run the install command.
<!--T:114-->
* Follow these manual steps to set the Environment variable
Search in Windows search for <code>Edit the system environment variables</code> > <code>Environment Variables...</code> > User variables for USER > New > Add insert the Variable name: <code>VINTAGE_STORY</code> and add the path to your Vintage Story installation.
[[File:Win env.png|400px|center|frameless|alt=Setup Windows environment variable]]


{| class="wikitable"
<!--T:115-->
|-
For example, the default installation directory would be <code>C:\Users\<Username>\AppData\Roaming\Vintagestory</code>, (replace <Username> with your username) if you are not sure where yours is type <code>%appdata%</code> into the URL field in the File Explorer and hit ENTER. It will take you to your AppData\Roaming folder where Vintage Story should be installed if you haven't changed the default installation location.
! scope="col"| OS
! scope="col"| Repo
! scope="col"| Command
|-
| Windows
| [https://www.jetbrains.com/rider/download/#section=windows download exe]
| Run downloaded file
|-
| Arch
| [https://aur.archlinux.org/packages/rider package]
| <code>yay -S rider mono mono-msbuild</code>
|-
| Using Toolbox
| [https://www.jetbrains.com/help/idea/installation-guide.html#toolbox Download Toolbox]
| Follow the given instructions for your OS
|-
| Using Snap
| [https://snapcraft.io/docs/installing-snapd Download <code>snapd</code>]
| <code>sudo snap install rider --classic</code>
|-
| macOS
| [https://www.jetbrains.com/rider/download/#section=mac download dmg]
| Run downloaded file
|-
| Other
| [https://www.jetbrains.com/rider/download/#section=linux download <code>.tar.gz</code>]
| <code>sudo tar -xzf ideaIU.tar.gz -C /opt && cd /opt/idea-dir-edit-me && sudo ./idea.sh</code>
|}


== Mod templates == <!--T:18-->
<!--T:116-->
[[File:Win appdata.png|400px|frameless|center|alt=find the default installation path of vintage story]]
Note:
Do not confuse the <code>AppData\Roaming\VintagestoryData</code> directory with the <code>AppData\Roaming\Vintagestory</code> folder. The <code>VintagestoryData</code> directory only holds your Settings, Logs, Savegames and Mods.


<!--T:19-->
=== Linux and Mac === <!--T:117-->
* [https://github.com/anegostudios/vsmodtemplate/releases/latest '''The official Mod template for Visual Studio'''] which is covered in this article.
To set an environment variable in Linux/Mac you need to add the following to your shells startup file:
* [https://github.com/copygirl/howto-example-mod copygirl's VSCode setup]


== Setup using the official Mod template == <!--T:20-->
<!--T:118-->
<syntaxhighlight lang=shell>
export VINTAGE_STORY="$HOME/ApplicationData/vintagestory"
</syntaxhighlight>
Replace the path with the one where your Vintagestory installation is located. The above path should point to your install when you installed Vintagestory using the install.sh script.


<!--T:21-->
<!--T:119-->
To get started download the VSModTemplate solution from [https://github.com/anegostudios/vsmodtemplate/releases/latest GitHub]. Now you simply need to extract the zip archive to your desired destination.
Run <code>echo $SHELL</code> in a terminal to see what shell you are using.


<!--T:22-->
<!--T:120-->
[[File:VSMods9.png]]
* For Bash place it in: <code>~/.bashrc</code> or <code>~/.bash_profile</code> at the end
* For Zsh place it in: <code>~/.zshrc</code> or <code>~/.zprofile</code> at the end
* If you use another shell see their documentation on how their shell startup file is called
Note: If you are using <code>~/.bash_profile</code> or <code>~/.zprofile</code> you will have to Logout and Login again to apply the changes. When using <code>~/.bashrc</code> or <code>~/.zshrc</code> you only need to restart the application that needs to use the environment variable (Visual Studio, Rider, Visual Studio Code, Terminal).


<!--T:23-->
== Setup a Mod  == <!--T:121-->
Navigate to <code>vsmodtemplate-x.y.z</code> and double click <code>VSMods.sln</code> (only works if Visual Studio is installed). Alternatively you can also start Visual Studio hit <code>File -> Open -> Project/Solution...</code>, navigate to the extracted folder and select <code>VSMods.sln</code>.


<!--T:24-->
=== Mod Template package === <!--T:122-->
But before you can start to work, you have to set up the ModTools first.


=== Run ModTools === <!--T:25-->
<!--T:123-->
The first and recommended option would be to use the ''template package''.


<!--T:26-->
<!--T:124-->
The ModTools allow you to do various tasks rather easily by typing in a command. Each platform requires a different way to start it.
Install the mod [https://github.com/anegostudios/VSdotnetModTemplates Mod template package] using the following command on the Terminal:
Please note, the mod tools have only been tested with Visual Studio and MonoDevelop, but they are not required to mod the game, it merely simplifies some of the tasks.


==== Windows ==== <!--T:27-->
<!--T:125-->
<syntaxhighlight lang=shell>
dotnet new install VintageStory.Mod.Templates
</syntaxhighlight>


<!--T:28-->
<!--T:126-->
Double-click <code>vsmodtools.exe</code>.
This will download the latest mod template package from [https://www.nuget.org/packages/VintageStory.Mod.Templates nuget.org]


==== Linux ==== <!--T:29-->
<!--T:127-->
Once the template is installed you will see it inside Visual Studio and Rider. From there you can use the templates to create a new Project.


<!--T:30-->
==== Visual Studio ==== <!--T:128-->
Open a terminal and navigate to this folder and enter <code>mono vsmodtools.exe</code>.


==== Mac OS ==== <!--T:31-->
<!--T:129-->
We advise you to check the option  <code>Place solution and project in the same directory</code> for Visual Studio
This will flatten the project tree by one folder and make it easier to navigate.


<!--T:32-->
<!--T:130-->
Double-click <code>modtools.command</code>.
If you don't want the log output to show up in a separate window you can check the option <code>SuppressWindowsConsoleWindow</code>. When enabled the log output will still be shown inside the Visual Studio Debug Output.


<!--T:33-->
<!--T:131-->
[[File:ModTools.png]]
Open Visual Studio and click on ''Create a new project''. If you installed the ''VintageStory.Mod.Templates'' you can then select '''Vintage Story Mod'''. Your project name ''must'' be only lowercase letters for your mod to function correctly. It is recommended to enable the various include options in the setup.
<gallery mode="packed-hover" widths="400px" heights="300px">
File:Vs-cp-template.png|Visual Studio Mod Template
File:Vs-cp-template-setup.png|Visual Studio Mod Template setup
File:Vs-cp-template-options.png|Visual Studio Mod Template options
</gallery>


=== Setup === <!--T:34-->
==== Rider ==== <!--T:132-->


<!--T:35-->
<!--T:133-->
{{Syntax|setup [optional path]}}
We advise you to check the option <code>Put solution and project in the same directory</code> for Rider.
This will flatten the project tree by one folder and make it easier to navigate.


<!--T:36-->
<!--T:134-->
Before doing anything else you have to setup your workspace. Just type in <code>setup</code> and it will automatically try to locate your Vintage Story installation directory. If it cannot find it, you will be asked to type in the full path. Once you have entered your path, it will update your project files so you can start to work with it. Now open the project again and run your first test by clicking <code>Start</code> [[File:VSMods10.png]]. If everything has been set up correctly, Vintage Story should start.
Open Rider and click on ''New Solution''. If you installed the ''VintageStory.Mod.Templates'' you can then select '''Vintage Story Mod'''.


=== Project Setup (ZIP) === <!--T:37-->
<!--T:135-->
[[File:Rider-cp-template.png|400px|frameless|center|alt=Rider Mod Template|Rider Mod Template]]


<!--T:38-->
The source mod system lets you build zip mods that can ship source code besides their assets. The source code is compiled during run-time by the modloader engine. This has the great advantage of being open and easy to modify on the run, but also comes with one major disadvantage, which is that the inbuilt source code compiler can only compile code up until C# 6.0, so newer language features are not supported. If this is a showstopper for you, we advise you to build a compiled mod.


==== Add Mod ==== <!--T:39-->
<!--T:136-->
Note: Unfortunately as of writing this Rider does not support those template options as Visual Studio in the UI yet but you can check this [https://youtrack.jetbrains.com/issue/RIDER-16759/Support-parameters-in-custom-project-templates issue] for updates.
For now you can make use of those options using the CLI see the  [[#Visual Studio Code|Visual Studio Code]] section for how to use it.


<!--T:40-->
==== Visual Studio Code ==== <!--T:137-->
{{Syntax|add <modid>}}


<!--T:41-->
<!--T:138-->
Adds a new mod project to your workspace, also generates an <code>assets</code>, a <code>src</code> folder and a <code>modinfo.json</code> file. The mod will be added to your visual studio solution and will be loaded by Vintage Story. Make sure to fill in all additional information in <code>modinfo.json</code>.
To create a new Mod using the template when using Visual Studio Code you have to resort to using the command line for now since there is no UI that supports the options as well.
Open a folder in Visual Studio Code where you want your mod to be. Then open the terminal within Visual Studio code ''Terminal'' > ''New Terminal''.  


==== Delete Mod ==== <!--T:42-->
<!--T:139-->
* Create a new Mod Project with launch.json and task.json to easily start it and a Solution file. ''Other IDE's create that automatically so that is why we have this additional <code>--AddSolutionFile</code> flag for VS Code.''
<syntaxhighlight lang=shell>
dotnet new vsmod --IncludeVSCode --AddSolutionFile
</syntaxhighlight>


<!--T:43-->
<!--T:140-->
{{Syntax|delete <modid>}}
For all possible options run the following in the terminal:
<syntaxhighlight lang=shell>
dotnet new vsmod --help
</syntaxhighlight>


<!--T:44-->
<!--T:141-->
Before executing, make sure your project is closed, otherwise you might run into some difficulties. Running this command will delete the given mod completely (irreversible).
Once you opened or created a template Visual Studio will prompt you to install the recommended extension, click on ''Show Recommendations'' and install the the pre-release version of the [https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csdevkit C# Dev Kit] extension. This will also install the required [https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp C#] automatically for you.
These two extensions are recommended and will add Syntax highlighting, Autocomlet and many more much-needed features for writing C# code.


==== Pack Mod ==== <!--T:45-->
==== Other / Commandline ==== <!--T:142-->
{{Syntax|pack <modid>}}
If want to use the Commandline you can use all template options on the Commandline in any Terminal Application with the help of the ''dotnet'' command.


<!--T:46-->
<!--T:143-->
Creates a mod zip file and places it in <code>/releases/<modid>/<modid>_v<modversion>.zip</code>. This file is ready for use and can be shared with other people.
* Create a new VS mod in the folder ''mytestmod'' in the current location of the terminal.
<syntaxhighlight lang=shell>
dotnet new vsmod --AddSolutionFile -o mytestmod
</syntaxhighlight>


<!--T:47-->
<!--T:144-->
{{Syntax|pack-all}}
* or create a dll/code only mod
<syntaxhighlight lang=shell>
dotnet new vsmoddll --AddSolutionFile -o mytestmod
</syntaxhighlight>


<!--T:48-->
<!--T:145-->
Packs all mods in your workspace at once.
For all possible options run the following in the terminal:
<syntaxhighlight lang=shell>
dotnet new vsmod --help
</syntaxhighlight>


=== Project Setup (Compiled Zip) === <!--T:49-->
<!--T:146-->
When using the Commandline you can specify these options like so:


<!--T:50-->
<!--T:147-->
Both dll and zip mods have their advantages and disadvantages, so why not combine them together? This project setup is very similar to zip. Instead of adding the source code to the zip file, it uses the compiled dll. This allows you to have all the dll benefits while still having the zip mod asset structure.
<syntaxhighlight lang=shell>
# Will create a new Mod Project with the dependency for VSSurvivalMod
dotnet new vsmod --AddSolutionFile --IncludeVSSurvivalMod -o mytestmod
</syntaxhighlight>


==== Add Mod ==== <!--T:51-->
<!--T:149-->
Finally in <code>mytestmod\mytestmod\modinfo.json</code>
change the <code>name</code>, <code>modid</code> , <code>authors</code> and <code>description</code> to describe your mod.


<!--T:52-->
==== Updating the template package ==== <!--T:150-->
{{Syntax|add <modid> compiled}}


<!--T:53-->
<!--T:151-->
Adds a compiled zip mod template to your workspace. The difference is that it uses a compiled dll instead of the actual source code. All other commands are equal to the zip ones.
To update all installed templates you can run:
<syntaxhighlight lang=shell>
dotnet new update
</syntaxhighlight>


==== Set Mod ==== <!--T:54-->
<!--T:152-->
But this should not be necessary when creating a new mod, since using the <code>dotnet new vsmod</code> will check automatically if a new version of the template is available and install it.


<!--T:55-->
==== Folder structure ==== <!--T:153-->
{{Syntax|set <modid> <source&#124;compiled>}}


<!--T:56-->
<!--T:8-->
Sets the given zip mod either to source or compiled. This can be useful if you want to change the type of your mod later on.
Here is an overview of where what files in your mod are located.
<table id="treeviewtable" class="table table-bordered tt-table" style='table-layout: fixed'>
  <tr style=''>
    <th width='300' align='left'>Name</th>
    <th width='600' align='left'>Description</th>
  </tr>
  <tr>
    <td><div class="tt" data-tt-id="root">mytestmod</div></td>
    <td></td>
  </tr>
  <tr>
    <td scope="row"><div class="tt" data-tt-id="p_vscode" data-tt-parent="root">.vscode</div></td>
    <td>If you checked --IncludeVSCode this folder will contain all stuff needed to run, package and debug your mod using Visual Studio Code.</td>
  </tr>
  <tr>
    <td scope="row"><div class="tt" data-tt-id="p_cakebuild" data-tt-parent="root">CakeBuild</div></td>
    <td>This Project contains the code that builds mytestmod_x.x.x.zip. You can execute it by selecting the Run Configuration CakeBuild.</td>
  </tr>
  <tr>
    <td scope="row"><div class="tt" data-tt-id="p_mytestmod" data-tt-parent="root">mytestmod</div></td>
    <td>This is your project. It contains all your code as well as the assets and the modinfo.json</td>
  </tr>
  <tr>
    <td scope="row"><div class="tt" data-tt-id="p_releases_assets" data-tt-parent="p_mytestmod">assets</div></td>
    <td>Contains your assets. See '''[[Modding:Asset System|Asset System]]'''</td>
  </tr>
  <tr>
    <td scope="row"><div class="tt" data-tt-id="p_releases_modinfo" data-tt-parent="p_mytestmod">modinfo.json</div></td>
    <td>Defines your mod. Author, Dependencies, Name and much more. See '''[[Modinfo | Modinfo]]'''</td>
  </tr>
  <tr>
    <td scope="row"><div class="tt" data-tt-id="p_releases_properties" data-tt-parent="p_mytestmod">Properties</div></td>
    <td></td>
  </tr>
  <tr>
    <td scope="row"><div class="tt" data-tt-id="p_releases_launchsettings" data-tt-parent="p_releases_properties">launchSettings.json</div></td>
    <td>This contains how to launch your mod (Visual Studio, Rider). You may want to add custom startup parameters to launch for example directly into a world when starting. Run Vintagestory.exe -h to see all options from a terminal opened in your Vintagestory install directory.</td>
  </tr>
  <tr>
    <td scope="row"><div class="tt" data-tt-id="p_releases_csproj" data-tt-parent="p_mytestmod">mymod.csproj</div></td>
    <td>Your mods csproj file. Contains information like the .NET version to build against and references to the VintagestoryAPI for example.</td>
  </tr>
  <tr>
    <td scope="row"><div class="tt" data-tt-id="p_releases" data-tt-parent="root">Releases</div></td>
    <td>Contains your mod release once the CakeBuild Run Configuration was run.</td>
  </tr>
  <tr>
    <td scope="row"><div class="tt" data-tt-id="p_releases_mymtestmod" data-tt-parent="p_releases">mytestmod</div></td>
    <td>Your mod files before getting packaged into a .zip file.</td>
  </tr>
  <tr>
    <td scope="row"><div class="tt" data-tt-id="p_releases_zip" data-tt-parent="p_releases">mytestmod_x.x.x.zip</div></td>
    <td>Ready to release mod zip.</td>
  </tr>


=== Project Setup (DLL) === <!--T:57-->
<!--T:154-->
</table>


<!--T:58-->
=== Template Mod Project === <!--T:155-->
Dll mods are way more powerful than source mods, but they have two major downsides. Firstly, they are not able to carry assets which will be loaded by the game, secondly because they are so powerful they can be dangerous. Besides that dll mods can (unlike the others) have cross mod references, provide APIs and use C# 7. We recommend this type of mod for more experienced mod developers.


==== Add Mod ==== <!--T:59-->
<!--T:156-->
Use the [https://github.com/anegostudios/vsmodtemplate Github Mod template] to create a new mod


<!--T:60-->
{{Syntax|add-dll <modid>}}


<!--T:61-->
<!--T:157-->
Adds a new mod project to your workspace. A generated <code>AssemblyInfo.cs</code> file will be in your properties folder, it contains the mod information (name, author, version, description etc.) so make sure to change them before you spread the mod to other people. The mod will be added to your visual studio solution and will be loaded by VintageStory.
The Github Mod template provides an easy way to get the basics for mod setup so you can start directly with adding your modifications to the game.


==== Delete Mod ==== <!--T:62-->
<!--T:158-->
* Without using git and Github account


<!--T:63-->
<!--T:159-->
{{Syntax|delete <modid>}}
To get a copy of the template click the <code><> Code</code> button on the GitHub repository site. Then download the template as ZIP file and extract it.


<!--T:64-->
<!--T:160-->
Before executing, make sure your project is closed, otherwise you might run into some difficulties. Running this command will delete the given mod completely (irreversible).
* Using Github and or git command


==== Release Mod ==== <!--T:65-->
<!--T:161-->
You can directly click on <code>Use this template</code> on the GitHub repository site to create a copy of it to your GitHub account.
After that, you can clone your new repository to your computer.


<!--T:66-->
<!--T:162-->
Once you launch VintageStory in your workspace, the DLL mod will be compiled. You can find it in your <code>mods</code> folder, with the give name: <code><modid>.dll</code>. If you want to upload this mod or share it with your friends this is the file you need to use.
If you do not use GitHub just clone the repo and upload it to your preferred Git hosting provider (Gitlab, BitBucket, ...)


== Manual setup == <!--T:67-->
<!--T:163-->
<br>
With a local copy of the template, you can go ahead and open the ''ModTemplate.sln'' either in Visual Studio, Rider or Open the folder in Visual Studio Code. The Template is ready to use for any of the aforementioned IDE's. Now you can already start the mod with the game.


<!--T:68-->
<!--T:164-->
* Create a new project
When opened you need to change the following to release the mod since a mod has to have a unique ''modid'' for the VSModDB:
* Add at least a library reference to the <code>VintageStoryApi.dll</code> (on Windows it's located in the installation directory)
<br>
* Open the project properties. Under debug select "Start external program" select the <code>VintageStory.exe</code>. Also adjust the working directory to where the executable is located.
 
* If you compile to a .dll file, set up a ModInfo attribute in your AssemblyInfo.cs ([https://github.com/anegostudios/vsmodtemplate/blob/master/Properties/AssemblyInfo.cs example])
<!--T:165-->
** Add the following command line arguments: <code>-openWorld="modsamplestest" -pcreativebuilding --addModPath="C:\path\to\your\bin\folder" --addOrigin="C:\path\to\your\assets\folder\if\you\have\any"</code>
In <code>modtemplate\resources\modinfo.json</code>
* If want to ship a source mod or mod with assets, set up a <code>modinfo.json</code> ([https://github.com/copygirl/CarryCapacity/blob/master/resources/modinfo.json example]). Make sure your folders are set up in a way that the modPath contains the modinfo.json and a folder named "src" containing your source files. Optionally it can also contain an assets folder for your assets.  
change the <code>name</code>, <code>modid</code> , <code>authors</code> and <code>description</code> to describe your mod.
** Add the following command line arguments: <code>-openWorld="modsamplestest" -pcreativebuilding --addModPath="C:\path\to\your\modPath"</code>
 
* I recommend adding the [https://github.com/anegostudios/vsmodexamples/blob/master/Mods/RedirectLogs/src/RedirectLogs.cs RedirectLogs.cs] file to your project, so that you can see the log output in your visual studio output window. Be sure not to ship your mod with it though.
<!--T:166-->
* Run the project and hope for the best :D
For a full list of available options see '''[[Modinfo | Modinfo]]'''.
* To ship a mod have a look at [[Mod Packaging]]
 
<!--T:167-->
The <code>modid</code> has to contain only lowercase letters and numbers. No special chars, whitespaces, uppercase letters, etc. are allowed.
 
<!--T:168-->
Next, rename the folder <code>modtemplate\resources\assets\mymodid</code> to match what your <code>modid</code> is.
 
<!--T:169-->
Finally change in the file <code>modtemplate\modtemplateModSystem.cs</code> the line
<syntaxhighlight lang=cs>
Lang.Get("mymodid:hello")
</syntaxhighlight>
from <code>mymodid</code> to your new modid.
 
<!--T:170-->
The file <code>modtemplate\modtemplateModSystem.cs</code> can be called whatever you like.
 
<!--T:171-->
(Optional) Further, you can change the name <code>ModTemplate</code> to whatever you like as long as you change all of the following:
* Folder <code>ModTemplate</code>
* File <code>ModTemplate\ModTemplate.csproj</code>
* In File <code>CakeBuild\Program.cs</code> on line
<syntaxhighlight lang=cs>
ProjectName = "ModTemplate";
</syntaxhighlight>
 
<!--T:172-->
''If you are on linux and used the Github Template'' make sure to update in <code>ModTemplate\Properties\launchSettings.json</code> the <code>executablePath</code>. There you need to remove the <code>.exe</code>
 
=== Launch the Mod === <!--T:173-->
 
==== Visual Studio Launch Mod ==== <!--T:174-->
[[File:Vs-cp-start.png|600px|frameless|center|alt=Visual Studio Launch Mod|Visual Studio Launch Mod]]
 
==== Rider Launch Mod ==== <!--T:175-->
<gallery mode="packed-hover" widths="500px" heights="100px">
File:Rider-cp-start-new-ui.png|Rider Launch Mod (New UI)
File:Rider-cp-start.png|Rider Launch Mod
</gallery>
==== Visual Studio Code Launch Mod ====
[[File:Vsc-cp-start.png|400px|frameless|center|alt=Visual Studio Code Launch Mod|Visual Studio Code Launch Mod]]


<!--T:69-->
<!--T:69-->
If you are having troubles, you might be able to learn from the [https://github.com/anegostudios/vsmodexamples vsmodexamples project]
If you are having trouble, you might be able to learn from the [https://github.com/anegostudios/vsmodexamples vsmodexamples project]
 
== Packaging the Mod == <!--T:176-->
 
<!--T:177-->
With the new template, we are using the [https://cakebuild.net/ Cake] build system. This includes a second Project within the Solution in the template.
In all supported IDE's you can select from the dropdown that you used to launch the mod the <code>CakeBuild</code> option and run it.
''When using Visual Studio Code you can also create a package by running the package task. For that got to <code>Terminal</code> > <code>Run Task...</code> and select <code>package</code>.''
 
<!--T:178-->
It will perform a JSON validation on your assets so they are at least parsable by Newtonsoft.Json and then build a <code>Releases/mymodid_1.0.0.zip</code>. The version is taken from the <code>mymod/modinfo.json</code> file.
 
<!--T:179-->
Congratulations now you have your mod development environment set up and a ready-to-release mod package.
 
<!--T:180-->
Note: Make sure you use your own unique <code>modid</code> else you won't be able to upload the mod to the VSModDB.
 
== Updating the Mod ==
If your mod is set up correctly, updating can be done by updating the <code>VINTAGE_STORY</code> environment variable to your new path.
 
Repeat the steps made in [[Modding:Setting up your Development Environment#Setup the Environment|Setup the Environment]] to edit the environment variable, and rebuild your mod. It is likely that mods will break on certain game updates, however fixing them is often quite simple, and errors are well documented. To confirm that your update has worked, debug your mod and check the game version on the main menu.


= Moving Forward = <!--T:70-->
= Moving Forward = <!--T:70-->


<!--T:71-->
<!--T:71-->
If you've successfully managed to setup your development environment and can run Vintage Story through the '''VSMods''' project in your IDE then it's time to get started on your first code mod. At this point there are many options you can choose from as code mods can alter almost any aspect of the game. However, the best place to start is likely one again with Blocks and Items (since you should be familiar with making them using JSONS by this point).
If you've successfully managed to set up your development environment and can run Vintage Story through your Mod project in your IDE then it's time to get started on your first code mod. At this point there are many options you can choose from as code mods can alter almost any aspect of the game. However, the best place to start is likely once again with Blocks and Items (since you should be familiar with making them using JSONS by this point).


<!--T:72-->
<!--T:72-->
Line 314: Line 404:
<!--T:73-->
<!--T:73-->
Or head over to the '''[[Modding:Advanced Items | Advanced Items]]''' page to make your first advanced code item.
Or head over to the '''[[Modding:Advanced Items | Advanced Items]]''' page to make your first advanced code item.
<!--T:74-->
{{Navbox/modding|Vintage Story}}
</translate>
</translate>
{{Navbox/codemodding}}

Revision as of 17:40, 11 July 2024

Other languages:

This page was last verified for Vintage Story version 1.19.3.


New Template

If you are using Windows and Visual Studio, it is highly recommended that you instead follow the following tutorials:

  1. Preparing For Code Mods
  2. Creating A Code Mod

There exists a new template that is significantly easier to setup and work with.

Software

When you are ready to begin coding mods for Vintage Story, you probably would want to set up a development environment that simplifies the process of coding and testing your creations. VintageStory is developed in C# and since version 1.18.8 we switched to .NET 7.

There are several applications that aid you in the development of mods and the following Software is supported by our modding template:

  • Visual Studio Community is a free and very powerful development environment made by Microsoft and has probably the best support for working with C# and dotnet which are also developed by Microsoft. This is also the tool the Vintage Story Team uses to build the game. It also has a MacOS Version. When using it make sure to install the .NET desktop development Workload as well.
  • JetBrains Rider is a paid powerful and user-friendly integrated development environment (IDE) specifically designed for .NET. Since it is made for the .NET it also has very good support for all the features you may need while being fully cross-platform. It is also used to develop the game by the Vintage Story Team.
  • Visual Studio Code is a free, lightweight and versatile source code editor developed by Microsoft. It supports numerous programming languages and offers a wide range of extensions to enhance its functionality. There are extensions to support C# and thus can be a good choice to start out making mods. It is a lot used by our modding community.

While you can use any text editor to edit source code files we highly recommend using any of the above tools to make your experience much more manageable.

Choose one of the above IDEs or other alternatives. We suggest using Visual Studio Community for Windows. For Linux you may want to use Visual Studio Code or Rider.

Note: If you are on Linux and plan to install Visual Studio Code or Rider using Flatpak or snap be aware that there might be some issues with using system tools like .NET 7 or the environment variables. So if possible please use a native installation method if possible.


Next, you will need to install the .NET 7 SDK (Software Development Kit) this will also include the .NET 7 Dekstop Runtime which is needed to run the game since version 1.18.8-rc.1.

Verify that the .NET 7 SDK is correctly installed by running the following command in a Terminal:

On Windows open the Application Windows PowerShell which is preinstalled on all modern Windows versions (10 / 11).

dotnet --list-sdks

It should return a list of installed SDK's and should contain a line with 7.0.xxx. If that is the case you are good to continue.

Setup the Environment

Our Mod template makes use of the VINTAGE_STORY environment variable. An environment variable is a stored value within your operating system that can be easily accessed from any program. In this case, the VINTAGE_STORY variable allows our mod template to easily find our installation folder. The first step for setting up our environment is to create this environment variable to link to our game installation path.

The VINTAGE_STORY environment variable simplifies reusing your Vintage Story game installation path and helps if multiple modders work on the same project to reference the VINTAGE_STORY Environment Variable and have their game installed where ever they want. As well as this, it increases the efficiency of updating any existing mods.

Windows

Here you have two options to set the environment variable:

  • Use this short PowerShell script. Open the Windows PowerShell Application, paste the following into it, and hit ENTER.
[Environment]::SetEnvironmentVariable("VINTAGE_STORY", "$Env:AppData\Vintagestory", "User")

Change $Env:AppData\Vintagestory if you used a custom installation path for your game.

  • Follow these manual steps to set the Environment variable

Search in Windows search for Edit the system environment variables > Environment Variables... > User variables for USER > New > Add insert the Variable name: VINTAGE_STORY and add the path to your Vintage Story installation.

Setup Windows environment variable

For example, the default installation directory would be C:\Users\<Username>\AppData\Roaming\Vintagestory, (replace <Username> with your username) if you are not sure where yours is type %appdata% into the URL field in the File Explorer and hit ENTER. It will take you to your AppData\Roaming folder where Vintage Story should be installed if you haven't changed the default installation location.

find the default installation path of vintage story

Note: Do not confuse the AppData\Roaming\VintagestoryData directory with the AppData\Roaming\Vintagestory folder. The VintagestoryData directory only holds your Settings, Logs, Savegames and Mods.

Linux and Mac

To set an environment variable in Linux/Mac you need to add the following to your shells startup file:

export VINTAGE_STORY="$HOME/ApplicationData/vintagestory"

Replace the path with the one where your Vintagestory installation is located. The above path should point to your install when you installed Vintagestory using the install.sh script.

Run echo $SHELL in a terminal to see what shell you are using.

  • For Bash place it in: ~/.bashrc or ~/.bash_profile at the end
  • For Zsh place it in: ~/.zshrc or ~/.zprofile at the end
  • If you use another shell see their documentation on how their shell startup file is called

Note: If you are using ~/.bash_profile or ~/.zprofile you will have to Logout and Login again to apply the changes. When using ~/.bashrc or ~/.zshrc you only need to restart the application that needs to use the environment variable (Visual Studio, Rider, Visual Studio Code, Terminal).

Setup a Mod

Mod Template package

The first and recommended option would be to use the template package.

Install the mod Mod template package using the following command on the Terminal:

dotnet new install VintageStory.Mod.Templates

This will download the latest mod template package from nuget.org

Once the template is installed you will see it inside Visual Studio and Rider. From there you can use the templates to create a new Project.

Visual Studio

We advise you to check the option Place solution and project in the same directory for Visual Studio This will flatten the project tree by one folder and make it easier to navigate.

If you don't want the log output to show up in a separate window you can check the option SuppressWindowsConsoleWindow. When enabled the log output will still be shown inside the Visual Studio Debug Output.

Open Visual Studio and click on Create a new project. If you installed the VintageStory.Mod.Templates you can then select Vintage Story Mod. Your project name must be only lowercase letters for your mod to function correctly. It is recommended to enable the various include options in the setup.

Rider

We advise you to check the option Put solution and project in the same directory for Rider. This will flatten the project tree by one folder and make it easier to navigate.

Open Rider and click on New Solution. If you installed the VintageStory.Mod.Templates you can then select Vintage Story Mod.

Rider Mod Template


Note: Unfortunately as of writing this Rider does not support those template options as Visual Studio in the UI yet but you can check this issue for updates. For now you can make use of those options using the CLI see the Visual Studio Code section for how to use it.

Visual Studio Code

To create a new Mod using the template when using Visual Studio Code you have to resort to using the command line for now since there is no UI that supports the options as well. Open a folder in Visual Studio Code where you want your mod to be. Then open the terminal within Visual Studio code Terminal > New Terminal.

  • Create a new Mod Project with launch.json and task.json to easily start it and a Solution file. Other IDE's create that automatically so that is why we have this additional --AddSolutionFile flag for VS Code.
dotnet new vsmod --IncludeVSCode --AddSolutionFile

For all possible options run the following in the terminal:

dotnet new vsmod --help

Once you opened or created a template Visual Studio will prompt you to install the recommended extension, click on Show Recommendations and install the the pre-release version of the C# Dev Kit extension. This will also install the required C# automatically for you. These two extensions are recommended and will add Syntax highlighting, Autocomlet and many more much-needed features for writing C# code.

Other / Commandline

If want to use the Commandline you can use all template options on the Commandline in any Terminal Application with the help of the dotnet command.

  • Create a new VS mod in the folder mytestmod in the current location of the terminal.
dotnet new vsmod --AddSolutionFile -o mytestmod
  • or create a dll/code only mod
dotnet new vsmoddll --AddSolutionFile -o mytestmod

For all possible options run the following in the terminal:

dotnet new vsmod --help

When using the Commandline you can specify these options like so:

# Will create a new Mod Project with the dependency for VSSurvivalMod
dotnet new vsmod --AddSolutionFile --IncludeVSSurvivalMod -o mytestmod

Finally in mytestmod\mytestmod\modinfo.json change the name, modid , authors and description to describe your mod.

Updating the template package

To update all installed templates you can run:

dotnet new update

But this should not be necessary when creating a new mod, since using the dotnet new vsmod will check automatically if a new version of the template is available and install it.

Folder structure

Here is an overview of where what files in your mod are located.

Name Description
mytestmod
.vscode
If you checked --IncludeVSCode this folder will contain all stuff needed to run, package and debug your mod using Visual Studio Code.
CakeBuild
This Project contains the code that builds mytestmod_x.x.x.zip. You can execute it by selecting the Run Configuration CakeBuild.
mytestmod
This is your project. It contains all your code as well as the assets and the modinfo.json
assets
Contains your assets. See Asset System
modinfo.json
Defines your mod. Author, Dependencies, Name and much more. See Modinfo
Properties
launchSettings.json
This contains how to launch your mod (Visual Studio, Rider). You may want to add custom startup parameters to launch for example directly into a world when starting. Run Vintagestory.exe -h to see all options from a terminal opened in your Vintagestory install directory.
mymod.csproj
Your mods csproj file. Contains information like the .NET version to build against and references to the VintagestoryAPI for example.
Releases
Contains your mod release once the CakeBuild Run Configuration was run.
mytestmod
Your mod files before getting packaged into a .zip file.
mytestmod_x.x.x.zip
Ready to release mod zip.

Template Mod Project

Use the Github Mod template to create a new mod


The Github Mod template provides an easy way to get the basics for mod setup so you can start directly with adding your modifications to the game.

  • Without using git and Github account

To get a copy of the template click the <> Code button on the GitHub repository site. Then download the template as ZIP file and extract it.

  • Using Github and or git command

You can directly click on Use this template on the GitHub repository site to create a copy of it to your GitHub account. After that, you can clone your new repository to your computer.

If you do not use GitHub just clone the repo and upload it to your preferred Git hosting provider (Gitlab, BitBucket, ...)


With a local copy of the template, you can go ahead and open the ModTemplate.sln either in Visual Studio, Rider or Open the folder in Visual Studio Code. The Template is ready to use for any of the aforementioned IDE's. Now you can already start the mod with the game.

When opened you need to change the following to release the mod since a mod has to have a unique modid for the VSModDB:

In modtemplate\resources\modinfo.json change the name, modid , authors and description to describe your mod.

For a full list of available options see Modinfo.

The modid has to contain only lowercase letters and numbers. No special chars, whitespaces, uppercase letters, etc. are allowed.

Next, rename the folder modtemplate\resources\assets\mymodid to match what your modid is.

Finally change in the file modtemplate\modtemplateModSystem.cs the line

Lang.Get("mymodid:hello")

from mymodid to your new modid.

The file modtemplate\modtemplateModSystem.cs can be called whatever you like.

(Optional) Further, you can change the name ModTemplate to whatever you like as long as you change all of the following:

  • Folder ModTemplate
  • File ModTemplate\ModTemplate.csproj
  • In File CakeBuild\Program.cs on line
ProjectName = "ModTemplate";

If you are on linux and used the Github Template make sure to update in ModTemplate\Properties\launchSettings.json the executablePath. There you need to remove the .exe

Launch the Mod

Visual Studio Launch Mod

Visual Studio Launch Mod

Rider Launch Mod

Visual Studio Code Launch Mod

Visual Studio Code Launch Mod

If you are having trouble, you might be able to learn from the vsmodexamples project

Packaging the Mod

With the new template, we are using the Cake build system. This includes a second Project within the Solution in the template. In all supported IDE's you can select from the dropdown that you used to launch the mod the CakeBuild option and run it. When using Visual Studio Code you can also create a package by running the package task. For that got to Terminal > Run Task... and select package.

It will perform a JSON validation on your assets so they are at least parsable by Newtonsoft.Json and then build a Releases/mymodid_1.0.0.zip. The version is taken from the mymod/modinfo.json file.

Congratulations now you have your mod development environment set up and a ready-to-release mod package.

Note: Make sure you use your own unique modid else you won't be able to upload the mod to the VSModDB.

Updating the Mod

If your mod is set up correctly, updating can be done by updating the VINTAGE_STORY environment variable to your new path.

Repeat the steps made in Setup the Environment to edit the environment variable, and rebuild your mod. It is likely that mods will break on certain game updates, however fixing them is often quite simple, and errors are well documented. To confirm that your update has worked, debug your mod and check the game version on the main menu.

Moving Forward

If you've successfully managed to set up your development environment and can run Vintage Story through your Mod project in your IDE then it's time to get started on your first code mod. At this point there are many options you can choose from as code mods can alter almost any aspect of the game. However, the best place to start is likely once again with Blocks and Items (since you should be familiar with making them using JSONS by this point).

Head over to the Advanced Blocks page to make your first advanced code block.

Or head over to the Advanced Items page to make your first advanced code item.

Code Modding
Basics Code Mods Preparing For Code Mods Creating A Code Mod
Tutorials
Advanced Server-Client Considerations Setting up your Development Environment Advanced Blocks Advanced Items Block and Item Interactions Block Behavior Block Entity Particle Effects World Access Inventory Handling Commands GUIs Network API Monkey patching (Harmony)
Data Management VCDBS format Savegame Moddata ModConfig File Chunk Moddata Serialization Formats TreeAttribute
Worldgen WorldGen API NatFloat EvolvingNatFloat
Rendering Shaders and Renderers
Icon Sign.png

Wondering where some links have gone?
The modding navbox is going through some changes! Check out Navigation Box Updates for more info and help finding specific pages.

Modding
Modding Introduction Getting Started Theme Pack
Content Modding Content Mods Developing a Content Mod Basic Tutorials Intermediate Tutorials Advanced Tutorials Content Mod Concepts
Code Modding Code Mods Setting up your Development Environment
Property Overview ItemEntityEntity BehaviorsBlockBlock BehaviorsBlock ClassesBlock EntitiesBlock Entity BehaviorsCollectible BehaviorsWorld properties
Workflows & Infrastructure Modding Efficiency TipsMod-engine compatibilityMod ExtensibilityVS Engine
Additional Resources Community Resources Modding API Updates Programming Languages List of server commandsList of client commandsClient startup parametersServer startup parameters
Example ModsAPI DocsGitHub Repository