|
| 1 | +use std::{arch::global_asm, ffi::c_void}; |
| 2 | + |
| 3 | +use hooklet::windows::x86::{deploy_rel32_raw, replace_slice_rwx, X86Rel32Type}; |
| 4 | +use log::debug; |
| 5 | +use p3_api::{game_world::GAME_WORLD_PTR, mods}; |
| 6 | + |
| 7 | +const COUNT_BRIBED_COUNCILLORS_PATCH_ADDRESS: u32 = 0x0053AC99; |
| 8 | +static COUNT_BRIBED_COUNCILLORS_CONTINUATION: u32 = 0x0053ACA6; |
| 9 | + |
| 10 | +#[no_mangle] |
| 11 | +pub unsafe extern "C" fn start() -> u32 { |
| 12 | + mods::init_mod(); |
| 13 | + |
| 14 | + debug!("Properly set the councillor's briber to -1 if the bribe failed"); |
| 15 | + match replace_slice_rwx(0x0053AD85, &[0xc6, 0x01, 0xff]) { |
| 16 | + Ok(_) => {} |
| 17 | + Err(_) => return 1, |
| 18 | + } |
| 19 | + |
| 20 | + debug!("Reset annoyed councillors when opening the bath house"); |
| 21 | + match replace_slice_rwx(0x005B17B7, &[0x90, 0x90]) { |
| 22 | + Ok(_) => {} |
| 23 | + Err(_) => return 2, |
| 24 | + } |
| 25 | + |
| 26 | + debug!("Fix the limit of 2 bribed councillors"); |
| 27 | + match deploy_rel32_raw( |
| 28 | + COUNT_BRIBED_COUNCILLORS_PATCH_ADDRESS, |
| 29 | + (&count_bribed_councillors_detour) as *const _ as _, |
| 30 | + X86Rel32Type::Jump, |
| 31 | + ) { |
| 32 | + Ok(_) => {} |
| 33 | + Err(_) => return 3, |
| 34 | + } |
| 35 | + |
| 36 | + 0 |
| 37 | +} |
| 38 | + |
| 39 | +#[no_mangle] |
| 40 | +unsafe extern "thiscall" fn count_bribed_councillors(merchant_index: u8, town_index: u8) -> i32 { |
| 41 | + debug!("count_bribed_councillors(merchant_index={merchant_index:#x}, town_index={town_index:#x})"); |
| 42 | + let town = GAME_WORLD_PTR.get_town(town_index); |
| 43 | + let mut bribed = 0; |
| 44 | + let bribers = town.get_councillor_bribes(); |
| 45 | + for briber in bribers { |
| 46 | + if briber == merchant_index { |
| 47 | + bribed += 1; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + debug!("Found {bribed} local councillors already bribed by the merchant"); |
| 52 | + bribed |
| 53 | +} |
| 54 | + |
| 55 | +extern "C" { |
| 56 | + static count_bribed_councillors_detour: c_void; |
| 57 | +} |
| 58 | + |
| 59 | +global_asm!(" |
| 60 | +.global {count_bribed_councillors_detour} |
| 61 | +{count_bribed_councillors_detour}: |
| 62 | +# save regs except eax and edx (not read) |
| 63 | +push ecx |
| 64 | +
|
| 65 | +push [esi+0x10] |
| 66 | +call {count_bribed_councillors} |
| 67 | +mov edi, eax |
| 68 | +
|
| 69 | +# restore regs |
| 70 | +pop ecx |
| 71 | +
|
| 72 | +jmp [{continuation}] |
| 73 | +", |
| 74 | +count_bribed_councillors_detour = sym count_bribed_councillors_detour, |
| 75 | +count_bribed_councillors = sym count_bribed_councillors, |
| 76 | +continuation = sym COUNT_BRIBED_COUNCILLORS_CONTINUATION); |
0 commit comments