From 7887bf5e95e76a9d7e0de0d3c73c609839cc9dea Mon Sep 17 00:00:00 2001 From: NovaRain Date: Thu, 30 Jan 2025 13:44:04 +0800 Subject: [PATCH] Simplified some jumps in ASM code * Replaced single-use "mov reg, addr; jmp reg" with constant variables. --- sfall/Modules/Animations.cpp | 6 +-- sfall/Modules/BugFixes.cpp | 72 +++++++++++++--------------- sfall/Modules/Explosions.cpp | 21 ++++---- sfall/Modules/ExtraSaveSlots.cpp | 4 +- sfall/Modules/HeroAppearance.cpp | 9 ++-- sfall/Modules/HookScripts/MiscHs.cpp | 4 +- sfall/Modules/LoadOrder.cpp | 7 +-- sfall/Modules/Perks.cpp | 4 +- sfall/Modules/Skills.cpp | 4 +- 9 files changed, 62 insertions(+), 69 deletions(-) diff --git a/sfall/Modules/Animations.cpp b/sfall/Modules/Animations.cpp index b352c3bd..4f2ed21a 100644 --- a/sfall/Modules/Animations.cpp +++ b/sfall/Modules/Animations.cpp @@ -313,6 +313,7 @@ static void __fastcall CheckAppendReg(long, long totalAnims) { } static __declspec(naked) void register_end_hack_begin() { + static const DWORD register_end_begin_Ret = 0x413D14; __asm { mov edx, ds:[FO_VAR_curr_anim_counter]; mov esi, animSet; @@ -324,10 +325,9 @@ isAppend: mov esi, eax; // keep offset to anim_set slot call CheckAppendReg; xor ecx, ecx; - add esp, 4; - mov edx, 0x413D14; mov eax, esi; - jmp edx; + add esp, 4; + jmp register_end_begin_Ret; } } diff --git a/sfall/Modules/BugFixes.cpp b/sfall/Modules/BugFixes.cpp index 18271cd9..f0682d68 100644 --- a/sfall/Modules/BugFixes.cpp +++ b/sfall/Modules/BugFixes.cpp @@ -266,6 +266,8 @@ writeBlock: } static __declspec(naked) void protinst_default_use_item_hack() { + static const DWORD protinst_default_use_item_Ret = 0x49C38B; + static const DWORD protinst_default_use_item_End = 0x49C3C5; __asm { mov eax, dword ptr [edx + protoId]; // eax = target pid cmp eax, PID_DRIVABLE_CAR; @@ -278,15 +280,13 @@ isCar: cmp eax, -1; jne skip; notCar: - push 0x49C38B; - retn; // "That does nothing." + jmp protinst_default_use_item_Ret; // "That does nothing." skip: test eax, eax; jnz end; dec eax; end: - push 0x49C3C5; - retn; + jmp protinst_default_use_item_End; } } @@ -656,14 +656,14 @@ end: } static __declspec(naked) void PrintBasicStat_hack() { + static const DWORD PrintBasicStat_Ret = 0x434C21; __asm { test eax, eax; jle skip; cmp eax, 10; jg end; add esp, 4; // Destroy the return address - push 0x434C21; - retn; + jmp PrintBasicStat_Ret; skip: xor eax, eax; end: @@ -687,17 +687,18 @@ end: } static __declspec(naked) void StatButtonDown_hook() { + static const DWORD StatButtonDown_Ret = 0x437B41; __asm { call fo::funcoffs::stat_level_; cmp eax, 1; - jg end; + jle skip; + retn; +skip: add esp, 4; // Destroy the return address xor eax, eax; inc eax; mov [esp + 0xC], eax; - push 0x437B41; -end: - retn; + jmp StatButtonDown_Ret; } } @@ -838,6 +839,9 @@ static __declspec(naked) void inven_pickup_hack() { } static __declspec(naked) void inven_pickup_hack2() { + static const DWORD inven_pickup_End = 0x47125C; + static const DWORD inven_pickup_Ret1 = 0x471181; + static const DWORD inven_pickup_Ret2 = 0x4711DF; __asm { test eax, eax; jz end; @@ -868,12 +872,10 @@ next: jnz found; inc edx; cmp edx, ds:[FO_VAR_inven_cur_disp]; - jb next; + jl next; end: - push 0x47125C; - retn; + jmp inven_pickup_End; found: - mov ebx, 0x4711DF; add edx, [esp + 0x40]; // inventory_offset mov eax, ds:[FO_VAR_pud]; mov ecx, [eax]; // itemsCount @@ -881,12 +883,12 @@ found: jz skip; dec ecx; cmp edx, ecx; - ja skip; + jg skip; sub ecx, edx; mov edx, ecx; - mov ebx, 0x471181; + jmp inven_pickup_Ret1; skip: - jmp ebx; + jmp inven_pickup_Ret2; } } @@ -1241,6 +1243,7 @@ delay: } static __declspec(naked) void dude_standup_hook() { + static const DWORD dude_standup_Ret = 0x4185AD; __asm { mov edx, [ecx + artFid]; and edx, 0xFF0000; @@ -1252,8 +1255,7 @@ standup: jmp fo::funcoffs::register_begin_; skip: add esp, 4; - mov edx, 0x4185AD; - jmp edx; + jmp dude_standup_Ret; } } @@ -1333,14 +1335,14 @@ skip: } static __declspec(naked) void barter_attempt_transaction_hack() { + static const DWORD barter_attempt_transaction_Ret = 0x474D34; __asm { mov edx, [eax + protoId]; cmp edx, PID_ACTIVE_GEIGER_COUNTER; je found; cmp edx, PID_ACTIVE_STEALTH_BOY; je found; - mov eax, 0x474D34; // Can't sell - jmp eax; + jmp barter_attempt_transaction_Ret; // Can't sell found: push 0x474D17; // Is there any other activated items among the ones being sold? jmp fo::funcoffs::item_m_turn_off_; @@ -1435,21 +1437,21 @@ end: } static __declspec(naked) void gdActivateBarter_hook() { + static const DWORD gdActivateBarter_Ret = 0x44A5CC; __asm { call fo::funcoffs::gdialog_barter_pressed_; cmp ds:[FO_VAR_dialogue_state], ecx; jne skip; cmp ds:[FO_VAR_dialogue_switch_mode], esi; - je end; + jne skip; + retn; skip: push ecx; push esi; push edi; push ebp; sub esp, 0x18; - push 0x44A5CC; -end: - retn; + jmp gdActivateBarter_Ret; } } @@ -1729,14 +1731,6 @@ static bool showItemDescription = false; static void __stdcall AppendText(const char* text, const char* desc) { if (showItemDescription && currDescLen == 0) { -// std::string descMsg = desc; -// size_t pos = 0; - -// while ((pos = descMsg.find("\\n", pos)) != std::string::npos) { -// descMsg.replace(pos, 2, " "); -// } - -// strncpy_s(messageBuffer, descMsg.c_str(), 161); if (desc == nullptr) { desc = fo::util::MessageSearch(&fo::var::proto_main_msg_file, 493); } @@ -2274,6 +2268,7 @@ static __declspec(naked) void combat_attack_hack() { } static __declspec(naked) void op_use_obj_on_obj_hack() { + static const DWORD op_use_obj_on_obj_Ret = 0x45C3A3; __asm { test eax, eax; // source jz fail; @@ -2286,12 +2281,12 @@ skip: retn; fail: add esp, 4; - mov edx, 0x45C3A3; // exit func - jmp edx; + jmp op_use_obj_on_obj_Ret; // exit func } } static __declspec(naked) void op_use_obj_hack() { + static const DWORD op_use_obj_Ret = 0x456ABA; __asm { test eax, eax; // source jz fail; @@ -2300,8 +2295,7 @@ static __declspec(naked) void op_use_obj_hack() { retn; fail: add esp, 4; - mov edx, 0x456ABA; // exit func - jmp edx; + jmp op_use_obj_Ret; // exit func } } @@ -2709,6 +2703,7 @@ largeLoc: } static __declspec(naked) void wmTownMapFunc_hack() { + static const DWORD wmTownMapFunc_Ret = 0x4C4976; __asm { cmp dword ptr [edi][eax * 4 + 0], 0; // Visited je end; @@ -2722,8 +2717,7 @@ static __declspec(naked) void wmTownMapFunc_hack() { retn; end: add esp, 4; // destroy the return address - mov eax, 0x4C4976; - jmp eax; + jmp wmTownMapFunc_Ret; } } diff --git a/sfall/Modules/Explosions.cpp b/sfall/Modules/Explosions.cpp index 332c7010..21c04d31 100644 --- a/sfall/Modules/Explosions.cpp +++ b/sfall/Modules/Explosions.cpp @@ -70,7 +70,6 @@ skip: static DWORD explosion_effect_starting_dir = 0; static __declspec(naked) void explosion_effect_hook() { - static const DWORD explosion_effect_hook_back = 0x411AB9; __asm { mov bl, lightingEnabled; test bl, bl; @@ -85,13 +84,13 @@ next: mov al, lightingEnabled; test al, al; jz skiplight; - mov eax, [esp + 40]; // projectile ptr - 1st arg - mov edx, 0xFFFF0008; // maximum radius + intensity (see anim_set_check_light_fix) + mov eax, [esp + 40 + 4]; // projectile ptr - 1st arg + mov edx, 0xFFFF0008; // maximum radius + intensity (see anim_set_check_light_fix) xor ebx, ebx; call fo::funcoffs::register_object_light_; skiplight: mov edi, explosion_effect_starting_dir; // starting direction - jmp explosion_effect_hook_back; // jump back + retn; } } @@ -144,8 +143,7 @@ end: } // enable lighting for burning poor guy -static __declspec(naked) void fire_dance_lighting_fix1() { - static const DWORD fire_dance_lighting_back = 0x410A4F; +static __declspec(naked) void fire_dance_lighting_fix() { __asm { push edx; push ebx; @@ -160,8 +158,7 @@ static __declspec(naked) void fire_dance_lighting_fix1() { mov eax, esi; // projectile ptr - 1st arg mov edx, 0x00010000; // maximum radius + intensity (see anim_set_check_light_fix) mov ebx, -1; - call fo::funcoffs::register_object_light_; - jmp fire_dance_lighting_back; // jump back + jmp fo::funcoffs::register_object_light_; } } @@ -479,14 +476,14 @@ static void ResetExplosionDamage() { } void Explosions::init() { - MakeJump(0x411AB4, explosion_effect_hook); // required for explosions_metarule + HookCall(0x411AB4, explosion_effect_hook); // required for explosions_metarule lightingEnabled = IniReader::GetConfigInt("Misc", "ExplosionsEmitLight", 0) != 0; if (lightingEnabled) { dlogr("Applying Explosion changes.", DL_INIT); - MakeJump(0x4118E1, ranged_attack_lighting_fix); - MakeJump(0x410A4A, fire_dance_lighting_fix1); - MakeJump(0x415A3F, anim_set_check_light_fix); // this allows to change light intensity + MakeJump(0x4118E1, ranged_attack_lighting_fix, 1); + HookCall(0x410A4A, fire_dance_lighting_fix); + MakeJump(0x415A3F, anim_set_check_light_fix, 2); // this allows to change light intensity } // initialize explosives diff --git a/sfall/Modules/ExtraSaveSlots.cpp b/sfall/Modules/ExtraSaveSlots.cpp index 7d85c9ef..ab998960 100644 --- a/sfall/Modules/ExtraSaveSlots.cpp +++ b/sfall/Modules/ExtraSaveSlots.cpp @@ -342,10 +342,10 @@ static __declspec(naked) void add_page_offset_hack1(void) { // getting info for the 10 currently displayed save slots from save.dats static __declspec(naked) void add_page_offset_hack2(void) { __asm { + pop edx; // ret addr push 0x50A514; // ASCII "SAVE.DAT" lea eax, [ebx + 1]; add eax, LSPageOffset; // add page num offset - mov edx, 0x47E5E9; // ret addr jmp edx; } } @@ -389,7 +389,7 @@ static void EnableSuperSaving() { 0x480767, 0x4807E6, 0x480839, 0x4808D3 // EraseSave_ }); - MakeJump(0x47E5E1, add_page_offset_hack2); // GetSlotList_ + MakeCall(0x47E5E1, add_page_offset_hack2, 3); // GetSlotList_ MakeCall(0x47E756, add_page_offset_hack3); // ShowSlotList_ } diff --git a/sfall/Modules/HeroAppearance.cpp b/sfall/Modules/HeroAppearance.cpp index 09e81046..52cd2a85 100644 --- a/sfall/Modules/HeroAppearance.cpp +++ b/sfall/Modules/HeroAppearance.cpp @@ -243,6 +243,7 @@ isNotReading: } static __declspec(naked) void CheckHeroExist() { + static const DWORD CheckHeroExist_Back = 0x4194E2; __asm { cmp esi, critterArraySize; // check if loading hero art jg checkArt; @@ -257,8 +258,7 @@ checkArt: notExists: // if file not found load regular critter art instead sub esi, critterArraySize; add esp, 4; // drop func ret address - mov eax, 0x4194E2; - jmp eax; + jmp CheckHeroExist_Back; } } @@ -1015,14 +1015,15 @@ static int __stdcall CheckCharButtons() { } static __declspec(naked) void CheckCharScrnButtons() { + static const DWORD CheckCharScrnButtons_Back = 0x431E8A; __asm { call CheckCharButtons; cmp eax, 0x500; jl endFunc; cmp eax, 0x515; jg endFunc; - add esp, 4; // ditch old ret addr - push 0x431E8A; // recheck buttons if app mod button + add esp, 4; // ditch old ret addr + jmp CheckCharScrnButtons_Back; // recheck buttons if app mod button endFunc: retn; } diff --git a/sfall/Modules/HookScripts/MiscHs.cpp b/sfall/Modules/HookScripts/MiscHs.cpp index 0aca21f3..af5bfb23 100644 --- a/sfall/Modules/HookScripts/MiscHs.cpp +++ b/sfall/Modules/HookScripts/MiscHs.cpp @@ -632,6 +632,7 @@ static __declspec(naked) void wmWorldMap_hook() { } static __declspec(naked) void wmRndEncounterOccurred_hook() { + static const DWORD wmRndEncounterOccurred_Ret = 0x4C0BC7; static long hkEncounterMapID = -1; __asm { cmp hkEncounterMapID, -1; @@ -678,8 +679,7 @@ cancelEnc: inc eax; // 0 - continue movement, 1 - interrupt mov dword ptr ds:[FO_VAR_wmEncounterIconShow], 0; add esp, 4; - mov ebx, 0x4C0BC7; - jmp ebx; + jmp wmRndEncounterOccurred_Ret; } } diff --git a/sfall/Modules/LoadOrder.cpp b/sfall/Modules/LoadOrder.cpp index 2fc4f420..70552430 100644 --- a/sfall/Modules/LoadOrder.cpp +++ b/sfall/Modules/LoadOrder.cpp @@ -86,9 +86,10 @@ default: } static __declspec(naked) void scr_get_dialog_msg_file_hack2() { + static const DWORD scr_get_dialog_msg_file_Ret = 0x4A6C0E; + static const DWORD scr_get_dialog_msg_file_Error = 0x4A6BFA; __asm { cmp eax, 1; // checking existence of msg file - mov eax, 0x4A6C0E; jz exist; cmp femaleCheck, 1; jnz error; // no exist default msg file @@ -97,9 +98,9 @@ static __declspec(naked) void scr_get_dialog_msg_file_hack2() { mov femaleCheck, 0; // reset flag jmp scr_get_dialog_msg_file_Back; // check default msg file error: - mov eax, 0x4A6BFA; // jump to Error + jmp scr_get_dialog_msg_file_Error; exist: - jmp eax; + jmp scr_get_dialog_msg_file_Ret; } } diff --git a/sfall/Modules/Perks.cpp b/sfall/Modules/Perks.cpp index f7583f93..f809894a 100644 --- a/sfall/Modules/Perks.cpp +++ b/sfall/Modules/Perks.cpp @@ -805,7 +805,7 @@ end: } } -static __declspec(naked) void HeaveHoHook() { +static __declspec(naked) void HeaveHoHack() { using namespace fo; __asm { xor edx, edx; @@ -831,7 +831,7 @@ static __declspec(naked) void HeaveHoHook() { bool Perks::perkHeaveHoModTweak = false; void __stdcall Perks::ApplyHeaveHoFix() { // not really a fix - MakeJump(0x478AC4, HeaveHoHook); + MakeJump(0x478AC4, HeaveHoHack); perks[fo::Perk::PERK_heave_ho].strengthMin = 0; perkHeaveHoModTweak = true; } diff --git a/sfall/Modules/Skills.cpp b/sfall/Modules/Skills.cpp index bc852d67..47e8ad7f 100644 --- a/sfall/Modules/Skills.cpp +++ b/sfall/Modules/Skills.cpp @@ -121,12 +121,12 @@ static int __fastcall SkillNegative(fo::GameObject* critter, int base, int skill } static __declspec(naked) void skill_level_hack() { + static const DWORD skill_level_Ret = 0x4AA64B; __asm { push ebx; // skill mov edx, esi; // level skill (base) call SkillNegative; // ecx - critter - mov edx, 0x4AA64B; - jmp edx; + jmp skill_level_Ret; } }