Skip to content

Commit

Permalink
Implement Xinput vibration cheat
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaMoo committed Apr 12, 2020
1 parent 7d3552a commit 78b9f3d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
29 changes: 25 additions & 4 deletions Core/CwCheat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ std::string gameTitle;
std::string activeCheatFile;
static CWCheatEngine *cheatEngine;
static bool cheatsEnabled;
static u16 leftVibration = 0;
static u16 rightVibration = 0;
void hleCheat(u64 userdata, int cyclesLate);

static inline std::string TrimString(const std::string &s) {
Expand Down Expand Up @@ -412,6 +414,7 @@ enum class CheatOp {
MultiWrite,

CopyBytesFrom,
Vibration,
Delay,

Assert,
Expand Down Expand Up @@ -469,6 +472,8 @@ CheatOperation CWCheatEngine::InterpretNextCwCheat(const CheatCode &cheat, size_

// Filled as needed.
u32 addr;
leftVibration = 0;
rightVibration = 0;

int type = line1.part1 >> 28;
switch (type) {
Expand Down Expand Up @@ -517,7 +522,7 @@ CheatOperation CWCheatEngine::InterpretNextCwCheat(const CheatCode &cheat, size_
case 0x4: // 32-bit multi-write patch data.
addr = GetAddress(line1.part1 & 0x0FFFFFFF);
if (i < cheat.lines.size()) {
const CheatLine &line2 = cheat.lines[i++];
const CheatLine& line2 = cheat.lines[i++];

CheatOperation op = { CheatOp::MultiWrite, addr, 4, line2.part1 };
op.multiWrite.count = arg >> 16;
Expand All @@ -530,7 +535,7 @@ CheatOperation CWCheatEngine::InterpretNextCwCheat(const CheatCode &cheat, size_
case 0x5: // Memcpy command.
addr = GetAddress(line1.part1 & 0x0FFFFFFF);
if (i < cheat.lines.size()) {
const CheatLine &line2 = cheat.lines[i++];
const CheatLine& line2 = cheat.lines[i++];

CheatOperation op = { CheatOp::CopyBytesFrom, addr, 0, arg };
op.copyBytesFrom.destAddr = GetAddress(line2.part1 & 0x0FFFFFFF);
Expand All @@ -541,7 +546,7 @@ CheatOperation CWCheatEngine::InterpretNextCwCheat(const CheatCode &cheat, size_
case 0x6: // Pointer commands.
addr = GetAddress(line1.part1 & 0x0FFFFFFF);
if (i < cheat.lines.size()) {
const CheatLine &line2 = cheat.lines[i++];
const CheatLine& line2 = cheat.lines[i++];
int count = (line2.part1 & 0xFFFF) - 1;

// Validate lines to process - make sure we stay inside cheat.lines.
Expand Down Expand Up @@ -579,7 +584,7 @@ CheatOperation CWCheatEngine::InterpretNextCwCheat(const CheatCode &cheat, size_
case 0x8: // 8-bit or 16-bit multi-write patch data.
addr = GetAddress(line1.part1 & 0x0FFFFFFF);
if (i < cheat.lines.size()) {
const CheatLine &line2 = cheat.lines[i++];
const CheatLine& line2 = cheat.lines[i++];
const bool is8Bit = (line2.part1 & 0xFFFF0000) == 0;
const uint32_t val = is8Bit ? (line2.part1 & 0xFF) : (line2.part1 & 0xFFFF);

Expand All @@ -591,6 +596,11 @@ CheatOperation CWCheatEngine::InterpretNextCwCheat(const CheatCode &cheat, size_
}
return { CheatOp::Invalid };

case 0xA: // Vibration command(PPSSPP specific)
leftVibration = line1.part1 & 0x0000FFFF;
rightVibration = line1.part2 & 0x0000FFFF;
return { CheatOp::Invalid };

case 0xB: // Delay command.
return { CheatOp::Delay, 0, 0, arg };

Expand Down Expand Up @@ -859,6 +869,9 @@ void CWCheatEngine::ExecuteOp(const CheatOperation &op, const CheatCode &cheat,
}
break;

case CheatOp::Vibration:
break;

case CheatOp::Delay:
// TODO: Not supported.
break;
Expand Down Expand Up @@ -1065,6 +1078,14 @@ bool CWCheatEngine::HasCheats() {
return !cheats_.empty();
}

u16 CWCheatEngine::GetLefttVibration() {
return leftVibration;
}

u16 CWCheatEngine::GetRightVibration() {
return rightVibration;
}

bool CheatsInEffect() {
if (!cheatEngine || !cheatsEnabled)
return false;
Expand Down
2 changes: 2 additions & 0 deletions Core/CwCheat.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class CWCheatEngine {
void CreateCheatFile();
void Run();
bool HasCheats();
u16 GetLefttVibration();
u16 GetRightVibration();

void InvalidateICache(u32 addr, int size);
private:
Expand Down
4 changes: 2 additions & 2 deletions Windows/PPSSPP.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@
<AdditionalOptions>$(EXTERNAL_COMPILE_OPTIONS)</AdditionalOptions>
</ClCompile>
<Link>
<AdditionalDependencies>mf.lib;mfplat.lib;mfreadwrite.lib;mfuuid.lib;shlwapi.lib;Winmm.lib;Ws2_32.lib;opengl32.lib;dsound.lib;glu32.lib;comctl32.lib;d3d9.lib;dxguid.lib;..\ffmpeg\Windows\x86_64\lib\avcodec.lib;..\ffmpeg\Windows\x86_64\lib\avformat.lib;..\ffmpeg\Windows\x86_64\lib\avutil.lib;..\ffmpeg\Windows\x86_64\lib\swresample.lib;..\ffmpeg\Windows\x86_64\lib\swscale.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>mf.lib;mfplat.lib;mfreadwrite.lib;mfuuid.lib;shlwapi.lib;Winmm.lib;Ws2_32.lib;opengl32.lib;dsound.lib;glu32.lib;comctl32.lib;d3d9.lib;dxguid.lib;..\ffmpeg\Windows\x86_64\lib\avcodec.lib;..\ffmpeg\Windows\x86_64\lib\avformat.lib;..\ffmpeg\Windows\x86_64\lib\avutil.lib;..\ffmpeg\Windows\x86_64\lib\swresample.lib;..\ffmpeg\Windows\x86_64\lib\swscale.lib;%(AdditionalDependencies);xinput.lib</AdditionalDependencies>
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Windows</SubSystem>
Expand Down Expand Up @@ -1027,4 +1027,4 @@
<UserProperties RESOURCE_FILE="DaSh.rc" />
</VisualStudio>
</ProjectExtensions>
</Project>
</Project>
23 changes: 23 additions & 0 deletions Windows/XinputDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#include "input/input_state.h"
#include "input/keycodes.h"
#include "XinputDevice.h"
#include "Core/CwCheat.h"
#include "UI/GameInfoCache.h"
#include "Core/Core.h"

// Utilities to dynamically load XInput. Adapted from SDL.

Expand Down Expand Up @@ -254,6 +257,7 @@ void XinputDevice::UpdatePad(int pad, const XINPUT_STATE &state) {
KeyMap::NotifyPadConnected("Xbox 360 Pad");
}
ApplyButtons(pad, state);
ApplyVibration(pad);

const float STICK_DEADZONE = g_Config.fXInputAnalogDeadzone;
const int STICK_INV_MODE = g_Config.iXInputAnalogInverseMode;
Expand Down Expand Up @@ -338,3 +342,22 @@ void XinputDevice::ApplyButtons(int pad, const XINPUT_STATE &state) {
}
}
}


void XinputDevice::ApplyVibration(int pad) {
if (PSP_IsInited) {
XINPUT_VIBRATION vibration;
ZeroMemory(&vibration, sizeof(XINPUT_VIBRATION));
vibration.wLeftMotorSpeed = 0;
vibration.wRightMotorSpeed = 0;

engine_ = new CWCheatEngine(gameID_);
if (engine_) {
vibration.wLeftMotorSpeed = engine_->GetLefttVibration(); // use any value between 0-65535 here
vibration.wRightMotorSpeed = engine_->GetRightVibration(); // use any value between 0-65535 here
}
WARN_LOG(COMMON, "PAD TEST LEFT: %d, RIGHT: %d, PAD: %d", vibration.wLeftMotorSpeed, vibration.wRightMotorSpeed, pad);
XInputSetState(pad, &vibration);
}
}

6 changes: 5 additions & 1 deletion Windows/XinputDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "InputDevice.h"
#include "Xinput.h"

#include "Core/CwCheat.h"

class XinputDevice final : public InputDevice {
public:
Expand All @@ -13,6 +13,10 @@ class XinputDevice final : public InputDevice {
private:
void UpdatePad(int pad, const XINPUT_STATE &state);
void ApplyButtons(int pad, const XINPUT_STATE &state);
void ApplyVibration(int pad);
CWCheatEngine* engine_ = nullptr;
std::string gamePath_;
std::string gameID_;
int check_delay[4]{};
XINPUT_STATE prevState[4]{};
u32 prevButtons[4]{};
Expand Down

0 comments on commit 78b9f3d

Please sign in to comment.