RacesOfValheim/JotunnModStub/RaceInput.cs
Ferdinando fcb6cae3ee fix(input): abilities no longer fire while the hammer's build menu is open
Skadi's cold spell is bound to middle mouse and kept casting with the
piece selection panel open. The gate was already there - HandleColdSpell
sits behind RaceInput.Allowed - but the gate itself was incomplete:
Player.TakeInput tests Chat, Console, TextInput, StoreGui, InventoryGui,
Menu, TextViewer, the map, free-fly and the barber, and nothing else
(read off the IL). Vanilla does not need the build menu in that list
because build input is handled separately in UpdatePlacement; a mod
ability reading UnityEngine.Input directly does.

Hud.IsPieceSelectionVisible is static, public and null-safe on its own
instance, so it costs one call and no reflection. Checked before
TakeInput so it holds on the fail-open path too.

Fixed in RaceInput rather than in Skadi: that class is the mod's single
input gate, so this covers every race and every bound ability - the
transformations, the flight toggle and the three dragon-form attacks
included.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-29 08:45:17 +02:00

116 lines
5.5 KiB
C#

using System.Reflection;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Gate for race ability input.
///
/// UnityEngine.Input reads the device directly, so abilities bound to keys or mouse buttons
/// kept firing while the map, the inventory, a chest, the chat or a menu had focus - dragging
/// an item inside a container also swung the transformed attack, for instance. Valheim runs
/// its own controls through Player.TakeInput, which reports false for exactly those states
/// (InventoryGui covers the backpack and chests alike), so mirror that instead of maintaining
/// a parallel list of GUI checks that would drift as the game changes.
///
/// ONE GUI THAT TakeInput DOES NOT COVER: the hammer's piece selection panel. Read off the IL,
/// TakeInput tests Chat, Console, TextInput, StoreGui, InventoryGui, Menu, TextViewer, the map,
/// free-fly and the barber - and nothing else. Vanilla does not need it there because build
/// input is handled separately in UpdatePlacement, but a mod ability reading the device
/// directly does: Skadi's cold spell is bound to middle mouse and kept casting while the build
/// menu was open. Hud.IsPieceSelectionVisible is static, public and null-safe on its own
/// instance, so it costs one call and no reflection.
/// </summary>
public static class RaceInput
{
// Not public on Player, hence the reflection; resolved once rather than per frame.
private static readonly MethodInfo TakeInputMethod = AccessTools.Method(typeof(Player), "TakeInput");
public static bool Allowed
{
get
{
Player player = Player.m_localPlayer;
if (player == null) return false;
// Checked before TakeInput, so it holds even on the fail-open path below.
if (Hud.IsPieceSelectionVisible()) return false;
// Fail open: if a game update ever renames the method, abilities stay usable
// rather than silently going dead.
if (TakeInputMethod == null) return true;
return (bool)TakeInputMethod.Invoke(player, null);
}
}
// Aswid/Skadi/Fenrir all borrow the native "gpower" Animator trigger to get its
// arms-raised cast gesture for their own transformations. The "gpower" clip carries a
// baked-in Animation Event that calls Player.ActivateGuardianPower() partway through -
// that fires no matter who requested the animation, so borrowing the trigger silently
// cast the player's real Guardian Power (and put it on cooldown) whenever it happened
// to be available. Races set this right before SetTrigger("gpower") to swallow that one
// call. Time-based rather than a plain latch, so a skipped/interrupted animation can't
// leave the real power blocked forever.
public static float SuppressGuardianPowerUntil = -1f;
public static void SuppressGuardianPowerBriefly(float seconds = 2f)
{
SuppressGuardianPowerUntil = Time.time + seconds;
}
/// <summary>
/// What KeyboardShortcut.IsDown() should have been for a game key.
///
/// BepInEx's own IsDown() ends in a check that no key outside the shortcut is being held
/// (ModifierKeyTest -> _modifierBlockKeyCodes.All(c => !GetKey(c) || allKeys.Contains(c))),
/// which is right for a config-menu hotkey and completely wrong for a game ability: it
/// only exempts the mouse buttons, so holding W - or any other movement key - silently
/// killed every bound ability in the mod, in every race, including the ones bound to mouse
/// buttons. Vanilla attacks kept working because they go through ZInput instead, which is
/// what made it look like the abilities specifically had broken.
///
/// This keeps the part that matters (main key down, every modifier held) and drops the
/// blocking rule, so an ability fires while the player is moving.
/// </summary>
public static bool IsDownIgnoringHeldKeys(this KeyboardShortcut shortcut)
{
if (shortcut.MainKey == KeyCode.None) return false;
if (!Input.GetKeyDown(shortcut.MainKey)) return false;
foreach (KeyCode modifier in shortcut.Modifiers)
{
if (!Input.GetKey(modifier)) return false;
}
return true;
}
/// <summary>Held-down counterpart of <see cref="IsDownIgnoringHeldKeys"/>.</summary>
public static bool IsPressedIgnoringHeldKeys(this KeyboardShortcut shortcut)
{
if (shortcut.MainKey == KeyCode.None) return false;
if (!Input.GetKey(shortcut.MainKey)) return false;
foreach (KeyCode modifier in shortcut.Modifiers)
{
if (!Input.GetKey(modifier)) return false;
}
return true;
}
}
[HarmonyPatch(typeof(Player), "ActivateGuardianPower")]
public static class Player_ActivateGuardianPower_SuppressBorrowed_Patch
{
public static bool Prefix(Player __instance)
{
if (__instance != Player.m_localPlayer) return true;
if (Time.time > RaceInput.SuppressGuardianPowerUntil) return true;
RaceInput.SuppressGuardianPowerUntil = -1f; // one-shot: don't block real future casts
return false;
}
}
}