/
LabelUtil.cs
224 lines (194 loc) · 7.38 KB
/
LabelUtil.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using Oculus.Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEngine;
using Logger = QModManager.Utility.Logger;
namespace QuickSlotsPlus.Utility
{
public static class LabelUtil
{
public static string[] slotNames = Enumerable.Range(1, 20).Select(n => "QuickSlot" + n).ToArray();
public static Dictionary<string, string> CustomLabels = new Dictionary<string, string>();
public static string getSlotKeyText(int slotId)
{
// Hotkeys 1-5
if (slotId < Player.quickSlotButtonsCount)
{
string inputName = GameInput.GetBindingName(GameInput.Button.Slot1 + slotId, GameInput.BindingSet.Primary);
if (inputName == null)
{
// A HotKey 1-5 is not set
return "";
}
string input = uGUI.GetDisplayTextForBinding(inputName);
return KeyCodeToString(input);
}
var v = (KeyCode)Mod.Config.GetType().GetField("HotKey" + (slotId + 1)).GetValue(Mod.Config);
return KeyCodeToString(v);
}
public static void LoadCustomLabels()
{
var assemblyDir = Assembly.GetExecutingAssembly().Location;
var path = Path.Combine(assemblyDir, "customLabels.json");
if (File.Exists(path))
{
var JsonConfig = File.ReadAllText(path);
Logger.Log(Logger.Level.Debug, "Custom hotkey label json: \n" + JsonConfig);
CustomLabels = CreateFromJSON(JsonConfig);
}
else
{
Logger.Log(Logger.Level.Debug, "Did not find label file: " + path);
}
}
private static Dictionary<string, string> CreateFromJSON(string jsonString)
{
return JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonString);
}
public static string KeyCodeToString(KeyCode keyCode)
{
if (CustomLabels.TryGetValue(keyCode.ToString(), out string label_1))
{
Logger.Log(Logger.Level.Debug, "Found custom label for keycode: " + keyCode.ToString());
return label_1;
}
else if (CharToKeyCode.TryGetValue(keyCode, out char label_2))
{
Logger.Log(Logger.Level.Debug, "Found label for keycode: " + keyCode.ToString());
return label_2.ToString();
}
else if (keyCode == KeyCode.None)
{
Logger.Log(Logger.Level.Debug, "Found empty label.");
// Can't enter empty char into dictionary
return "";
}
Logger.Log(Logger.Level.Debug, "Returning unmodified keycode text.");
return keyCode.ToString();
}
public static string KeyCodeToString(string keyCode)
{
try
{
KeyCode kc = (KeyCode)Enum.Parse(typeof(KeyCode), keyCode);
return KeyCodeToString(kc);
}
catch (ArgumentException)
{
// KeyCode was not found in Enum, try custom labels
if (CustomLabels.TryGetValue(keyCode, out string label_1))
{
Logger.Log(Logger.Level.Debug, "Found custom label for keycode: " + keyCode);
return label_1;
}
return keyCode;
}
}
/* https://gist.github.com/b-cancel/c516990b8b304d47188a7fa8be9a1ad9
*
* You'd think a method exists for this...
*/
public static readonly Dictionary<KeyCode, char> CharToKeyCode = new Dictionary<KeyCode, char>()
{
//-------------------------LOGICAL mappings-------------------------
{KeyCode.A, 'A'},
{KeyCode.B, 'B'},
{KeyCode.C, 'C'},
{KeyCode.D, 'D'},
{KeyCode.E, 'E'},
{KeyCode.F, 'F'},
{KeyCode.G, 'G'},
{KeyCode.H, 'H'},
{KeyCode.I, 'I'},
{KeyCode.J, 'J'},
{KeyCode.K, 'K'},
{KeyCode.L, 'L'},
{KeyCode.M, 'M'},
{KeyCode.N, 'N'},
{KeyCode.O, 'O'},
{KeyCode.P, 'P'},
{KeyCode.Q, 'Q'},
{KeyCode.R, 'R'},
{KeyCode.S, 'S'},
{KeyCode.T, 'T'},
{KeyCode.U, 'U'},
{KeyCode.V, 'V'},
{KeyCode.W, 'W'},
{KeyCode.X, 'X'},
{KeyCode.Y, 'Y'},
{KeyCode.Z, 'Z'},
//KeyPad Numbers
{KeyCode.Keypad1, '1'},
{KeyCode.Keypad2, '2'},
{KeyCode.Keypad3, '3'},
{KeyCode.Keypad4, '4'},
{KeyCode.Keypad5, '5'},
{KeyCode.Keypad6, '6'},
{KeyCode.Keypad7, '7'},
{KeyCode.Keypad8, '8'},
{KeyCode.Keypad9, '9'},
{KeyCode.Keypad0, '0'},
//Other Symbols
{KeyCode.Exclaim, '!'},
{KeyCode.At, '@'},
{KeyCode.Hash, '#'},
{KeyCode.Dollar, '$'},
{KeyCode.Caret, '^'},
{KeyCode.Ampersand, '&'},
{KeyCode.Asterisk, '*'},
{KeyCode.LeftParen, '('},
{KeyCode.RightParen, ')'},
{KeyCode.Plus, '+'},
{KeyCode.Comma, ','},
{KeyCode.Minus, '-'},
{KeyCode.Period, '.'},
{KeyCode.Slash, '/'},
{KeyCode.Colon, ':'},
{KeyCode.Semicolon, ';'},
{KeyCode.Less, '<'},
{KeyCode.Equals, '='},
{KeyCode.Greater, '>'},
{KeyCode.Question, '?'},
{KeyCode.LeftBracket, '['},
{KeyCode.Backslash, '\\' },
{KeyCode.RightBracket, ']'},
{KeyCode.Underscore, '_'},
{KeyCode.BackQuote, '`'},
{KeyCode.Quote, '\'' },
{KeyCode.DoubleQuote, '"'},
//-------------------------NON-LOGICAL mappings-------------------------
//Alpha Numbers
{KeyCode.Alpha1, '1'},
{KeyCode.Alpha2, '2'},
{KeyCode.Alpha3, '3'},
{KeyCode.Alpha4, '4'},
{KeyCode.Alpha5, '5'},
{KeyCode.Alpha6, '6'},
{KeyCode.Alpha7, '7'},
{KeyCode.Alpha8, '8'},
{KeyCode.Alpha9, '9'},
{KeyCode.Alpha0, '0'},
{KeyCode.KeypadPeriod, '.'},
{KeyCode.KeypadDivide, '/'},
{KeyCode.KeypadMultiply, '*'},
{KeyCode.KeypadMinus, '-'},
{KeyCode.KeypadPlus, '+'},
{KeyCode.KeypadEquals, '='},
//-------------------------KEYCODES with NO CHARACER KEY-------------------------
//-----KeyCodes without Logical Mappings
//-Anything above "KeyCode.Space" in Unity's Documentation (9 KeyCodes)
//-Anything between "KeyCode.UpArrow" and "KeyCode.F15" in Unity's Documentation (24 KeyCodes)
//-Anything Below "KeyCode.Numlock" in Unity's Documentation [(28 KeyCodes) + (9 * 20 = 180 JoyStickCodes) = 208 KeyCodes]
//-------------------------other-------------------------
//-----KeyCodes that are inaccesible for some reason
//{'~', KeyCode.tilde},
//{'{', KeyCode.LeftCurlyBrace},
//{'}', KeyCode.RightCurlyBrace},
//{'|', KeyCode.Line},
//{'%', KeyCode.percent},
};
}
}