dllmain.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <Windows.h>
  2. #include <intrin.h>
  3. #include "client/hooks/egress/runtime.h"
  4. #include "core/runtime/core_runtime.h"
  5. #include "steam/runtime/internal.h"
  6. #include "steam/runtime/runtime.h"
  7. namespace {
  8. HMODULE g_module{};
  9. }
  10. /** @return True when the egress guard and Core initialize from this DLL module. */
  11. extern "C" __declspec(dllexport) bool SunriseInitialize() noexcept {
  12. if (!sunrise::client::hooks::egress::install() || !sunrise::core::initialize(g_module)) {
  13. return false;
  14. }
  15. // This export is one of two entry points; the guard reports once, whichever ran first.
  16. sunrise::client::hooks::egress::report_installation();
  17. return true;
  18. }
  19. /** @return True when the guard is in place and Client hooks activate. */
  20. extern "C" __declspec(dllexport) bool SunriseActivateClient() noexcept {
  21. return sunrise::client::hooks::egress::is_installed()
  22. && sunrise::steam::runtime::activate_main_once();
  23. }
  24. /** @return True when the whole runtime shuts down cleanly. */
  25. extern "C" __declspec(dllexport) bool SunriseShutdown() noexcept {
  26. return sunrise::steam::shutdown();
  27. }
  28. /** @return True when the Steam-compatible runtime initializes. */
  29. extern "C" __declspec(dllexport) bool SteamAPI_Init() noexcept {
  30. return sunrise::steam::initialize(g_module);
  31. }
  32. /** Stops the Steam-compatible runtime. */
  33. extern "C" __declspec(dllexport) void SteamAPI_Shutdown() noexcept {
  34. (void)sunrise::steam::shutdown();
  35. }
  36. /** Delivers one batch of callbacks on the caller thread. */
  37. extern "C" __declspec(dllexport) void SteamAPI_RunCallbacks() noexcept {
  38. sunrise::steam::run_callbacks();
  39. }
  40. /**
  41. * Stores the app id. The process is never restarted.
  42. * @return False because the in-process shim never asks for a restart.
  43. */
  44. extern "C" __declspec(dllexport) bool SteamAPI_RestartAppIfNecessary(DWORD appId) noexcept {
  45. sunrise::steam::set_app_id(appId);
  46. return false;
  47. }
  48. /** @return True because this DLL provides the process-local Steam runtime. */
  49. extern "C" __declspec(dllexport) bool SteamAPI_IsSteamRunning() noexcept {
  50. return sunrise::steam::is_running();
  51. }
  52. /** @return The shim's single Steam user handle. */
  53. extern "C" __declspec(dllexport) sunrise::steam::UserHandle SteamAPI_GetHSteamUser() noexcept {
  54. return sunrise::steam::user_handle();
  55. }
  56. /** @return The shim's single Steam pipe handle. */
  57. extern "C" __declspec(dllexport) sunrise::steam::PipeHandle SteamAPI_GetHSteamPipe() noexcept {
  58. return sunrise::steam::pipe_handle();
  59. }
  60. /**
  61. * Registers a Steam callback object.
  62. * @param callback Steam-owned callback object.
  63. * @param callbackId Steam callback type id.
  64. */
  65. extern "C" __declspec(dllexport) void SteamAPI_RegisterCallback(void* callback,
  66. int callbackId) noexcept {
  67. sunrise::steam::register_callback(callback, callbackId);
  68. }
  69. /** @param callback Steam-owned callback object removed from registrations. */
  70. extern "C" __declspec(dllexport) void SteamAPI_UnregisterCallback(void* callback) noexcept {
  71. sunrise::steam::unregister_callback(callback);
  72. }
  73. /**
  74. * Registers a Steam API call-result object.
  75. * @param callback Steam-owned call-result object.
  76. * @param call Async API call id. Must not be zero.
  77. */
  78. extern "C" __declspec(dllexport) void
  79. SteamAPI_RegisterCallResult(void* callback, sunrise::steam::ApiCall call) noexcept {
  80. sunrise::steam::register_call_result(callback, call);
  81. }
  82. /**
  83. * Removes a Steam API call-result object.
  84. * @param callback Steam-owned call-result object.
  85. * @param call API call to remove, or zero for every call owned by the object.
  86. */
  87. extern "C" __declspec(dllexport) void
  88. SteamAPI_UnregisterCallResult(void* callback, sunrise::steam::ApiCall call) noexcept {
  89. sunrise::steam::unregister_call_result(callback, call);
  90. }
  91. /** @param data Steam-owned context table. @return Address of the interface field, after init. */
  92. extern "C" __declspec(dllexport) void* SteamInternal_ContextInit(void* data) noexcept {
  93. return sunrise::steam::context_init(data);
  94. }
  95. /** @param version Interface version. @return Supported client interface or null. */
  96. extern "C" __declspec(dllexport) void* SteamInternal_CreateInterface(const char* version) noexcept {
  97. return sunrise::steam::create_interface(version, _ReturnAddress());
  98. }
  99. /**
  100. * Finds a supported user interface.
  101. * @param user Process-global zero handle, or the shim's local user handle.
  102. * @param version Interface version.
  103. * @return Supported user interface or null.
  104. */
  105. extern "C" __declspec(dllexport) void*
  106. SteamInternal_FindOrCreateUserInterface(sunrise::steam::UserHandle user,
  107. const char* version) noexcept {
  108. return sunrise::steam::find_or_create_user_interface(user, version);
  109. }
  110. /**
  111. * Saves the DLL module and turns off unused thread notifications. Runs under the loader lock.
  112. * @param instance Loaded DLL module.
  113. * @return TRUE for every supported loader notification.
  114. */
  115. BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID) {
  116. if (reason == DLL_PROCESS_ATTACH) {
  117. g_module = instance;
  118. DisableThreadLibraryCalls(instance);
  119. } else if (reason == DLL_PROCESS_DETACH) {
  120. g_module = nullptr;
  121. }
  122. return TRUE;
  123. }