Skip to content

Commit

Permalink
Make spear autoaim not prefer enemies
Browse files Browse the repository at this point in the history
  • Loading branch information
timoschwarzer committed May 31, 2024
1 parent 370291b commit 32c2a94
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
11 changes: 11 additions & 0 deletions projects/Modloader/windows_api/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,15 @@ namespace modloader::win::memory {
uint64_t resolve_unity_player_rva(uint64_t rva) {
return get_unity_player_address() + rva;
}

void modify_memory(uint64_t address, uint64_t length, const std::function<void(uint8_t* memory)>& fn) {
auto out = 0UL;
const auto address_ptr = reinterpret_cast<void*>(address);
VirtualProtect(address_ptr, length, PAGE_EXECUTE_READWRITE, &out);

fn(reinterpret_cast<uint8_t*>(address));

VirtualProtect(address_ptr, length, PAGE_EXECUTE_READ, &out);
FlushInstructionCache(GetCurrentProcess(), address_ptr, length);
}
} // namespace modloader::win::memory
8 changes: 7 additions & 1 deletion projects/Modloader/windows_api/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

#include <Modloader/macros.h>
#include <cstdint>
#include <functional>

namespace modloader::win::memory {
enum class Instruction: uint8_t {
NOP = 0x90,
};

IL2CPP_MODLOADER_DLLEXPORT uint64_t get_game_assembly_address();
IL2CPP_MODLOADER_DLLEXPORT uint64_t get_unity_player_address();
IL2CPP_MODLOADER_DLLEXPORT uint64_t resolve_rva(uint64_t rva);
IL2CPP_MODLOADER_DLLEXPORT uint64_t unresolve_rva(uint64_t ptr);
IL2CPP_MODLOADER_DLLEXPORT uint64_t resolve_unity_player_rva(uint64_t rva);
}
IL2CPP_MODLOADER_DLLEXPORT void modify_memory(uint64_t address, uint64_t length, const std::function<void(uint8_t* memory)>& fn);
}
20 changes: 18 additions & 2 deletions projects/Randomizer/features/controls/autoaim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
#include <Modloader/app/methods/Game/Targets.h>
#include <Modloader/app/methods/SeinBowAttack.h>
#include <Modloader/app/methods/SeinChakramSpell.h>
#include <Modloader/app/methods/SeinSpiritSpearSpell.h>
#include <Modloader/app/types/IAttackable.h>
#include <Modloader/il2cpp_helpers.h>
#include <Modloader/interception_macros.h>
#include <Modloader/modloader.h>

Expand All @@ -23,6 +21,24 @@ namespace {
// overwrite_attackables = false;
// }

[[maybe_unused]]
auto on_injection_complete = modloader::event_bus().register_handler(ModloaderEvent::InjectionComplete, [](auto) {
// In vanilla, the game divides the distance to enemies by 10, making the spear autoaim heavily prefer targeting them
// over anything else. The DIVSS instruction lives at 0xa74431, and here we replace it with NOP instructions to
// disable that behavior.
modloader::win::memory::modify_memory(
modloader::win::memory::resolve_rva(0xa74431),
5,
[](auto memory) {
memory[0] = static_cast<uint8_t>(modloader::win::memory::Instruction::NOP);
memory[1] = static_cast<uint8_t>(modloader::win::memory::Instruction::NOP);
memory[2] = static_cast<uint8_t>(modloader::win::memory::Instruction::NOP);
memory[3] = static_cast<uint8_t>(modloader::win::memory::Instruction::NOP);
memory[4] = static_cast<uint8_t>(modloader::win::memory::Instruction::NOP);
}
);
});

IL2CPP_INTERCEPT(SeinChakramSpell, void, UpdateCharacterState, (app::SeinChakramSpell * this_ptr)) {
this_ptr->fields.AutoAimEnabled = core::settings::autoaim();
// Maybe we still want this on?
Expand Down

0 comments on commit 32c2a94

Please sign in to comment.