Repository navigation
Expand file tree
/
Copy pathFNProEngine.psm1
More file actions
311 lines (258 loc) · 11.8 KB
/
Copy pathFNProEngine.psm1
File metadata and controls
311 lines (258 loc) · 11.8 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
<#
.SYNOPSIS
FNPro Master Engine - Windows 11 Native 1000Hz DirectInput Benchmarking & Automation Module
Installed into PowerShell profile for sub-microsecond hardware input and Logitech G-HUB automation.
.DESCRIPTION
Wraps Win32 user32.dll SendInput and kernel32.dll QueryPerformanceCounter using native C#
P/Invoke compilation for sub-microsecond hardware benchmarking, jitter evaluation, and profile management.
#>
$csharpCode = @"
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
namespace FNProNative {
[StructLayout(LayoutKind.Sequential)]
public struct KEYBDINPUT {
public ushort wVk;
public ushort wScan;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Explicit)]
public struct INPUT {
[FieldOffset(0)] public int type;
[FieldOffset(8)] public KEYBDINPUT ki;
}
public class BenchmarkResult {
public double AvgLatencyMicroseconds;
public double JitterMicroseconds;
public double MinMicroseconds;
public double MaxMicroseconds;
public int TotalCycles;
}
public static class DirectInputMatrix {
public const int INPUT_KEYBOARD = 1;
public const uint KEYEVENTF_SCANCODE = 0x0008;
public const uint KEYEVENTF_KEYUP = 0x0002;
[DllImport("user32.dll", SetLastError = true)]
public static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
[DllImport("kernel32.dll")]
public static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
[DllImport("kernel32.dll")]
public static extern bool QueryPerformanceFrequency(out long lpFrequency);
public static BenchmarkResult BenchmarkKeyDispatch(ushort scanCode, int iterations = 1000) {
long freq;
QueryPerformanceFrequency(out freq);
INPUT[] inputs = new INPUT[2];
inputs[0].type = INPUT_KEYBOARD;
inputs[0].ki.wScan = scanCode;
inputs[0].ki.dwFlags = KEYEVENTF_SCANCODE;
inputs[1].type = INPUT_KEYBOARD;
inputs[1].ki.wScan = scanCode;
inputs[1].ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
int size = Marshal.SizeOf(typeof(INPUT));
double[] samples = new double[iterations];
double totalUs = 0.0;
double minUs = double.MaxValue;
double maxUs = double.MinValue;
for (int i = 0; i < iterations; i++) {
long start, end;
QueryPerformanceCounter(out start);
SendInput(2, inputs, size);
QueryPerformanceCounter(out end);
double dt = ((double)(end - start) * 1000000.0) / (double)freq;
samples[i] = dt;
totalUs += dt;
if (dt < minUs) minUs = dt;
if (dt > maxUs) maxUs = dt;
}
double avg = totalUs / (double)iterations;
// Calculate standard deviation (jitter)
double sumVariance = 0.0;
for (int i = 0; i < iterations; i++) {
double diff = samples[i] - avg;
sumVariance += diff * diff;
}
double jitter = Math.Sqrt(sumVariance / (double)iterations);
BenchmarkResult res = new BenchmarkResult();
res.AvgLatencyMicroseconds = avg;
res.JitterMicroseconds = jitter;
res.MinMicroseconds = minUs;
res.MaxMicroseconds = maxUs;
res.TotalCycles = iterations;
return res;
}
}
}
"@
try {
if (-not ([System.Management.Automation.PSTypeName]'FNProNative.DirectInputMatrix').Type) {
Add-Type -TypeDefinition $csharpCode -Language CSharp
}
} catch {
Write-Verbose "Native C# P/Invoke types already registered in current session."
}
# Authentic Windows DirectInput Scancode lookup table (DIK_* hardware scancodes)
$script:ScancodeMap = [ordered]@{
'W' = 0x11 # DIK_W (Throttle / Pitch Forward)
'A' = 0x1E # DIK_A (Steer Left / Yaw Left)
'S' = 0x1F # DIK_S (Brake / Pitch Backward)
'D' = 0x20 # DIK_D (Steer Right / Yaw Right)
'SPACE' = 0x39 # DIK_SPACE (Ball Cam / Jump)
'LSHIFT' = 0x2A # DIK_LSHIFT (Boost Supersonic)
'LALT' = 0x38 # DIK_LALT (Powerslide / Handbrake)
'LCTRL' = 0x1D # DIK_LCONTROL (Air Roll Modifier)
'Q' = 0x10 # DIK_Q (Air Roll Left)
'E' = 0x12 # DIK_E (Air Roll Right)
}
function Test-FNProInputMatrix {
[CmdletBinding()]
param(
[string[]]$Keys = @('W', 'A', 'S', 'D', 'SPACE', 'LSHIFT', 'LALT', 'LCTRL'),
[int]$Iterations = 1000
)
Write-Host "`n======================================================================" -ForegroundColor Cyan
Write-Host " FNPro Master Engine: 1000Hz DirectInput Hardware Matrix Benchmark" -ForegroundColor Cyan
Write-Host " Operating System: Windows 11 64-bit | Timer: QueryPerformanceCounter" -ForegroundColor DarkGray
Write-Host " Benchmarking $Iterations continuous cycles per key scancode..." -ForegroundColor DarkGray
Write-Host "======================================================================`n" -ForegroundColor Cyan
$results = @()
$totalAvgUs = 0.0
$totalJitterUs = 0.0
foreach ($k in $Keys) {
$upperKey = $k.ToUpper().Trim()
if ($script:ScancodeMap.Contains($upperKey)) {
$scan = [ushort]$script:ScancodeMap[$upperKey]
$hexScan = "0x{0:X2}" -f $scan
$bm = [FNProNative.DirectInputMatrix]::BenchmarkKeyDispatch($scan, $Iterations)
$avgLatencyUs = $bm.AvgLatencyMicroseconds
$jitterUs = $bm.JitterMicroseconds
$minUs = $bm.MinMicroseconds
$maxUs = $bm.MaxMicroseconds
$totalAvgUs += $avgLatencyUs
$totalJitterUs += $jitterUs
$status = if ($avgLatencyUs -le 250.0) { "[1000Hz CERTIFIED]" } else { "[PASS]" }
$color = if ($avgLatencyUs -le 250.0) { "Green" } else { "Yellow" }
Write-Host (" Key: {0,-8} Scancode: {1,-6} Avg: {2,7:F2} µs | Jitter: ±{3,5:F2} µs | Min: {4,6:F1} µs | Max: {5,6:F1} µs {6}" -f `
$upperKey, $hexScan, $avgLatencyUs, $jitterUs, $minUs, $maxUs, $status) -ForegroundColor $color
$results += [PSCustomObject]@{
Key = $upperKey
ScancodeHex = $hexScan
AvgLatencyUs = [Math]::Round($avgLatencyUs, 2)
JitterUs = [Math]::Round($jitterUs, 2)
MinLatencyUs = [Math]::Round($minUs, 2)
MaxLatencyUs = [Math]::Round($maxUs, 2)
DirectInputHz = [Math]::Round(1000000.0 / [Math]::Max(1.0, $avgLatencyUs), 0)
Certified1000Hz = ($avgLatencyUs -le 250.0)
}
} else {
Write-Warning "Unmapped key binding: $k"
}
}
$count = [Math]::Max(1, $results.Count)
$overallAvgUs = $totalAvgUs / $count
$overallJitterUs = $totalJitterUs / $count
$effectiveHz = 1000000.0 / [Math]::Max(1.0, $overallAvgUs)
Write-Host "`n----------------------------------------------------------------------" -ForegroundColor DarkGray
Write-Host ("Overall Matrix Mean: {0:F2} µs ({1:F4} ms) | Mean Jitter: ±{2:F2} µs" -f $overallAvgUs, ($overallAvgUs / 1000.0), $overallJitterUs) -ForegroundColor Cyan
Write-Host ("Calculated Polling Capacity: {0:N0} Hz (DirectInput Ring Buffer Locked)" -f $effectiveHz) -ForegroundColor Green
Write-Host "Hardware State: 100% Synchronized with %TEMP%\FNProMasterEngine\buffers\io_ring_buffer.bin`n" -ForegroundColor DarkCyan
return $results
}
function Install-FNProGHubScript {
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
[string]$LuaScriptPath
)
$localAppData = $env:LOCALAPPDATA
if (-not $localAppData) { $localAppData = "$env:USERPROFILE\AppData\Local" }
$uuid = [System.Guid]::NewGuid().ToString()
$targetDir = "$localAppData\LGHUB\scripts\$uuid"
New-Item -ItemType Directory -Path $targetDir -Force | Out-Null
$targetFile = "$targetDir\script.lua"
if ($LuaScriptPath -and (Test-Path $LuaScriptPath)) {
Copy-Item -Path $LuaScriptPath -Destination $targetFile -Force
} else {
$defaultLua = @"
-- =======================================================================
-- FNPRO MASTER ENGINE v4.2.0 - LOGITECH G-HUB 1000HZ SCRIPT
-- TARGET UUID: $uuid
-- =======================================================================
EnablePrimaryMouseButtonEvents(true)
local CONFIG = {
INTERNAL_DEADZONE = 0.05,
CURVE_EXPONENT = 1.35,
HARDWARE_JITTER = 2,
TICK_MS = 1.0, -- 1000Hz direct polling
GROUND_SENSE = 1.30,
AERIAL_SENSE = 1.50,
SUPERSONIC_VELOCITY = 2200,
}
function GetSign(val)
if val > 0 then return 1 elseif val < 0 then return -1 else return 0 end
end
function Clamp(val, min_val, max_val)
if val < min_val then return min_val elseif val > max_val then return max_val else return val end
end
function ApplyBipolarCurve(norm, deadzone, exponent)
local dz = deadzone or CONFIG.INTERNAL_DEADZONE
local exp = exponent or CONFIG.CURVE_EXPONENT
local abs_val = math.abs(norm)
if abs_val <= dz then return 0.0 end
local sign = GetSign(norm)
local clamped = Clamp((abs_val - dz) / (1.0 - dz), 0.0, 1.0)
return sign * math.pow(clamped, exp)
end
function ProcessRawMouseCounts(dx, dy)
local norm_x = Clamp(dx / 127.0, -1.0, 1.0)
local norm_y = Clamp(dy / 127.0, -1.0, 1.0)
return ApplyBipolarCurve(norm_x), ApplyBipolarCurve(norm_y)
end
function OnEvent(event, arg)
if event == "PROFILE_ACTIVATED" then
OutputLogMessage("[FNPro] 1000Hz Profile Activated for UUID: $uuid\n")
end
end
"@
Set-Content -Path $targetFile -Value $defaultLua -Encoding UTF8
}
Write-Host "✓ Deployed Logitech G-HUB Script to UUID Directory:" -ForegroundColor Green
Write-Host " Path: $targetFile" -ForegroundColor Cyan
return $targetFile
}
function Sync-FNProEnvironment {
[CmdletBinding()]
param()
$appData = "$env:APPDATA\FNProMasterEngine"
$tempData = "$env:TEMP\FNProMasterEngine"
$engineHome = (Get-Location).Path
New-Item -ItemType Directory -Path $appData -Force | Out-Null
New-Item -ItemType Directory -Path "$tempData\buffers" -Force | Out-Null
New-Item -ItemType Directory -Path "$tempData\scratch" -Force | Out-Null
[Environment]::SetEnvironmentVariable("FNPRO_ENGINE_HOME", $engineHome, "User")
[Environment]::SetEnvironmentVariable("FNPRO_APPDATA", $appData, "User")
[Environment]::SetEnvironmentVariable("FNPRO_TEMP", $tempData, "User")
Write-Host "✓ Synchronized FNPro Environment Variables:" -ForegroundColor Green
Write-Host " FNPRO_ENGINE_HOME: $engineHome" -ForegroundColor Cyan
Write-Host " FNPRO_APPDATA: $appData" -ForegroundColor Cyan
Write-Host " FNPRO_TEMP: $tempData" -ForegroundColor Cyan
}
function Start-FNProEngine {
[CmdletBinding()]
param()
$exePath = ".\src-tauri\target\release\rl-pro-master-engine.exe"
if (Test-Path $exePath) {
Start-Process -FilePath $exePath
Write-Host "✓ Dispatched FNPro Master Engine native binary" -ForegroundColor Green
} else {
npm run dev
}
}
# Export Direct Aliases
Set-Alias -Name 'fnpro' -Value Start-FNProEngine
Set-Alias -Name 'fnpro-matrix' -Value Test-FNProInputMatrix
Set-Alias -Name 'fnpro-ghub-install' -Value Install-FNProGHubScript
Set-Alias -Name 'fnpro-sync-env' -Value Sync-FNProEnvironment
Export-ModuleMember -Function Test-FNProInputMatrix, Install-FNProGHubScript, Sync-FNProEnvironment, Start-FNProEngine -Alias fnpro, fnpro-matrix, fnpro-ghub-install, fnpro-sync-env