From ef4b1488dcb4022e031753078924473c0d735c12 Mon Sep 17 00:00:00 2001 From: LunaMoo Date: Mon, 13 Apr 2020 01:24:30 +0200 Subject: [PATCH 1/2] Clean code and add possibility to read vibration from memory. --- Core/CwCheat.cpp | 54 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/Core/CwCheat.cpp b/Core/CwCheat.cpp index 4f2e115d2e..126c58440a 100644 --- a/Core/CwCheat.cpp +++ b/Core/CwCheat.cpp @@ -27,8 +27,6 @@ std::string activeCheatFile; static CWCheatEngine *cheatEngine; static bool cheatsEnabled; SceCtrl vibrationCheat; -static u16 checkLeftVibration = 0; -static u16 checkRightVibration = 0; void hleCheat(u64 userdata, int cyclesLate); static inline std::string TrimString(const std::string &s) { @@ -415,6 +413,8 @@ enum class CheatOp { MultiWrite, CopyBytesFrom, + Vibration, + VibrationFromMemory, Delay, Assert, @@ -440,6 +440,10 @@ struct CheatOperation { uint32_t addr; int sz; uint32_t val; + uint16_t vibrL; + uint16_t vibrR; + uint8_t vibrLTime; + uint8_t vibrRTime; union { struct { @@ -595,15 +599,17 @@ CheatOperation CWCheatEngine::InterpretNextCwCheat(const CheatCode &cheat, size_ return { CheatOp::Invalid }; case 0xA: // Vibration command(PPSSPP specific) - checkLeftVibration = line1.part1 & 0x0000FFFF; - checkRightVibration = line1.part2 & 0x0000FFFF; - if (checkLeftVibration > 0) { - vibrationCheat.SetLeftVibration(checkLeftVibration); - vibrationCheat.SetVibrationLeftDropout(line1.part1 >> 16 & 0x000000FF); - } - if (checkRightVibration > 0) { - vibrationCheat.SetRightVibration(checkRightVibration); - vibrationCheat.SetVibrationRightDropout(line1.part2 >> 16 & 0x000000FF); + // 0xA1 reads vibration from memory + if (((line1.part1 >> 24) & 0xFF) == 0xA1) { + addr = line1.part2; + return { CheatOp::VibrationFromMemory, addr }; + } else { // 0xA0 sets vibration by cheat parameters + CheatOperation op = { CheatOp::Vibration }; + op.vibrL = line1.part1 & 0x0000FFFF; + op.vibrR = line1.part2 & 0x0000FFFF; + op.vibrLTime = (line1.part1 >> 16) & 0x000000FF; + op.vibrRTime = (line1.part2 >> 16) & 0x000000FF; + return op; } return { CheatOp::Invalid }; @@ -875,6 +881,32 @@ void CWCheatEngine::ExecuteOp(const CheatOperation &op, const CheatCode &cheat, } break; + case CheatOp::Vibration: + if (op.vibrL > 0) { + vibrationCheat.SetLeftVibration(op.vibrL); + vibrationCheat.SetVibrationLeftDropout(op.vibrLTime); + } + if (op.vibrR > 0) { + vibrationCheat.SetRightVibration(op.vibrR); + vibrationCheat.SetVibrationRightDropout(op.vibrRTime); + } + break; + + case CheatOp::VibrationFromMemory: + if (Memory::IsValidAddress(op.addr) && Memory::IsValidAddress(op.addr + 0x4)) { + uint16_t checkLeftVibration = Memory::Read_U16(op.addr); + uint16_t checkRightVibration = Memory::Read_U16(op.addr + 0x2); + if (checkLeftVibration > 0) { + vibrationCheat.SetLeftVibration(checkLeftVibration); + vibrationCheat.SetVibrationLeftDropout(Memory::Read_U8(op.addr + 0x4)); + } + if (checkRightVibration > 0) { + vibrationCheat.SetRightVibration(checkRightVibration); + vibrationCheat.SetVibrationRightDropout(Memory::Read_U8(op.addr + 0x6)); + } + } + break; + case CheatOp::Delay: // TODO: Not supported. break; From e7f1bf0b2ede9741011aaa938839af50bfa13ff7 Mon Sep 17 00:00:00 2001 From: LunaMoo Date: Mon, 13 Apr 2020 02:41:18 +0200 Subject: [PATCH 2/2] Improve some code and stop vibration outside of the game. --- Core/CwCheat.cpp | 41 +++++++++++++++++++++------------------- Core/HLE/sceCtrl.cpp | 29 ++++++++++++++-------------- Core/HLE/sceCtrl.h | 7 +++---- Windows/XinputDevice.cpp | 10 ++++++++-- 4 files changed, 48 insertions(+), 39 deletions(-) diff --git a/Core/CwCheat.cpp b/Core/CwCheat.cpp index 126c58440a..ec2a2a2a23 100644 --- a/Core/CwCheat.cpp +++ b/Core/CwCheat.cpp @@ -26,7 +26,8 @@ std::string gameTitle; std::string activeCheatFile; static CWCheatEngine *cheatEngine; static bool cheatsEnabled; -SceCtrl vibrationCheat; +using namespace SceCtrl; + void hleCheat(u64 userdata, int cyclesLate); static inline std::string TrimString(const std::string &s) { @@ -440,10 +441,6 @@ struct CheatOperation { uint32_t addr; int sz; uint32_t val; - uint16_t vibrL; - uint16_t vibrR; - uint8_t vibrLTime; - uint8_t vibrRTime; union { struct { @@ -467,6 +464,12 @@ struct CheatOperation { int count; int type; } pointerCommands; + struct { + uint16_t vibrL; + uint16_t vibrR; + uint8_t vibrLTime; + uint8_t vibrRTime; + } vibrationValues; }; }; @@ -605,10 +608,10 @@ CheatOperation CWCheatEngine::InterpretNextCwCheat(const CheatCode &cheat, size_ return { CheatOp::VibrationFromMemory, addr }; } else { // 0xA0 sets vibration by cheat parameters CheatOperation op = { CheatOp::Vibration }; - op.vibrL = line1.part1 & 0x0000FFFF; - op.vibrR = line1.part2 & 0x0000FFFF; - op.vibrLTime = (line1.part1 >> 16) & 0x000000FF; - op.vibrRTime = (line1.part2 >> 16) & 0x000000FF; + op.vibrationValues.vibrL = line1.part1 & 0x0000FFFF; + op.vibrationValues.vibrR = line1.part2 & 0x0000FFFF; + op.vibrationValues.vibrLTime = (line1.part1 >> 16) & 0x000000FF; + op.vibrationValues.vibrRTime = (line1.part2 >> 16) & 0x000000FF; return op; } return { CheatOp::Invalid }; @@ -882,13 +885,13 @@ void CWCheatEngine::ExecuteOp(const CheatOperation &op, const CheatCode &cheat, break; case CheatOp::Vibration: - if (op.vibrL > 0) { - vibrationCheat.SetLeftVibration(op.vibrL); - vibrationCheat.SetVibrationLeftDropout(op.vibrLTime); + if (op.vibrationValues.vibrL > 0) { + SetLeftVibration(op.vibrationValues.vibrL); + SetVibrationLeftDropout(op.vibrationValues.vibrLTime); } - if (op.vibrR > 0) { - vibrationCheat.SetRightVibration(op.vibrR); - vibrationCheat.SetVibrationRightDropout(op.vibrRTime); + if (op.vibrationValues.vibrR > 0) { + SetRightVibration(op.vibrationValues.vibrR); + SetVibrationRightDropout(op.vibrationValues.vibrRTime); } break; @@ -897,12 +900,12 @@ void CWCheatEngine::ExecuteOp(const CheatOperation &op, const CheatCode &cheat, uint16_t checkLeftVibration = Memory::Read_U16(op.addr); uint16_t checkRightVibration = Memory::Read_U16(op.addr + 0x2); if (checkLeftVibration > 0) { - vibrationCheat.SetLeftVibration(checkLeftVibration); - vibrationCheat.SetVibrationLeftDropout(Memory::Read_U8(op.addr + 0x4)); + SetLeftVibration(checkLeftVibration); + SetVibrationLeftDropout(Memory::Read_U8(op.addr + 0x4)); } if (checkRightVibration > 0) { - vibrationCheat.SetRightVibration(checkRightVibration); - vibrationCheat.SetVibrationRightDropout(Memory::Read_U8(op.addr + 0x6)); + SetRightVibration(checkRightVibration); + SetVibrationRightDropout(Memory::Read_U8(op.addr + 0x6)); } } break; diff --git a/Core/HLE/sceCtrl.cpp b/Core/HLE/sceCtrl.cpp index 42fc548d38..17ab7084cd 100644 --- a/Core/HLE/sceCtrl.cpp +++ b/Core/HLE/sceCtrl.cpp @@ -574,24 +574,25 @@ void Register_sceCtrl_driver() RegisterModule("sceCtrl_driver", ARRAY_SIZE(sceCtrl), sceCtrl); } -u16 GetRightVibration() { +u16 sceCtrlGetRightVibration() { return rightVibration; } -u16 GetLeftVibration() { +u16 sceCtrlGetLeftVibration() { return leftVibration; } -void SceCtrl::SetRightVibration(u16 rVibration) { - rightVibration = rVibration; -} -void SceCtrl::SetLeftVibration(u16 lVibration) { - leftVibration = lVibration; -} -void SceCtrl::SetVibrationRightDropout(u8 vibrationRDropout) { - vibrationRightDropout = vibrationRDropout; - -} -void SceCtrl::SetVibrationLeftDropout(u8 vibrationLDropout) { - vibrationLeftDropout = vibrationLDropout; +namespace SceCtrl { + void SetRightVibration(u16 rVibration) { + rightVibration = rVibration; + } + void SetLeftVibration(u16 lVibration) { + leftVibration = lVibration; + } + void SetVibrationRightDropout(u8 vibrationRDropout) { + vibrationRightDropout = vibrationRDropout; + } + void SetVibrationLeftDropout(u8 vibrationLDropout) { + vibrationLeftDropout = vibrationLDropout; + } } diff --git a/Core/HLE/sceCtrl.h b/Core/HLE/sceCtrl.h index ab82291fea..039b4e7404 100644 --- a/Core/HLE/sceCtrl.h +++ b/Core/HLE/sceCtrl.h @@ -88,11 +88,10 @@ u32 __CtrlReadLatch(); void Register_sceCtrl_driver(); -u16 GetRightVibration(); -u16 GetLeftVibration(); +u16 sceCtrlGetRightVibration(); +u16 sceCtrlGetLeftVibration(); -class SceCtrl { -public: +namespace SceCtrl { void SetLeftVibration(u16 lVibration); void SetRightVibration(u16 rVibration); void SetVibrationLeftDropout(u8 vibrationLDropout); diff --git a/Windows/XinputDevice.cpp b/Windows/XinputDevice.cpp index a948c3ab3b..d1905d7b94 100644 --- a/Windows/XinputDevice.cpp +++ b/Windows/XinputDevice.cpp @@ -361,8 +361,14 @@ void XinputDevice::ApplyButtons(int pad, const XINPUT_STATE &state) { void XinputDevice::ApplyVibration(int pad, XINPUT_VIBRATION &vibration) { if (PSP_IsInited()) { - vibration.wLeftMotorSpeed = GetLeftVibration(); // use any value between 0-65535 here - vibration.wRightMotorSpeed = GetRightVibration(); // use any value between 0-65535 here + if (GetUIState() == UISTATE_INGAME) { + vibration.wLeftMotorSpeed = sceCtrlGetLeftVibration(); // use any value between 0-65535 here + vibration.wRightMotorSpeed = sceCtrlGetRightVibration(); // use any value between 0-65535 here + } else { + vibration.wLeftMotorSpeed = 0; + vibration.wRightMotorSpeed = 0; + } + if (prevVibration[pad].wLeftMotorSpeed != vibration.wLeftMotorSpeed || prevVibration[pad].wRightMotorSpeed != vibration.wRightMotorSpeed) { PPSSPP_XInputSetState(pad, &vibration); prevVibration[pad] = vibration;