Closed MegAmi24 closed 5 months ago
Have you stepped through the code using a debugger like Cheat Engine?
Unmanaged functions are a bit of a hack, as .NET doesn't support the usual calling convention used by x64 (__fastcall
), so you might have to preserve any registers before the function gets called yourself and restore them later.
You can check which registers are being affected by placing a breakpoint before and after the call to your unmanaged function and checking which registers have changed post-call. Whichever registers have changed, try pushing them pre-call and popping them afterwards. It's possible code after your call could be relying on the original data in these registers.
An alternative method to what you're trying to do would be to do this check in your ASM instead. For example;
fixed (bool* p_isInitialised = &_isInitialised)
{
WriteAsmHook
(
@$"
push rdi
mov rdi, {(long)p_isInitialised}
cmp byte ptr [rdi], 1
je exit
mov rdi, {GET_UNMANAGED_FUNCTION_PTR(SetV3GlobalVariables)}
call rdi
exit:
pop rdi
",
(nint)(Memory.ReadEffectiveAddress(sig + 20)),
HookBehavior.After
);
}
Did a check and there were indeed a bunch of registers getting changed. I'll try the push and pop thing next time I run into something like this, but the alternative method you gave works fine so I'll use that for this. Thanks for the response!
I've been working on updating the
Bridge.SetGlobalVariables
library in Sonic Origins, however I've been running into an issue with unmanaged functions. I've discovered that the code that my ASM hook is hooked into runs every frame, so I tried to implement an initialization check so that the unmanaged function only runs once per time an RSDK game is opened. However, no matter what I try, having a check that I'm 100% confident should work simply crashes the game when the unmanaged function runs with_isInitialised
set to true. Even trying to wrap all of my function's code in an initialization check instead of usingreturn
results in a crash. I've done some testing and could not determine what the cause of the crash could be; removing the check for_isInitialised
or replacing itsreturn
with a write to the console log seems to not crash, but of course, that's not what I'm trying to do with my code.Here's what I currently have for the function. I've been testing this by directly replacing the code in
Codes.hmm
. Also included is a blank code for loading the library.