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