Skip to content

Commit

Permalink
Simplified some jumps in ASM code
Browse files Browse the repository at this point in the history
* Replaced single-use "mov reg, addr; jmp reg" with constant variables.
  • Loading branch information
NovaRain committed Jan 30, 2025
1 parent 1cbca9c commit 7887bf5
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 69 deletions.
6 changes: 3 additions & 3 deletions sfall/Modules/Animations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -324,10 +325,9 @@ static __declspec(naked) void register_end_hack_begin() {
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;
}
}

Expand Down
72 changes: 33 additions & 39 deletions sfall/Modules/BugFixes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ static __declspec(naked) void scr_write_ScriptNode_hook() {
}

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;
Expand All @@ -278,15 +280,13 @@ static __declspec(naked) void protinst_default_use_item_hack() {
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;
}
}

Expand Down Expand Up @@ -656,14 +656,14 @@ static __declspec(naked) void is_supper_bonus_hack() {
}

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:
Expand All @@ -687,17 +687,18 @@ static __declspec(naked) void StatButtonUp_hook() {
}

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;
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -868,25 +872,23 @@ static __declspec(naked) void inven_pickup_hack2() {
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
test ecx, ecx;
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;
}
}

Expand Down Expand Up @@ -1241,6 +1243,7 @@ static __declspec(naked) void combat_over_hack() {
}

static __declspec(naked) void dude_standup_hook() {
static const DWORD dude_standup_Ret = 0x4185AD;
__asm {
mov edx, [ecx + artFid];
and edx, 0xFF0000;
Expand All @@ -1252,8 +1255,7 @@ static __declspec(naked) void dude_standup_hook() {
jmp fo::funcoffs::register_begin_;
skip:
add esp, 4;
mov edx, 0x4185AD;
jmp edx;
jmp dude_standup_Ret;
}
}

Expand Down Expand Up @@ -1333,14 +1335,14 @@ static __declspec(naked) void barter_attempt_transaction_hook_weight() {
}

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_;
Expand Down Expand Up @@ -1435,21 +1437,21 @@ static __declspec(naked) void db_get_file_list_hack() {
}

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;
}
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -2286,12 +2281,12 @@ static __declspec(naked) void op_use_obj_on_obj_hack() {
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;
Expand All @@ -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
}
}

Expand Down Expand Up @@ -2709,6 +2703,7 @@ static __declspec(naked) void wmWorldMap_hack() {
}

static __declspec(naked) void wmTownMapFunc_hack() {
static const DWORD wmTownMapFunc_Ret = 0x4C4976;
__asm {
cmp dword ptr [edi][eax * 4 + 0], 0; // Visited
je end;
Expand All @@ -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;
}
}

Expand Down
21 changes: 9 additions & 12 deletions sfall/Modules/Explosions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ static __declspec(naked) void ranged_attack_lighting_fix() {
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;
Expand All @@ -85,13 +84,13 @@ static __declspec(naked) void explosion_effect_hook() {
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;
}
}

Expand Down Expand Up @@ -144,8 +143,7 @@ static __declspec(naked) void anim_set_check_light_fix() {
}

// 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;
Expand All @@ -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_;
}
}

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions sfall/Modules/ExtraSaveSlots.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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_
}
Expand Down
9 changes: 5 additions & 4 deletions sfall/Modules/HeroAppearance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ static __declspec(naked) void LoadNewHeroArt() {
}

static __declspec(naked) void CheckHeroExist() {
static const DWORD CheckHeroExist_Back = 0x4194E2;
__asm {
cmp esi, critterArraySize; // check if loading hero art
jg checkArt;
Expand All @@ -257,8 +258,7 @@ static __declspec(naked) void CheckHeroExist() {
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;
}
}

Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions sfall/Modules/HookScripts/MiscHs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -678,8 +679,7 @@ static __declspec(naked) void wmRndEncounterOccurred_hook() {
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;
}
}

Expand Down
Loading

0 comments on commit 7887bf5

Please sign in to comment.