Skip to content

Commit

Permalink
Replace printf with ZHL::Log (and add logs in ASMPatcher)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylmir committed Nov 16, 2024
1 parent fead992 commit 357e3c4
Show file tree
Hide file tree
Showing 36 changed files with 215 additions and 201 deletions.
4 changes: 4 additions & 0 deletions libzhl/ASMPatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,14 @@ void* ASMPatcher::Patch(void* at, void* targetPage, const char* with, size_t len
memset((char*)at + 5, 0x90, count - 5);
}

logger.Log("ASMPatcher::Patch: Encoding and writing jump...\n");
EncodeAndWriteJump(at, targetPage);
size_t textLen = len;
if (textLen == 0) {
textLen = strlen(with);
}

logger.Log("ASMPatcher::Patch: copying content of the patch to the new page (writing %u bytes at %p)\n", textLen, targetPage);
memcpy(targetPage, with, textLen);
_firstAvailable = (char*)_firstAvailable + textLen;
_bytesRemaining -= textLen;
Expand All @@ -189,6 +192,7 @@ void* ASMPatcher::Patch(void* at, void* targetPage, const char* with, size_t len
return nullptr;
}

logger.Log("ASMPatcher::Patch: flushing instruction pipeline\n");
FlushInstructionCache(GetModuleHandle(NULL), NULL, 0);

return targetPage;
Expand Down
82 changes: 41 additions & 41 deletions repentogon/HookTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,60 +29,60 @@ HOOK_METHOD(Level, SetStage, (int stageid, int alt) -> void) {

/*HOOK_METHOD(Game, MakeShockwave, (const Vector &pos, float amp, float speed, int dur) -> void) {
super(pos, amp, speed, dur);
printf("%g %g %g %g %d\n", pos.x,pos.y, amp, speed, dur);
ZHL::Log("%g %g %g %g %d\n", pos.x,pos.y, amp, speed, dur);
}
*/

/*
HOOK_METHOD(Game, Update, () -> void) {
super();
printf("I'm a buff baby that can dance like a man,\n");
printf("I can shake-ah my fanny, I can shake-ah my can!\n");
printf("I'm a tough tootin' baby, I can punch-ah yo buns!\n");
printf("Punch-ah yo buns, I can punch-ah yo buns!\n");
ZHL::Log("I'm a buff baby that can dance like a man,\n");
ZHL::Log("I can shake-ah my fanny, I can shake-ah my can!\n");
ZHL::Log("I'm a tough tootin' baby, I can punch-ah yo buns!\n");
ZHL::Log("Punch-ah yo buns, I can punch-ah yo buns!\n");
}
*/

/*HOOK_METHOD(GridEntity_Rock, Update, () -> void) {
super();
printf("GridRock unk: %d type %d variant %d\n", _unk, _type, _variant);
ZHL::Log("GridRock unk: %d type %d variant %d\n", _unk, _type, _variant);
}
*/


/*HOOK_METHOD(Entity_Player, Init, (unsigned int type, unsigned int variant, unsigned int subtype, unsigned int initSeed) -> void) {
super(type, variant, subtype, initSeed);
printf("Entity_Player::Init type %d variant %d sub %d and this thing %d\n", type,variant,subtype,initSeed);
ZHL::Log("Entity_Player::Init type %d variant %d sub %d and this thing %d\n", type,variant,subtype,initSeed);
};
*/

/*HOOK_METHOD(Entity_Player, Update, () -> void) {
Weapon* wep = this->GetWeapon();
int mem = wep->GetFireDelay();
if (wep != nullptr) {
printf("wep delay %d\n", mem);
ZHL::Log("wep delay %d\n", mem);
}
super();
}
*/

/*HOOK_STATIC(Isaac, CreateWeapon, (WeaponType wepType, Entity* ent) -> Weapon*) {
Weapon* ret = super(wepType, ent);
printf("createweapon %d %d\n", wepType, ret->GetWeaponType());
ZHL::Log("createweapon %d %d\n", wepType, ret->GetWeaponType());
return ret;
}
*/

/*HOOK_METHOD(Weapon_MonstrosLung, Fire, (const Vector& pos, bool unk1, bool unk2)-> void) {
printf("Weapon::Fire %f %f %d %d %f\n", pos.x, pos.y, unk1, unk2, this->GetCharge());
printf("Weapon::DirVector: %f %f\n", this->GetDirection()->x, this->GetDirection()->y);
ZHL::Log("Weapon::Fire %f %f %d %d %f\n", pos.x, pos.y, unk1, unk2, this->GetCharge());
ZHL::Log("Weapon::DirVector: %f %f\n", this->GetDirection()->x, this->GetDirection()->y);
super(pos, unk1, unk2);
}
*/

/*HOOK_METHOD(PlayerManager, SpawnCoPlayer2, (int unk) -> Entity_Player*) {
printf("PlayerManager::SpawnCoPlayer2 %d\n", unk);
ZHL::Log("PlayerManager::SpawnCoPlayer2 %d\n", unk);
Entity_Player* ret = super(unk);
return ret;
Expand All @@ -91,15 +91,15 @@ HOOK_METHOD(Game, Update, () -> void) {

/*HOOK_GLOBAL(CreateGridEntity, (GridEntityType type, unsigned int seed) -> GridEntity*) {
GridEntity* ret = super(type, seed);
printf("CreateGridEntity type: %d, seed %d\n", type, seed);
ZHL::Log("CreateGridEntity type: %d, seed %d\n", type, seed);
return ret;
}
*/

/*HOOK_METHOD(Game, Spawn, (unsigned int type, unsigned int variant, const Vector &pos, const Vector &vel, Entity* spawner, unsigned int sub, unsigned int seed, unsigned int unk) -> Entity*) {
Entity* ret = super(type,variant,pos,vel,spawner,sub,seed, unk);
printf("Game::Spawn %d, %d, %f, %f, %f, %f,%d,%d,%d\n", type, variant, pos.x,pos.y, vel.x, vel.y, sub, seed, unk);
ZHL::Log("Game::Spawn %d, %d, %f, %f, %f, %f,%d,%d,%d\n", type, variant, pos.x,pos.y, vel.x, vel.y, sub, seed, unk);
return ret;
}
Expand All @@ -108,22 +108,22 @@ HOOK_METHOD(Game, Update, () -> void) {

/*HOOK_METHOD(Entity, Init, (unsigned int type, unsigned int variant, unsigned int subtype, unsigned int initSeed) -> void) {
super(type, variant, subtype, initSeed);
printf("Entity::Init type %d variant %d sub %d and this thing %d\n", type,variant,subtype,initSeed);
ZHL::Log("Entity::Init type %d variant %d sub %d and this thing %d\n", type,variant,subtype,initSeed);
}
*/

/*
HOOK_METHOD(Entity, Update, () -> void) {
super();
//printf("hellp");
//ZHL::Log("hellp");
}
*/

/*HOOK_METHOD(Room, GetDevilRoomChance, () -> float) {
float ret = super();
ret = 0.134f;
printf("GetDevilRoomChance: %f\n", ret);
ZHL::Log("GetDevilRoomChance: %f\n", ret);
return ret;
}
*/
Expand All @@ -133,46 +133,46 @@ HOOK_METHOD(Entity, Update, () -> void) {
Weapon_MultiShotParams params = g_Game->GetPlayer(0)->GetMultiShotParams(WEAPON_TEARS);
printf("%d\n", params.numTears);
ZHL::Log("%d\n", params.numTears);
PosVel shotPosVel = g_Game->GetPlayer(0)->GetMultiShotPositionVelocity(0, WEAPON_TEARS, Vector(10, 0), 1.0f, params);
printf("(%f, %f) (%f, %f)", shotPosVel.pos.x, shotPosVel.pos.y, shotPosVel.vel.x, shotPosVel.vel.y);
ZHL::Log("(%f, %f) (%f, %f)", shotPosVel.pos.x, shotPosVel.pos.y, shotPosVel.vel.x, shotPosVel.vel.y);
}*/


/*HOOK_METHOD(Entity_Player, AddCoins, (int amount) -> void) {
//printf("EntityPlayer::AddCoins call %d\n", amount);
//ZHL::Log("EntityPlayer::AddCoins call %d\n", amount);
}
HOOK_METHOD(Entity_Player, AddBombs, (int amount) -> void) {
//printf("EntityPlayer::AddBombs call %d\n", amount);
//ZHL::Log("EntityPlayer::AddBombs call %d\n", amount);
super(amount);
}
HOOK_METHOD(Entity_Player, AddKeys, (int amount) -> void) {
//printf("EntityPlayer::AddKeys call %d\n", amount); // please
//ZHL::Log("EntityPlayer::AddKeys call %d\n", amount); // please
super(amount);
}*/

/*
HOOK_METHOD(Entity_Player, AddJarFlies, (int amount) -> void) {
printf("EntityPlayer::AddJarFlies call %d\n", amount); // func for lua?
ZHL::Log("EntityPlayer::AddJarFlies call %d\n", amount); // func for lua?
}
HOOK_METHOD(Entity_Player, AddPrettyFly, () -> void) {
printf("EntityPlayer::AddPrettyFly call \n"); // func for lua?
ZHL::Log("EntityPlayer::AddPrettyFly call \n"); // func for lua?
}
*/

/*HOOK_STATIC(Entity_Player, GetMultiShotPositionVelocity, (int loopIndex, WeaponType weaponType, Vector shotDirection, float shotSpeed, Weapon_MultiShotParams multiShotParams) -> PosVel) {
printf("GetMultiShotPositionVelocity hook!\n");
printf("loopIndex %d weaponType %d ShotDirection (%f,%f) shotSpeed %f)", loopIndex, weaponType, shotDirection.x, shotDirection.y, shotSpeed);
ZHL::Log("GetMultiShotPositionVelocity hook!\n");
ZHL::Log("loopIndex %d weaponType %d ShotDirection (%f,%f) shotSpeed %f)", loopIndex, weaponType, shotDirection.x, shotDirection.y, shotSpeed);
return super(loopIndex, weaponType, shotDirection, shotSpeed, multiShotParams);
}*/


/*HOOK_METHOD(PersistentGameData, IncreaseEventCounter, (int eEvent, int num) -> void) {
printf("IncreaseEventCounter %d %d\n", eEvent, num);
ZHL::Log("IncreaseEventCounter %d %d\n", eEvent, num);
super(eEvent, num);
}
*/
Expand All @@ -192,11 +192,11 @@ int Lua_TEST(lua_State* L)
int* y = (int *)(g_Manager + menuid);
float* z = (float *)(g_Manager + menuid);
long* zz = (long *)(g_Manager + menuid);
printf("tests: %s \n", x);
printf("testd: %d \n", *y);
printf("testf: %f \n", *z);
printf("testl: %l \n", *zz);
printf("--------------------- \n");
ZHL::Log("tests: %s \n", x);
ZHL::Log("testd: %d \n", *y);
ZHL::Log("testf: %f \n", *z);
ZHL::Log("testl: %l \n", *zz);
ZHL::Log("--------------------- \n");
return 0;
}
Expand All @@ -217,13 +217,13 @@ HOOK_METHOD(LuaEngine, RegisterClasses, () -> void) {
*/

/*HOOK_METHOD(ShapeRenderer, RenderLine, (Vector* pos1, Vector* pos2, KColor* col1, KColor* col2, const float& thickness) -> int) { // why the FUCK is this float a POINTER
printf("ShapeRenderer::RenderLine hook!\n");
printf("pos1 (%f, %f) pos2 (%f, %f) unk %f\n", pos1->x, pos1->y, pos2->x, pos2->y, thickness);
ZHL::Log("ShapeRenderer::RenderLine hook!\n");
ZHL::Log("pos1 (%f, %f) pos2 (%f, %f) unk %f\n", pos1->x, pos1->y, pos2->x, pos2->y, thickness);
return super(pos1, pos2, col1, col2, thickness);
}*/

/*HOOK_METHOD(PlayerHUDActiveItem, RenderGfx, (SourceQuad* source, DestinationQuad* dest, const ColorMod& color) -> void) {
printf("RenderGfx: %f, %f %f %f %f, %f %f %f\n", source->_topLeft.x, source->_topLeft.y, source->_topRight.x, source->_topRight.y, source->_bottomLeft.x, source->_bottomLeft.y, source->_bottomRight.x, source->_bottomRight.y);
ZHL::Log("RenderGfx: %f, %f %f %f %f, %f %f %f\n", source->_topLeft.x, source->_topLeft.y, source->_topRight.x, source->_topRight.y, source->_bottomLeft.x, source->_bottomLeft.y, source->_bottomRight.x, source->_bottomRight.y);
source->_topLeft.x = source->_topLeft.x + 16.0f;
source->_topRight.x = source->_topRight.x + 16.0f;
source->_bottomLeft.x = source->_bottomLeft.x + 16.0f;
Expand All @@ -234,7 +234,7 @@ HOOK_METHOD(LuaEngine, RegisterClasses, () -> void) {
*/

/*HOOK_STATIC(GridEntity_Rock, SpawnDrops, (const Vector& pos, int GridEntityType, int GridEntityVariant, unsigned int Seed, bool Unk, int BackdropType) -> void) {
printf("GridEntity::SpawnDrops %f %f, %d %d %d %d %d \n", pos.x, pos.y, GridEntityType, GridEntityVariant, Seed, Unk, BackdropType );
ZHL::Log("GridEntity::SpawnDrops %f %f, %d %d %d %d %d \n", pos.x, pos.y, GridEntityType, GridEntityVariant, Seed, Unk, BackdropType );
super(pos, GridEntityType, GridEntityVariant, Seed, Unk, BackdropType);
}
*/
Expand All @@ -243,9 +243,9 @@ extern void LuaReset();
bool netStartInitialized = false; // temp
/*HOOK_METHOD(Manager, ExecuteStartGame, () -> void) {
if (this->GetNetStart() && !netStartInitialized) {
printf("Resetting Lua- please be patient...\n");
ZHL::Log("Resetting Lua- please be patient...\n");
//LuaReset();
printf("Done.\n");
ZHL::Log("Done.\n");
netStartInitialized = true;
}
super();
Expand All @@ -254,9 +254,9 @@ bool netStartInitialized = false; // temp
// Too crashy... let's try earlier.
/*HOOK_METHOD(Game, NetStart, (void* unk, int challenge, Seeds seed, unsigned int difficulty, GameState* state) -> void) {
if (!netStartInitialized) {
printf("Resetting Lua- please be patient...\n");
ZHL::Log("Resetting Lua- please be patient...\n");
LuaReset();
printf("Done.\n");
ZHL::Log("Done.\n");
netStartInitialized = true;
}
Expand All @@ -278,7 +278,7 @@ HOOK_METHOD(LevelGenerator, get_neighbor_candidates, (vector_LevelGenRoom* neigh

/*HOOK_METHOD(Entity, GetCollisionCapsule, (Capsule* capsule, const Vector& vec)-> Capsule*) {
Capsule* ret = super(capsule, vec);
printf("GetCollisionCapsule: %f %f %f %f\n", ret->GetVec1()->x, ret->GetVec1()->y, ret->GetVec2()->x, ret->GetVec2()->y);
ZHL::Log("GetCollisionCapsule: %f %f %f %f\n", ret->GetVec1()->x, ret->GetVec1()->y, ret->GetVec2()->x, ret->GetVec2()->y);
return ret;
}
*/
Expand Down
4 changes: 2 additions & 2 deletions repentogon/ImGuiFeatures/ImGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ void __stdcall RunImGui(HDC hdc) {
imguiInitialized = true;
ImGui::GetIO().FontAllowUserScaling = true;
logViewer.AddLog("[REPENTOGON]", "Initialized Dear ImGui v%s\n", IMGUI_VERSION);
printf("[REPENTOGON] Dear ImGui v%s initialized! Any further logs can be seen in the in-game log viewer.\n", IMGUI_VERSION);
ZHL::Log("[REPENTOGON] Dear ImGui v%s initialized! Any further logs can be seen in the in-game log viewer.\n", IMGUI_VERSION);
}
ImGui_ImplOpenGL2_NewFrame();
ImGui_ImplWin32_NewFrame();
Expand Down Expand Up @@ -651,7 +651,7 @@ void HookImGui() {
SigScan scanner("ffb0????????ffd75f");
scanner.Scan();
void* addr = scanner.GetAddress();
printf("[REPENTOGON] Injecting Dear ImGui at %p\n", addr);
ZHL::Log("[REPENTOGON] Injecting Dear ImGui at %p\n", addr);
void* imguiAddr = &RunImGui;
ASMPatch::SavedRegisters registers(ASMPatch::SavedRegisters::GP_REGISTERS_STACKLESS, true);
ASMPatch patch;
Expand Down
2 changes: 1 addition & 1 deletion repentogon/LuaInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ LUA_FUNCTION(Lua_ToDegrees) {

HOOK_METHOD_PRIORITY(LuaEngine, RegisterClasses, 100, () -> void) {
super();
printf("[REPENTOGON] Registering Lua functions and metatables\n");
ZHL::Log("[REPENTOGON] Registering Lua functions and metatables\n");
lua_State *state = g_LuaEngine->_state;
lua::LuaStackProtector protector(state);
// luaL_openlibs(state);
Expand Down
4 changes: 2 additions & 2 deletions repentogon/LuaInterfaces/Entities/LuaEntityPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2059,9 +2059,9 @@ LUA_FUNCTION(Lua_PlayerReviveCoopGhost) {
Entity_Player* player = lua::GetUserdata<Entity_Player*>(L, 1, lua::Metatables::ENTITY_PLAYER, "EntityPlayer");
bool res = false;

//printf("IsPlayerGhost: %s\n", player->_isCoopGhost ? "TRUE" : "FALSE");
//ZHL::Log("IsPlayerGhost: %s\n", player->_isCoopGhost ? "TRUE" : "FALSE");
if (player->_isCoopGhost) {
//printf("attempting revive");
//ZHL::Log("attempting revive");
player->RevivePlayerGhost();
res = true;
}
Expand Down
4 changes: 2 additions & 2 deletions repentogon/LuaInterfaces/Room/LuaRoom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,10 +483,10 @@ HOOK_METHOD(Room, Init, (int param_1, RoomDescriptor * desc) -> void) {
roomASM.WaterDisabled = false;
roomASM.ItemPool = POOL_NULL;
super(param_1, desc);
//printf("WaterDisabled is %s, stage is %d\n", roomASM.WaterDisabled ? "TRUE" : "FALSE", g_Game->_stage);
//ZHL::Log("WaterDisabled is %s, stage is %d\n", roomASM.WaterDisabled ? "TRUE" : "FALSE", g_Game->_stage);
if (g_Game->_stage == 12 && !roomASM.WaterDisabled && (this->_descriptor->Data->StageId == 27 || this->_descriptor->Data->StageId == 28)) {
this->_waterAmount = 1.0f;
//printf("setting water\n");
//ZHL::Log("setting water\n");
}
}

Expand Down
3 changes: 2 additions & 1 deletion repentogon/LuaInterfaces/Room/LuaRoomConfig.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "IsaacRepentance.h"
#include "Log.h"
#include "LuaCore.h"
#include "HookSystem.h"

Expand Down Expand Up @@ -118,7 +119,7 @@ LUA_FUNCTION(Lua_RoomConfig_GetStage) {
}

RoomConfig_Stage* configStage = &roomConfig->_stages[stage];
printf("%p, %p, %d\n", roomConfig, configStage, configStage->_musicId);
ZHL::Log("%p, %p, %d\n", roomConfig, configStage, configStage->_musicId);

RoomConfig_Stage** ud = (RoomConfig_Stage**)lua_newuserdata(L, sizeof(RoomConfig_Stage*));
*ud = configStage;
Expand Down
7 changes: 4 additions & 3 deletions repentogon/LuaInterfaces/Room/LuaRoomDescriptor.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "IsaacRepentance.h"
#include "Log.h"
#include "LuaCore.h"
#include "HookSystem.h"

Expand Down Expand Up @@ -101,7 +102,7 @@ static void FixRoomDescriptorProperties(lua_State* L) {

if (type != LUA_TTABLE) {
lua_pop(L, 2);
printf("__propget is not a table\n");
ZHL::Log("__propget is not a table\n");
return;
}

Expand All @@ -121,7 +122,7 @@ static void FixRoomDescriptorProperties(lua_State* L) {

if (type != LUA_TTABLE) {
lua_pop(L, 2);
printf("__propget is not a table\n");
ZHL::Log("__propget is not a table\n");
return;
}

Expand All @@ -141,7 +142,7 @@ static void FixRoomDescriptorProperties(lua_State* L) {

if (type != LUA_TTABLE) {
lua_pop(L, 2);
printf("__propset is not a table\n");
ZHL::Log("__propset is not a table\n");
return;
}

Expand Down
5 changes: 3 additions & 2 deletions repentogon/Patches/ASMPatches.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "ASMPatches.h"
#include "Log.h"

#include "../LuaInterfaces/LuaRender.h"
#include "NullItemsAndCostumes.h"
Expand Down Expand Up @@ -40,7 +41,7 @@ void ASMPatchLogMessage() {
scanner.Scan();
void* addr = scanner.GetAddress();

printf("[REPENTOGON] Adding LogMessage callback for ImGui at %p\n", addr);
ZHL::Log("[REPENTOGON] Adding LogMessage callback for ImGui at %p\n", addr);

ASMPatch patch;
patch.AddBytes("\x51") // Push ECX to the stack, so we can restore it as the function expects later
Expand Down Expand Up @@ -69,7 +70,7 @@ void ASMPatchConsoleRunCommand() {
scanner.Scan();
void* addr = scanner.GetAddress();

printf("[REPENTOGON] Patching Console::RunCommand Player requirement at %p\n", addr);
ZHL::Log("[REPENTOGON] Patching Console::RunCommand Player requirement at %p\n", addr);

ASMPatch patch;
patch.AddBytes("\xEB");
Expand Down
Loading

0 comments on commit 357e3c4

Please sign in to comment.