From 051642c848d16ef50fda4f19849af1a472a1873e Mon Sep 17 00:00:00 2001 From: Fluto Date: Sat, 12 Feb 2022 13:50:55 +1100 Subject: [PATCH 1/2] Added Hori Taiko Drum support --- TakoTako/Plugin.cs | 17 +++- TakoTako/TaikoDrumSupport.cs | 153 +++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 TakoTako/TaikoDrumSupport.cs diff --git a/TakoTako/Plugin.cs b/TakoTako/Plugin.cs index 0a099b8..894bcda 100644 --- a/TakoTako/Plugin.cs +++ b/TakoTako/Plugin.cs @@ -16,6 +16,8 @@ namespace TakoTako public ConfigEntry ConfigDisableScreenChangeOnFocus; public ConfigEntry ConfigFixSignInScreen; public ConfigEntry ConfigEnableCustomSongs; + public ConfigEntry ConfigEnableTaikoDrumSupport; + public ConfigEntry ConfigTaikoDrumUseNintendoLayout; public ConfigEntry ConfigSongDirectory; public ConfigEntry ConfigSaveEnabled; @@ -89,6 +91,16 @@ namespace TakoTako "DisableScreenChangeOnFocus", false, "When focusing this wont do anything jank, I thnk"); + + ConfigEnableTaikoDrumSupport = Config.Bind("Controller.TaikoDrum", + "ConfigEnableTaikoDrumSupport", + true, + "This will enable support for Taiko drums, current tested with official Hori Drum"); + + ConfigTaikoDrumUseNintendoLayout = Config.Bind("Controller.TaikoDrum", + "ConfigTaikoDrumUseNintendoLayout", + false, + "This will use the Nintendo layout YX/BA for the Hori Taiko Drum"); } private void SetupHarmony() @@ -105,6 +117,9 @@ namespace TakoTako if (ConfigDisableScreenChangeOnFocus.Value) _harmony.PatchAll(typeof(DisableScreenChangeOnFocus)); + if (ConfigEnableTaikoDrumSupport.Value) + _harmony.PatchAll(typeof(TaikoDrumSupport)); + if (ConfigEnableCustomSongs.Value) { _harmony.PatchAll(typeof(MusicPatch)); @@ -117,4 +132,4 @@ namespace TakoTako StartCoroutine(enumerator); } } -} \ No newline at end of file +} diff --git a/TakoTako/TaikoDrumSupport.cs b/TakoTako/TaikoDrumSupport.cs new file mode 100644 index 0000000..5b844e2 --- /dev/null +++ b/TakoTako/TaikoDrumSupport.cs @@ -0,0 +1,153 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using HarmonyLib; +using UnityEngine; + +namespace TakoTako; + +[HarmonyPatch] +[SuppressMessage("ReSharper", "InconsistentNaming")] +[SuppressMessage("Member Access", "Publicizer001:Accessing a member that was not originally public")] +public class TaikoDrumSupport +{ + private const float analogThreshold = 0.333f; + + [HarmonyPatch(typeof(ControllerManager), "GetAxis")] + [HarmonyPostfix] + private static void GetAxis_Postfix(ControllerManager __instance, ref float __result, ControllerManager.ControllerPlayerNo controllerPlayerNo, ControllerManager.Axes axis) + { + int controllerIndex = __instance.GetContollersIndex(controllerPlayerNo); + if (controllerIndex <= 0 || !__instance.Controllers[controllerIndex].joystickName.Contains("Taiko")) + return; + + var prefix = $"J{controllerIndex - 1}"; + switch (axis) + { + case ControllerManager.Axes.L_Horizontal: + { + __result = Input.GetAxis($"{prefix}H3"); + break; + } + case ControllerManager.Axes.L_Vertical: + { + __result = Input.GetAxis($"{prefix}V3"); + break; + } + case ControllerManager.Axes.D_Right: + { + var axisValue = Input.GetAxis($"{prefix}H3"); + if (axisValue > 0) + __result = axisValue; + break; + } + case ControllerManager.Axes.D_Left: + { + var axisValue = Input.GetAxis($"{prefix}H3"); + if (axisValue < 0) + __result = -axisValue; + break; + } + case ControllerManager.Axes.D_Up: + { + var axisValue = Input.GetAxis($"{prefix}V3"); + if (axisValue > 0) + __result = axisValue; + break; + } + case ControllerManager.Axes.D_Down: + { + var axisValue = Input.GetAxis($"{prefix}V3"); + if (axisValue < 0) + __result = -axisValue; + break; + } + } + } + + [HarmonyPatch(typeof(ControllerManager), "GetButtonDown")] + [HarmonyPostfix] + private static void GetButtonDown_Postfix(ControllerManager __instance, ref bool __result, ControllerManager.ControllerPlayerNo controllerPlayerNo, ControllerManager.Buttons btn) + { + int controllerIndex = __instance.GetContollersIndex(controllerPlayerNo); + if (controllerIndex <= 0 || !__instance.Controllers[controllerIndex].joystickName.Contains("Taiko")) + return; + + var prefix = $"J{controllerIndex}"; + var previous = __instance.prevButtons[(int) (controllerPlayerNo - 1), (int) btn]; + + __result = btn switch + { + ControllerManager.Buttons.D_Right => Input.GetAxis($"{prefix}H3") > analogThreshold && !previous, + ControllerManager.Buttons.D_Left => Input.GetAxis($"{prefix}H3") < -analogThreshold && !previous, + ControllerManager.Buttons.D_Up => Input.GetAxis($"{prefix}V3") > analogThreshold && !previous, + ControllerManager.Buttons.D_Down => Input.GetAxis($"{prefix}V3") < -analogThreshold && !previous || Input.GetButtonDown($"{prefix}B10"), + _ => RunMethodWithButton(btn, controllerIndex, Input.GetButtonDown) + }; + } + + [HarmonyPatch(typeof(ControllerManager), "GetButton")] + [HarmonyPostfix] + private static void GetButton_Postfix(ControllerManager __instance, ref bool __result, ControllerManager.ControllerPlayerNo controllerPlayerNo, ControllerManager.Buttons btn) + { + int controllerIndex = __instance.GetContollersIndex(controllerPlayerNo); + if (controllerIndex <= 0 || !__instance.Controllers[controllerIndex].joystickName.Contains("Taiko")) + return; + + var prefix = $"J{controllerIndex}"; + + __result = btn switch + { + ControllerManager.Buttons.D_Right => Input.GetAxis($"{prefix}H3") > analogThreshold, + ControllerManager.Buttons.D_Left => Input.GetAxis($"{prefix}H3") < -analogThreshold, + ControllerManager.Buttons.D_Up => Input.GetAxis($"{prefix}V3") > analogThreshold, + ControllerManager.Buttons.D_Down => Input.GetAxis($"{prefix}V3") < -analogThreshold || Input.GetButton($"{prefix}B10"), + _ => RunMethodWithButton(btn, controllerIndex, Input.GetButton) + }; + } + + [HarmonyPatch(typeof(ControllerManager), "GetButtonUp")] + [HarmonyPostfix] + private static void GetButtonUp_Postfix(ControllerManager __instance, ref bool __result, ControllerManager.ControllerPlayerNo controllerPlayerNo, ControllerManager.Buttons btn) + { + int controllerIndex = __instance.GetContollersIndex(controllerPlayerNo); + if (controllerIndex <= 0 || !__instance.Controllers[controllerIndex].joystickName.Contains("Taiko")) + return; + + var prefix = $"J{controllerIndex}"; + var previous = __instance.prevButtons[(int) (controllerPlayerNo - 1), (int) btn]; + + __result = btn switch + { + ControllerManager.Buttons.D_Right => Input.GetAxis($"{prefix}H3") < analogThreshold && previous, + ControllerManager.Buttons.D_Left => Input.GetAxis($"{prefix}H3") > -analogThreshold && previous, + ControllerManager.Buttons.D_Up => Input.GetAxis($"{prefix}V3") < analogThreshold && previous, + ControllerManager.Buttons.D_Down => (Input.GetAxis($"{prefix}V3") > -analogThreshold && previous) || Input.GetButtonUp($"{prefix}B10"), + _ => RunMethodWithButton(btn, controllerIndex, Input.GetButtonUp) + }; + } + + private static bool RunMethodWithButton(ControllerManager.Buttons button, int controllerIndex, Func function) + { + var nintendoLayout = Plugin.Instance.ConfigTaikoDrumUseNintendoLayout.Value; + var prefix = $"J{controllerIndex}"; + + return button switch + { + ControllerManager.Buttons.Menu1 => function($"{prefix}B13"), + ControllerManager.Buttons.Menu2 => function($"{prefix}B12"), + ControllerManager.Buttons.A => (nintendoLayout ? function($"{prefix}B2") : function($"{prefix}B1")) || function($"{prefix}B11"), + ControllerManager.Buttons.B => (nintendoLayout ? function($"{prefix}B1") : function($"{prefix}B2")), + ControllerManager.Buttons.X => nintendoLayout ? function($"{prefix}B3") : function($"{prefix}B0"), + ControllerManager.Buttons.Y => nintendoLayout ? function($"{prefix}B0") : function($"{prefix}B3"), + ControllerManager.Buttons.L1 => function($"{prefix}B4") || function($"{prefix}B6"), + ControllerManager.Buttons.L2 => function($"{prefix}B6"), + ControllerManager.Buttons.R1 => function($"{prefix}B5") || function($"{prefix}B7"), + ControllerManager.Buttons.R2 => function($"{prefix}B7"), + ControllerManager.Buttons.L3 => function($"{prefix}B10"), + ControllerManager.Buttons.R3 => function($"{prefix}B11"), + ControllerManager.Buttons.Start => function($"{prefix}B9"), + ControllerManager.Buttons.Back => function($"{prefix}B8"), + _ => false + }; + } +} From 112d9a80a8983b952db55dfa0cf23266af7dd95a Mon Sep 17 00:00:00 2001 From: Fluto Date: Sat, 12 Feb 2022 13:52:12 +1100 Subject: [PATCH 2/2] Updated Mod Version to v2.2.0 --- TakoTako/TakoTako.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TakoTako/TakoTako.csproj b/TakoTako/TakoTako.csproj index 9c8f67c..58dd84a 100644 --- a/TakoTako/TakoTako.csproj +++ b/TakoTako/TakoTako.csproj @@ -4,12 +4,12 @@ net48 com.fluto.takotako Fixes Taiko issues and allows custom songs - 2.1.0 + 2.2.0 true latest TakoTako com.fluto.takotako - 2.1.0 + 2.2.0