Skip to content

Commit

Permalink
Add in a MouseInputEvent for AmethystAPI/VidereLonge#8
Browse files Browse the repository at this point in the history
Before building [email protected], no need to update other mods.
  • Loading branch information
FrederoxDev committed Jul 30, 2024
1 parent 18b1e0d commit b651645
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
14 changes: 14 additions & 0 deletions AmethystAPI/src/amethyst/runtime/events/EventBus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ class BaseEvent {
virtual ~BaseEvent() = default;
};

class CancelableEvent : public BaseEvent {
private:
bool mCanceled = false;

public:
void Cancel() {
mCanceled = true;
}

bool IsCanceled() const {
return mCanceled;
}
};

namespace Amethyst {
class EventBus {
public:
Expand Down
13 changes: 13 additions & 0 deletions AmethystAPI/src/amethyst/runtime/events/InputEvents.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,17 @@ class RegisterInputsEvent : public BaseEvent {

RegisterInputsEvent(Amethyst::InputManager& inputManager)
: inputManager(inputManager) {}
};

class MouseInputEvent : public CancelableEvent {
public:
char mActionButtonId;
char mButtonData;
short x;
short y;
short dx;
short dy;

MouseInputEvent(char actionButton, char buttonData, short x, short y, short dx, short dy)
: mActionButtonId(actionButton), mButtonData(buttonData), x(x), y(y), dx(dx), dy(dy), CancelableEvent() {}
};
2 changes: 1 addition & 1 deletion AmethystRuntime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set(MOD_AUTHOR "FrederoxDev")

set(MOD_TARGET_VERSION_MAJOR 1)
set(MOD_TARGET_VERSION_MINOR 21)
set(MOD_TARGET_VERSION_PATCH 3)
set(MOD_TARGET_VERSION_PATCH 3)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand Down
24 changes: 19 additions & 5 deletions AmethystRuntime/src/hooks/InputHooks.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "hooks/InputHooks.hpp"
#include <amethyst/runtime/events/InputEvents.hpp>
#include <minecraft/src-deps/input/MouseDevice.hpp>

SafetyHookInline _addFullKeyboardGamePlayControls;
SafetyHookInline _createInputMappingTemplates;
SafetyHookInline _MouseDevice_feed;

void addFullKeyboardGamePlayControls(VanillaClientInputMappingFactory* self, KeyboardInputMapping* keyboard, MouseInputMapping* mouse)
{
Expand All @@ -27,13 +29,25 @@ void createInputMappingTemplates(VanillaClientInputMappingFactory* self, Options
AmethystRuntime::getEventBus()->Invoke(event);
}

void MouseDevice_feed(MouseDevice* mouse, char actionButtonId, char buttonData, short x, short y, short dx, short dy, bool forceMotionlessPointer) {
MouseInputEvent event(actionButtonId, buttonData, x, y, dx, dy);
AmethystRuntime::getEventBus()->Invoke(event);

if (!event.IsCanceled()) {
_MouseDevice_feed.thiscall(mouse, actionButtonId, buttonData, x, y, dx, dy, forceMotionlessPointer);
}
}

void CreateInputHooks()
{
Amethyst::HookManager* hookManager = AmethystRuntime::getHookManager();
Amethyst::HookManager& hooks = *AmethystRuntime::getHookManager();

hooks.RegisterFunction<&VanillaClientInputMappingFactory::createInputMappingTemplates>("48 89 5C 24 ? 48 89 74 24 ? 55 57 41 56 48 8D AC 24 ? ? ? ? B8 ? ? ? ? E8 ? ? ? ? 48 2B E0 48 8B 05 ? ? ? ? 48 33 C4 48 89 85 ? ? ? ? 48 8B FA");
hooks.CreateHook<&VanillaClientInputMappingFactory::createInputMappingTemplates>(_createInputMappingTemplates, &createInputMappingTemplates);

hookManager->RegisterFunction<&VanillaClientInputMappingFactory::createInputMappingTemplates>("48 89 5C 24 ? 48 89 74 24 ? 55 57 41 56 48 8D AC 24 ? ? ? ? B8 ? ? ? ? E8 ? ? ? ? 48 2B E0 48 8B 05 ? ? ? ? 48 33 C4 48 89 85 ? ? ? ? 48 8B FA");
hookManager->CreateHook<&VanillaClientInputMappingFactory::createInputMappingTemplates>(_createInputMappingTemplates, &createInputMappingTemplates);
hooks.RegisterFunction<&VanillaClientInputMappingFactory::_addFullKeyboardGamePlayControls>("40 55 53 56 57 41 56 48 8B EC 48 83 EC ? 45 0F B6 F1");
hooks.CreateHook<&VanillaClientInputMappingFactory::_addFullKeyboardGamePlayControls>(_addFullKeyboardGamePlayControls, &addFullKeyboardGamePlayControls);

hookManager->RegisterFunction<&VanillaClientInputMappingFactory::_addFullKeyboardGamePlayControls>("40 55 53 56 57 41 56 48 8B EC 48 83 EC ? 45 0F B6 F1");
hookManager->CreateHook<&VanillaClientInputMappingFactory::_addFullKeyboardGamePlayControls>(_addFullKeyboardGamePlayControls, &addFullKeyboardGamePlayControls);
hooks.RegisterFunction<&MouseDevice::feed>("48 8B C4 48 89 58 ? 48 89 68 ? 48 89 70 ? 57 41 54 41 55 41 56 41 57 48 83 EC ? 44 0F B7 BC 24");
hooks.CreateHook<&MouseDevice::feed>(_MouseDevice_feed, &MouseDevice_feed);
}

0 comments on commit b651645

Please sign in to comment.