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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
#include "StarLogging.hpp"
#include "StarPlatformServices_pc.hpp"
#include "StarP2PNetworkingService_pc.hpp"
#ifdef STAR_ENABLE_STEAM_INTEGRATION
#include "StarStatisticsService_pc_steam.hpp"
#include "StarUserGeneratedContentService_pc_steam.hpp"
#include "StarDesktopService_pc_steam.hpp"
#endif
namespace Star {
#ifdef STAR_ENABLE_DISCORD_INTEGRATION
uint64_t const DiscordClientId = 467102538278109224;
#endif
PcPlatformServicesState::PcPlatformServicesState()
#ifdef STAR_ENABLE_STEAM_INTEGRATION
: callbackGameOverlayActivated(this, &PcPlatformServicesState::onGameOverlayActivated) {
#else
{
#endif
#ifdef STAR_ENABLE_STEAM_INTEGRATION
SteamErrMsg errMsg;
if (SteamAPI_InitEx(&errMsg) == k_ESteamAPIInitResult_OK) {
steamAvailable = true;
Logger::info("Initialized Steam platform services");
} else {
Logger::info("Failed to initialize Steam platform services: {}", errMsg);
}
#endif
#ifdef STAR_ENABLE_DISCORD_INTEGRATION
static int64_t const DiscordEventSleep = 3;
discord::Core* discordCorePtr = nullptr;
discord::Result res = discord::Core::Create(DiscordClientId, DiscordCreateFlags_NoRequireDiscord, &discordCorePtr);
if (res == discord::Result::Ok && discordCorePtr) {
discordCore.reset(discordCorePtr);
discordAvailable = true;
discordCore->UserManager().OnCurrentUserUpdate.Connect([this](){
discord::User user;
auto res = discordCore->UserManager().GetCurrentUser(&user);
if (res != discord::Result::Ok)
Logger::error("Could not get current Discord user. (err {})", (int)res);
else
discordCurrentUser = user;
});
} else {
Logger::error("Failed to instantiate Discord core (err {})", (int)res);
}
if (discordAvailable) {
MutexLocker locker(discordMutex);
discordCore->SetLogHook(discord::LogLevel::Info, [](discord::LogLevel level, char const* msg) {
if (level == discord::LogLevel::Debug)
Logger::debug("[Discord]: {}", msg);
else if (level == discord::LogLevel::Error)
Logger::debug("[Discord]: {}", msg);
else if (level == discord::LogLevel::Info)
Logger::info("[Discord]: {}", msg);
else if (level == discord::LogLevel::Warn)
Logger::warn("[Discord]: {}", msg);
});
discordEventShutdown = false;
discordEventThread = Thread::invoke("PcPlatformServices::discordEventThread", [this]() {
while (!discordEventShutdown) {
{
MutexLocker locker(discordMutex);
discordCore->RunCallbacks();
discordCore->LobbyManager().FlushNetwork();
}
Thread::sleep(DiscordEventSleep);
}
});
Logger::info("Initialized Discord platform services");
} else {
Logger::info("Was not able to authenticate with Discord and create all components, Discord services will be unavailable");
}
#endif
}
PcPlatformServicesState::~PcPlatformServicesState() {
#ifdef STAR_ENABLE_DISCORD_INTEGRATION
if (discordAvailable) {
discordEventShutdown = true;
discordEventThread.finish();
}
#endif
}
#ifdef STAR_ENABLE_STEAM_INTEGRATION
void PcPlatformServicesState::onGameOverlayActivated(GameOverlayActivated_t* callback) {
overlayActive = callback->m_bActive;
}
#endif
PcPlatformServicesUPtr PcPlatformServices::create([[maybe_unused]] String const& path, StringList platformArguments) {
auto services = unique_ptr<PcPlatformServices>(new PcPlatformServices);
services->m_state = make_shared<PcPlatformServicesState>();
bool provideP2PNetworking = false;
#ifdef STAR_ENABLE_STEAM_INTEGRATION
provideP2PNetworking |= services->m_state->steamAvailable;
#endif
#ifdef STAR_ENABLE_DISCORD_INTEGRATION
provideP2PNetworking |= services->m_state->discordAvailable;
#endif
if (provideP2PNetworking) {
auto p2pNetworkingService = make_shared<PcP2PNetworkingService>(services->m_state);
for (auto& argument : platformArguments) {
if (argument.beginsWith("+platform:connect:")) {
Logger::info("PC platform services joining from command line argument '{}'", argument);
p2pNetworkingService->addPendingJoin(std::move(argument));
} else {
throw ApplicationException::format("Unrecognized PC platform services command line argument '{}'", argument);
}
}
services->m_p2pNetworkingService = p2pNetworkingService;
}
#ifdef STAR_ENABLE_STEAM_INTEGRATION
if (services->m_state->steamAvailable) {
services->m_statisticsService = make_shared<SteamStatisticsService>(services->m_state);
services->m_userGeneratedContentService = make_shared<SteamUserGeneratedContentService>(services->m_state);
services->m_desktopService = make_shared<SteamDesktopService>(services->m_state);
}
#endif
#ifdef STAR_ENABLE_DISCORD_INTEGRATION
MutexLocker discordLocker(services->m_state->discordMutex);
if (services->m_state->discordAvailable) {
Logger::debug("Registering Starbound to Discord at path: {}", path);
services->m_state->discordCore->ActivityManager().RegisterCommand(path.utf8Ptr());
}
#endif
return services;
}
StatisticsServicePtr PcPlatformServices::statisticsService() const {
return m_statisticsService;
}
P2PNetworkingServicePtr PcPlatformServices::p2pNetworkingService() const {
return m_p2pNetworkingService;
}
UserGeneratedContentServicePtr PcPlatformServices::userGeneratedContentService() const {
return m_userGeneratedContentService;
}
DesktopServicePtr PcPlatformServices::desktopService() const {
return m_desktopService;
}
bool PcPlatformServices::overlayActive() const {
return m_state->overlayActive;
}
void PcPlatformServices::update() {
#ifdef STAR_ENABLE_STEAM_INTEGRATION
SteamAPI_RunCallbacks();
#endif
}
}
|