1. Getting Started - LionShield/Kingdoms-and-Castles-Toolkit GitHub Wiki

Tools You Will Need

Harmony patcher:

https://github.com/pardeike/Harmony/wiki/Patching

dnSpy

dnSpy is a C# NET decompiler. It’s useful to see the game code as code. It will help you understand the programming logic and find good places to inject code for your mod. As of this writing, you’ll have to build from source code. They stopped releasing binary versions of the tool.

https://github.com/0xd4d/dnSpy/releases

Asset Studio

This will help you dig into the various assets and resources bundled up by unity. It’ll also help you get a sense for how things exist in the scene hierarchy.

https://ci.appveyor.com/project/Perfare/assetstudio/branch/master/artifacts

Unity 2018.2.5f1

This is the version of Unity that our game is built in. The Toolkit also expects to run in this version. Using a different version is not supported and you may have unexpected results.

https://unity3d.com/get-unity/download/archive

If you have the UnityHub installed already, here’s the direct link to the correct version:

unityhub://2018.2.5f1/3071d1717b71

During installation, be sure to add build support for Windows, Mac, and Linux.

C# Code Editor

I use Visual Studio 2017 Community, others use Visual Studio Code. There are also newer versions of Visual Studio available that will work just fine. For developing your Kingdoms and Castles mod, you’ll find it useful to include these DLL references in your mod’s project so you can leverage intellisense when interacting with game code. Here’s a common, but not necessarily complete, set of references modders end up using.

  • 0Harmony //Harmony, Learn more
  • Assembly-CSharp //Game code
  • Assembly-CSharp-firstpass //Game plugin code
  • System
  • System.Core
  • System.Data
  • System.Drawing
  • System.IO.Compression.FileSystem
  • System.Numerics
  • System.Runtime.Serialization
  • System.Xml
  • System.Xml.Linq
  • Unity.TextMeshPro
  • UnityEngine
  • UnityEngine.AudioModule
  • UnityEngine.AssetBundleModule
  • UnityEngine.CoreModule
  • UnityEngine.IMGUIModule
  • UnityEngine.InputModule
  • UnityEngine.StandardEvents
  • UnityEngine.UI
  • UnityEngine.UIElementsModule
  • UnityEngine.UIModule

You can find these DLLs inside the game’s folder.

For Windows:

[PathToGame]\KingdomsAndCastles_Data\Managed

For Mac:

[PathToGame]/Kingdoms and Castles.app/Contents/Resources/Data/Managed

For Linux:

[PathToGame]/KingdomsAndCastles_Data/Managed

Kingdoms and Castles Toolkit

In lieu of a dedicated modder’s API, we’ve put together a basic unity scene with some models for scale and lot of reference assets that are used in the game. It should help you create and wrap up assets for mods into asset bundles for loading into the game.

Download Kingdoms and Castles Toolkit

See ‘Making a Mod with Asset Bundles’.

Setting It All Up

Download and install the tools listed above (dnSpy, Harmony, VS, and Unity, etc). Open Visual Studio and create a new .NET class library project.

File >> New >> Project

Be sure the .NET framework version the project is targeting is 4.5. There is no requirement about where to save the project. Next, open the solution explorer and right-click References, click “Add references”.

You don’t need to add all the DLLs listed above, but that is a complete list. To get started, all you really need is:
UnityEngine,

UnityEngine.CoreModule

UnityEngine.UI

Assembly-CSharp

Assembly-CSharp-firstpass

0Harmony

This is what a good reference manager might look like: \

[INSERT IMAGE HERE]

Please refer to the C# Code Editor section to find the required DLLs

Kingdoms and Castles workshop

The official tool to upload mods to the steam workshop.

Steam:
Library > Games (Top left) > Select "Tools" > Kingdoms and Castles Workshop
Click install

GOG:
Unknown?

Logging

Launch logging

Mod loading information is logged inside the game’s directory inside the mods folder (log.txt). ~\steamapps\common\Kingdoms and Castles\KingdomsAndCastles_Data\mods\log.txt

This will contain any compile errors encountered when trying to launch the game. If your mod does not show up when you launch the game check here for errors.

This is written to using Console.Out.WriteLine(); but this is not allowed inside KC mods and will cause the game to not compile your mod. See What is an "Illegal Reference"? for more information.

Individual Mod Logging

Mods that include a KCModHelper parameter in their mod’s entry point function get a reference to a class that can log to mod specific file. You will likely use this one the most.

This file (output.txt) lives inside the mod folder itself.
~\steamapps\common\Kingdoms and Castles\KingdomsAndCastles_Data\mods\KingdomRenderer\output.txt (Manually installed/developing)
~\steamapps\workshop\content\569480\2306848108\output.txt (Workshop Subscription)

This is written to using Helper.Log();

Runtime logging

Where the game logs information while it is running. Debug.Log() can be used to log here in your mod. Do not use this too much as it will become harder to debug when many mods are running.

Windows: C:\Users\[UserName]\AppData\LocalLow\LionShield\Kingdoms and Castles\Player.log

Mac: ~/Library/Logs/Unity/Player.log

Linux: ~/.config/unity3d/CompanyName/ProductName/Player.log

This is written to using Debug.Log();