spawn_hold.cpp 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include <atomic>
  2. #include <cstdint>
  3. #include <string_view>
  4. #include "../../../core/logging/log.h"
  5. #include "../../../core/settings/settings.h"
  6. #include "../../../state/activity/runtime.h"
  7. #include "../../hooking/detour.h"
  8. #include "internal.h"
  9. namespace sunrise::client::hooks::bootflow {
  10. namespace {
  11. /**
  12. * The player spawn gate. Anchored on the load of the encrypted manager global, then run on
  13. * through the stack-cookie store because the wildcarded frame size leaves the head too short.
  14. */
  15. constexpr std::string_view kSpawnGateSignatureText =
  16. "40 53 57 41 57 48 81 EC ? ? ? ? 48 8B 05 ? ? ? ? 48 33 C4 48 89 84 24 ? ? ? ? 8B D9 "
  17. "40 B7 01";
  18. /** Compiled pattern bytes of the signature text above. */
  19. constexpr auto kSpawnGateSignature =
  20. signature<signature_length(kSpawnGateSignatureText)>(kSpawnGateSignatureText);
  21. /** Answer that holds the spawn for this tick. The gate is polled, so a refusal only delays it. */
  22. constexpr bool kHeld = false;
  23. using SpawnGate = bool(__fastcall*)(std::int32_t) noexcept;
  24. hooking::detour::Handle g_handle{};
  25. std::atomic<SpawnGate> g_original{nullptr};
  26. /**
  27. * Puts the spawn after the world-transition fade is armed.
  28. * A release on a channel that is not up does nothing, so a spawn during the load leaves the
  29. * screen black. The client's own predicate reads a host field this destination never fills.
  30. * @param datum Borrowed player datum handle; the answer does not depend on it.
  31. * @return The native answer, or held while a destination load is still running.
  32. */
  33. __declspec(noinline) bool __fastcall spawn_gate(std::int32_t datum) noexcept {
  34. const SpawnGate original = g_original.load(std::memory_order_acquire);
  35. const bool allowed = original != nullptr && original(datum);
  36. observe_world_step();
  37. const state::activity::WorldPhase phase = state::activity::world_phase();
  38. const bool transitioning = phase == state::activity::WorldPhase::transitioning;
  39. // Zero unless a load is running.
  40. const std::uint64_t age = state::activity::world_transition_age();
  41. const core::settings::client::Settings& client = core::settings::get().client;
  42. const bool gaveUp = age >= client.spawnHoldMs;
  43. const bool loading = transitioning && !gaveUp && client.holdSpawn;
  44. // Release only on arrival. The step-37 exit re-arms the fade unless one is already up, and
  45. // nothing polls this gate after the spawn, so an early release leaves a fade nobody clears.
  46. if (phase == state::activity::WorldPhase::arrived) {
  47. release_world_fade();
  48. }
  49. return allowed && loading ? kHeld : allowed;
  50. }
  51. } // namespace
  52. /** Attaches the spawn hold. */
  53. bool install_spawn_hold() noexcept {
  54. if (g_handle.attached) {
  55. return true;
  56. }
  57. std::byte* const target = scan_main_image_unique(kSpawnGateSignature, "player_spawn_gate");
  58. if (target == nullptr) {
  59. core::log::write(core::log::Channel::client,
  60. core::log::Level::warn,
  61. "ev=bootflow stage=spawn_hold result=fail reason=target");
  62. return false;
  63. }
  64. const hooking::detour::Spec spec{target, reinterpret_cast<void*>(&spawn_gate)};
  65. if (!hooking::detour::install(spec, g_handle)) {
  66. core::log::write(core::log::Channel::client,
  67. core::log::Level::warn,
  68. "ev=bootflow stage=spawn_hold result=fail reason=attach");
  69. return false;
  70. }
  71. g_original.store(reinterpret_cast<SpawnGate>(g_handle.original), std::memory_order_release);
  72. core::log::write(core::log::Channel::client,
  73. core::log::Level::info,
  74. "ev=bootflow stage=spawn_hold result=ok");
  75. return true;
  76. }
  77. /** Detaches the spawn hold. */
  78. void uninstall_spawn_hold() noexcept {
  79. if (g_handle.attached) {
  80. (void)hooking::detour::uninstall(g_handle);
  81. }
  82. g_original.store(nullptr, std::memory_order_release);
  83. }
  84. } // namespace sunrise::client::hooks::bootflow