Skip to content

What's new in 1.3?

Liam edited this page May 11, 2020 · 1 revision

How you incorporate ModLib has now changed

Integrating ModLib now creates a 'soft dependancy'. This means that if you are only using the Settings system from ModLib, your mod will still be able to load if the end user decides not to install ModLib. Load order is also no longer important for ModLib as long as it is implemented properly.
This change is only implemented in the Bannerlord beta 1.3 version of ModLib, but will move to the base version of ModLib once Bannerlord beta 1.3 is incorporated into the game. This is to try and minimise the effect of the change on existing mods.

ModLib.Definitions

All abstract definitions have been moved into the new ModLib.Definitions.dll file. This file will be bundled along with your mod and will ensure there won't be errors if ModLib isn't installed.

Project References

There are two files you can reference from ModLib: ModLib.dll and ModLib.Definitions.dll.
If you only wish to use the settings menu from ModLib, then reference only MobLib.Definitions.dll. This file should be included inside the bin/Win64_Shipping_Client directory of your mod. It does not need to be referenced inside your SubModule.xml file.
If you wish to use the functions included inside ModLib, add a reference to ModLib.dll as well as ModLib.Definitions.dll. Include ModLib.Definitions.dll inside the bin/Win64_Shipping_Client directory of your mod, but not ModLib.dll. It is recommended you add ModLib as a depended module in your SubModule.xml file if you are referencing ModLib.dll.

Implementing the Settings System

Using the settings system is now much easier. All Settings classes are now automatically collected which means you no longer need to register them manually. Simply inherit from the ModLib.Definitions.SettingsBase class and define the abtract properties it contains (Visual studio will warn you about what needs to be done). If you need to do an action using your settings, this should be done in the OnBeforeInitialModuleScreenSetAsRoot() method of your SubModule class, as this is called for all mods after all mods have been loaded. This ensures that ModLib has been loaded first (if it is installed).
Here is an example from Bannerlord Tweaks:

public class SubModule : MBSubModuleBase
{
     protected override void OnBeforeInitialModuleScreenSetAsRoot()
     {
          try
          {
               var harmony = new Harmony("mod.bannerlord.mipen");
               harmony.PatchAll();
          }
          catch (Exception ex)
          {
               MessageBox.Show($"Error Initialising Bannerlord Tweaks:\n\n{ex.ToStringFull()}");
          }
     }
}

The harmony.PatchAll() method relies on data contained inside Bannerlord Tweaks' Settings class. Calling this method inside the OnBeforeInitialModuleScreenSetAsRoot() method ensures that, if ModLib is installed, the settings class is deserialised before providing that data.

Accessing the Instance of your Settings Class

As in the old implementation, you can easily access the instance of your settings class through a static property. Add the following property to your settings class:

public static MySettingsClass Instance
{
     get
     {
          return (MySettingsClass)SettingsDatabase.GetSettings<MySettingsClass>();
     }
}

Note that the static class SettingsDatabase is located in the ModLib.Definitions namespace (in the ModLib.Definitions.dll library). This method will return the deserialised data for your settings class if ModLib is loaded or the default values for your settings class if ModLib is not loaded. In this way, the end user change choose to whether have the settings menu installed or not and it will not affect the functioning of your mod.