steam_tracker.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**************************************************************************/
  2. /* steam_tracker.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #if defined(STEAMAPI_ENABLED)
  31. #include "steam_tracker.h"
  32. #include "core/io/file_access.h"
  33. // https://partner.steamgames.com/doc/sdk/api#initialization_and_shutdown
  34. SteamTracker::SteamTracker() {
  35. String path;
  36. if (OS::get_singleton()->has_feature("linuxbsd")) {
  37. path = OS::get_singleton()->get_executable_path().get_base_dir().path_join("libsteam_api.so");
  38. if (!FileAccess::exists(path)) {
  39. path = OS::get_singleton()->get_executable_path().get_base_dir().path_join("../lib").path_join("libsteam_api.so");
  40. if (!FileAccess::exists(path)) {
  41. return;
  42. }
  43. }
  44. } else if (OS::get_singleton()->has_feature("windows")) {
  45. if (OS::get_singleton()->has_feature("64")) {
  46. path = OS::get_singleton()->get_executable_path().get_base_dir().path_join("steam_api64.dll");
  47. } else {
  48. path = OS::get_singleton()->get_executable_path().get_base_dir().path_join("steam_api.dll");
  49. }
  50. if (!FileAccess::exists(path)) {
  51. return;
  52. }
  53. } else if (OS::get_singleton()->has_feature("macos")) {
  54. path = OS::get_singleton()->get_executable_path().get_base_dir().path_join("libsteam_api.dylib");
  55. if (!FileAccess::exists(path)) {
  56. path = OS::get_singleton()->get_executable_path().get_base_dir().path_join("../Frameworks").path_join("libsteam_api.dylib");
  57. if (!FileAccess::exists(path)) {
  58. return;
  59. }
  60. }
  61. } else {
  62. return;
  63. }
  64. Error err = OS::get_singleton()->open_dynamic_library(path, steam_library_handle);
  65. if (err != OK) {
  66. steam_library_handle = nullptr;
  67. return;
  68. }
  69. print_verbose("Loaded SteamAPI library");
  70. void *symbol_handle = nullptr;
  71. err = OS::get_singleton()->get_dynamic_library_symbol_handle(steam_library_handle, "SteamAPI_InitFlat", symbol_handle, true); // Try new API, 1.59+.
  72. if (err != OK) {
  73. err = OS::get_singleton()->get_dynamic_library_symbol_handle(steam_library_handle, "SteamAPI_Init", symbol_handle); // Try old API.
  74. if (err != OK) {
  75. return;
  76. }
  77. steam_init_function = (SteamAPI_InitFunction)symbol_handle;
  78. } else {
  79. steam_init_flat_function = (SteamAPI_InitFlatFunction)symbol_handle;
  80. }
  81. err = OS::get_singleton()->get_dynamic_library_symbol_handle(steam_library_handle, "SteamAPI_Shutdown", symbol_handle);
  82. if (err != OK) {
  83. return;
  84. }
  85. steam_shutdown_function = (SteamAPI_ShutdownFunction)symbol_handle;
  86. if (steam_init_flat_function) {
  87. char err_msg[1024] = {};
  88. steam_initialized = (steam_init_flat_function(&err_msg[0]) == SteamAPIInitResult_OK);
  89. } else if (steam_init_function) {
  90. steam_initialized = steam_init_function();
  91. }
  92. }
  93. SteamTracker::~SteamTracker() {
  94. if (steam_shutdown_function && steam_initialized) {
  95. steam_shutdown_function();
  96. }
  97. if (steam_library_handle) {
  98. OS::get_singleton()->close_dynamic_library(steam_library_handle);
  99. }
  100. }
  101. #endif // STEAMAPI_ENABLED