Skip to content

Commit

Permalink
Move ItemPhysics hooks to GeneralHooks
Browse files Browse the repository at this point in the history
  • Loading branch information
marioCST committed Aug 25, 2024
1 parent ce35113 commit aa9a729
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 161 deletions.
1 change: 0 additions & 1 deletion LatiteRewrite.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,6 @@
<ClCompile Include="deps\mnem\scan\scanner.cpp" />
<ClCompile Include="deps\mnem\scan\scanner_avx2.cpp" />
<ClCompile Include="src\client\feature\module\impl\visual\ItemPhysics.cpp" />
<ClCompile Include="src\client\feature\module\impl\visual\ItemPhysicsHooks.cpp" />
<ClCompile Include="src\client\feature\module\impl\visual\NoHurtCam.cpp" />
<ClCompile Include="src\client\feature\module\impl\game\EnvironmentChanger.cpp" />
<ClCompile Include="src\client\feature\module\impl\game\Freelook.cpp" />
Expand Down
20 changes: 0 additions & 20 deletions src/client/feature/module/impl/visual/ItemPhysics.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "pch.h"
#include "ItemPhysics.h"

#include "sdk/common/world/actor/ItemActor.h"
#include "util/mem/buffer.h"

ItemPhysics::ItemPhysics() : Module("ItemPhysics", LocalizeString::get("client.module.itemPhysics.name"),
Expand Down Expand Up @@ -30,14 +29,6 @@ void ItemPhysics::onEnable() {
static auto posAddr = Signatures::ItemPositionConst.result + 4;
origPosRel = *reinterpret_cast<uint32_t*>(posAddr);

static auto rotateAddr = reinterpret_cast<void*>(Signatures::glm_rotateRef.result);

if (glm_rotateHook == nullptr)
glm_rotateHook = std::make_shared<Hook>(Signatures::glm_rotateRef.result, glm_rotate, "glm::rotate");

if (ItemRenderer_renderHook == nullptr)
ItemRenderer_renderHook = std::make_shared<Hook>(Signatures::ItemRenderer_render.result, ItemRenderer_render, "ItemRenderer::render");

static auto translateAddr = reinterpret_cast<void*>(Signatures::glm_translateRef.result);
static auto translateAddr2 = reinterpret_cast<void*>(Signatures::glm_translateRef2.result);

Expand All @@ -48,11 +39,6 @@ void ItemPhysics::onEnable() {

memory::patchBytes(reinterpret_cast<void*>(posAddr), newRipRel.data(), 4);

glm_rotateHook->enable();
ItemRenderer_renderHook->enable();

memory::patchBytes(rotateAddr, (BYTE*)"\xE8", 1);

memory::copyBytes(translateAddr, data, 5);
memory::copyBytes(translateAddr2, data2, 5);

Expand All @@ -73,12 +59,6 @@ void ItemPhysics::onDisable() {
memory::patchBytes(reinterpret_cast<void*>(posAddr), &origPosRel, 4);
FreeBuffer(newPosRel);

if (glm_rotateHook != nullptr)
glm_rotateHook->disable();

if (ItemRenderer_renderHook != nullptr)
ItemRenderer_renderHook->disable();

memory::patchBytes(translateAddr, data, 5);
memory::patchBytes(translateAddr2, data2, 5);

Expand Down
6 changes: 0 additions & 6 deletions src/client/feature/module/impl/visual/ItemPhysics.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ class ItemPhysics : public Module {
std::unordered_map<SDK::Actor*, std::tuple<float, Vec3, Vec3i>> actorData;
SDK::ActorRenderData* renderData = nullptr;

std::shared_ptr<Hook> glm_rotateHook = nullptr;
std::shared_ptr<Hook> ItemRenderer_renderHook = nullptr;

static void __fastcall glm_rotate(glm::mat4x4& mat, float angle, float x, float y, float z);
static void __fastcall ItemRenderer_render(SDK::ItemRenderer* _this, SDK::BaseActorRenderContext* renderContext, SDK::ActorRenderData* actorRenderData);

void onEnable() override;
void onDisable() override;
};
133 changes: 0 additions & 133 deletions src/client/feature/module/impl/visual/ItemPhysicsHooks.cpp

This file was deleted.

6 changes: 6 additions & 0 deletions src/client/hook/Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ void LatiteHooks::uninit() {

void LatiteHooks::enable() {
MH_EnableHook(MH_ALL_HOOKS);

// Maybe enable hooks manually instead of using "MH_EnableHook(MH_ALL_HOOKS)" and use a parameter for a custom callback if a hook is enabled/disabled
static auto rotateAddr = reinterpret_cast<void*>(Signatures::glm_rotateRef.result);
// glm::rotate hook in ItemRenderer::render
// Replaces 0xE9 with 0xE8 so a "CALL" instruction is used instead of a "JMP" instruction as we don't want to overwrite the other code
memory::patchBytes(rotateAddr, (BYTE*)"\xE8", 1);
}

void LatiteHooks::disable() {
Expand Down
142 changes: 142 additions & 0 deletions src/client/hook/impl/GeneralHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "client/script/PluginManager.h"
#include "../Hooks.h"
#include "PlayerHooks.h"
#include "client/feature/module/impl/visual/ItemPhysics.h"
#include "sdk/common/world/actor/ItemActor.h"

namespace {
std::shared_ptr<Hook> Level_tickHook;
Expand Down Expand Up @@ -34,6 +36,8 @@ namespace {
std::shared_ptr<Hook> UpdatePlayerHook;
std::shared_ptr<Hook> OnUriHook;
std::shared_ptr<Hook> BobHurtHook;
std::shared_ptr<Hook> ItemRenderer_renderHook;
std::shared_ptr<Hook> ItemRenderer_render_glm_rotateHook;
}

void GenericHooks::Level_tick(SDK::Level* level) {
Expand Down Expand Up @@ -405,6 +409,141 @@ void GenericHooks::hkBobHurt(void* obj, void* a2, void* a3) {
BobHurtHook->oFunc<decltype(&hkBobHurt)>()(obj, a2, a3);
}

void __fastcall GenericHooks::ItemRenderer_render(SDK::ItemRenderer* _this, SDK::BaseActorRenderContext* renderContext, SDK::ActorRenderData* actorRenderData) {
static auto physMod = reinterpret_cast<ItemPhysics*>(Latite::getModuleManager().find("ItemPhysics").get());

physMod->renderData = actorRenderData;

ItemRenderer_renderHook->oFunc<decltype(&GenericHooks::ItemRenderer_render)>()(_this, renderContext, actorRenderData);
}

void __fastcall GenericHooks::glm_rotate(glm::mat4x4& mat, float angle, float x, float y, float z) {
static auto rotateSig = Signatures::glm_rotate.result;
using glm_rotate_t = void(__fastcall*)(glm::mat4x4&, float, float, float, float);
static auto glm_rotate = reinterpret_cast<glm_rotate_t>(rotateSig);

static auto physMod = reinterpret_cast<ItemPhysics*>(Latite::getModuleManager().find("ItemPhysics").get());

if (!physMod->isEnabled()) {
glm_rotate(mat, angle, x, y, z);
return;
}

if (physMod->renderData == nullptr)
return;

auto curr = reinterpret_cast<SDK::ItemActor*>(physMod->renderData->actor);

static float height = 0.5f;

if (!physMod->actorData.contains(curr)) {
std::random_device rd;
std::mt19937 gen(rd());

std::uniform_int_distribution<> dist(0, 1);
std::uniform_int_distribution<> dist2(0, 359);

const auto vec = Vec3(dist2(gen), dist2(gen), dist2(gen));
const auto sign = Vec3i(dist(gen) * 2 - 1, dist(gen) * 2 - 1, dist(gen) * 2 - 1);

auto def = std::tuple{curr->isOnGround() ? 0.f : height, vec, sign};
physMod->actorData.emplace(curr, def);
}

const float deltaTime = 1.f / static_cast<float>(Latite::get().getTimings().getFPS());

float& yMod = std::get<0>(physMod->actorData.at(curr));

yMod -= height * deltaTime;

float threshold = curr->stack.block != nullptr ? 0.f : -0.125f;

if (yMod <= threshold)
yMod = threshold;

Vec3 pos = physMod->renderData->position;
pos.y += yMod;

auto& vec = std::get<1>(physMod->actorData.at(curr));
auto& sign = std::get<2>(physMod->actorData.at(curr));

if (!curr->isOnGround() || yMod > threshold) {
vec.x += static_cast<float>(sign.x) * deltaTime * std::get<FloatValue>(physMod->speed) * std::get<FloatValue>(physMod->xMul);
vec.y += static_cast<float>(sign.y) * deltaTime * std::get<FloatValue>(physMod->speed) * std::get<FloatValue>(physMod->yMul);
vec.z += static_cast<float>(sign.z) * deltaTime * std::get<FloatValue>(physMod->speed) * std::get<FloatValue>(physMod->zMul);

if (vec.x > 360.f)
vec.x -= 360.f;

if (vec.x < 0.f)
vec.x += 360.f;

if (vec.y > 360.f)
vec.y -= 360.f;

if (vec.y < 0.f)
vec.y += 360.f;

if (vec.z > 360.f)
vec.z -= 360.f;

if (vec.z < 0.f)
vec.z += 360.f;
}

Vec3 renderVec = vec;

if (curr->isOnGround() && yMod == threshold && !std::get<BoolValue>(physMod->preserveRotations) && (sign.x != 0 || sign.y != 0 && sign.z != 0)) {
if (std::get<BoolValue>(physMod->smoothRotations) && (sign.x != 0 || sign.y != 0 && sign.z != 0)) {
vec.x += static_cast<float>(sign.x) * deltaTime * std::get<FloatValue>(physMod->speed) * std::get<FloatValue>(physMod->xMul);

if (curr->stack.block != nullptr) {
vec.z += static_cast<float>(sign.z) * deltaTime * std::get<FloatValue>(physMod->speed) * std::get<FloatValue>(physMod->zMul);

if (vec.x > 360.f || vec.x < 0.f) {
vec.x = 0.f;
sign.x = 0;
}

if (vec.z > 360.f || vec.z < 0.f) {
vec.z = 0.f;
sign.z = 0;
}
}
else {
vec.y += static_cast<float>(sign.y) * deltaTime * std::get<FloatValue>(physMod->speed) * std::get<FloatValue>(physMod->yMul);

if (vec.x - 90.f > 360.f || vec.x - 90.f < 0.f) {
vec.x = 90.f;
sign.x = 0;
}

if (vec.y > 360.f || vec.y < 0.f) {
vec.y = 0.f;
sign.y = 0;
}
}
}

if (!std::get<BoolValue>(physMod->smoothRotations)) {
if (curr->stack.block != nullptr) {
renderVec.x = 0.f;
renderVec.z = 0.f;
}
else {
renderVec.x = 90.f;
renderVec.y = 0.f;
}
}
}

mat = translate(mat, {pos.x, pos.y, pos.z});

glm_rotate(mat, renderVec.x, 1.f, 0.f, 0.f);
glm_rotate(mat, renderVec.y, 0.f, 1.f, 0.f);
glm_rotate(mat, renderVec.z, 0.f, 0.f, 1.f);
}

GenericHooks::GenericHooks() : HookGroup("General") {
//LoadLibraryAHook = addHook(reinterpret_cast<uintptr_t>(&::LoadLibraryW), hkLoadLibraryW);
//LoadLibraryWHook = addHook(reinterpret_cast<uintptr_t>(&::LoadLibraryA), hkLoadLibraryW);
Expand Down Expand Up @@ -454,5 +593,8 @@ GenericHooks::GenericHooks() : HookGroup("General") {
UpdatePlayerHook = addHook(Signatures::_updatePlayer.result, hkUpdatePlayer, "`anonymous namespace'::_updatePlayer");
OnUriHook = addHook(Signatures::GameArguments__onUri.result, hkOnUri, "GameArguments::_onUri");
BobHurtHook = addHook(Signatures::_bobHurt.result, hkBobHurt, "`anonymous namespace`::_bobHurt");

ItemRenderer_renderHook = addHook(Signatures::ItemRenderer_render.result, ItemRenderer_render, "ItemRenderer::render");
ItemRenderer_render_glm_rotateHook = addHook(Signatures::glm_rotateRef.result, glm_rotate, "glm::rotate");
}

Loading

0 comments on commit aa9a729

Please sign in to comment.