-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathMySteamClient.h
106 lines (94 loc) · 4.22 KB
/
MySteamClient.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#pragma once
#include "../../steam/steam_api.h"
#include "../common/functions.h"
#include "../globals.h"
#include "MySteam.h"
#include <iostream>
#include <dlfcn.h>
#define RELATIVE_STEAM_CLIENT_LIB_PATH "/linux64/steamclient.so"
/**
* Purpose:
* The SteamClient() accessor does not work. It always returns NULL for some reason.
* We use the Steam Client library to call the CreateInterface to create an instance anyway.
*
* This is used so we can communicate with the Steam Client without being marked as "In-Game"
*
* I did not experiment a lot with this class, but I would advise to create only one instance for the whole program.
*
* I get SteamApps to get owned games (if one AppId is subscribed)
* I could use ISteamUser to get the current user's Steam ID. (what for idk, but later features maybe)
*
* https://partner.steamgames.com/doc/api/ISteamClient
*/
class MySteamClient
{
public:
HSteamPipe getPipe() const { return m_pipe; };
HSteamUser getUser() const { return m_user; };
ISteamApps* getSteamApps() const { return m_steamapps; };
ISteamUser* getSteamUser() const { return m_steamuser; };
// Needed for child usage..
void unloadLibrary() {
dlclose(m_handle);
}
~MySteamClient() {
m_steamclient->ReleaseUser(m_pipe, m_user);
m_steamclient->BReleaseSteamPipe(m_pipe);
m_steamclient->BShutdownIfAllPipesClosed();
dlclose(m_handle);
}
MySteamClient() {
char* error;
std::string steam_client_lib_path = g_steam->get_steam_install_path() + RELATIVE_STEAM_CLIENT_LIB_PATH;
m_handle = dlopen(steam_client_lib_path.c_str(), RTLD_LAZY);
if (!m_handle) {
std::cerr << "Error opening the Steam Client library. " << steam_client_lib_path << " may not exist. Info:" << std::endl;
std::cerr << dlerror() << std::endl;
std::cerr << "Trying alternate Steam Client library path." << std::endl;
steam_client_lib_path = g_steam->get_steam_install_path() + "/.." + RELATIVE_STEAM_CLIENT_LIB_PATH;
m_handle = dlopen(steam_client_lib_path.c_str(), RTLD_LAZY);
if (!m_handle) {
std::cerr << "Error opening the Steam Client library. Exiting. Info:" << std::endl;
std::cerr << dlerror() << std::endl;
zenity();
exit(EXIT_FAILURE);
}
}
ISteamClient* (*CreateInterface)(const char* version, void*);
CreateInterface = (ISteamClient* (*)(const char*, void*))dlsym(m_handle, "CreateInterface");
if ((error = dlerror()) != NULL) {
std::cerr << "Error reading the CreateInterface symbol from the Steam Client library. Exiting. Info:" << std::endl;
std::cerr << error << std::endl;
zenity();
exit(EXIT_FAILURE);
}
m_steamclient = CreateInterface(STEAMCLIENT_INTERFACE_VERSION, NULL);
m_pipe = m_steamclient->CreateSteamPipe();
m_user = m_steamclient->ConnectToGlobalUser(m_pipe);
if (m_pipe == 0 || m_user == 0) {
std::cout << "Unable to interact with Steam. Make sure Steam is launched and you are logged in to your account." << std::endl;
std::cerr << "Unable to create Steam Pipe or connect to global user. Exitting." << std::endl;
zenity("Unable to interact with Steam. Make sure Steam is launched and you are logged in to your account.");
exit(EXIT_FAILURE);
}
m_steamapps = m_steamclient->GetISteamApps(m_user, m_pipe, STEAMAPPS_INTERFACE_VERSION);
if (m_steamapps == NULL) {
std::cerr << "Unable to get ISteamApps interface for MySteamClient. Exitting." << std::endl;
zenity();
exit(EXIT_FAILURE);
}
m_steamuser = m_steamclient->GetISteamUser(m_user, m_pipe, STEAMUSER_INTERFACE_VERSION);
if (m_steamuser == NULL) {
std::cerr << "Unable to get ISteamUser interface for MySteamClient. Exitting." << std::endl;
zenity();
exit(EXIT_FAILURE);
}
};
private:
HSteamPipe m_pipe;
HSteamUser m_user;
void* m_handle = nullptr;
ISteamApps* m_steamapps = nullptr;
ISteamUser* m_steamuser = nullptr;
ISteamClient* m_steamclient = nullptr;
};