steam_tracker.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // https://partner.steamgames.com/doc/sdk/api#initialization_and_shutdown
  33. SteamTracker::SteamTracker() {
  34. String path;
  35. if (OS::get_singleton()->has_feature("linuxbsd")) {
  36. path = OS::get_singleton()->get_executable_path().get_base_dir().path_join("libsteam_api.so");
  37. if (!FileAccess::exists(path)) {
  38. path = OS::get_singleton()->get_executable_path().get_base_dir().path_join("../lib").path_join("libsteam_api.so");
  39. if (!FileAccess::exists(path)) {
  40. return;
  41. }
  42. }
  43. } else if (OS::get_singleton()->has_feature("windows")) {
  44. if (OS::get_singleton()->has_feature("64")) {
  45. path = OS::get_singleton()->get_executable_path().get_base_dir().path_join("steam_api64.dll");
  46. } else {
  47. path = OS::get_singleton()->get_executable_path().get_base_dir().path_join("steam_api.dll");
  48. }
  49. if (!FileAccess::exists(path)) {
  50. return;
  51. }
  52. } else if (OS::get_singleton()->has_feature("macos")) {
  53. path = OS::get_singleton()->get_executable_path().get_base_dir().path_join("libsteam_api.dylib");
  54. if (!FileAccess::exists(path)) {
  55. path = OS::get_singleton()->get_executable_path().get_base_dir().path_join("../Frameworks").path_join("libsteam_api.dylib");
  56. if (!FileAccess::exists(path)) {
  57. return;
  58. }
  59. }
  60. } else {
  61. return;
  62. }
  63. Error err = OS::get_singleton()->open_dynamic_library(path, steam_library_handle);
  64. if (err != OK) {
  65. steam_library_handle = nullptr;
  66. return;
  67. }
  68. print_verbose("Loaded SteamAPI library");
  69. void *symbol_handle = nullptr;
  70. err = OS::get_singleton()->get_dynamic_library_symbol_handle(steam_library_handle, "SteamAPI_InitFlat", symbol_handle, true); // Try new API, 1.59+.
  71. if (err != OK) {
  72. err = OS::get_singleton()->get_dynamic_library_symbol_handle(steam_library_handle, "SteamAPI_Init", symbol_handle); // Try old API.
  73. if (err != OK) {
  74. return;
  75. }
  76. steam_init_function = (SteamAPI_InitFunction)symbol_handle;
  77. } else {
  78. steam_init_flat_function = (SteamAPI_InitFlatFunction)symbol_handle;
  79. }
  80. err = OS::get_singleton()->get_dynamic_library_symbol_handle(steam_library_handle, "SteamAPI_Shutdown", symbol_handle);
  81. if (err != OK) {
  82. return;
  83. }
  84. steam_shutdown_function = (SteamAPI_ShutdownFunction)symbol_handle;
  85. if (steam_init_flat_function) {
  86. char err_msg[1024] = {};
  87. steam_initalized = (steam_init_flat_function(&err_msg[0]) == SteamAPIInitResult_OK);
  88. } else if (steam_init_function) {
  89. steam_initalized = steam_init_function();
  90. }
  91. }
  92. SteamTracker::~SteamTracker() {
  93. if (steam_shutdown_function && steam_initalized) {
  94. steam_shutdown_function();
  95. }
  96. if (steam_library_handle) {
  97. OS::get_singleton()->close_dynamic_library(steam_library_handle);
  98. }
  99. }
  100. #endif // STEAMAPI_ENABLED