-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow integration of poc's configuration file in DADG's module …
…(#26) * Use a Harmony patch to override POC's default configuration path * Move dadg.config.xml at the root config folder * Add dadg's poc config file named poc.config.json at root config folder * Rename code source configuration folder to technical-config * Document POC's integration * Change logging configuration so that the level is a single uppercase letter. This ensures logs to be well aligned regardless of the log's severity
- Loading branch information
Showing
11 changed files
with
960 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,4 +95,4 @@ AssetPackages/ | |
Shaders/ | ||
|
||
#DADG | ||
/config/ | ||
/config/nlog.config |
File renamed without changes.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# POC Integration | ||
|
||
## What is POC? | ||
|
||
POC is a Bannerlord mod allowing to freely set specific or randomised colours to banners and shields for given kingdoms, clans and units. For instance, companions or unit types can have their own colours and banners. | ||
|
||
For more information, check out [POC's nexus page](https://www.nexusmods.com/mountandblade2bannerlord/mods/792?tab=description). | ||
|
||
## DADG Use Cases | ||
|
||
### Configuration | ||
|
||
The highly customisable mod uses a unique configuration file named `config.json` which is expected to be at its root module folder. POC does not natively read the configuration file anywhere else. | ||
|
||
This causes third party mods needing to fully integrate the mod in its module folder to rely on an external POC instance. It also does not support the Steam Workshop. | ||
|
||
So in order to kill two birds with one stone, we implemented a [Harmony patch](../src/DellarteDellaGuerra/Patches/PocConfigReaderPatch.cs) addressing both issues. | ||
|
||
It detects whether POC is integrated in DADG or is in an external module and overrides the configuration path to a custom location. For now, the POC configuration is expected to be in DellarteDellaGuerra config folder and named `poc.config.json`. | ||
|
||
You can check out [DADG's configuration in the repository](../config/poc.config.json). | ||
|
||
### Heraldry | ||
|
||
All English clans and kingdoms have a custom banner. We use POC to set custom banners as Native enforces all clans to have the same background colour as their kingdom. | ||
|
||
Custom banners can be created with the [online Banner Editor](https://bannerlord.party/banner). | ||
|
||
 | ||
|
||
### Randomised Colour Clothing | ||
|
||
POC enables Bannerlord armour pieces to have random colours. It allows to have many variations of an armour piece. It also allows to have many civilian clothes. | ||
|
||
 | ||
|
||
 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
using DellarteDellaGuerra.Logging; | ||
using DellarteDellaGuerra.Utils; | ||
using HarmonyLib; | ||
using NLog; | ||
|
||
namespace DellarteDellaGuerra.Patches; | ||
|
||
/** | ||
* <summary> | ||
* This script patches the PocColor mod to override the config file path. | ||
* It is expected to be in the DellarteDellaGuerra config folder and named poc.config.json. | ||
* </summary> | ||
* <remarks> | ||
* It has to be called explicitly and can not rely on Harmony's automatic patching. | ||
* This is due to the fact that the PocColor mod is not a reference of the DellarteDellaGuerra mod. | ||
* </remarks> | ||
*/ | ||
public class PocConfigReaderPatch | ||
{ | ||
private static readonly Logger Logger = LoggerFactory.GetLogger<PocConfigReaderPatch>(); | ||
|
||
private PocConfigReaderPatch() { } | ||
|
||
/** | ||
* <summary> | ||
* Patches the PocColorMod assembly to read the config file from the DellarteDellaGuerra config folder. | ||
* If the PocColorMod assembly is not loaded, then this method does nothing. | ||
* </summary> | ||
*/ | ||
public static void Patch(Harmony harmony) | ||
{ | ||
MethodInfo? originalMethod = ResolveOriginalMethod(); | ||
MethodInfo? patchMethod = ResolvePatchMethod(); | ||
if (originalMethod == null || patchMethod == null) | ||
{ | ||
Logger.Warn($"{nameof(PocConfigReaderPatch)} failed to resolve the original method or the patch method"); | ||
return; | ||
} | ||
harmony.Patch(originalMethod, transpiler: new HarmonyMethod(patchMethod)); | ||
} | ||
|
||
private static MethodInfo? ResolveOriginalMethod() | ||
{ | ||
return AppDomain.CurrentDomain.GetAssemblies().SingleOrDefault(assembly => assembly.GetName().Name == "PocColor") | ||
?.GetType("PocColor.PocColorMod") | ||
?.GetMethod("OnGameInitializationFinished", BindingFlags.Instance | BindingFlags.Public); | ||
} | ||
|
||
private static MethodInfo? ResolvePatchMethod() | ||
{ | ||
return typeof(PocConfigReaderPatch).GetMethod(nameof(Transpiler), BindingFlags.Static | BindingFlags.NonPublic); | ||
} | ||
|
||
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> codeInstructions) | ||
{ | ||
var instructions = codeInstructions.ToList(); | ||
var isInstructionFound = false; | ||
|
||
foreach (var instruction in instructions) | ||
{ | ||
if (instruction.opcode != OpCodes.Ldstr || !instruction.OperandIs("..\\..\\modules\\PocColor\\config.json")) | ||
{ | ||
continue; | ||
} | ||
string? configFolderPath = ResourceLocator.GetConfigurationFolderPath(); | ||
if (configFolderPath == null) | ||
{ | ||
continue; | ||
} | ||
instruction.operand = Path.Combine(configFolderPath, "poc.config.json"); | ||
isInstructionFound = true; | ||
break; | ||
} | ||
|
||
if (!isInstructionFound) | ||
{ | ||
Logger.Error($"{nameof(PocConfigReaderPatch)} failed to find the config file path instruction"); | ||
} | ||
return instructions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters