mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-28 09:39:08 +00:00
fix for keybind loading
This commit is contained in:
1 parent
809e7a07ab
commit
8a71f13874
5 files changed
+48
-37
No files matched your search
@@ -1,4 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MiraAPI.Utilities;
|
||||
using Reactor.Utilities;
|
||||
using Rewired;
|
||||
|
||||
namespace MiraAPI.Keybinds;
|
||||
@@ -13,6 +17,33 @@ public static class KeybindManager
|
||||
/// </summary>
|
||||
public static List<MiraKeybind> Keybinds { get; } = new();
|
||||
|
||||
internal static void RewiredInit()
|
||||
{
|
||||
try
|
||||
{
|
||||
var instance = KeybindUtils.RewiredInputManager!;
|
||||
foreach (var keybind in KeybindManager.Keybinds)
|
||||
{
|
||||
if (instance.userData.actions.ToArray().Any(x => x.name == keybind.Id))
|
||||
{
|
||||
Logger<MiraApiPlugin>.Warning($"Keybind of id {keybind.Id} already exists. Skipping it");
|
||||
continue;
|
||||
}
|
||||
|
||||
keybind.RewiredInputAction = instance.userData.RegisterModBind(
|
||||
keybind.Id,
|
||||
keybind.Name,
|
||||
keybind.SourcePluginName,
|
||||
keybind.DefaultKey,
|
||||
modifiers: keybind.ModifierKeys);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger<MiraApiPlugin>.Error($"Error while registering keybinds in Rewired: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns all conflicts where keybinds use the same key.
|
||||
/// </summary>
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using BepInEx.Unity.IL2CPP;
|
||||
using Reactor.Utilities;
|
||||
using Rewired;
|
||||
|
||||
@@ -52,7 +51,7 @@ public class MiraKeybind
|
||||
/// </summary>
|
||||
private Action Handler { get; set; }
|
||||
|
||||
internal BasePlugin? SourcePlugin { get; set; }
|
||||
internal string? SourcePluginName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MiraKeybind"/> class.
|
||||
|
||||
@@ -1,11 +1,5 @@
|
||||
using System.Linq;
|
||||
using BepInEx;
|
||||
using BepInEx.Unity.IL2CPP;
|
||||
using HarmonyLib;
|
||||
using MiraAPI.Keybinds;
|
||||
using MiraAPI.PluginLoading;
|
||||
using MiraAPI.Utilities;
|
||||
using Reactor.Utilities;
|
||||
using Rewired;
|
||||
|
||||
namespace MiraAPI.Patches.Keybinds;
|
||||
@@ -24,29 +18,7 @@ public static class KeybindMenuPatch
|
||||
}
|
||||
|
||||
KeybindUtils.RewiredInputManager = __instance;
|
||||
foreach (var keybind in KeybindManager.Keybinds)
|
||||
{
|
||||
if (__instance.userData.actions.ToArray().Any(x => x.name == keybind.Id))
|
||||
{
|
||||
Logger<MiraApiPlugin>.Warning($"Keybind of id {keybind.Id} already exists. Skipping it");
|
||||
continue;
|
||||
}
|
||||
|
||||
PluginInfo? info = IL2CPPChainloader.Instance.Plugins.Values
|
||||
.FirstOrDefault(p => p.Instance == keybind.SourcePlugin);
|
||||
string group = info!.Metadata.Name;
|
||||
if (keybind.SourcePlugin is IMiraPlugin miraPlugin)
|
||||
{
|
||||
group = miraPlugin.OptionsTitleText;
|
||||
}
|
||||
|
||||
keybind.RewiredInputAction = __instance.userData.RegisterModBind(
|
||||
keybind.Id,
|
||||
keybind.Name,
|
||||
group,
|
||||
keybind.DefaultKey,
|
||||
modifiers: keybind.ModifierKeys);
|
||||
}
|
||||
KeybindManager.RewiredInit();
|
||||
_registered = true;
|
||||
}
|
||||
}
|
||||
@@ -108,7 +108,7 @@ public sealed class MiraPluginManager
|
||||
}
|
||||
|
||||
RegisterColorClasses(type);
|
||||
RegisterKeybindClasses(type, plugin);
|
||||
RegisterKeybinds(type, plugin);
|
||||
}
|
||||
|
||||
info.PluginConfig.Save();
|
||||
@@ -136,7 +136,7 @@ public sealed class MiraPluginManager
|
||||
ModifierManager.Modifiers = new ReadOnlyCollection<BaseModifier>(ModifierManager.InternalModifiers);
|
||||
};
|
||||
|
||||
RegisterKeybindClasses(typeof(MiraGlobalKeybinds), PluginSingleton<MiraApiPlugin>.Instance);
|
||||
RegisterKeybinds(typeof(MiraGlobalKeybinds), PluginSingleton<MiraApiPlugin>.Instance);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -308,7 +308,7 @@ public sealed class MiraPluginManager
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void RegisterKeybindClasses(Type type, BasePlugin source)
|
||||
private static void RegisterKeybinds(Type type, BasePlugin source)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -337,7 +337,14 @@ public sealed class MiraPluginManager
|
||||
}
|
||||
|
||||
KeybindManager.Keybinds.Add(keybind);
|
||||
keybind.SourcePlugin = source;
|
||||
if (source is IMiraPlugin miraPlugin)
|
||||
{
|
||||
keybind.SourcePluginName = miraPlugin.OptionsTitleText;
|
||||
}
|
||||
else if (source is MiraApiPlugin)
|
||||
{
|
||||
keybind.SourcePluginName = "MiraAPI";
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var field in type.GetFields().Where(f => f.FieldType.IsAssignableTo(typeof(MiraKeybind))))
|
||||
|
||||
@@ -563,13 +563,15 @@ public static class Extensions
|
||||
/// <param name="type">The <see cref="InputActionType"/> for this action (default is Button).</param>
|
||||
/// <param name="modifiers">Optional modifier keys (e.g., <c>Control</c>, <c>Shift</c>, <c>Alt</c>) that must be held together with the main key.</param>
|
||||
/// <returns>The action ID of the newly registered action.</returns>
|
||||
public static InputAction RegisterModBind(this UserData userData, string id, string name, string group, KeyboardKeyCode key, int category = 0, int elementIdentifierId = -1, InputActionType type = InputActionType.Button, ModifierKey[]? modifiers = null)
|
||||
public static InputAction RegisterModBind(this UserData userData, string id, string name, string? group, KeyboardKeyCode key, int category = 0, int elementIdentifierId = -1, InputActionType type = InputActionType.Button, ModifierKey[]? modifiers = null)
|
||||
{
|
||||
userData.AddAction(category);
|
||||
var action = userData.GetAction(userData.actions.Count - 1)!;
|
||||
|
||||
action.name = id;
|
||||
action.descriptiveName = $"<b><size=70%>{Palette.CrewmateRoleHeaderDarkBlue.ToTextColor()}{group.ReplaceLineEndings(" ")}</color></size></b>\n{name}";
|
||||
action.descriptiveName = group != null
|
||||
? $"<b><size=70%>{Palette.CrewmateRoleHeaderDarkBlue.ToTextColor()}{group.ReplaceLineEndings(" ")}</color></size></b>\n{name}"
|
||||
: name;
|
||||
action.categoryId = category;
|
||||
action.type = type;
|
||||
action.userAssignable = true;
|
||||
|
||||
Reference in new issue
Block a user