Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

Commit

Permalink
Move more hooks to geode modify
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaghettDev committed Jan 26, 2024
1 parent 1a9b847 commit 189d5b7
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 58 deletions.
89 changes: 45 additions & 44 deletions src/Hacks/SafeMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,51 @@
using namespace geode::prelude;
using namespace SafeMode;

class $modify(EndLevelLayer)
{
void customSetup()
{
EndLevelLayer::customSetup();

if (
!Mod::get()->getSavedValue<bool>("level/safe_mode/enabled") ||
!Mod::get()->getSavedValue<bool>("level/safe_mode/endscreen_enabled")
) return;

auto layer = reinterpret_cast<CCLayer*>(this->getChildren()->objectAtIndex(0));
CCLabelBMFont* endScreenMessageLabel = nullptr;
TextArea* endScreenTextArea = nullptr;


CCObject* object;
CCARRAY_FOREACH(layer->getChildren(), object)
{
if (auto textArea = dynamic_cast<TextArea*>(object); textArea)
endScreenTextArea = textArea;
else if (
auto messageLabel = dynamic_cast<CCLabelBMFont*>(object);
messageLabel
) {
std::string labelText = messageLabel->getString();

if (
!labelText.starts_with("Attempts: ") && !labelText.starts_with("Jumps: ") &&
!labelText.starts_with("Time: ") && !labelText.starts_with("Points: ")
)
endScreenMessageLabel = messageLabel;
}
}

// TODO: Create a CCLabelBMFont if both of these are false

if (endScreenTextArea)
endScreenTextArea->setString("- Safe Mode -");

if (endScreenMessageLabel)
endScreenMessageLabel->setString("- Safe Mode -");
}
};

void SafeMode::updateState()
{
for (auto& patch : patches)
Expand All @@ -29,55 +74,11 @@ void SafeMode::updateState()
}
}

void SafeMode::endLevelLayerCustomSetupHook(CCLayer* self)
{
reinterpret_cast<void(__thiscall*)(cocos2d::CCLayer*)>(geode::base::get() + 0xE74F0)(self);

if (
!Mod::get()->getSavedValue<bool>("level/safe_mode/enabled") ||
!Mod::get()->getSavedValue<bool>("level/safe_mode/endscreen_enabled")
) return;

auto layer = getChildOfType<CCLayer>(self, 0);
CCLabelBMFont* endScreenMessageLabel = nullptr;
TextArea* endScreenTextArea = nullptr;


CCObject* object;
CCARRAY_FOREACH(layer->getChildren(), object)
{
if (auto textArea = dynamic_cast<TextArea*>(object); textArea)
endScreenTextArea = textArea;
else if (
auto messageLabel = dynamic_cast<CCLabelBMFont*>(object);
messageLabel
) {
std::string labelText = messageLabel->getString();

if (
!labelText.starts_with("Attempts: ") && !labelText.starts_with("Jumps: ") &&
!labelText.starts_with("Time: ") && !labelText.starts_with("Points: ")
)
endScreenMessageLabel = messageLabel;
}
}

// TODO: Create a CCLabelBMFont if both of these are false

if (endScreenTextArea)
endScreenTextArea->setString("- Safe Mode -");

if (endScreenMessageLabel)
endScreenMessageLabel->setString("- Safe Mode -");
}

$execute
{
for (std::size_t i = 0; i < PATCHES_SIZE; i++)
{
patches[i] = Mod::get()->patch(reinterpret_cast<void*>(base::get() + std::get<0>(opcodes[i])), std::get<1>(opcodes[i])).unwrap();
patches[i]->disable();
}

Mod::get()->hook(reinterpret_cast<void*>(geode::base::get() + 0xE74F0), &endLevelLayerCustomSetupHook, "EndLevelLayer::customSetup", tulip::hook::TulipConvention::Thiscall);
}
2 changes: 0 additions & 2 deletions src/Hacks/SafeMode.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,4 @@ namespace SafeMode
{ 0x2EA83D, { 0x90 } }
};
inline std::array<geode::Patch*, PATCHES_SIZE> patches;

void endLevelLayerCustomSetupHook(cocos2d::CCLayer*);
}
27 changes: 16 additions & 11 deletions src/Macrobot/Macrobot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <Geode/Geode.hpp>
#include <Geode/modify/GJBaseGameLayer.hpp>
#include <Geode/modify/CheckpointObject.hpp>
#include <Geode/modify/PlayLayer.hpp>
#include <Geode/modify/CCKeyboardDispatcher.hpp>

Expand Down Expand Up @@ -152,20 +153,25 @@ Macrobot::Action *Macrobot::recordAction(PlayerButton key, double frame, bool pr
return &macro.inputs[macro.inputs.size() - 1];
}

void *Macrobot::checkpointObjectCreateHook()
class $modify(CheckpointObject)
{
void *self = reinterpret_cast<void *(__thiscall *)()>(base::get() + 0x2EB9A0)();
if (playerMode != -1 && gameTime > 0 && playerObject1)
CheckpointObject* create()
{
CheckpointData data;
data.time = gameTime;
data.p1.fromPlayer(playerObject1, true);
data.p2.fromPlayer(playerObject2, true);
auto self = CheckpointObject::create();

checkpoints[self] = data;
if (playerMode != -1 && gameTime > 0 && playerObject1)
{
CheckpointData data;
data.time = gameTime;
data.p1.fromPlayer(playerObject1, true);
data.p2.fromPlayer(playerObject2, true);

checkpoints[self] = data;
}

return self;
}
return self;
}
};

void Macrobot::GJBaseGameLayerProcessCommands(GJBaseGameLayer *self)
{
Expand Down Expand Up @@ -201,7 +207,6 @@ void Macrobot::GJBaseGameLayerProcessCommands(GJBaseGameLayer *self)

$execute
{
Mod::get()->hook(reinterpret_cast<void *>(base::get() + 0x2EB9A0), &checkpointObjectCreateHook, "CheckpointObject::create", tulip::hook::TulipConvention::Optcall);
Mod::get()->hook(reinterpret_cast<void *>(base::get() + 0x1BD240), &GJBaseGameLayerProcessCommands, "GJBaseGameLayer::processCommands", tulip::hook::TulipConvention::Thiscall);
}

Expand Down
1 change: 0 additions & 1 deletion src/Macrobot/Macrobot.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ inline std::unordered_map<void*, Macrobot::CheckpointData> checkpoints;
inline std::string macroName;
inline std::string macroDescription;

void* checkpointObjectCreateHook();
void GJBaseGameLayerProcessCommands(GJBaseGameLayer* self);

Action* recordAction(PlayerButton key, double frame, bool press, bool player1);
Expand Down

0 comments on commit 189d5b7

Please sign in to comment.