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

This commit is contained in:
nastys
2020-06-05 20:19:43 +00:00
committed by Gogs
17 changed files with 590 additions and 1 deletions
+9
View File
@@ -0,0 +1,9 @@
This is the source code! It can NOT be used to play PDAFT!
If you just wanted to play the game, you downloaded the wrong thing.
Do not click the download button next to the git URL, that's for the source code.
For information on getting and installing the latest release,
please read the wiki at https://notabug.org/nastys/PD-Loader/wiki
If you've already read it, pay more attention and you'll find the right link.
You should also read the wiki if you have any issues, before asking questions.
@@ -0,0 +1,10 @@
#Check you Vendor ID and Product ID in Device Manager or dxdiag tool
[genericusbinput]
VID = 046D
PID = C216
[customkeymapping]
GU_BUTTON8 = GU_BUTTON1 + GU_BUTTON2 + GU_BUTTON3 + GU_BUTTON4
+14
View File
@@ -114,3 +114,17 @@ RIGHT_SIDE_SLIDE_RIGHT = O, Ds4_R_Stick_Right, XINPUT_RRIGHT
# - Right Joystick
# "XINPUT_RLEFT", "XINPUT_RRIGHT", "XINPUT_RUP", "XINPUT_RDOWN", "XINPUT_RSB"
# --- Generic USB BUTTON NAMES: ---
# - Standard Buttons (Button number in "Game controller settings", max allowed 13 buttons at the moment)
# "GU_BUTTON1", "GU_BUTTON2", "GU_BUTTON3", "GU_BUTTON4", "GU_BUTTON5", "GU_BUTTON6", "GU_BUTTON7", "GU_BUTTON8", "GU_BUTTON9", "GU_BUTTON10", "GU_BUTTON11", "GU_BUTTON12", "GU_BUTTON13"
# - D-Pad Directions
# "GU_DPAD_UP", "GU_DPAD_RIGHT", "GU_DPAD_DOWN", "GU_DPAD_LEFT"
# - Left Joystick
# "GU_L_STICK_UP", "GU_L_STICK_RIGHT", "GU_L_STICK_DOWN", "GU_L_STICK_LEFT"
# - Right Joystick
# "GU_R_STICK_UP", "GU_R_STICK_RIGHT", "GU_R_STICK_DOWN", "GU_R_STICK_LEFT"
@@ -47,6 +47,14 @@ namespace TLAC::FileSystem
return found ? (float)atof(pair->second.c_str()) : 0.0f;
}
std::string ConfigFile::GetStringValue(const std::string& key)
{
auto pair = ConfigMap.find(key);
bool found = pair != ConfigMap.end();
return found ? pair->second : "";
}
void ConfigFile::Parse(std::ifstream &fileStream)
{
std::string line;
@@ -17,6 +17,7 @@ namespace TLAC::FileSystem
int GetIntegerValue(const std::string& key);
bool GetBooleanValue(const std::string& key);
float GetFloatValue(const std::string& key);
std::string GetStringValue(const std::string& key);
protected:
virtual void Parse(std::ifstream &fileStream) override;
@@ -0,0 +1,29 @@
#include "GuBinding.h"
namespace TLAC::Input
{
#define GuInstanceCheckDefault(checkFunc) (GenericUsbInput::InstanceInitialized() ? GenericUsbInput::GetInstance()->checkFunc : false)
GuBinding::GuBinding(GuButton button) : Button(button)
{
}
GuBinding::~GuBinding()
{
}
bool GuBinding::IsDown()
{
return GuInstanceCheckDefault(IsDown(Button));
}
bool GuBinding::IsTapped()
{
return GuInstanceCheckDefault(IsTapped(Button));
}
bool GuBinding::IsReleased()
{
return GuInstanceCheckDefault(IsReleased(Button));
}
}
@@ -0,0 +1,19 @@
#pragma once
#include "IInputBinding.h"
#include "../DirectInput/GenericUsb/GenericUsbInput.h"
namespace TLAC::Input
{
class GuBinding : public IInputBinding
{
public:
GuButton Button;
GuBinding(GuButton button);
~GuBinding();
bool IsDown() override;
bool IsTapped() override;
bool IsReleased() override;
};
}
@@ -0,0 +1,205 @@
#include "GenericUsbInput.h"
#include "../../../Utilities/Math.h"
#include "../../../Utilities/Operations.h"
#include "../../../framework.h"
#include "../../../FileSystem/ConfigFile.h"
#include <stdio.h>
const std::string GU_CONFIG_FILE_NAME = "genericusbinput.ini";
namespace TLAC::Input
{
GenericUsbInput* GenericUsbInput::instance;
GenericUsbInput::GenericUsbInput()
{
}
GenericUsbInput::~GenericUsbInput()
{
DI_Dispose();
}
bool GenericUsbInput::TryInitializeInstance()
{
if (InstanceInitialized())
return true;
if (!DirectInputInitialized())
return false;
GenericUsbInput *genericUsbInput = new GenericUsbInput ();
bool success = genericUsbInput->Initialize();
instance = success ? genericUsbInput : nullptr;
if (!success)
delete genericUsbInput;
return success;
}
bool GenericUsbInput::Initialize()
{
HRESULT result = NULL;
FileSystem::ConfigFile configFile(framework::GetModuleDirectory(), GU_CONFIG_FILE_NAME);
configFile.OpenRead();
customKeyMapping = configFile.ConfigMap;
std::string stt = "0x";
std::string vid = configFile.GetStringValue("VID");
std::string pid = configFile.GetStringValue("PID");
unsigned int gid = std::strtoul((stt + pid + vid).c_str(), 0, 16);
GUID GUID_GenericUsb = { gid, 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };
result = DI_CreateDevice(GUID_GenericUsb);
if (!FAILED(result))
{
if (FAILED(result = DI_SetDataFormat(&c_dfDIJoystick2)))
return false;
result = DI_Acquire();
return true;
}
else
{
return false;
}
}
bool GenericUsbInput::PollInput()
{
lastState = currentState;
HRESULT result = NULL;
result = DI_Poll();
result = DI_GetDeviceState(sizeof(DIJOYSTATE2), &currentState.DI_JoyState);
if (result != DI_OK)
return false;
UpdateInternalGuState(currentState);
for (int button = 0; button < GU_BUTTON_MAX; button++)
{
bool buttonState = GetButtonState(currentState, (GuButton)button);
currentState.Buttons[button] = buttonState;
// If custom button is pressed
if (buttonState)
{
// Check whether this button is a custom mapped button
auto customMapping = customKeyMapping.find(buttonNames[button]);
if (customMapping != customKeyMapping.end())
{
std::vector<std::string> keys = Utilities::Split(customMapping->second, "+");
for (std::string key : keys)
{
Utilities::Trim(key);
auto guButton = KeyConfig::Config::GuMap.find(key.c_str());
currentState.Buttons[guButton->second] = buttonState;
}
}
}
}
return true;
}
void GenericUsbInput::UpdateInternalGuState(GuState &state)
{
if (state.Dpad.IsDown = state.DI_JoyState.rgdwPOV[0] != -1)
{
state.Dpad.Angle = (state.DI_JoyState.rgdwPOV[0] / 100.0f);
auto direction = Utilities::GetDirection(state.Dpad.Angle);
state.Dpad.Stick = { direction.Y, -direction.X };
}
else
{
state.Dpad.Angle = 0;
state.Dpad.Stick = GuJoystick();
}
state.LeftStick = NormalizeStick(state.DI_JoyState.lX, state.DI_JoyState.lY);
state.RightStick = NormalizeStick(state.DI_JoyState.lZ, state.DI_JoyState.lRz);
state.LeftTrigger = { NormalizeTrigger(state.DI_JoyState.lRx) };
state.RightTrigger = { NormalizeTrigger(state.DI_JoyState.lRy) };
}
bool GenericUsbInput::IsDown(GuButton button)
{
return currentState.Buttons[button];
}
bool GenericUsbInput::IsUp(GuButton button)
{
return !IsDown(button);
}
bool GenericUsbInput::IsTapped(GuButton button)
{
return IsDown(button) && WasUp(button);
}
bool GenericUsbInput::IsReleased(GuButton button)
{
return IsUp(button) && WasDown(button);
}
bool GenericUsbInput::WasDown(GuButton button)
{
return lastState.Buttons[button];
}
bool GenericUsbInput::WasUp(GuButton button)
{
return !WasDown(button);
}
bool GenericUsbInput::MatchesDirection(GuJoystick joystick, GuDirection directionEnum, float threshold)
{
switch (directionEnum)
{
case TLAC::Input::GU_DIR_UP:
return joystick.YAxis <= -threshold;
case TLAC::Input::GU_DIR_RIGHT:
return joystick.XAxis >= +threshold;
case TLAC::Input::GU_DIR_DOWN:
return joystick.YAxis >= +threshold;
case TLAC::Input::GU_DIR_LEFT:
return joystick.XAxis <= -threshold;
default:
return false;
}
}
bool GenericUsbInput::GetButtonState(GuState &state, GuButton button)
{
if (button >= GU_BUTTON1 && button <= GU_BUTTON13)
return state.DI_JoyState.rgbButtons[button];
if (button >= GU_DPAD_UP && button <= GU_DPAD_LEFT)
return state.Dpad.IsDown ? MatchesDirection(state.Dpad.Stick, (GuDirection)(button - GU_DPAD_UP), dpadThreshold) : false;
if (button >= GU_L_STICK_UP && button <= GU_L_STICK_LEFT)
return MatchesDirection(state.LeftStick, (GuDirection)(button - GU_L_STICK_UP), joystickThreshold);
if (button >= GU_R_STICK_UP && button <= GU_R_STICK_LEFT)
return MatchesDirection(state.RightStick, (GuDirection)(button - GU_R_STICK_UP), joystickThreshold);
return false;
}
}
@@ -0,0 +1,85 @@
#pragma once
#include "../Controller.h"
#include "../../IInputDevice.h"
#include "../../../Input/KeyConfig/Config.h"
#include "GuState.h"
#include <unordered_map>
namespace TLAC::Input
{
class GenericUsbInput : public Controller, public IInputDevice
{
public:
GenericUsbInput();
~GenericUsbInput();
static bool TryInitializeInstance();
bool Initialize();
bool PollInput() override;
bool IsDown(GuButton button);
bool IsUp(GuButton button);
bool IsTapped(GuButton button);
bool IsReleased(GuButton button);
bool WasDown(GuButton button);
bool WasUp(GuButton button);
inline GuJoystick GetLeftStick() { return currentState.LeftStick; };
inline GuJoystick GetRightStick() { return currentState.RightStick; };
inline GuJoystick GetDpad() { return currentState.Dpad.Stick; };
static inline bool InstanceInitialized() { return instance != nullptr; };
static inline GenericUsbInput* GetInstance() { return instance; };
static inline void DeleteInstance() { delete instance; instance = nullptr; };
private:
static GenericUsbInput* instance;
GuState lastState;
GuState currentState;
std::unordered_map<std::string, std::string> customKeyMapping;
std::vector<std::string> buttonNames = { "GU_BUTTON1",
"GU_BUTTON2",
"GU_BUTTON3",
"GU_BUTTON4",
"GU_BUTTON5",
"GU_BUTTON6",
"GU_BUTTON7",
"GU_BUTTON8",
"GU_BUTTON9",
"GU_BUTTON10",
"GU_BUTTON11",
"GU_BUTTON12",
"GU_BUTTON13",
"GU_DPAD_UP",
"GU_DPAD_RIGHT",
"GU_DPAD_DOWN",
"GU_DPAD_LEFT",
"GU_L_STICK_UP",
"GU_L_STICK_RIGHT",
"GU_L_STICK_DOWN",
"GU_L_STICK_LEFT",
"GU_R_STICK_UP",
"GU_R_STICK_RIGHT",
"GU_R_STICK_DOWN",
"GU_R_STICK_LEFT",
"GU_BUTTON_MAX"
};
const float triggerThreshold = 0.5f;
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 GuJoystick NormalizeStick(long x, long y) { return GuJoystick(NormalizeStick(x), NormalizeStick(y)); };
void UpdateInternalGuState(GuState &state);
bool MatchesDirection(GuJoystick joystick, GuDirection directionEnum, float threshold);
bool GetButtonState(GuState &state, GuButton button);
};
}
@@ -0,0 +1,52 @@
#pragma once
namespace TLAC::Input
{
enum GuDirection
{
GU_DIR_UP,
GU_DIR_RIGHT,
GU_DIR_DOWN,
GU_DIR_LEFT
};
enum GuButton : int
{
GU_BUTTON1 = 0,
GU_BUTTON2 = 1,
GU_BUTTON3 = 2,
GU_BUTTON4 = 3,
GU_BUTTON5 = 4,
GU_BUTTON6 = 5,
GU_BUTTON7 = 6,
GU_BUTTON8 = 7,
GU_BUTTON9 = 8,
GU_BUTTON10 = 9,
GU_BUTTON11 = 10,
GU_BUTTON12 = 11,
GU_BUTTON13 = 12,
GU_DPAD_UP = 14,
GU_DPAD_RIGHT = 15,
GU_DPAD_DOWN = 16,
GU_DPAD_LEFT = 17,
GU_L_STICK_UP = 18,
GU_L_STICK_RIGHT = 19,
GU_L_STICK_DOWN = 20,
GU_L_STICK_LEFT = 21,
GU_R_STICK_UP = 22,
GU_R_STICK_RIGHT = 23,
GU_R_STICK_DOWN = 24,
GU_R_STICK_LEFT = 25,
GU_BUTTON_MAX = 26,
};
}
@@ -0,0 +1,14 @@
#include "GuState.h"
namespace TLAC::Input
{
GuJoystick::GuJoystick() : XAxis(0.0f), YAxis(0.0f)
{
return;
};
GuJoystick::GuJoystick(float xAxis, float yAxis) : XAxis(xAxis), YAxis(yAxis)
{
return;
};
}
@@ -0,0 +1,39 @@
#pragma once
#include "../DirectInput.h"
#include "GuButton.h"
namespace TLAC::Input
{
struct GuJoystick
{
FLOAT XAxis, YAxis;
GuJoystick();
GuJoystick(float xAxis, float yAxis);
};
struct GuDpad
{
BOOL IsDown;
FLOAT Angle;
GuJoystick Stick;
};
struct GuTrigger
{
FLOAT Axis;
};
struct GuState
{
DIJOYSTATE2 DI_JoyState;
BYTE Buttons[GU_BUTTON_MAX];
GuDpad Dpad;
GuJoystick LeftStick;
GuJoystick RightStick;
GuTrigger LeftTrigger;
GuTrigger RightTrigger;
};
}
@@ -2,6 +2,7 @@
#include "windows.h"
#include "../Bindings/KeyboardBinding.h"
#include "../Bindings/Ds4Binding.h"
#include "../Bindings/GuBinding.h"
#include "../../Utilities/Operations.h"
#include "../Bindings/XinputBinding.h"
#include "../../Constants.h"
@@ -218,6 +219,43 @@ namespace TLAC::Input::KeyConfig
{ "Ds4_R_Stick_Left", DS4_R_STICK_LEFT },
};
GuButtonMap Config::GuMap =
{
// Face Buttons
{ "GU_BUTTON1", GU_BUTTON1 },
{ "GU_BUTTON2", GU_BUTTON2 },
{ "GU_BUTTON3", GU_BUTTON3 },
{ "GU_BUTTON4", GU_BUTTON4 },
{ "GU_BUTTON5", GU_BUTTON5 },
{ "GU_BUTTON6", GU_BUTTON6 },
{ "GU_BUTTON7", GU_BUTTON7 },
{ "GU_BUTTON8", GU_BUTTON8 },
{ "GU_BUTTON9", GU_BUTTON9 },
{ "GU_BUTTON10", GU_BUTTON10 },
{ "GU_BUTTON11", GU_BUTTON11 },
{ "GU_BUTTON12", GU_BUTTON12 },
{ "GU_BUTTON13", GU_BUTTON13 },
// D-Pad Directions
{ "GU_DPAD_UP", GU_DPAD_UP },
{ "GU_DPAD_RIGHT", GU_DPAD_RIGHT },
{ "GU_DPAD_DOWN", GU_DPAD_DOWN },
{ "GU_DPAD_LEFT", GU_DPAD_LEFT },
// Left Joystick
{ "GU_L_STICK_UP", GU_L_STICK_UP },
{ "GU_L_STICK_RIGHT", GU_L_STICK_RIGHT },
{ "GU_L_STICK_DOWN", GU_L_STICK_DOWN },
{ "GU_L_STICK_LEFT", GU_L_STICK_LEFT },
// Right Joystick
{ "GU_R_STICK_UP", GU_R_STICK_UP },
{ "GU_R_STICK_RIGHT", GU_R_STICK_RIGHT },
{ "GU_R_STICK_DOWN", GU_R_STICK_DOWN },
{ "GU_R_STICK_LEFT", GU_R_STICK_LEFT },
};
void Config::BindConfigKeys(std::unordered_map<std::string, std::string> &configMap, const char *configKeyName, Binding &bindObj, std::vector<std::string> defaultKeys)
{
std::vector<std::string> keys;
@@ -271,7 +309,16 @@ namespace TLAC::Input::KeyConfig
}
else
{
printf("[TLAC] Config::BindConfigKeys(): Unable to parse key: '%s'\n", key.c_str());
auto guButton = Config::GuMap.find(key.c_str());
if (guButton != Config::GuMap.end())
{
bindObj.AddBinding(new GuBinding(guButton->second));
}
else
{
printf("[TLAC] Config::BindConfigKeys(): Unable to parse key: '%s'\n", key.c_str());
}
}
}
}
@@ -5,11 +5,13 @@
#include "KeyStringHash.h"
#include "../Bindings/Binding.h"
#include "../DirectInput/Ds4/Ds4Button.h"
#include "../DirectInput/GenericUsb/GuButton.h"
namespace TLAC::Input::KeyConfig
{
typedef std::unordered_map<KeyString, uint8_t, KeyStringHash> KeycodeMap;
typedef std::unordered_map<KeyString, Ds4Button, KeyStringHash> Ds4ButtonMap;
typedef std::unordered_map<KeyString, GuButton, KeyStringHash> GuButtonMap;
class Config
{
@@ -17,6 +19,7 @@ namespace TLAC::Input::KeyConfig
static KeycodeMap Keymap;
static Ds4ButtonMap Ds4Map;
static KeycodeMap XinputMap;
static GuButtonMap GuMap;
static void BindConfigKeys(std::unordered_map<std::string, std::string> &configMap, const char *configKeyName, Binding &bindObj, std::vector<std::string> defaultKeys);
};
@@ -140,6 +140,7 @@
<ClInclude Include="framework.h" />
<ClInclude Include="Input\Bindings\Binding.h" />
<ClInclude Include="Input\Bindings\Ds4Binding.h" />
<ClInclude Include="Input\Bindings\GuBinding.h" />
<ClInclude Include="Input\Bindings\IInputBinding.h" />
<ClInclude Include="Input\Bindings\KeyboardBinding.h" />
<ClInclude Include="Input\Bindings\MouseBinding.h" />
@@ -151,6 +152,9 @@
<ClInclude Include="Input\DirectInput\Ds4\Ds4Button.h" />
<ClInclude Include="Input\DirectInput\Ds4\Ds4State.h" />
<ClInclude Include="Input\DirectInput\Ds4\DualShock4.h" />
<ClInclude Include="Input\DirectInput\GenericUsb\GenericUsbInput.h" />
<ClInclude Include="Input\DirectInput\GenericUsb\GuButton.h" />
<ClInclude Include="Input\DirectInput\GenericUsb\GuState.h" />
<ClInclude Include="Input\IInputDevice.h" />
<ClInclude Include="Input\Keyboard\Keyboard.h" />
<ClInclude Include="Input\Keyboard\KeyboardState.h" />
@@ -194,6 +198,7 @@
<ClCompile Include="framework.cpp" />
<ClCompile Include="Input\Bindings\Binding.cpp" />
<ClCompile Include="Input\Bindings\Ds4Binding.cpp" />
<ClCompile Include="Input\Bindings\GuBinding.cpp" />
<ClCompile Include="Input\Bindings\KeyboardBinding.cpp" />
<ClCompile Include="Input\Bindings\MouseBinding.cpp" />
<ClCompile Include="Input\Bindings\XinputBinding.cpp" />
@@ -203,6 +208,8 @@
<ClCompile Include="Input\DirectInput\DirectInputMouse.cpp" />
<ClCompile Include="Input\DirectInput\Ds4\Ds4State.cpp" />
<ClCompile Include="Input\DirectInput\Ds4\DualShock4.cpp" />
<ClCompile Include="Input\DirectInput\GenericUsb\GenericUsbInput.cpp" />
<ClCompile Include="Input\DirectInput\GenericUsb\GuState.cpp" />
<ClCompile Include="Input\Keyboard\Keyboard.cpp" />
<ClCompile Include="Input\Keyboard\KeyboardState.cpp" />
<ClCompile Include="Input\KeyConfig\Config.cpp" />
@@ -201,6 +201,18 @@
<ClInclude Include="Utilities\Drawing.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Input\DirectInput\GenericUsb\GuButton.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Input\DirectInput\GenericUsb\GuState.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Input\DirectInput\GenericUsb\GenericUsbInput.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Input\Bindings\GuBinding.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp">
@@ -341,5 +353,14 @@
<ClCompile Include="Utilities\Drawing.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Input\DirectInput\GenericUsb\GuState.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Input\DirectInput\GenericUsb\GenericUsbInput.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Input\Bindings\GuBinding.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
@@ -11,6 +11,7 @@
#include "Input/Keyboard/Keyboard.h"
#include "Input/DirectInput/DirectInput.h"
#include "Input/DirectInput/Ds4/DualShock4.h"
#include "Input/DirectInput/GenericUsb/GenericUsbInput.h"
#include "Components/ComponentsManager.h"
#include <tchar.h>
#include <GL/freeglut.h>
@@ -74,6 +75,12 @@ namespace TLAC
if (Input::DualShock4::TryInitializeInstance())
printf("[TLAC] UpdateTick(): DualShock4 connected and initialized\n");
}
if (!Input::GenericUsbInput::InstanceInitialized())
{
if (Input::GenericUsbInput::TryInitializeInstance())
printf("[TLAC] UpdateTick(): GenericUsbInput connected and initialized\n");
}
}
ComponentsManager.Update();
@@ -96,6 +103,15 @@ namespace TLAC
}
}
if (Input::GenericUsbInput::GetInstance() != nullptr)
{
if (!Input::GenericUsbInput::GetInstance()->PollInput())
{
Input::GenericUsbInput::DeleteInstance();
printf("[TLAC] UpdateTick(): GenericUsbInput connection lost\n");
}
}
ComponentsManager.UpdateInput();
}
@@ -115,6 +131,15 @@ namespace TLAC
printf("[TLAC] UpdateTick(): DualShock4 connection lost\n");
}
}
if (Input::GenericUsbInput::GetInstance() != nullptr)
{
if (!Input::GenericUsbInput::GetInstance()->PollInput())
{
Input::GenericUsbInput::DeleteInstance();
printf("[TLAC] UpdateTick(): GenericUsbInput connection lost\n");
}
}
}
if (HasWindowFocus && !HadWindowFocus)
@@ -232,6 +257,7 @@ namespace TLAC
delete Input::Keyboard::GetInstance();
delete Input::Mouse::GetInstance();
delete Input::DualShock4::GetInstance();
delete Input::GenericUsbInput::GetInstance();
Input::DisposeDirectInput();