diff --git a/Tangerine/Game/TangerineCharacter.cs b/Tangerine/Game/TangerineCharacter.cs new file mode 100644 index 0000000..757b727 --- /dev/null +++ b/Tangerine/Game/TangerineCharacter.cs @@ -0,0 +1,59 @@ +using HarmonyLib; +using Il2CppInterop.Runtime; +using Il2CppInterop.Runtime.Injection; +using System; +using System.Collections.Generic; + +namespace Tangerine.Game +{ + public static class TangerineCharacter + { + private static readonly Dictionary _characterDict = new(); + + internal static void InitializeHarmony(Harmony harmony) + { + harmony.PatchAll(typeof(TangerineCharacter)); + } + + /// + /// Adds a controller class by injecting it into the game's runtime + /// + /// n_ID of the character that will use this controller + /// the controller class + /// Interfaces the class should implement (e.g. ) + public static void AddController(int characterId, Type controllerType, Type[] interfaces) + { + // Throw an exception if a controller with the same ID is already registered + _characterDict[characterId] = controllerType; + + if (!ClassInjector.IsTypeRegisteredInIl2Cpp(controllerType)) + { + Plugin.Log.LogWarning($"Registering character controller: {controllerType}"); + + var options = new RegisterTypeOptions() + { + Interfaces = new Il2CppInterfaceCollection(interfaces), + }; + + ClassInjector.RegisterTypeInIl2Cpp(controllerType, options); + } + + // Example of injecting an enum value. This is not needed as the game can cast the values itself + // EnumInjector.InjectEnumValues(new Dictionary() { { "X_DMC", 139 } }); + } + + [HarmonyPatch(typeof(CharacterControlFactory), nameof(CharacterControlFactory.GetCharacterControlType))] + [HarmonyPrefix] + private static bool CharacterControlTypePrefix(EControlCharacter character, int subID, ref Il2CppSystem.Type __result) + { + if (_characterDict.TryGetValue((int)character, out var type)) + { + __result = Il2CppType.From(type); + Plugin.Log.LogWarning($"Loading character controller {__result}"); + return false; + } + + return true; + } + } +} diff --git a/Tangerine/Plugin.cs b/Tangerine/Plugin.cs index 4ff8a50..56f09bb 100644 --- a/Tangerine/Plugin.cs +++ b/Tangerine/Plugin.cs @@ -20,6 +20,7 @@ public override void Load() _harmony = new Harmony(MyPluginInfo.PLUGIN_GUID); TangerineDataManager.InitializeHarmony(_harmony); TangerineTextDataManager.InitializeHarmony(_harmony); + Game.TangerineCharacter.InitializeHarmony(_harmony); TangerineLoader.InitializeHarmony(_harmony); } }