-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using BepInEx; | ||
using BepInEx.Configuration; | ||
using UniTAS.Patcher.Services; | ||
using UnityEngine; | ||
|
||
namespace UniTAS.Patcher.Models.Customization; | ||
|
||
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")] | ||
public class Bind : IEquatable<Bind> | ||
{ | ||
private KeyCode Key | ||
{ | ||
get | ||
{ | ||
var configEntry = _keyConfigEntry?.Value; | ||
if (configEntry != null && _key.ToString() != configEntry && Enum.IsDefined(typeof(KeyCode), configEntry)) | ||
{ | ||
_key = (KeyCode)Enum.Parse(typeof(KeyCode), configEntry); | ||
} | ||
|
||
return _key; | ||
} | ||
} | ||
|
||
public string Name { get; } | ||
private ConfigEntry<string> _keyConfigEntry; | ||
|
||
private readonly IPatchReverseInvoker _patchReverseInvoker; | ||
private readonly IConfig _config; | ||
private KeyCode _key; | ||
|
||
private const string CONFIG_SECTION = "Binds"; | ||
|
||
public Bind(BindConfig bindConfig, IPatchReverseInvoker patchReverseInvoker, IConfig config) | ||
{ | ||
if (bindConfig == null) throw new ArgumentNullException(nameof(bindConfig)); | ||
_key = bindConfig.Key; | ||
_patchReverseInvoker = patchReverseInvoker; | ||
_config = config; | ||
Name = bindConfig.Name; | ||
} | ||
|
||
private bool _initialized; | ||
|
||
public void InitConfig() | ||
{ | ||
if (_initialized) return; | ||
_initialized = true; | ||
_keyConfigEntry = _config.ConfigFile.Bind(CONFIG_SECTION, Name, _key.ToString(), | ||
"Key to press to trigger this bind. See https://docs.unity3d.com/ScriptReference/KeyCode.html for a list of keys."); | ||
} | ||
|
||
public bool IsPressed() | ||
{ | ||
return _patchReverseInvoker.Invoke(key => UnityInput.Current.GetKeyDown(key), Key); | ||
} | ||
|
||
public bool Equals(Bind other) | ||
{ | ||
if (ReferenceEquals(null, other)) return false; | ||
if (ReferenceEquals(this, other)) return true; | ||
return Key == other.Key && string.Equals(Name, other.Name, StringComparison.InvariantCulture); | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
if (ReferenceEquals(null, obj)) return false; | ||
if (ReferenceEquals(this, obj)) return true; | ||
if (obj.GetType() != GetType()) return false; | ||
return Equals((Bind)obj); | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
unchecked | ||
{ | ||
return ((int)Key * 397) ^ StringComparer.InvariantCulture.GetHashCode(Name); | ||
} | ||
} | ||
} | ||
|
||
public class BindConfig | ||
{ | ||
public string Name { get; } | ||
public KeyCode Key { get; } | ||
|
||
public BindConfig(string name, KeyCode key) | ||
{ | ||
Name = name ?? throw new ArgumentNullException(nameof(name)); | ||
Key = key; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters