Merge branch 'master' of somewhatlurker/PD-Loader into master

This commit is contained in:
somewhatlurker
2020-08-12 13:32:55 +00:00
committed by Gogs
12 changed files with 198 additions and 45 deletions
@@ -223,9 +223,6 @@ ConfigOptionBase* optionsArray[] = {
new NumericOption(L"xinput_preferred", KEYCONFIG_SECTION, KEYCONFIG_FILE, L"XInput Controller Num:", L"Sets the preferred XInput controller.\nIf unavailable, the next connected controller is used.", 0, 0, 3),
new OptionMetaSpacer(8),
new BooleanOption(L"hardware_slider", PATCHES_SECTION, CONFIG_FILE, L"Use Hardware Slider", L"Enable this if using a real arcade slider.\n(set the slider to port COM11)", false, false),
new OptionMetaSpacer(8),
new BooleanOption(L"opengl_patch_a", LAUNCHER_SECTION, CONFIG_FILE, L"OpenGL Patch A", L"Ignores some OpenGL-related errors. Don't use both patches at the same time unless you're know what you're doing.", false, false),
new BooleanOption(L"opengl_patch_b", LAUNCHER_SECTION, CONFIG_FILE, L"OpenGL Patch B", L"Ignores some OpenGL-related errors. Don't use both patches at the same time unless you're know what you're doing.", false, false),
new OptionMetaSpacer(8),
@@ -787,12 +787,6 @@ void ApplyPatches() {
{
InjectCode((void*)0x0000000140565E6B, { 0x90, 0x90 });
}
// The original slider update needs to run for hardware sliders to work -- only patch it when using emulation
if (!nHardwareSlider)
{
// Don't update the touch slider state so we can write our own
InjectCode((void*)0x000000014061579B, { 0x90, 0x90, 0x90, 0x8B, 0x42, 0xE0, 0x90, 0x90, 0x90 });
}
// Quick start
{
if (nQuickStart == 1) // skip the card/guest screen
@@ -6,6 +6,7 @@
#include "../../Input/Keyboard/Keyboard.h"
#include "../../Input/Bindings/KeyboardBinding.h"
#include "../../Input/KeyConfig/Config.h"
#include "../../Input/DirectInput/Ds4/DualShock4.h"
#include "../../FileSystem/ConfigFile.h"
#include "../../Utilities/Math.h"
#include <algorithm>
@@ -55,16 +56,36 @@ namespace TLAC::Components
FileSystem::ConfigFile configFile(framework::GetModuleDirectory(), KEY_CONFIG_FILE_NAME);
configFile.OpenRead();
Config::BindConfigKeys(configFile.ConfigMap, "LEFT_SIDE_SLIDE_LEFT", *LeftSideSlideLeft, { "Q" });
Config::BindConfigKeys(configFile.ConfigMap, "LEFT_SIDE_SLIDE_RIGHT", *LeftSideSlideRight, { "E" });
usePs4OfficialSlider = configFile.GetBooleanValue("ps4_official_slider");
enableInMenus = configFile.GetBooleanValue("slider_in_menus");
Config::BindConfigKeys(configFile.ConfigMap, "RIGHT_SIDE_SLIDE_LEFT", *RightSideSlideLeft, { "U" });
Config::BindConfigKeys(configFile.ConfigMap, "RIGHT_SIDE_SLIDE_RIGHT", *RightSideSlideRight, { "O" });
if (usePs4OfficialSlider)
{
DualShock4* ds4 = DualShock4::GetInstance();
if (ds4 == nullptr)
DualShock4::rawMode = true; // set raw mode for future instances if no current instance
else
ds4->SetRawMode(true); // if is a current instance, set raw mode on it (also sets for future instances)
}
else
{
DualShock4* ds4 = DualShock4::GetInstance();
if (ds4 == nullptr)
DualShock4::rawMode = false; // set raw mode for future instances if no current instance
else
ds4->SetRawMode(false); // if is a current instance, set raw mode on it (also sets for future instances)
float touchSliderEmulationSpeed = configFile.GetFloatValue("touch_slider_emulation_speed");
if (touchSliderEmulationSpeed != 0.0f)
sliderSpeed = touchSliderEmulationSpeed;
Config::BindConfigKeys(configFile.ConfigMap, "LEFT_SIDE_SLIDE_LEFT", *LeftSideSlideLeft, { "Q" });
Config::BindConfigKeys(configFile.ConfigMap, "LEFT_SIDE_SLIDE_RIGHT", *LeftSideSlideRight, { "E" });
Config::BindConfigKeys(configFile.ConfigMap, "RIGHT_SIDE_SLIDE_LEFT", *RightSideSlideLeft, { "U" });
Config::BindConfigKeys(configFile.ConfigMap, "RIGHT_SIDE_SLIDE_RIGHT", *RightSideSlideRight, { "O" });
float touchSliderEmulationSpeed = configFile.GetFloatValue("touch_slider_emulation_speed");
if (touchSliderEmulationSpeed != 0.0f)
sliderSpeed = touchSliderEmulationSpeed;
}
}
void TouchSliderEmulator::Update()
@@ -74,20 +95,45 @@ namespace TLAC::Components
void TouchSliderEmulator::UpdateInput()
{
if (!componentsManager->GetUpdateGameInput() || componentsManager->IsDwGuiActive() || !(*(GameState*)CURRENT_GAME_STATE_ADDRESS == GS_GAME && *(SubGameState*)CURRENT_GAME_SUB_STATE_ADDRESS == SUB_GAME_MAIN))
if (!componentsManager->GetUpdateGameInput() || componentsManager->IsDwGuiActive() || (!enableInMenus && !(*(GameState*)CURRENT_GAME_STATE_ADDRESS == GS_GAME && *(SubGameState*)CURRENT_GAME_SUB_STATE_ADDRESS == SUB_GAME_MAIN)))
return;
sliderIncrement = GetElapsedTime() / sliderSpeed;
if (usePs4OfficialSlider)
{
DualShock4* ds4 = DualShock4::GetInstance();
if (ds4 == nullptr)
return;
constexpr float sensorStep = (1.0f / SLIDER_SENSORS);
Joystick ls = ds4->GetLeftStick();
Joystick rs = ds4->GetRightStick();
EmulateSliderInput(LeftSideSlideLeft, LeftSideSlideRight, ContactPoints[0], 0.0f, 0.5f);
EmulateSliderInput(RightSideSlideLeft, RightSideSlideRight, ContactPoints[1], 0.5f + sensorStep, 1.0f + sensorStep);
uint32_t state = 0;
sliderState->ResetSensors();
// DualShock4 normalises sticks to floats -- this undoes that
//printf("left stick: %9.6f, %9.6f, ", ls.XAxis, ls.YAxis);
state |= (uint8_t)((ls.XAxis + 1.0 + 0.001) * 127.5) ^ 0b10000000; // 0.001 to prevent rounding errors
//printf("%d, ", (int)(state ^ 0b10000000));
state |= ((uint8_t)((ls.YAxis + 1.0 + 0.001) * 127.5) ^ 0b10000000) << 8;
//printf("%d\n", (int)((state >> 8) ^ 0b10000000));
state |= ((uint8_t)((rs.XAxis + 1.0 + 0.001) * 127.5) ^ 0b10000000) << 16;
state |= ((uint8_t)((rs.YAxis + 1.0 + 0.001) * 127.5) ^ 0b10000000) << 24;
for (int i = 0; i < CONTACT_POINTS; i++)
ApplyContactPoint(ContactPoints[i], i);
ApplyBitfieldState(state);
}
else
{
sliderIncrement = GetElapsedTime() / sliderSpeed;
constexpr float sensorStep = (1.0f / SLIDER_SENSORS);
EmulateSliderInput(LeftSideSlideLeft, LeftSideSlideRight, ContactPoints[0], 0.0f, 0.5f);
EmulateSliderInput(RightSideSlideLeft, RightSideSlideRight, ContactPoints[1], 0.5f + sensorStep, 1.0f + sensorStep);
sliderState->ResetSensors();
for (int i = 0; i < CONTACT_POINTS; i++)
ApplyContactPoint(ContactPoints[i]);
}
}
void TouchSliderEmulator::OnFocusLost()
@@ -120,23 +166,25 @@ namespace TLAC::Components
contactPoint.InContact = leftDown || rightDown;
}
void TouchSliderEmulator::ApplyContactPoint(ContactPoint &contactPoint, int section)
void TouchSliderEmulator::ApplyContactPoint(ContactPoint& contactPoint)
{
sliderState->SectionTouched[section] = contactPoint.InContact;
int pressure = contactPoint.InContact ? FULL_PRESSURE : NO_PRESSURE;
float position = std::clamp(contactPoint.Position, 0.0f, 1.0f);
if (contactPoint.InContact)
{
float position = std::clamp(contactPoint.Position, 0.0f, 1.0f);
int sensor = (int)(position * (SLIDER_SENSORS - 1));
sliderState->SetSensor(sensor, pressure);
sliderState->SetSensor(sensor, FULL_PRESSURE);
}
}
constexpr float startRange = -1.0f;
constexpr float endRange = +1.0f;
sliderState->SectionPositions[section] = contactPoint.InContact ? (ConvertRange(0.0f, 1.0f, startRange, endRange, position)) : 0.0f;
void TouchSliderEmulator::ApplyBitfieldState(uint32_t state)
{
for (int i = 0; i < 32; i++)
{
if (state & (1 << (31 - i)))
sliderState->SetSensor(i, FULL_PRESSURE);
else
sliderState->SetSensor(i, NO_PRESSURE);
}
}
}
@@ -25,6 +25,10 @@ namespace TLAC::Components
Input::Binding* RightSideSlideLeft;
Input::Binding* RightSideSlideRight;
bool usePs4OfficialSlider;
bool enableInMenus;
TouchSliderEmulator();
~TouchSliderEmulator();
@@ -53,7 +57,8 @@ namespace TLAC::Components
ContactPoint ContactPoints[CONTACT_POINTS];
void EmulateSliderInput(Input::Binding *leftBinding, Input::Binding *rightBinding, ContactPoint &contactPoint, float start, float end);
void ApplyContactPoint(ContactPoint &contactPoint, int section);
void ApplyContactPoint(ContactPoint &contactPoint);
void ApplyBitfieldState(uint32_t state);
};
}
@@ -6,9 +6,19 @@ namespace TLAC::Components
{
if (index < 0 || index >= SLIDER_SENSORS)
return;
SensorPressureLevels[index] = value;
SensorTouched[index].IsTouched = value > 0;
if (SerialState == nullptr)
return;
uint32_t* ph = SerialState->sliderSerialResponse.sensors[index].pressureHistory;
ph[3] = ph[2];
ph[2] = ph[1];
ph[1] = ph[0];
ph[0] = value;
SerialState->sliderSerialResponse.scanMode = 1;
SerialState->sliderSerialResponse.scanCount = 1;
SerialState->sliderResponseCnt = 1;
}
void TouchSliderState::ResetSensors()
@@ -10,9 +10,37 @@
namespace TLAC::Components
{
struct TouchSliderSerialState
{
uint8_t padding[0x1960];
int sliderResponseCnt;
struct
{
int scanMode;
int scanCount;
struct
{
uint32_t pressureHistory[4]; // first: current, second: last sample, ...
} sensors[SLIDER_SENSORS];
} sliderSerialResponse;
struct
{
int scanMode;
int scanCount;
struct
{
uint32_t pressureHistory[4]; // first: current, second: last sample, ...
} sensors[SLIDER_SENSORS];
} sliderSerialResponseCopy; // the other one is copied here by the game, feel free to ignore it
};
struct TouchSliderState
{
uint8_t Padding0000[112];
uint8_t Padding0000[104];
TouchSliderSerialState* SerialState;
int32_t State;
@@ -53,6 +53,45 @@ namespace TLAC::Input
return result;
}
HRESULT DirectInputDevice::DI_SetRange(LONG min, LONG max)
{
DIPROPRANGE propData;
propData.lMin = min;
propData.lMax = max;
propData.diph.dwSize = sizeof(DIPROPRANGE);
propData.diph.dwHeaderSize = sizeof(DIPROPHEADER);
propData.diph.dwHow = DIPH_DEVICE;
propData.diph.dwObj = 0;
HRESULT result = directInputdevice->SetProperty(DIPROP_RANGE, &propData.diph);
return result;
}
HRESULT DirectInputDevice::DI_SetRawMode(BOOL raw)
{
// extra unacquire/acquire logic so this can be called on an active device
BOOL wasAcquired = DI_Unacquire() == DI_OK;
DIPROPDWORD propData;
propData.dwData = raw ? DIPROPCALIBRATIONMODE_RAW : DIPROPCALIBRATIONMODE_COOKED;
propData.diph.dwSize = sizeof(DIPROPDWORD);
propData.diph.dwHeaderSize = sizeof(DIPROPHEADER);
propData.diph.dwHow = DIPH_DEVICE;
propData.diph.dwObj = 0;
HRESULT result = NULL;
if (FAILED(result = directInputdevice->SetProperty(DIPROP_CALIBRATIONMODE, &propData.diph)))
return result;
if (wasAcquired)
result = DI_Acquire();
return result;
}
void DirectInputDevice::DI_Dispose()
{
if (directInputdevice == nullptr)
@@ -16,6 +16,8 @@ namespace TLAC::Input
HRESULT DI_Release();
HRESULT DI_Poll();
HRESULT DI_GetDeviceState(DWORD size, LPVOID data);
HRESULT DI_SetRange(LONG min, LONG max);
HRESULT DI_SetRawMode(BOOL raw);
void DI_Dispose();
};
@@ -6,6 +6,7 @@
namespace TLAC::Input
{
DualShock4* DualShock4::instance;
bool DualShock4::rawMode = false;
DualShock4::DualShock4()
{
@@ -53,11 +54,29 @@ namespace TLAC::Input
if (FAILED(result = DI_SetDataFormat(&c_dfDIJoystick2)))
return false;
if (FAILED(result = DI_SetRange(0, 255))) // use 8-bit data
return false;
// set raw mode from previous instance
if (rawMode && FAILED(result = DI_SetRawMode(true)))
return false;
result = DI_Acquire();
return true;
}
bool DualShock4::SetRawMode(bool raw)
{
HRESULT result = NULL;
if (FAILED(result = DI_SetRawMode(raw)))
return false;
rawMode = raw;
return true;
}
bool DualShock4::PollInput()
{
lastState = currentState;
@@ -31,6 +31,7 @@ namespace TLAC::Input
static bool TryInitializeInstance();
bool Initialize();
bool SetRawMode(bool raw);
bool PollInput() override;
bool IsDown(Ds4Button button);
@@ -48,6 +49,8 @@ namespace TLAC::Input
static inline DualShock4* GetInstance() { return instance; };
static inline void DeleteInstance() { delete instance; instance = nullptr; };
static bool rawMode; // kinda dodgy, used to set raw mode for all instances without waiting for a single one
private:
static DualShock4* instance;
@@ -58,8 +61,8 @@ namespace TLAC::Input
const float joystickThreshold = 0.5f;
const float dpadThreshold = 0.5f;
inline float NormalizeTrigger(long value) { return (float)value / USHRT_MAX; };
inline float NormalizeStick(long value) { return (float)value / USHRT_MAX * 2.0f - 1.0f; };
inline float NormalizeTrigger(long value) { return (float)value / UCHAR_MAX; };
inline float NormalizeStick(long value) { return (float)value / UCHAR_MAX * 2.0f - 1.0f; };
inline Joystick NormalizeStick(long x, long y) { return Joystick(NormalizeStick(x), NormalizeStick(y)); };
void UpdateInternalDs4State(Ds4State &state);