Repository navigation
Expand file tree
/
Copy pathInputMappingReporter.cs
More file actions
365 lines (326 loc) · 16.1 KB
/
Copy pathInputMappingReporter.cs
File metadata and controls
365 lines (326 loc) · 16.1 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
using System;
using System.Collections.Generic;
using System.Text;
using Rewired;
namespace DateEverythingAccess
{
internal static class InputMappingReporter
{
// Rewired action id for Move_Vertical (RewiredConsts.Action.Move_Vertical = 4). The game ships
// an UNDOCUMENTED secondary keyboard binding of the "O" key to this action (walk), NOT shown in
// the Controls UI. Because the mod's O accessibility shortcut is POLLED via GetAsyncKeyState
// (which doesn't consume the key), pressing O both opens the object tracker AND walks the
// player. We strip just that one binding at startup so O is free for the tracker; W/A/S/D and
// the arrows are untouched. See the hidden-keybinds reference memo.
private const int MoveVerticalActionId = 4;
// Display label of the key we strip off Move_Vertical. Rewired labels the letter keys by their
// letter, so matching elementIdentifierName is layout-stable rather than depending on a raw
// Rewired KeyboardKeyCode enum value.
private const string MoveVerticalKeyToStrip = "O";
// Removes the game's undocumented "O" -> Move_Vertical keyboard binding so the mod's O
// accessibility shortcut no longer also walks the player. One-shot: returns true once it has
// actually deleted the binding (or confirmed it's already gone); returns false while Rewired
// isn't ready yet so the caller can retry next frame. Scans ALL keyboard controller maps
// regardless of their enabled state — the input-mode state machine toggles map enabled flags,
// but the binding itself lives in the map and DeleteElementMap removes it permanently for the
// session. W/A/S/D and the arrows are left alone; only the O element map on action 4 is touched.
public static bool TryStripObjectTrackerMovementBinding()
{
try
{
if (!ReInput.isReady)
return false;
Player player = ReInput.players.GetPlayer(0);
if (player == null || !player.controllers.hasKeyboard)
return false;
Keyboard keyboard = player.controllers.Keyboard;
IList<ControllerMap> maps = player.controllers.maps.GetMaps(keyboard.type, keyboard.id);
if (maps == null || maps.Count == 0)
return false; // maps not populated yet — retry next frame rather than latch as done
bool sawMoveVerticalMap = false;
int removed = 0;
for (int i = 0; i < maps.Count; i++)
{
ControllerMap map = maps[i];
if (map == null)
continue;
// Collect the element-map ids to delete first, then delete — deleting while
// iterating AllMaps would mutate the collection under the loop.
List<int> toDelete = null;
for (int j = 0; j < map.AllMaps.Count; j++)
{
ActionElementMap m = map.AllMaps[j];
if (m == null || m.actionId != MoveVerticalActionId)
continue;
sawMoveVerticalMap = true;
if (!string.Equals(m.elementIdentifierName, MoveVerticalKeyToStrip, StringComparison.OrdinalIgnoreCase))
continue;
(toDelete ?? (toDelete = new List<int>())).Add(m.id);
}
if (toDelete != null)
{
for (int k = 0; k < toDelete.Count; k++)
if (map.DeleteElementMap(toDelete[k]))
removed++;
}
}
// Only consider the job done once the Move_Vertical bindings actually exist to inspect.
// Before the game loads its controller maps, no action-4 map is present — keep retrying
// so we don't latch "done" against an empty map and miss the real binding.
if (!sawMoveVerticalMap)
return false;
if (Main.Log != null)
Main.Log.LogInfo("[INPUTMAP] Stripped " + removed + " '" + MoveVerticalKeyToStrip +
"' -> Move_Vertical keyboard binding(s) so the object-tracker key no longer moves the player.");
return true;
}
catch (Exception ex)
{
if (Main.Log != null)
Main.Log.LogWarning("[INPUTMAP] TryStripObjectTrackerMovementBinding failed: " + ex.Message);
return false; // let the caller retry; a transient not-ready state shouldn't be permanent
}
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr GetKeyboardLayout(uint idThread);
// One compact line set at startup describing the keyboard as the game sees it: the Windows
// keyboard layout, which Rewired layout the keyboard maps loaded (the game swaps in different
// maps for AZERTY, Dvorak and Colemak keyboards, which moves keys like Z and X), and which
// keys are bound to every non-movement action. Written for remote testers (CrossOver/Wine
// in particular) whose keys don't do what the Controls screen says, so their log answers it.
public static void LogKeyboardSummary()
{
try
{
string hkl = "?";
try { hkl = "0x" + GetKeyboardLayout(0).ToInt64().ToString("X8"); } catch { }
if (!ReInput.isReady)
return;
Player player = ReInput.players.GetPlayer(0);
if (player == null || !player.controllers.hasKeyboard)
return;
Keyboard keyboard = player.controllers.Keyboard;
IList<ControllerMap> maps = player.controllers.maps.GetMaps(keyboard.type, keyboard.id);
StringBuilder sb = new StringBuilder();
sb.Append("[INPUTMAP] Keyboard summary: windowsLayout=").Append(hkl)
.Append(" keyboard=").Append(Safe(keyboard.name))
.Append(" maps=").Append(maps != null ? maps.Count : 0);
if (maps != null)
{
for (int i = 0; i < maps.Count; i++)
{
ControllerMap map = maps[i];
if (map == null) continue;
sb.AppendLine();
sb.Append("[INPUTMAP] map category=").Append(GetCategoryName(map.categoryId))
.Append(" layout=").Append(GetLayoutName(map.layoutId))
.Append(" enabled=").Append(map.enabled).Append(" :");
for (int j = 0; j < map.AllMaps.Count; j++)
{
ActionElementMap m = map.AllMaps[j];
if (m == null) continue;
string action = GetActionName(m.actionId, m.actionDescriptiveName);
if (action.StartsWith("Move", StringComparison.OrdinalIgnoreCase)) continue;
sb.Append(' ').Append(action).Append('=').Append(Safe(m.elementIdentifierName));
}
}
}
if (Main.Log != null)
Main.Log.LogInfo(sb.ToString());
}
catch (Exception ex)
{
if (Main.Log != null)
Main.Log.LogWarning("[INPUTMAP] Keyboard summary failed: " + ex.Message);
}
}
private static string GetLayoutName(int layoutId)
{
try
{
// Looked up by reflection so a Rewired version without this helper can't break the build.
object mapping = ReInput.mapping;
System.Reflection.MethodInfo getLayout = mapping != null
? mapping.GetType().GetMethod("GetLayout", new[] { typeof(ControllerType), typeof(int) })
: null;
object layout = getLayout != null ? getLayout.Invoke(mapping, new object[] { ControllerType.Keyboard, layoutId }) : null;
System.Reflection.PropertyInfo nameProp = layout != null ? layout.GetType().GetProperty("name") : null;
string name = nameProp != null ? nameProp.GetValue(layout, null) as string : null;
if (!string.IsNullOrEmpty(name))
return name + "(" + layoutId + ")";
}
catch { }
return layoutId.ToString();
}
public static bool TryDumpCurrentMappings(out int dumpedControllerCount)
{
dumpedControllerCount = 0;
try
{
if (!ReInput.isReady)
{
Main.Log.LogWarning("[INPUTMAP] ReInput is not ready yet.");
return false;
}
StringBuilder builder = new StringBuilder();
builder.AppendLine("[INPUTMAP] Rewired mapping dump start");
HashSet<string> dumpedControllers = new HashSet<string>(StringComparer.Ordinal);
DumpPlayerMappings(ReInput.players.GetPlayer(0), "Player0", builder, dumpedControllers, ref dumpedControllerCount);
DumpPlayerMappings(ReInput.players.GetSystemPlayer(), "SystemPlayer", builder, dumpedControllers, ref dumpedControllerCount);
if (dumpedControllerCount == 0)
{
Main.Log.LogWarning("[INPUTMAP] No active keyboard, mouse, or joystick controllers were available to dump from Player0 or SystemPlayer.");
return false;
}
builder.AppendLine("[INPUTMAP] Rewired mapping dump end");
Main.Log.LogInfo(builder.ToString());
return true;
}
catch (Exception ex)
{
Main.Log.LogError("[INPUTMAP] Failed to dump current mappings: " + ex);
return false;
}
}
private static void DumpPlayerMappings(Player player, string playerLabel, StringBuilder builder, HashSet<string> dumpedControllers, ref int dumpedControllerCount)
{
if (player == null)
{
builder.AppendLine("[INPUTMAP] Player=" + playerLabel + " is unavailable.");
return;
}
bool dumpedAnyForPlayer = false;
if (player.controllers.hasKeyboard && TryMarkController(dumpedControllers, player.controllers.Keyboard.type, player.controllers.Keyboard.id))
{
dumpedAnyForPlayer = true;
dumpedControllerCount++;
DumpControllerMaps(
builder,
playerLabel,
"Keyboard",
player.controllers.Keyboard.type,
player.controllers.Keyboard.id,
player.controllers.Keyboard.name,
player.controllers.maps.GetMaps(player.controllers.Keyboard.type, player.controllers.Keyboard.id));
}
if (player.controllers.hasMouse && TryMarkController(dumpedControllers, player.controllers.Mouse.type, player.controllers.Mouse.id))
{
dumpedAnyForPlayer = true;
dumpedControllerCount++;
DumpControllerMaps(
builder,
playerLabel,
"Mouse",
player.controllers.Mouse.type,
player.controllers.Mouse.id,
player.controllers.Mouse.name,
player.controllers.maps.GetMaps(player.controllers.Mouse.type, player.controllers.Mouse.id));
}
for (int i = 0; i < player.controllers.joystickCount; i++)
{
Joystick joystick = player.controllers.Joysticks[i];
if (joystick == null || !TryMarkController(dumpedControllers, joystick.type, joystick.id))
{
continue;
}
dumpedAnyForPlayer = true;
dumpedControllerCount++;
string label = string.IsNullOrEmpty(joystick.hardwareName) ? joystick.name : joystick.hardwareName;
DumpControllerMaps(
builder,
playerLabel,
"Joystick",
joystick.type,
joystick.id,
label,
player.controllers.maps.GetMaps(joystick.type, joystick.id));
}
if (!dumpedAnyForPlayer)
{
builder.AppendLine("[INPUTMAP] Player=" + playerLabel + " has no uniquely assigned active controllers.");
}
}
private static void DumpControllerMaps(StringBuilder builder, string playerLabel, string label, ControllerType controllerType, int controllerId, string controllerName, IList<ControllerMap> maps)
{
builder.AppendLine(string.Format(
"[INPUTMAP] Player={0} Controller={1} Type={2} Id={3} Name={4}",
playerLabel,
label,
controllerType,
controllerId,
Safe(controllerName)));
if (maps == null || maps.Count == 0)
{
builder.AppendLine("[INPUTMAP] No maps returned.");
return;
}
for (int i = 0; i < maps.Count; i++)
{
ControllerMap map = maps[i];
if (map == null)
{
continue;
}
builder.AppendLine(string.Format(
"[INPUTMAP] Map Category={0} LayoutId={1} Enabled={2}",
GetCategoryName(map.categoryId),
map.layoutId,
map.enabled));
for (int j = 0; j < map.AllMaps.Count; j++)
{
ActionElementMap actionMap = map.AllMaps[j];
if (actionMap == null)
{
continue;
}
builder.AppendLine(string.Format(
"[INPUTMAP] {0} ({1}) -> {2} [ElementType={3}, AxisRange={4}]",
Safe(GetActionName(actionMap.actionId, actionMap.actionDescriptiveName)),
actionMap.actionId,
Safe(actionMap.elementIdentifierName),
actionMap.elementType,
actionMap.axisRange));
}
}
}
private static bool TryMarkController(HashSet<string> dumpedControllers, ControllerType controllerType, int controllerId)
{
return dumpedControllers.Add(controllerType + ":" + controllerId);
}
private static string GetActionName(int actionId, string fallbackName)
{
InputAction action = ReInput.mapping.GetAction(actionId);
if (action != null && !string.IsNullOrEmpty(action.name))
{
return action.name;
}
return string.IsNullOrEmpty(fallbackName) ? "Action " + actionId : fallbackName;
}
private static string GetCategoryName(int categoryId)
{
switch (categoryId)
{
case 0:
return "Default";
case 1:
return "Dialog";
case 2:
return "CharacterController";
case 3:
return "Engagement";
case 4:
return "Debug";
case 5:
return "UI";
case 6:
return "Toggle_Dateviators";
default:
return "Category_" + categoryId;
}
}
private static string Safe(string value)
{
return string.IsNullOrEmpty(value) ? "<unnamed>" : value;
}
}
}