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