AHK 为 RetroArch 模拟器重新分配鼠标索引

AHK 为 RetroArch 模拟器重新分配鼠标索引

我有一个使用 RetroArch 的光枪街机。它依赖于鼠标索引,可以通过控制器重映射实用工具获取。这会产生以下信息

ControllerRemap Utility v0.0.11.0 - For Mame
(c) 2011 drventure Enterprises


Joystick Device List (Devices are in enumeration order) ...

   1.  'SindenLightgun', ID=1
   2.  'SindenLightgun', ID=2

Mouse Device List (Devices are in enumeration order) ...

   1.  'HID#VID_16C0_PID_0F38_MI_02_Col02#9_9dde10_0_0001#'
   2.  'HID#VID_16C0_PID_0F39_MI_02_Col02#9_230b0b6a_0_0001#'
   3.  'HID#VID_046D_PID_C534_MI_01_Col01#8_8d9ff6d_0_0000#'

End of list

此信息用于输入模拟器配置文件(retroarch.cfg)中鼠标索引的任何更改

执行 AHK 并提取此信息,但总是得出错误的值

input_player1_mouse_index = "4"
input_player2_mouse_index = "3"

应该

input_player1_mouse_index = "2"
input_player2_mouse_index = "1"

最后,这是完整的 AHK(如果有点长,请原谅)如果有人碰巧知道可以在代码中进行哪些更改以反映正确的结果,那就太好了。

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance force


FileDelete, %A_Scriptdir%\list.txt
FileDelete, %A_Scriptdir%\retroarch.cfg.temp
ToolTip, Initializing Sinden Remap, mouseX, mouseY
iniread, P1pid, sindenremap.ini, general, P1pid, 1
iniread, P2pid, sindenremap.ini, general, P2pid, 2
iniread, P1name, sindenremap.ini, general, P1name, 0
iniread, P2name, sindenremap.ini, general, P2name, 0
iniread, guns, sindenremap.ini, general, guns, 0

tooltip

if (guns>0)
    gosub emuconfig

start:
MsgBox, 49, Sinden ID Remap utility configuration, This tool wil configure your lightgun indexes for Retroarch and Supermodel.`n`nUnplug your Sinden Lightgun(s) to and press OK to begin

Ifmsgbox, Cancel
    ExitApp
ifmsgbox, OK
    gosub checkgun1

checkgun1:
sleep 300
ToolTip, Reading device IDs, mouseX, mouseY
Run %comspec% /c `"%A_Scriptdir%\ControllerRemap.exe`" /list > list.txt ,, hide
sleep 500

ID = 0
numID = 0
guns = 0

ToolTip, Identifying Lightguns, mouseX, mouseY
Loop, read, %A_ScriptDir%\list.txt  , %A_scriptdir%\IDs.txt
{ 
ID := SubStr(A_LoopReadLine, 4 , 1)
if (ID > 0 AND ID < 20 )
{
    If InStr(A_LoopReadLine, "VID_16C0_PID_0F01") or InStr(A_LoopReadLine, "VID_16C0_PID_0F02") or InStr(A_LoopReadLine, "VID_16C0_PID_0F38") or InStr(A_LoopReadLine, "VID_16C0_PID_0F39")
        guns += 1
    }
tooltip
If (guns>0)
{
    MsgBox, 17, Sinden ID Remap utility configuration, Gun(s) still connected. Disconnect your gun(s) and press OK
    gosub checkgun1
}

}
; detect player 1 gun----------------------------------------------------------------------------------------
noguns:
FileDelete, %A_Scriptdir%\list.txt
MsgBox, 65, Sinden ID Remap utility configuration, Connect your Player 1 lightgun now.`n`nWait until it's detected by Windows (You will hear the USB connection sound) and press OK.
IfMsgBox, cancel
    ExitApp
sleep 1000
ToolTip, Reading device IDs, mouseX, mouseY
Run %comspec% /c `"%A_Scriptdir%\ControllerRemap.exe`" /list > list.txt ,, hide

sleep 500

ID = 0
numID = 0
guns = 0

ToolTip, Identifying Player 1 Lightgun, mouseX, mouseY
Loop, read, %A_ScriptDir%\list.txt  , %A_scriptdir%\IDs.txt
{ 
ID := SubStr(A_LoopReadLine, 4 , 1)
if (ID > 0 AND ID < 20 )
{
    If InStr(A_LoopReadLine, "VID_16C0_PID_0F01")
    {
        P1pid = %ID%
        P1name = F01
        guns += 1
        break
    }
    If InStr(A_LoopReadLine, "VID_16C0_PID_0F02")
    {
        P1pid = %ID%
        P1name = F02
        guns += 1
        break
    }
    If InStr(A_LoopReadLine, "VID_16C0_PID_0F38")
    {
        P1pid = %ID%
        P1name = F38
        guns += 1
        break
    }
    If InStr(A_LoopReadLine, "VID_16C0_PID_0F39")
    {
        P1pid = %ID%
        P1name = F39
        guns += 1
        break
    }
numID+=1    
}
}

if (guns=0)
    gosub noguns
ToolTip

; detect player 2 gun----------------------------------------------------------------------------------------

MsgBox, 36, Sinden ID Remap utility configuration, Lightgun1 (%P1name%) detected with index %P1pid%`nDo you have 2 lightguns?

IfMsgBox, Yes
{ 
    gosub detectp2gun
}
else
{
    P2pid=nul
    gosub saveini
}

detectp2gun:
FileDelete, %A_Scriptdir%\list.txt
MsgBox, 65, Sinden ID Remap utility configuration, Connect your Player 2 lightgun now.`n`nWait until it's detected by Windows (You will hear the USB connection sound) and press OK.
IfMsgBox, cancel
    ExitApp
sleep 1000
ToolTip, Reading device IDs, mouseX, mouseY
Run %comspec% /c `"%A_Scriptdir%\ControllerRemap.exe`" /list > list.txt ,, hide

sleep 500

ID = 0

ToolTip, Identifying Player 2 Lightgun, mouseX, mouseY
Loop, read, %A_ScriptDir%\list.txt  , %A_scriptdir%\IDs.txt
{ 
ID2 := SubStr(A_LoopReadLine, 4 , 1)
if (ID2 > 0 AND ID2 < 20 )
{
    If InStr(A_LoopReadLine, "VID_16C0_PID_0F01")
    {
        if (ID2 != P1pid)
        {
            P2pid = %ID2%
            P2name = F01
            break
        }
    }
    If InStr(A_LoopReadLine, "VID_16C0_PID_0F02")
    {
        
        if (ID2 != P1pid)
        {
            P2pid = %ID2%
            P2name = F02
            break
        }
    }
    If InStr(A_LoopReadLine, "VID_16C0_PID_0F38")
    {
        if (ID2 != P1pid)
        {
            P2pid = %ID2%
            P2name = F38
            break
        }
    }
    If InStr(A_LoopReadLine, "VID_16C0_PID_0F39")
    {
        if (ID2 != P1pid)
        {
            P2pid = %ID2%
            P2name = F39
            break
        }
    }
}
if (P2name!=0)
    break
}

if (P2name=0)
    gosub detectp2gun

guns+=1
ToolTip
gosub saveini

saveini:
change = 0
sleep 500
IniWrite, %p2pid%, sindenremap.ini, general, P2pid
iniwrite, %guns%, sindenremap.ini, general, guns
IniWrite, %P1pid%, sindenremap.ini, general, P1pid
Iniwrite, %P1name%, sindenremap.ini, general, P1name
Iniwrite, %P2name%, sindenremap.ini, general, P2name

emuconfig:

Tooltip, Number of guns: %guns%`nPlayer 1 %P1name%: %P1pid% `nPlayer 2 %P1name%: %P2pid%
sleep 1000


;check if PID have changed--------------------------------------------------------------------------------
ToolTip, Checking if indexes have changed, mouseX, mouseY
Run %comspec% /c `"%A_Scriptdir%\ControllerRemap.exe`" /list > list.txt ,, hide
sleep 500
currentguns = 0
newP1pid = %P1pid%
newP2pid = %P2pid%
numID = 0
Loop, read, %A_ScriptDir%\list.txt  , %A_scriptdir%\IDs.txt
{ 
IDC := SubStr(A_LoopReadLine, 4 , 1)
if (IDC > 0 AND IDC < 20 )
{
    If InStr(A_LoopReadLine, P1name)
    {
        if (IDC != P1pid)
        {
            tooltip, Lightgun 1 Index changed. Adjusting new index
            newP1pid = %IDC%
            
            change = 1
        }
        currentguns += 1
    }
    If InStr(A_LoopReadLine, P2name)
    {
        if (IDC != P2pid)
        {
            tooltip, Lightgun 1 Index changed. Adjusting new index
            newP2pid = %IDC%
            change = 1
            
        }
        currentguns += 1
    }
    numID +=1
}
}

if (guns!=currentguns)
{
    MsgBox, 53, Sinden ID Remap utility configuration, One or both lightguns can't be found.`n(Re)Connect your guns and click Retry or press Cancel to run the configuration tool.
    ifmsgbox, Retry
        gosub emuconfig
    ifmsgbox, Cancel
        gosub start
}

if (change=1)
{
    P1pid = %newP1pid%
    P2pid = %newP2pid%
    gosub saveini
}
tooltip

ToolTip, Finding emulator, mouseX, mouseY
; Retroarch config----------------------------------------------------------------------------------------

if FileExist(A_scriptDir "\retroarch.exe")
{
    P1pid := numID - P1pid
    if (guns > 1)
        P2pid := numID - P2pid
    else
        P2pid = nul
ToolTip, Editing Retroarch.cfg, mouseX, mouseY
Loop, read, retroarch.cfg  , retroarch.cfg.temp
{
    If InStr(A_LoopReadLine, "input_player1_mouse_index")
    {
        Fileappend, input_player1_mouse_index = `"%P1pid%`"`n, retroarch.cfg.temp
    }
    else if InStr(A_LoopReadLine, "input_player2_mouse_index")
    {
        Fileappend, input_player2_mouse_index = `"%P2pid%`"`n, retroarch.cfg.temp
    }
    else
    {
        FileAppend, %A_LoopReadLine%`n, retroarch.cfg.temp
    }
}
sleep 500
FileMove, retroarch.cfg, retroarch.cfg.bak, 1
sleep 500
Filemove, retroarch.cfg.temp, retroarch.cfg, 1
sleep 2000
}

; Supermodel config----------------------------------------------------------------------------------------
ToolTip, Editing Supermodel.ini, mouseX, mouseY
if FileExist(A_scriptDir "\supermodel.exe")
{
    ;Write changes to supermodel.ini
    ;Player1 GUN
    iniwrite, %a_Space%`"MOUSE%P1pid%_XAXIS`,JOY1_XAXIS`" , %A_ScriptDir%\Config\Supermodel.ini, global, InputGunX  
    iniwrite, %a_Space%`"MOUSE%P1pid%_YAXIS`,JOY1_YAXIS`" , %A_ScriptDir%\Config\Supermodel.ini, global, InputGunY 
    iniwrite, %a_Space%`"KEY_A`,JOY1_BUTTON1`,MOUSE%P1pid%_LEFT_BUTTON`" , %A_ScriptDir%\Config\Supermodel.ini, global, InputTrigger
    iniwrite, %a_Space%`"KEY_S`,JOY1_BUTTON2`,MOUSE%P1pid%_RIGHT_BUTTON`" , %A_ScriptDir%\Config\Supermodel.ini, global, InputOffscreen
    
    ;Player1 ANALOG GUN
    iniwrite, %a_Space%`"MOUSE%P1pid%_XAXIS`,JOY1_XAXIS`" , %A_ScriptDir%\Config\Supermodel.ini, global, InputAnalogGunX  
    iniwrite, %a_Space%`"MOUSE%P1pid%_YAXIS`,JOY1_YAXIS`" , %A_ScriptDir%\Config\Supermodel.ini, global, InputAnalogGunY 
    iniwrite, %a_Space%`"KEY_A`,JOY1_BUTTON1`,MOUSE%P1pid%_LEFT_BUTTON`" , %A_ScriptDir%\Config\Supermodel.ini, global, InputAnalogTriggerLeft
    iniwrite, %a_Space%`"KEY_S`,JOY1_BUTTON2`,MOUSE%P1pid%_RIGHT_BUTTON`" , %A_ScriptDir%\Config\Supermodel.ini, global, InputAnalogTriggerRight
    
    ;Player1 ANALOG JOYSTICK
    iniwrite, %a_Space%`"MOUSE%P1pid%_XAXIS`,JOY1_XAXIS`" , %A_ScriptDir%\Config\Supermodel.ini, global, InputAnalogJoyX  
    iniwrite, %a_Space%`"MOUSE%P1pid%_YAXIS`,JOY1_YAXIS`" , %A_ScriptDir%\Config\Supermodel.ini, global, InputAnalogJoyY 

    
    if (guns>1)
    {
    ;Player2 GUN
    iniwrite, %a_Space%`"MOUSE%P2pid%_XAXIS`,JOY2_XAXIS`" , %A_ScriptDir%\Config\Supermodel.ini, global, InputGunX2
    iniwrite, %a_Space%`"MOUSE%P2pid%_YAXIS`,JOY2_YAXIS`" , %A_ScriptDir%\Config\Supermodel.ini, global, InputGunY2
    iniwrite, %a_Space%`"KEY_Z`,JOY2_BUTTON1`,MOUSE%P2pid%_LEFT_BUTTON`" , %A_ScriptDir%\Config\Supermodel.ini, global, InputTrigger2
    iniwrite, %a_Space%`"KEY_X`,JOY2_BUTTON2`,MOUSE%P2pid%_RIGHT_BUTTON`" , %A_ScriptDir%\Config\Supermodel.ini, global, InputOffscreen2

    ;Player2 ANALOG GUN
    iniwrite, %a_Space%`"MOUSE%P2pid%_XAXIS`,JOY2_XAXIS`" , %A_ScriptDir%\Config\Supermodel.ini, global, InputAnalogGunX2
    iniwrite, %a_Space%`"MOUSE%P2pid%_YAXIS`,JOY2_YAXIS`" , %A_ScriptDir%\Config\Supermodel.ini, global, InputAnalogGunY2
    iniwrite, %a_Space%`"KEY_Z`,JOY2_BUTTON1`,MOUSE%P2pid%_LEFT_BUTTON`" , %A_ScriptDir%\Config\Supermodel.ini, global, InputAnalogTriggerLeft2
    iniwrite, %a_Space%`"KEY_X`,JOY2_BUTTON2`,MOUSE%P2pid%_RIGHT_BUTTON`" , %A_ScriptDir%\Config\Supermodel.ini, global, InputAnalogTriggerRight2

    }
}

ToolTip, Closing, mouseX, mouseY
Sleep 2000
ToolTip
ExitApp


ToolTip
ExitApp

答案1

您可能需要获取一个调试器,以便您在调试模式下(逐步)运行脚本,逐步检查实际发生的情况。虽然注释中的假设是合理的(即忽略操纵杆设备),但实际上可能并非如此。我还可以在检查中看到一些代码,这些代码会翻转数字,这可能是应该删除操纵杆项的代码,因此可能无法正确执行,或者可能发生了其他事情。

例如,FileExist(A_scriptDir "\retroarch.exe") 下的代码块翻转了检测到的 P1pid 和 P2pid 值,这些值稍后会写入磁盘。如果检测到总共 5 个项目(包括操纵杆项目),我预计该代码会执行,然后玩家 1 鼠标索引将是 5-4=1,玩家 2 鼠标索引将是 5-3=2,其中 4 和 3 是您上面显示的值,5 将是检测到的操纵杆+鼠标项目的总数。这只是一个假设。

我建议下载 SciTE4AutoHotkey,然后逐步执行代码并查看发生了什么。

相关内容