client_runtime_lifecycle.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "../../core/logging/log.h"
  2. #include "../../core/settings/settings.h"
  3. #include "../content/activity/activity_sdk_generation_worker.h"
  4. #include "../content/activity/scriptable_catalog_worker.h"
  5. #include "../content/investment/worker.h"
  6. #include "../hooks/assert_handler/assert_handler_lifecycle.h"
  7. #include "../hooks/async_io/async_io_lifetime_guard.h"
  8. #include "../hooks/bitmap/bitmap_hook_lifecycle.h"
  9. #include "../hooks/bootflow/bootflow_hook_lifecycle.h"
  10. #include "../hooks/bootflow/bootflow_texture_override.h"
  11. #include "../hooks/config_getter/config_getter_lifecycle.h"
  12. #include "../hooks/cursor/runtime.h"
  13. #include "../hooks/graphics/graphics_hook_lifecycle.h"
  14. #include "../hooks/inactivity/inactivity_override.h"
  15. #include "../hooks/infinite_ammo/infinite_ammo.h"
  16. #include "../hooks/membership_probe/membership_probe.h"
  17. #include "../hooks/network/runtime.h"
  18. #include "../hooks/noclip/runtime.h"
  19. #include "../hooks/package_trust/package_trust_bypass.h"
  20. #include "../hooks/polled_input/runtime.h"
  21. #include "../hooks/queuez/queuez_hook_lifecycle.h"
  22. #include "../hooks/retail_log/retail_log_lifecycle.h"
  23. #include "../hooks/teleport/runtime.h"
  24. #include "../hooks/world_objects/world_object_registry.h"
  25. #include "../inactivity/inactivity_settings_store.h"
  26. #include "../movement/movement_settings_store.h"
  27. #include "../player/player_settings_store.h"
  28. #include "../targets/game.h"
  29. #include "../targets/steam_targets.h"
  30. #include "../ui/activity/authored_placement_marker.h"
  31. #include "../ui/runtime/client_ui_module_runtime.h"
  32. #include "internal.h"
  33. #include "runtime.h"
  34. namespace sunrise::client {
  35. /** Initializes Client-owned process state without installing hooks. */
  36. bool initialize(void* module) noexcept {
  37. const core::settings::ActivitySdkGenerationSettings& generation =
  38. core::settings::get().activitySdkGeneration;
  39. content::activity::sdk_generation::initialize(module,
  40. {generation.enabled, generation.luaDeclarations});
  41. // Kept for activation, which resolves the artifact directory from Sunrise's own module.
  42. runtime::g_sunriseModule = module;
  43. // Loaded before the pages register, so each page draws saved values on its first frame.
  44. movement::initialize(module);
  45. player::initialize(module);
  46. inactivity::initialize(module);
  47. ui::activity::authored_placement_marker::initialize(module);
  48. return ui::runtime::initialize();
  49. }
  50. /** Detaches Client hooks before clearing their resolved target entries. */
  51. bool shutdown() noexcept {
  52. AcquireSRWLockExclusive(&runtime::g_lock);
  53. if (!hooks::graphics::uninstall()) {
  54. core::log::write(core::log::Channel::client,
  55. core::log::Level::error,
  56. "ev=shutdown stage=graphics_hooks result=fail");
  57. ReleaseSRWLockExclusive(&runtime::g_lock);
  58. return false;
  59. }
  60. // Detached after presentation, so no later frame can apply the cursor policy.
  61. hooks::cursor::uninstall();
  62. hooks::polled_input::uninstall();
  63. if (!hooks::world_objects::uninstall()) {
  64. core::log::write(core::log::Channel::client,
  65. core::log::Level::error,
  66. "ev=shutdown stage=world_objects result=fail");
  67. ReleaseSRWLockExclusive(&runtime::g_lock);
  68. return false;
  69. }
  70. if (!hooks::network::uninstall()) {
  71. core::log::write(core::log::Channel::client,
  72. core::log::Level::error,
  73. "ev=shutdown stage=network_hooks result=fail");
  74. ReleaseSRWLockExclusive(&runtime::g_lock);
  75. return false;
  76. }
  77. if (!hooks::bootflow::texture_override::uninstall()) {
  78. core::log::write(core::log::Channel::client,
  79. core::log::Level::error,
  80. "ev=shutdown stage=bootflow_texture result=fail");
  81. ReleaseSRWLockExclusive(&runtime::g_lock);
  82. return false;
  83. }
  84. if (!hooks::package_trust::uninstall()) {
  85. core::log::write(core::log::Channel::client,
  86. core::log::Level::error,
  87. "ev=shutdown stage=package_trust result=fail");
  88. ReleaseSRWLockExclusive(&runtime::g_lock);
  89. return false;
  90. }
  91. // Attached last, so it detaches first. The probe reads through a detour, so one left in
  92. // place is a branch into code a later unload unmaps.
  93. if (!hooks::membership_probe::uninstall()) {
  94. core::log::write(core::log::Channel::client,
  95. core::log::Level::error,
  96. "ev=shutdown stage=membership_probe result=fail");
  97. ReleaseSRWLockExclusive(&runtime::g_lock);
  98. return false;
  99. }
  100. hooks::bitmap::uninstall();
  101. hooks::bootflow::uninstall();
  102. hooks::infinite_ammo::uninstall();
  103. hooks::inactivity::uninstall();
  104. hooks::noclip::uninstall();
  105. hooks::teleport::uninstall();
  106. hooks::queuez::uninstall();
  107. if (!hooks::config_getter::uninstall()) {
  108. ReleaseSRWLockExclusive(&runtime::g_lock);
  109. return false;
  110. }
  111. if (!hooks::assert_handler::uninstall()) {
  112. ReleaseSRWLockExclusive(&runtime::g_lock);
  113. return false;
  114. }
  115. if (!hooks::retail_log::uninstall()) {
  116. core::log::write(core::log::Channel::client,
  117. core::log::Level::error,
  118. "ev=shutdown stage=retail_log result=fail");
  119. ReleaseSRWLockExclusive(&runtime::g_lock);
  120. return false;
  121. }
  122. content::activity::sdk_generation::reset();
  123. content::activity::scriptables::reset();
  124. content::investment::worker::reset();
  125. (void)hooks::async_io::uninstall();
  126. targets::steam::clear();
  127. if (runtime::g_platformModule != nullptr) {
  128. if (FreeLibrary(runtime::g_platformModule) == FALSE) {
  129. core::log::write(core::log::Channel::client,
  130. core::log::Level::error,
  131. "ev=shutdown stage=steam_module result=fail");
  132. ReleaseSRWLockExclusive(&runtime::g_lock);
  133. return false;
  134. }
  135. runtime::g_platformModule = nullptr;
  136. }
  137. targets::game::retail_log::clear();
  138. targets::game::content::clear();
  139. targets::game::network::clear();
  140. runtime::g_mainStage = runtime::StageState::pending;
  141. runtime::g_graphicsStage = runtime::StageState::pending;
  142. runtime::g_platformStage = runtime::StageState::pending;
  143. ui::runtime::shutdown();
  144. // The reverse of the order the stores initialize in.
  145. ui::activity::authored_placement_marker::shutdown();
  146. inactivity::shutdown();
  147. player::shutdown();
  148. movement::shutdown();
  149. core::log::write(core::log::Channel::client, core::log::Level::info, "ev=shutdown result=ok");
  150. ReleaseSRWLockExclusive(&runtime::g_lock);
  151. return true;
  152. }
  153. } // namespace sunrise::client