Code Modding

You can compile C# files or load dll libraries directly in Software Inc.

A good grasp of C# programming and the Unity3D API is required to make a code based mod.

# Setup

## Debugging console

Before you do anything, you should enable the in-game debugging console by binding the console key at the bottom of the key binding menu in the options window. This will help you debug your mod by giving you error messages and enabling the **RECOMPILE\_DLL\_MOD**, **RELOAD\_DLL\_MOD** and **UNLOAD\_DLL\_MOD** commands.

## Project setup

To create your own mod start a .NET Class Library project in Visual Studio, targeted at the .NET 4 profile. You should download and install the **.NET 4 Framework** if you don't have it, **.NET Core is not the same.**

You should add a reference to the following libraries: Note that you might need to add references to more, depending on what you want to do.

* Software Inc\_Data\Managed\UnityEngine.UI.dll

* Software Inc\_Data\Managed\UnityEngine.dll

* Software Inc\_Data\Managed\UnityEngine.CoreModule.dll

* Software Inc\_Data\Managed\Assembly-CSharp.dll

* Software Inc\_Data\Managed\Assembly-CSharp-firstpass.dll

Start by creating a class that implements the ModMeta abstract class, then implement as many classes that inherit from ModBehaviour as you want.

The ModMeta class will contain information about your mod and act as a manager for your mod.

```
namespace myMod
{
  internal class MyModMeta : ModMeta
    {
        public override void ConstructOptionsScreen(RectTransform parent, bool inGame)
        {
            Text text = WindowManager.SpawnLabel();
            text.text = "This is the description of my mod\nIt shows in dropdown 'My Mod!' in the 'Mods' section of the options area.";
            WindowManager.AddElementToElement(text.gameObject, parent.gameObject, new Rect(0f, 0f, 400f, 128f),
                new Rect(0f, 0f, 0f, 0f));
        }

        public override string Name => "My Mod!";
    }
}

```
ModBehaviours are just a subclass of Unity's MonoBehavior and work the same way, i.e. they use the same life cycle of Awake, Start, Update, OnDestroy, ModBehaviours only differ in that they have the abstract OnActive and OnDeactive methods, to signal when the mod is toggled. Each ModBehaviour implementation will be instantiated once the mod is loaded.

## Note on threading

You should stick to the ModBehaviour's Update method, rather than trying to write your own update loop with threads or timers, as you cannot interact with Unity's system from other threads and you will most likely end up causing race conditions when interacting with the game's systems. You can use Unity's Time class to keep track of elapsed time between frames.

## Compile

When you're done, create a subfolder for your mod in a folder called "DLLMods" in the game's root directory and put your compiled .dll file in it, or .cs files if you want the game to compile them for you. Note that if you let the game compile the C# files for you, you are limited to C# version 3, but **using the game's compiler is required if you want to upload your mod to the Steam Workshop**.

If you use the game's compiler, note that it will inject error handling code into each function body, you can disable this behaviour while you are developing your mod using the launch paramater **-DisableModErrors**.

**Please note that due to a bug in the compiler the game uses, you cannot use enums if you are using straight .cs files or the game will crash**

## Example mod

Here's a working mod example that lets you change how many floors you can build on, complete with comments: Floor Mod (Updated to work with Alpha 10.7+)

## Full access

By default, certain namespaces and types are off-limits to mods for security reasons. If you want to make a mod that writes to files or accesses the internet, you need to put a public static bool called GiveMeFreedom in your ModMeta implementation. Note that this only works for dll-based mods, which can't be uploaded to the Steam Workshop, and the user will be warned.

## Dependencies

If you want to reference external dll files, you can put them in the **Software Inc\_Data\Managed** folder.

You can also add this code snippet to your ModMeta class, to load dll files from a subfolder: (Note that this requires GiveMeFreedom and it might not find the correct dll filename, so you might need to adjust it to your situation)

```
public override void Initialize(ModController.DLLMod parentMod)
    {
    AppDomain.CurrentDomain.AssemblyResolve += (x, y) => Assembly.LoadFrom(Path.Combine(parentMod.FolderPath(), "**SubFolder**/" + y.Name.Substring(0, y.Name.IndexOf(",")) + ".dll"));
    base.Initialize(parentMod);
    }

```

# Events

As of Alpha 11.6.5.

Note that no MarketSimulation events are raised during the initial market simulation to avoid race conditions.

 GameSettings.OnQuit
 MarketSimulation.OnProductReleased
 MarketSimulation.OnCompanyFounded
 MarketSimulation.OnTechResearched
 MarketSimulation.OnFrameworkReleased
 TimeOfDay.OnHourPassed
 TimeOfDay.OnDayPassed
 TimeOfDay.OnMonthPassed

# Entry points

 GameSettings.Instance.MyCompany
 MarketSimulation.Active
 GameSettings.Instance.sRoomManager
 GameSettings.Instance.sActorManager
 SelectorController.Instance
 TimeOfDay.Instance
 HUD.Instance
 ObjectDatabase.Instance
 WindowManager