Skip to content

Commit

Permalink
refactor: Replace DefaultButtonBinding2 with DefaultButtonBinding
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Dec 30, 2024
1 parent 5eec268 commit 9640435
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 73 deletions.
41 changes: 0 additions & 41 deletions CelesteTAS-EverestInterop/Source/EverestInterop/Hotkeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,6 @@ private static void ReleaseAllKeys() {
[Load]
private static void Load() {
On.Celeste.Input.Initialize += InputOnInitialize;
typeof(ModuleSettingsKeyboardConfigUI)
.GetMethodInfo(nameof(ModuleSettingsKeyboardConfigUI.Reset))
.IlHook(ModReload);
}

[Unload]
Expand All @@ -247,36 +244,6 @@ private static void InputOnInitialize(On.Celeste.Input.orig_Initialize orig) {
CommunicationWrapper.SendCurrentBindings();
}

private static void ModReload(ILContext il) {
bindingProperties = typeof(CelesteTasSettings)
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.Where(info => info.PropertyType == typeof(ButtonBinding) &&
info.GetCustomAttribute<DefaultButtonBinding2Attribute>() is { } extraDefaultKeyAttribute &&
extraDefaultKeyAttribute.ExtraKey != Keys.None);

ILCursor ilCursor = new(il);
if (ilCursor.TryGotoNext(
MoveType.After,
ins => ins.OpCode == OpCodes.Callvirt && ins.Operand.ToString().Contains("<Microsoft.Xna.Framework.Input.Keys>::Add(T)")
)) {
ilCursor.Emit(OpCodes.Ldloc_1).EmitDelegate<Action<object>>(AddExtraDefaultKey);
}
}

private static void AddExtraDefaultKey(object bindingEntry) {
if (bindingFieldInfo == null) {
bindingFieldInfo = bindingEntry.GetType().GetFieldInfo("Binding");
}

if (bindingFieldInfo?.GetValue(bindingEntry) is not ButtonBinding binding) {
return;
}

if (bindingProperties.FirstOrDefault(info => info.GetValue(TasSettings) == binding) is { } propertyInfo) {
binding.Keys.Add(propertyInfo.GetCustomAttribute<DefaultButtonBinding2Attribute>().ExtraKey);
}
}

public class Hotkey {
private static readonly Regex keysNameFixRegex = new(@"^D(\d)$", RegexOptions.Compiled);

Expand Down Expand Up @@ -433,11 +400,3 @@ public void Update(ButtonState buttonState) {
}
}
}

public class DefaultButtonBinding2Attribute : DefaultButtonBindingAttribute {
public readonly XNAKeys ExtraKey;

public DefaultButtonBinding2Attribute(Buttons button, params XNAKeys[] keys) : base(button, keys.IsEmpty() ? XNAKeys.None : keys[0]) {
ExtraKey = keys.Length > 1 ? keys[1] : XNAKeys.None;
}
}
64 changes: 32 additions & 32 deletions CelesteTAS-EverestInterop/Source/Module/CelesteTasSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,68 +95,68 @@ public ActualCollideHitboxType ShowActualCollideHitboxes {
#region HotKey

[SettingName("TAS_KEY_START_STOP")]
[DefaultButtonBinding2(0, Keys.RightControl)]
public ButtonBinding KeyStart { get; set; } = new(0, Keys.RightControl);
[DefaultButtonBinding([0], [Keys.RightControl])]

Check failure on line 98 in CelesteTAS-EverestInterop/Source/Module/CelesteTasSettings.cs

View workflow job for this annotation

GitHub Actions / build-celestetas

Cannot initialize type 'Buttons' with a collection expression because the type is not constructible.

Check failure on line 98 in CelesteTAS-EverestInterop/Source/Module/CelesteTasSettings.cs

View workflow job for this annotation

GitHub Actions / build-celestetas

Cannot initialize type 'Keys' with a collection expression because the type is not constructible.

Check failure on line 98 in CelesteTAS-EverestInterop/Source/Module/CelesteTasSettings.cs

View workflow job for this annotation

GitHub Actions / build-celestetas

Cannot initialize type 'Buttons' with a collection expression because the type is not constructible.

Check failure on line 98 in CelesteTAS-EverestInterop/Source/Module/CelesteTasSettings.cs

View workflow job for this annotation

GitHub Actions / build-celestetas

Cannot initialize type 'Keys' with a collection expression because the type is not constructible.
public ButtonBinding KeyStart { get; set; } = null!;

[SettingName("TAS_KEY_RESTART")]
[DefaultButtonBinding2(0, Keys.OemPlus)]
public ButtonBinding KeyRestart { get; set; } = new(0, Keys.OemPlus);
[DefaultButtonBinding([0], [Keys.OemPlus])]

Check failure on line 102 in CelesteTAS-EverestInterop/Source/Module/CelesteTasSettings.cs

View workflow job for this annotation

GitHub Actions / build-celestetas

Cannot initialize type 'Buttons' with a collection expression because the type is not constructible.

Check failure on line 102 in CelesteTAS-EverestInterop/Source/Module/CelesteTasSettings.cs

View workflow job for this annotation

GitHub Actions / build-celestetas

Cannot initialize type 'Keys' with a collection expression because the type is not constructible.

Check failure on line 102 in CelesteTAS-EverestInterop/Source/Module/CelesteTasSettings.cs

View workflow job for this annotation

GitHub Actions / build-celestetas

Cannot initialize type 'Buttons' with a collection expression because the type is not constructible.

Check failure on line 102 in CelesteTAS-EverestInterop/Source/Module/CelesteTasSettings.cs

View workflow job for this annotation

GitHub Actions / build-celestetas

Cannot initialize type 'Keys' with a collection expression because the type is not constructible.
public ButtonBinding KeyRestart { get; set; } = null!;

[SettingName("TAS_KEY_FAST_FORWARD")]
[DefaultButtonBinding2(0, Keys.RightShift)]
public ButtonBinding KeyFastForward { get; set; } = new(0, Keys.RightShift);
[DefaultButtonBinding([0], [Keys.RightShift])]

Check failure on line 106 in CelesteTAS-EverestInterop/Source/Module/CelesteTasSettings.cs

View workflow job for this annotation

GitHub Actions / build-celestetas

Cannot initialize type 'Buttons' with a collection expression because the type is not constructible.

Check failure on line 106 in CelesteTAS-EverestInterop/Source/Module/CelesteTasSettings.cs

View workflow job for this annotation

GitHub Actions / build-celestetas

Cannot initialize type 'Keys' with a collection expression because the type is not constructible.

Check failure on line 106 in CelesteTAS-EverestInterop/Source/Module/CelesteTasSettings.cs

View workflow job for this annotation

GitHub Actions / build-celestetas

Cannot initialize type 'Buttons' with a collection expression because the type is not constructible.

Check failure on line 106 in CelesteTAS-EverestInterop/Source/Module/CelesteTasSettings.cs

View workflow job for this annotation

GitHub Actions / build-celestetas

Cannot initialize type 'Keys' with a collection expression because the type is not constructible.
public ButtonBinding KeyFastForward { get; set; } = null!;

[SettingName("TAS_KEY_FAST_FORWARD_COMMENT")]
[DefaultButtonBinding2(0, Keys.RightAlt, Keys.RightShift)]
public ButtonBinding KeyFastForwardComment { get; set; } = new(0, Keys.RightAlt, Keys.RightShift);
[DefaultButtonBinding([0], [Keys.RightAlt, Keys.RightShift])]

Check failure on line 110 in CelesteTAS-EverestInterop/Source/Module/CelesteTasSettings.cs

View workflow job for this annotation

GitHub Actions / build-celestetas

Cannot initialize type 'Buttons' with a collection expression because the type is not constructible.

Check failure on line 110 in CelesteTAS-EverestInterop/Source/Module/CelesteTasSettings.cs

View workflow job for this annotation

GitHub Actions / build-celestetas

Cannot initialize type 'Keys' with a collection expression because the type is not constructible.

Check failure on line 110 in CelesteTAS-EverestInterop/Source/Module/CelesteTasSettings.cs

View workflow job for this annotation

GitHub Actions / build-celestetas

Cannot initialize type 'Buttons' with a collection expression because the type is not constructible.

Check failure on line 110 in CelesteTAS-EverestInterop/Source/Module/CelesteTasSettings.cs

View workflow job for this annotation

GitHub Actions / build-celestetas

Cannot initialize type 'Keys' with a collection expression because the type is not constructible.
public ButtonBinding KeyFastForwardComment { get; set; } = null!;

[SettingName("TAS_KEY_SLOW_FORWARD")]
[DefaultButtonBinding2(0, Keys.OemPipe)]
public ButtonBinding KeySlowForward { get; set; } = new(0, Keys.OemPipe);
[DefaultButtonBinding([0], [Keys.OemPipe])]

Check failure on line 114 in CelesteTAS-EverestInterop/Source/Module/CelesteTasSettings.cs

View workflow job for this annotation

GitHub Actions / build-celestetas

Cannot initialize type 'Buttons' with a collection expression because the type is not constructible.

Check failure on line 114 in CelesteTAS-EverestInterop/Source/Module/CelesteTasSettings.cs

View workflow job for this annotation

GitHub Actions / build-celestetas

Cannot initialize type 'Keys' with a collection expression because the type is not constructible.

Check failure on line 114 in CelesteTAS-EverestInterop/Source/Module/CelesteTasSettings.cs

View workflow job for this annotation

GitHub Actions / build-celestetas

Cannot initialize type 'Buttons' with a collection expression because the type is not constructible.

Check failure on line 114 in CelesteTAS-EverestInterop/Source/Module/CelesteTasSettings.cs

View workflow job for this annotation

GitHub Actions / build-celestetas

Cannot initialize type 'Keys' with a collection expression because the type is not constructible.
public ButtonBinding KeySlowForward { get; set; } = null!;

[SettingName("TAS_KEY_FRAME_ADVANCE")]
[DefaultButtonBinding2(0, Keys.OemOpenBrackets)]
public ButtonBinding KeyFrameAdvance { get; set; } = new(0, Keys.OemOpenBrackets);
[DefaultButtonBinding([0], [Keys.OemOpenBrackets])]
public ButtonBinding KeyFrameAdvance { get; set; } = null!;

[SettingName("TAS_KEY_PAUSE_RESUME")]
[DefaultButtonBinding2(0, Keys.OemCloseBrackets)]
public ButtonBinding KeyPause { get; set; } = new(0, Keys.OemCloseBrackets);
[DefaultButtonBinding([0], [Keys.OemCloseBrackets])]
public ButtonBinding KeyPause { get; set; } = null!;

[SettingName("TAS_KEY_HITBOXES")]
[DefaultButtonBinding2(0, Keys.LeftControl, Keys.B)]
public ButtonBinding KeyHitboxes { get; set; } = new(0, Keys.LeftControl, Keys.B);
[DefaultButtonBinding([0], [Keys.LeftControl, Keys.B])]
public ButtonBinding KeyHitboxes { get; set; } = null!;

[SettingName("TAS_KEY_TRIGGER_HITBOXES")]
[DefaultButtonBinding2(0, Keys.LeftAlt, Keys.T)]
public ButtonBinding KeyTriggerHitboxes { get; set; } = new(0, Keys.LeftAlt, Keys.T);
[DefaultButtonBinding([0], [Keys.LeftAlt, Keys.T])]
public ButtonBinding KeyTriggerHitboxes { get; set; } = null!;

[SettingName("TAS_KEY_SIMPLIFIED_GRAPHICS")]
[DefaultButtonBinding2(0, Keys.LeftControl, Keys.N)]
public ButtonBinding KeyGraphics { get; set; } = new(0, Keys.LeftControl, Keys.N);
[DefaultButtonBinding([0], [Keys.LeftControl, Keys.N])]
public ButtonBinding KeyGraphics { get; set; } = null!;

[SettingName("TAS_KEY_CENTER_CAMERA")]
[DefaultButtonBinding2(0, Keys.LeftControl, Keys.M)]
public ButtonBinding KeyCamera { get; set; } = new(0, Keys.LeftControl, Keys.M);
[DefaultButtonBinding([0], [Keys.LeftControl, Keys.M])]
public ButtonBinding KeyCamera { get; set; } = null!;

[SettingName("TAS_KEY_LOCK_CAMERA")]
[DefaultButtonBinding2(0, Keys.LeftControl, Keys.H)]
public ButtonBinding KeyLockCamera { get; set; } = new(0, Keys.LeftControl, Keys.H);
[DefaultButtonBinding([0], [Keys.LeftControl, Keys.H])]
public ButtonBinding KeyLockCamera { get; set; } = null!;

[SettingName("TAS_KEY_SAVE_STATE")]
[DefaultButtonBinding2(0, Keys.RightAlt, Keys.OemMinus)]
public ButtonBinding KeySaveState { get; set; } = new(0, Keys.RightAlt, Keys.OemMinus);
[DefaultButtonBinding([0], [Keys.RightAlt, Keys.OemMinus])]
public ButtonBinding KeySaveState { get; set; } = null!;

[SettingName("TAS_KEY_CLEAR_STATE")]
[DefaultButtonBinding2(0, Keys.RightAlt, Keys.Back)]
public ButtonBinding KeyClearState { get; set; } = new(0, Keys.RightAlt, Keys.Back);
[DefaultButtonBinding([0], [Keys.RightAlt, Keys.Back])]
public ButtonBinding KeyClearState { get; set; } = null!;

[SettingName("TAS_KEY_INFO_HUD")]
[DefaultButtonBinding2(0, Keys.LeftControl)]
public ButtonBinding KeyInfoHud { get; set; } = new(0, Keys.LeftControl);
[DefaultButtonBinding([0], [Keys.LeftControl])]
public ButtonBinding KeyInfoHud { get; set; } = null!;

[SettingName("TAS_KEY_FREE_CAMERA")]
[DefaultButtonBinding2(0, Keys.LeftAlt)]
public ButtonBinding KeyFreeCamera { get; set; } = new(0, Keys.LeftAlt);
[DefaultButtonBinding([0], [Keys.LeftAlt])]
public ButtonBinding KeyFreeCamera { get; set; } = null!;

#endregion

Expand Down

0 comments on commit 9640435

Please sign in to comment.