-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKeybindings.cs
49 lines (48 loc) · 2.1 KB
/
Keybindings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Terraria.ModLoader;
namespace Origins {
public class Keybindings : ILoadable {
[Keybind("Trigger Set Bonus", "Q")]
public static ModKeybind TriggerSetBonus { get; private set; }
[Keybind("Forbidden Voice", "F")]
public static ModKeybind ForbiddenVoice { get; private set; }
[Keybind("Inspect Item", "Mouse3")]
public static ModKeybind InspectItem { get; private set; }
public void Load(Mod mod) {
Type type = typeof(ModKeybind);
foreach (FieldInfo field in GetType().GetFields(BindingFlags.Public | BindingFlags.Static)) {
if (field.FieldType == type && field.GetCustomAttribute<KeybindAttribute>() is KeybindAttribute data) {
field.SetValue(null, KeybindLoader.RegisterKeybind(mod, data.Name, data.DefaultBinding));
}
}
foreach (PropertyInfo property in GetType().GetProperties(BindingFlags.Public | BindingFlags.Static)) {
if (property.PropertyType == type && property.GetCustomAttribute<KeybindAttribute>() is KeybindAttribute data) {
property.SetValue(null, KeybindLoader.RegisterKeybind(mod, data.Name, data.DefaultBinding));
}
}
}
public void Unload() {
Type type = typeof(ModKeybind);
foreach (FieldInfo field in GetType().GetFields(BindingFlags.Public | BindingFlags.Static)) {
if (field.FieldType == type && field.GetCustomAttribute<KeybindAttribute>() is KeybindAttribute data) {
field.SetValue(null, null);
}
}
foreach (PropertyInfo property in GetType().GetProperties(BindingFlags.Public | BindingFlags.Static)) {
if (property.PropertyType == type && property.GetCustomAttribute<KeybindAttribute>() is KeybindAttribute data) {
property.SetValue(null, null);
}
}
}
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
protected sealed class KeybindAttribute(string name, string defaultBinding = "None") : Attribute {
public string Name { get; } = name;
public string DefaultBinding { get; } = defaultBinding;
}
}
}