Skip to content

Commit

Permalink
fix input being passed to game when alt tabbing with input window open
Browse files Browse the repository at this point in the history
fixed ESC key being passed to game when closing input window
cleanup
  • Loading branch information
aaronkirkham committed Jan 5, 2019
1 parent 38b452b commit 29e2f10
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 69 deletions.
8 changes: 7 additions & 1 deletion src/commands/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class EventCommand : public ICommand
{
public:
std::array<const char*, 13> m_Hints = {
std::array<const char*, 19> m_Hints = {
"__showfps",
"__reload_world",
"ply.invulnerable",
Expand All @@ -16,6 +16,12 @@ class EventCommand : public ICommand
"ply.pause",
"ply.unpause",
"ply.vehicle.burn",
"ply.jesus.enable",
"ply.jesus.disable",
"ply.bavarium.upgrade.enable",
"ply.bavarium.upgrade.disable",
"ply.lacrima.forcefield.enable",
"ply.lacrima.forcefield.disable",
"debug.vehicle.incrementtint",
"vocals.rico.enable",
"vocals.rico.disable",
Expand Down
4 changes: 2 additions & 2 deletions src/commands/spawn.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SpawnCommand : public ICommand
#pragma pack(pop)

public:
inline static std::vector<std::string> m_Hints = {};
inline static std::set<std::string> m_Hints = {};

virtual const char *GetCommand() override
{
Expand All @@ -39,7 +39,7 @@ class SpawnCommand : public ICommand
add_event_hook.call(file, &model, 0xF71C2A21);

if (model.entry) {
m_Hints.emplace_back((char *)(model.base + model.entry->data));
m_Hints.insert((char *)(model.base + model.entry->data));
}

return add_event_hook.call(file, buf, hash);
Expand Down
20 changes: 1 addition & 19 deletions src/commands/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class WorldCommand : public ICommand
std::array<const char*, 2> m_Hints = {
"time",
"timescale",
//"gravity",
//"resetgravity",
};

virtual const char* GetCommand() override
Expand All @@ -19,8 +17,7 @@ class WorldCommand : public ICommand

virtual bool Handler(const std::string& arguments) override
{
//static auto World = *(void**)0x142A8D430;
static auto WorldTime = *(void**)0x142F17250; // 1
static auto WorldTime = *(void**)0x142F17250;

// time
if (arguments.find("time ") != std::string::npos) {
Expand All @@ -39,21 +36,6 @@ class WorldCommand : public ICommand
return true;
}
}
#if 0
// gravity
else if (arguments.find("gravity ") != std::string::npos) {
float gravity = -9.810f;
if (sscanf_s(arguments.c_str(), "gravity %f", &gravity) == 1) {
*(float*)((char*)World + 0x974) = std::clamp(gravity, -5000.0f, 5000.0f);
return true;
}
}
// reset gravity
else if (arguments.find("resetgravity") != std::string::npos) {
*(float*)((char*)World + 0x974) = -9.810f;
return true;
}
#endif

return false;
}
Expand Down
10 changes: 5 additions & 5 deletions src/game/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace jc
class HDevice_t
{
public:
char _pad[0x28];
ID3D11Device* m_device;
char _pad2[0x170];
int32_t m_screenWidth;
int32_t m_screenHeight;
char _pad[0x28];
ID3D11Device* m_device;
char _pad2[0x170];
int32_t m_screenWidth;
int32_t m_screenHeight;
};
}; // namespace jc
#pragma pack(pop)
19 changes: 19 additions & 0 deletions src/game/input_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#pragma pack(push, 1)
namespace jc::Input
{
class CInputDeviceManager
{
public:
static CInputDeviceManager& instance()
{
return **(CInputDeviceManager**)0x142E2B6F8;
}

public:
char _pad[0x70];
bool m_hasFocus;
};
}; // namespace jc::Input
#pragma pack(pop)
3 changes: 2 additions & 1 deletion src/game/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class CPlayerAimControl
class CPlayer
{
public:
// NOTE(aaron): -16 bytes because we don't cast from CAvatar in CNetworkPlayer
// NOTE(aaron): everything here is out of alignment by 16 bytes because we
// don't use CAvatar in CNetworkPlayer
char _pad[0x100];
CCharacter* m_character;
char _pad2[0x40];
Expand Down
6 changes: 2 additions & 4 deletions src/game/player_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ namespace jc
class CNetworkPlayer
{
public:
char _pad[0x128];
std::shared_ptr<CPlayer> m_player;
char _pad[0x128];
std::shared_ptr<CPlayer> m_player;
std::shared_ptr<CCharacter> m_character;
};

static_assert(offsetof(CNetworkPlayer, m_character) == 0x138, "borked");

class CNetworkPlayerManager
{
public:
Expand Down
21 changes: 0 additions & 21 deletions src/game/render_engine.h

This file was deleted.

4 changes: 2 additions & 2 deletions src/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class Graphics : public Singleton<Graphics>
m_ready = false;
m_state.releaseSavedState();

if (m_context) {
if (m_context) {
m_context->Release();
}
}

if (m_font) {
m_font->Release();
Expand Down
36 changes: 28 additions & 8 deletions src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@

#include "game/debug_renderer.h"
#include "game/graphics_engine.h"
#include "game/input_manager.h"
#include "game/ui_manager.h"

static const int32_t _numHintsPerPage = 10;

void Input::EnableInput(bool toggle)
void Input::FocusChanged(bool lost_focus)
{
static void *input_thingy = nullptr;
if (!input_thingy) {
input_thingy = *(void **)0x142E2B6F8;
m_history.push_back("");
m_hasFocus = !lost_focus;

// if we are drawing input and regained focus, lock the input once again
// as the game will unlock it automatically
if (m_drawInput && !lost_focus) {
jc::Input::CInputDeviceManager::instance().m_hasFocus = false;
}
}

void Input::EnableInput(bool toggle)
{
m_drawInput = toggle;
m_currentHistory = 0;
m_selectedHint = -1;
Expand All @@ -30,7 +36,7 @@ void Input::EnableInput(bool toggle)
jc::CUIManager().instance().m_active = !toggle;

// freeze input
*(bool *)((char *)input_thingy + 0x70) = !toggle;
jc::Input::CInputDeviceManager::instance().m_hasFocus = !toggle;
}

void Input::Draw()
Expand Down Expand Up @@ -74,7 +80,23 @@ void Input::Draw()

bool Input::WndProc(uint32_t message, WPARAM wParam, LPARAM lParam)
{
if (!m_hasFocus) {
return false;
}

switch (message) {
// fix ESC key still getting passed to the game when closing the input window
// this is because EnableInput will give focus back to the input manager and
// if we leave it on KEYDOWN the game will then see the ESC key as down
case WM_KEYUP: {
if (wParam == VK_ESCAPE && m_selectedHint == -1 && m_history[0].size() == 0) {
EnableInput(false);
return true;
}

break;
}

case WM_KEYDOWN: {
// tilde key down and the previous key state was up
const auto vsc = MapVirtualKeyEx(static_cast<int32_t>(wParam), MAPVK_VK_TO_VSC, GetKeyboardLayout(0));
Expand Down Expand Up @@ -107,8 +129,6 @@ bool Input::WndProc(uint32_t message, WPARAM wParam, LPARAM lParam)
m_cmdArguments = "";
m_currentHistory = 0;
m_hints.clear();
} else {
EnableInput(false);
}
} else {
m_selectedHint = -1;
Expand Down
7 changes: 5 additions & 2 deletions src/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ class Input : public Singleton<Input>
using command_t = std::function<void(const std::string& arguments)>;

private:
bool m_drawInput = false;
std::vector<std::string> m_history;
bool m_drawInput = false;
std::vector<std::string> m_history = {""};
int32_t m_currentHistory = 0;
ICommand* m_cmd = nullptr;
std::string m_cmdText = "";
std::string m_cmdArguments = "";
std::vector<std::string> m_hints;
int32_t m_selectedHint = -1;
int32_t m_hintPage = 0;
bool m_hasFocus = true;

std::unordered_map<std::string, std::unique_ptr<ICommand>> m_commands;
std::unordered_map<std::string, command_t> m_fnCommands;
Expand All @@ -37,6 +38,8 @@ class Input : public Singleton<Input>
Input() = default;
virtual ~Input() = default;

void FocusChanged(bool lost_focus);

void RegisterCommand(std::unique_ptr<ICommand> cmd)
{
cmd->Initialize();
Expand Down
14 changes: 14 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ HRESULT D3D11CreateDevice(IDXGIAdapter *pAdapter, D3D_DRIVER_TYPE Drive
HWND hwnd = *(HWND *)0x142E22A18;
WndProc_orig = (WNDPROC)SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)WndProc);

// focus lost
static hk::inject_call<void, HWND, bool> lose_focus(0x143218620);
lose_focus.inject([](HWND hwnd, bool a2) {
lose_focus.call(hwnd, a2);
Input::Get()->FocusChanged(true);
});

// focus gained
static hk::inject_call<void, HWND> gain_focus(0x143218611);
gain_focus.inject([](HWND hwnd) {
gain_focus.call();
Input::Get()->FocusChanged(false);
});

static hk::inject_call<int64_t, jc::HDevice_t *> flip(0x1432E0071);
flip.inject([](jc::HDevice_t *device) {
Graphics::Get()->BeginDraw(device);
Expand Down
6 changes: 3 additions & 3 deletions src/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ struct CVector3f {
struct CVector4f {
float x, y, z, w;

static CVector4f FromARGB(const uint32_t hex)
{
static CVector4f FromARGB(const uint32_t hex)
{
CVector4f result;
result.w = ((hex >> 24) & 0xFF) / 255.0f;
result.x = ((hex >> 16) & 0xFF) / 255.0f;
result.y = ((hex >> 8) & 0xFF) / 255.0f;
result.z = (hex & 0xFF) / 255.0f;
return result;
}
}
};

struct CMatrix4f {
Expand Down
2 changes: 1 addition & 1 deletion src/xinput9_1_0.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ __declspec(dllexport) DWORD XInputSetState(DWORD dwUserIndex, uintptr_t pVibrati

return E_FAIL;
}
}; // namespace
}; // namespace

0 comments on commit 29e2f10

Please sign in to comment.