Skip to content

Commit

Permalink
Fix for crashes on systems with excessive numbers of Direct Input dev…
Browse files Browse the repository at this point in the history
…ices
  • Loading branch information
andon authored and andon committed Feb 3, 2017
1 parent 6b4e5fe commit e84fb7e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 38 deletions.
2 changes: 1 addition & 1 deletion include/DLL_VERSION.H
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#define TBF_MAJOR 0
#define TBF_MINOR 7
#define TBF_BUILD 0
#define TBF_REV 0
#define TBF_REV 1



Expand Down
15 changes: 15 additions & 0 deletions include/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,28 @@
#ifndef __TBF__INPUT_H__
#define __TBF__INPUT_H__

struct SDL_Joystick;

struct SDL_JoystickGUID {
uint8_t data [16];
};

namespace tbf
{
namespace InputFix
{
void Init ();
void Shutdown ();

struct ai_fix_s {
int num_virtual = 0;
int first_virtual = 1;

SDL_Joystick* pVirtual = (SDL_Joystick *)(LPVOID)0xDEADBEEFULL;
SDL_JoystickGUID virtual_guid { 0xff };
} extern ai_fix;
};
};


#endif /* __TBF__INPUT_H__ */
2 changes: 2 additions & 0 deletions src/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "keyboard.h"
#include "steam.h"
#include "render.h"
#include "input.h"

#include "command.h"
#include "hook.h"
Expand Down Expand Up @@ -176,6 +177,7 @@ SKPlugIn_Init (HMODULE hModSpecialK)
//tbf::SteamFix::Init ();
tbf::RenderFix::Init ();
//tbf::KeyboardFix::Init ();
tbf::InputFix::Init ();

extern void
TBF_InitSDLOverride (void);
Expand Down
71 changes: 34 additions & 37 deletions src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
**/

#define _CRT_SECURE_NO_WARNINGS
#define NOMINMAX

#include <string>
#include <algorithm>

#include "hook.h"
#include "input.h"
Expand Down Expand Up @@ -136,7 +138,7 @@ SK_TBF_PluginKeyPress ( BOOL Control,
debug_tex_id = 0;
} else {
if (tex_dbg_idx >= textures_used_last_dump.size ())
tex_dbg_idx = max (0, (uint32_t)textures_used_last_dump.size () - 1);
tex_dbg_idx = std::max (0UL, (uint32_t)textures_used_last_dump.size () - 1UL);

debug_tex_id = textures_used_last_dump [tex_dbg_idx];
}
Expand All @@ -158,7 +160,7 @@ SK_TBF_PluginKeyPress ( BOOL Control,
debug_tex_id = 0;
} else {
if (tex_dbg_idx >= textures_used_last_dump.size ())
tex_dbg_idx = max (0, (uint32_t)textures_used_last_dump.size () - 1);
tex_dbg_idx = std::max (0UL, (uint32_t)textures_used_last_dump.size () - 1UL);

debug_tex_id = textures_used_last_dump [tex_dbg_idx];
}
Expand All @@ -180,8 +182,9 @@ tbf::InputFix::Init (void)
SK_TBF_PluginKeyPress,
(LPVOID *)&SK_PluginKeyPress_Original );


TBF_ApplyQueuedHooks ();

ai_fix.num_virtual = config.input.gamepad.virtual_controllers;
}

void
Expand Down Expand Up @@ -381,27 +384,14 @@ SDL_GameControllerGetAxis_Detour ( LPVOID controller,
return SDL_GameControllerGetAxis_Original (controller, axis);
}


struct SDL_JoystickGUID {
uint8_t data [16];
};

struct {
int& num_virtual = config.input.gamepad.virtual_controllers;
int first_virtual = 0;

struct SDL_Joystick* pVirtual = (struct SDL_Joystick *)(LPVOID)0xDEADBEEFULL;
SDL_JoystickGUID virtual_guid { 0xff };
} ai_fix;

typedef BOOL (__cdecl *SDL_IsGameController_pfn)(int joystick_index);
SDL_IsGameController_pfn SDL_IsGameController_Original = nullptr;

BOOL
__cdecl
SDL_IsGameController_Detour (int joystick_index)
{
if (joystick_index >= ai_fix.first_virtual && joystick_index < ai_fix.first_virtual + ai_fix.num_virtual)
if ((tbf::InputFix::ai_fix.num_virtual > 0) && joystick_index >= tbf::InputFix::ai_fix.first_virtual && joystick_index < (tbf::InputFix::ai_fix.first_virtual + tbf::InputFix::ai_fix.num_virtual))
return FALSE;

return SDL_IsGameController_Original (joystick_index);
Expand All @@ -415,9 +405,10 @@ int
__cdecl
SDL_NumJoysticks_Detour (void)
{
ai_fix.first_virtual = SDL_NumJoysticks_Original ();
tbf::InputFix::ai_fix.first_virtual = SDL_NumJoysticks_Original ();
tbf::InputFix::ai_fix.num_virtual = std::min (4 - tbf::InputFix::ai_fix.first_virtual, config.input.gamepad.virtual_controllers);

return SDL_NumJoysticks_Original () + ai_fix.num_virtual;
return std::min (4, SDL_NumJoysticks_Original () + tbf::InputFix::ai_fix.num_virtual);
}


Expand All @@ -428,7 +419,7 @@ const char*
__cdecl
SDL_JoystickName_Detour (struct SDL_Joystick* joystick)
{
if (joystick == (struct SDL_Joystick *)ai_fix.pVirtual)
if (joystick == (struct SDL_Joystick *)tbf::InputFix::ai_fix.pVirtual)
return "TBFix Dummy Controller";

return SDL_JoystickName_Original (joystick);
Expand All @@ -441,7 +432,7 @@ const char*
__cdecl
SDL_JoystickNameForIndex_Detour (int device_index)
{
if (device_index >= ai_fix.first_virtual && device_index < ai_fix.first_virtual + ai_fix.num_virtual)
if ((tbf::InputFix::ai_fix.num_virtual > 0) && device_index >= tbf::InputFix::ai_fix.first_virtual && device_index < tbf::InputFix::ai_fix.first_virtual + tbf::InputFix::ai_fix.num_virtual)
return "TBFix Dummy Controller";

return SDL_JoystickNameForIndex_Original (device_index);
Expand All @@ -454,7 +445,7 @@ BOOL
__cdecl
SDL_JoystickGetAttached_Detour (struct SDL_Joystick* joystick)
{
if (joystick == (struct SDL_Joystick *)ai_fix.pVirtual)
if (joystick == (struct SDL_Joystick *)tbf::InputFix::ai_fix.pVirtual)
return TRUE;

return SDL_JoystickGetAttached_Original (joystick);
Expand All @@ -467,8 +458,8 @@ struct SDL_Joystick*
__cdecl
SDL_JoystickOpen_Detour (int device_index)
{
if (device_index >= ai_fix.first_virtual && device_index < ai_fix.first_virtual + ai_fix.num_virtual)
return (struct SDL_Joystick *)ai_fix.pVirtual;
if ((tbf::InputFix::ai_fix.num_virtual > 0) && device_index >= tbf::InputFix::ai_fix.first_virtual && device_index < tbf::InputFix::ai_fix.first_virtual + tbf::InputFix::ai_fix.num_virtual)
return (struct SDL_Joystick *)tbf::InputFix::ai_fix.pVirtual;

return SDL_JoystickOpen_Original (device_index);
}
Expand All @@ -481,7 +472,7 @@ void
__cdecl
SDL_JoystickClose_Detour (struct SDL_Joystick* joystick)
{
if (joystick == (struct SDL_Joystick *)ai_fix.pVirtual)
if (joystick == (struct SDL_Joystick *)tbf::InputFix::ai_fix.pVirtual)
return;

SDL_JoystickClose_Original (joystick);
Expand Down Expand Up @@ -510,7 +501,7 @@ int16_t
__cdecl
SDL_JoystickGetAxis_Detour (struct SDL_Joystick* joystick, int axis)
{
if (joystick == ai_fix.pVirtual)
if (joystick == tbf::InputFix::ai_fix.pVirtual)
return 0;

return SDL_JoystickGetAxis_Original (joystick, axis);
Expand All @@ -520,7 +511,7 @@ uint8_t
__cdecl
SDL_JoystickGetButton_Detour (struct SDL_Joystick* joystick, int button)
{
if (joystick == ai_fix.pVirtual)
if (joystick == tbf::InputFix::ai_fix.pVirtual)
return 0;

return SDL_JoystickGetButton_Original (joystick, button);
Expand All @@ -530,8 +521,8 @@ SDL_JoystickGUID
__cdecl
SDL_JoystickGetDeviceGUID_Detour (struct SDL_Joystick* joystick)
{
if (joystick == ai_fix.pVirtual)
return ai_fix.virtual_guid;
if (joystick == tbf::InputFix::ai_fix.pVirtual)
return tbf::InputFix::ai_fix.virtual_guid;

return SDL_JoystickGetDeviceGUID_Original (joystick);
}
Expand All @@ -540,8 +531,9 @@ void
__cdecl
SDL_JoystickGetGUIDString_Detour (SDL_JoystickGUID guid, char* pszGUID, int cbGUID)
{
if (! memcmp (&guid, &ai_fix.virtual_guid, 16)) {
snprintf (pszGUID, cbGUID, "TBFix Virtual");
if (! memcmp (&guid, &tbf::InputFix::ai_fix.virtual_guid, 16)) {
if (cbGUID > 0)
snprintf (pszGUID, cbGUID, "TBFix Virtual");
return;
}

Expand All @@ -552,7 +544,7 @@ uint8_t
__cdecl
SDL_JoystickGetHat_Detour (struct SDL_Joystick* joystick, int hat)
{
if (joystick == ai_fix.pVirtual)
if (joystick == tbf::InputFix::ai_fix.pVirtual)
return 0;

return SDL_JoystickGetHat_Original (joystick, hat);
Expand All @@ -562,7 +554,7 @@ int
__cdecl
SDL_JoystickNumHats_Detour (struct SDL_Joystick* joystick)
{
if (joystick == ai_fix.pVirtual)
if (joystick == tbf::InputFix::ai_fix.pVirtual)
return 1; // 1 D-Pad

return SDL_JoystickNumHats_Original (joystick);
Expand All @@ -572,7 +564,7 @@ int
__cdecl
SDL_JoystickNumButtons_Detour (struct SDL_Joystick* joystick)
{
if (joystick == ai_fix.pVirtual)
if (joystick == tbf::InputFix::ai_fix.pVirtual)
return 12;

return SDL_JoystickNumButtons_Original (joystick);
Expand All @@ -582,7 +574,7 @@ int
__cdecl
SDL_JoystickNumAxes_Detour (struct SDL_Joystick* joystick)
{
if (joystick == ai_fix.pVirtual)
if (joystick == tbf::InputFix::ai_fix.pVirtual)
return 6; // 2 sticks + 1 trigger

return SDL_JoystickNumAxes_Original (joystick);
Expand Down Expand Up @@ -710,6 +702,11 @@ TBF_InitSDLOverride (void)
SDL_JoystickNumHats_Detour,
(LPVOID *)&SDL_JoystickNumHats_Original );


TBF_ApplyQueuedHooks ();
}

tbf::InputFix::ai_fix.first_virtual = SDL_NumJoysticks_Original ();
tbf::InputFix::ai_fix.num_virtual = std::min (4 - tbf::InputFix::ai_fix.first_virtual, config.input.gamepad.virtual_controllers);
tbf::InputFix::ai_fix.pVirtual = (SDL_Joystick *)(LPVOID)0xDEADBEEFULL;
}

tbf::InputFix::ai_fix_s tbf::InputFix::ai_fix;
1 change: 1 addition & 0 deletions src/tbt/control_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "framerate.h"
#include "sound.h"
#include "hook.h"
#include "input.h"


#include <string>
Expand Down
Binary file modified version.ini
Binary file not shown.

0 comments on commit e84fb7e

Please sign in to comment.