Skip to content

Commit

Permalink
Update for the new input system (RW v1.9.07)
Browse files Browse the repository at this point in the history
Now it doesn't just completely crash the game! 🎉
  • Loading branch information
SabreML committed Mar 25, 2023
1 parent 37f0056 commit 24cd24f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 26 deletions.
4 changes: 4 additions & 0 deletions JollyRebind.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
<HintPath>..\..\DLLs\Additional\MonoMod.Utils.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Rewired_Core">
<HintPath>..\..\DLLs\Additional\Rewired_Core.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine">
<HintPath>..\..\DLLs\UnityEngine.dll</HintPath>
<Private>False</Private>
Expand Down
1 change: 1 addition & 0 deletions JollyRebind.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{BE1952D9-9AFB-4303-A262-5C1D1B28B34C}"
ProjectSection(SolutionItems) = preProject
JollyRebind\modinfo.json = JollyRebind\modinfo.json
README.md = README.md
EndProjectSection
EndProject
Global
Expand Down
4 changes: 2 additions & 2 deletions JollyRebind/modinfo.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"id": "sabreml.jollyrebind",
"name": "Jolly Rebind",
"version": "1.0.1",
"target_game_version": "v1.9.06",
"version": "1.1.0",
"target_game_version": "v1.9.07b",
"authors": "SabreML",
"description": "Adds a customisable input to change the Jolly Co-op pointing button.",
"requirements": ["jollycoop"],
Expand Down
33 changes: 16 additions & 17 deletions src/JollyMenuKeybinds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Mono.Cecil.Cil;
using MonoMod.Cil;
using MonoMod.RuntimeDetour;
using Rewired;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
Expand Down Expand Up @@ -89,6 +90,7 @@ private static void JollyPlayerSelectorHK(On.JollyCoop.JollyMenu.JollyPlayerSele
keyBinder.OnValueUpdate += KeybindValueUpdated;

keybindWrappers[index] = new UIelementWrapper(menu.tabWrapper, keyBinder);
Debug.Log($"(JollyRebind) Keybind UI added for player {index + 1}");
}
}

Expand Down Expand Up @@ -157,30 +159,27 @@ private static void InitBoundKey()
for (int i = 0; i < RWCustom.Custom.rainWorld.options.controls.Length; i++)
{
Options.ControlSetup controlSetup = RWCustom.Custom.rainWorld.options.controls[i];
if (controlSetup.preset == Options.ControlSetup.Preset.KeyboardSinglePlayer)
InputMapCategory mapCategory = ReInput.mapping.GetMapCategory(0);
if (mapCategory == null || controlSetup?.gameControlMap == null)
{
for (int j = 0; j < controlSetup.keyboardKeys.Length; j++)
{
if (!OpKeyBinder._BoundKey.ContainsValue(controlSetup.keyboardKeys[j].ToString()))
{
OpKeyBinder._BoundKey.Add($"Vanilla_{i}_{j}", controlSetup.keyboardKeys[j].ToString());
}
}
continue;
}
else
InputCategory actionCategory = ReInput.mapping.GetActionCategory(mapCategory.name);
if (actionCategory == null)
{
for (int j = 0; j < controlSetup.gamePadButtons.Length; j++)
continue;
}
int num = 0;
foreach (InputAction action in ReInput.mapping.ActionsInCategory(actionCategory.id))
{
foreach (ActionElementMap elementMap in controlSetup.gameControlMap.AllMaps)
{
string button = controlSetup.gamePadButtons[j].ToString();
if (button.Length <= 9 || !int.TryParse(button.Substring(8, 1), out _))
{
button = button.Substring(0, 8) + i + button.Substring(8);
}
if (!OpKeyBinder._BoundKey.ContainsValue(button))
if (elementMap != null && elementMap.actionId == action.id && !OpKeyBinder._BoundKey.ContainsValue(elementMap.elementIdentifierName))
{
OpKeyBinder._BoundKey.Add($"Vanilla_{i}_{j}", button);
OpKeyBinder._BoundKey[$"Vanilla_{i}_{num}"] = elementMap.elementIdentifierName;
}
}
num++;
}
}
}
Expand Down
49 changes: 42 additions & 7 deletions src/JollyRebindMod.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using BepInEx;
using Rewired;
using System.Collections.Generic;
using System.Security.Permissions;
using UnityEngine;

Expand All @@ -8,9 +10,24 @@

namespace JollyRebind
{
[BepInPlugin("sabreml.jollyrebind", "JollyRebind", "1.0.1")]
[BepInPlugin("sabreml.jollyrebind", "JollyRebind", "1.1.0")]
public class JollyRebindMod : BaseUnityPlugin
{
// A dictionary of rewired `elementIdentifierName`s to their corresponding Unity `KeyCode`s.
private static readonly Dictionary<string, KeyCode> rewiredElemToKeyCode = new Dictionary<string, KeyCode>
{
{ "A", KeyCode.JoystickButton0 },
{ "B", KeyCode.JoystickButton1 },
{ "X", KeyCode.JoystickButton2 },
{ "Y", KeyCode.JoystickButton3 },
{ "Left Shoulder", KeyCode.JoystickButton4 },
{ "Right Shoulder", KeyCode.JoystickButton5 },
{ "Back", KeyCode.JoystickButton6 },
{ "Start", KeyCode.JoystickButton7 },
{ "Left Stick Button", KeyCode.JoystickButton8 },
{ "Right Stick Button", KeyCode.JoystickButton9 }
};

public void OnEnable()
{
On.RainWorld.OnModsInit += Init;
Expand All @@ -27,25 +44,43 @@ private void Init(On.RainWorld.orig_OnModsInit orig, RainWorld self)
}

// If the player has a custom keybind set (i.e, not the map key), this method sets `jollyButtonDown` to `true` depending on if the key is being held down.
private static void JollyInputUpdateHK(On.Player.orig_JollyInputUpdate orig, Player self)
private void JollyInputUpdateHK(On.Player.orig_JollyInputUpdate orig, Player self)
{
orig(self);

// The player's pointing keybind from the jolly menu keybinder.
KeyCode playerKeybind = JollyRebindConfig.PlayerPointInputs[self.playerState.playerNumber].Value;
KeyCode defaultKeybind;
// The game's map keybind.
KeyCode? mapKeybind = null;

// If the player is using a controller.
if (self.input[0].gamePad)
{
defaultKeybind = RWCustom.Custom.rainWorld.options.controls[self.playerState.playerNumber].GamePadMap;
Options.ControlSetup controlSetup = RWCustom.Custom.rainWorld.options.controls[self.playerState.playerNumber];

// Loop through every button on the controller which is currently bound to something in the game.
// (I feel like there must be a method for this somewhere)
foreach (ActionElementMap elementMap in controlSetup.gameControlMap.ButtonMaps)
{
// If the button is bound to the 'Map' action (11).
if (elementMap.actionId == RewiredConsts.Action.Map)
{
// That's the one we're looking for.
mapKeybind = rewiredElemToKeyCode[elementMap.elementIdentifierName];
break;
}
}
}
// If the player is using a keyboard.
else
{
defaultKeybind = RWCustom.Custom.rainWorld.options.controls[self.playerState.playerNumber].KeyboardMap;
mapKeybind = RWCustom.Custom.rainWorld.options.controls[self.playerState.playerNumber].KeyboardMap;
}

// If the player has a custom keybind.
if (playerKeybind != defaultKeybind)
// If the player's keybind is different to the game's map key.
if (playerKeybind != mapKeybind)
{
// Override whatever `jollyButtonDown` was set to in `orig()`.
self.jollyButtonDown = Input.GetKey(playerKeybind);
}
}
Expand Down

0 comments on commit 24cd24f

Please sign in to comment.