-
Notifications
You must be signed in to change notification settings - Fork 5
/
DragonbornPresence.cpp
314 lines (243 loc) · 8.69 KB
/
DragonbornPresence.cpp
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "tinyfsm.hpp"
#include "DragonbornPresence.h"
#include "skse64/GameMenus.h"
#include "AdditionalFunctions.h"
#include <ctime>
bool is_user_connected = true;
int64_t start_time;
const char * application_id = "565627104608256015";
const char * steam_appid = "72850";
const char * current_player_info;
const char * current_position;
std::map<const char*, std::string> locales
{
{
{"MainMenu", "Main menu"}, {"EditingCharacter", "Editing character"}
}
};
#pragma region State Machine
#pragma region Event Declaration
struct StatusEvent : tinyfsm::Event {
};
struct GoToMainMenu : StatusEvent {
};
struct GoToLoading : StatusEvent {
};
struct GoToEditingCharacter : StatusEvent {
};
struct GoToPlaying : StatusEvent {
};
#pragma endregion
#pragma region State Machine Class Declaration
struct DiscordState : tinyfsm::Fsm<DiscordState> {
static void react(tinyfsm::Event const &) {
};
virtual void react(GoToMainMenu const &) {
};
virtual void react(GoToLoading const &) {
};
virtual void react(GoToEditingCharacter const &) {
};
virtual void react(GoToPlaying const &) {
};
virtual void entry() {
};
virtual void exit() {
};
};
#pragma endregion
#pragma region States Declaration
struct MainMenuState : DiscordState {
void entry() override;
void react(GoToLoading const &) override;
};
struct LoadingState : DiscordState {
void entry() override;
void react(GoToMainMenu const &) override;
void react(GoToEditingCharacter const &) override;
void react(GoToPlaying const &) override;
};
struct EditingCharacterState : DiscordState {
void entry() override;
void react(GoToPlaying const &) override;
};
struct PlayingState : DiscordState {
void entry() override;
void react(GoToEditingCharacter const &) override;
void react(GoToLoading const &) override;
void react(GoToMainMenu const &) override;
};
FSM_INITIAL_STATE(DiscordState, LoadingState)
#pragma endregion
#pragma region Actions and Event Reactions Implemention
void MainMenuState::entry() {
_MESSAGE("Main Menu State");
dragonborn_presence_namespace::UpdatePresence(
locales.find("MainMenu")->second.c_str(), nullptr);
}
void MainMenuState::react(GoToLoading const &) { transit<LoadingState>(); }
void LoadingState::entry() { _MESSAGE("Loading State"); }
void LoadingState::react(GoToMainMenu const &) { transit<MainMenuState>(); }
void LoadingState::react(GoToEditingCharacter const &) {
transit<EditingCharacterState>();
}
void LoadingState::react(GoToPlaying const &) { transit<PlayingState>(); }
void EditingCharacterState::entry() {
_MESSAGE("Editing Character State");
dragonborn_presence_namespace::UpdatePresence(
locales.find("EditingCharacter")->second.c_str(), nullptr);
}
void EditingCharacterState::react(GoToPlaying const &) {
transit<PlayingState>();
}
void PlayingState::entry() {
_MESSAGE("Playing State");
dragonborn_presence_namespace::UpdatePresence(current_position,
current_player_info);
}
void PlayingState::react(GoToEditingCharacter const &) {
transit<EditingCharacterState>();
}
void PlayingState::react(GoToLoading const &) { transit<LoadingState>(); }
void PlayingState::react(GoToMainMenu const &) { transit<MainMenuState>(); }
#pragma endregion
#pragma endregion
namespace dragonborn_presence_namespace {
void SetLocale() {
FILE * file = nullptr;
const auto err = fopen_s(&file,
R"(Data\SKSE\Plugins\DragonbornPresenceLocale.txt)",
"r");
if (err == 2 || file == nullptr) // Missing file.
return;
if (err != 0) // Unknown error code.
return;
char buffer[1024];
auto index = 0;
while (fgets(buffer, 1024, file) != nullptr) {
std::string line = buffer;
while (!line.empty() && (line[line.length() - 1] == '\r' || line[line.
length() - 1] == '\n'))
line = line.substr(0, line.length() - 1);
if (line.empty())
continue;
if (line[0] == ';')
continue;
if (index == 0) { locales.find("MainMenu")->second = line; } else if (
index == 1) { locales.find("EditingCharacter")->second = line; }
index++;
}
fclose(file);
}
#pragma region Discord Callbacks
//Calls when Discord RPC is successfully connected to user.
void HandleDiscordReady(const DiscordUser * connected_user) {
_MESSAGE("Discord RPC: connected to user %s#%s - %s",
connected_user->username, connected_user->discriminator,
connected_user->userId);
is_user_connected = true;
}
//Calls when discord reported an error.
void HandleDiscordError(const int error_code, const char * message) {
_MESSAGE("Discord RPC: an error occured (%d: %s)", error_code, message);
}
//Calls when Discord RPC is disconnected from user.
void HandleDiscordDisconnected(const int error_code, const char * message) {
_MESSAGE("Discord RPC: disconnected (%d: %s)", error_code, message);
is_user_connected = false;
}
#pragma endregion
#pragma region Discord Functions
//Initializes Discord RPC. Sets the timer, counting down the time from the
//start of the game. Assigns functions to be called when callbacks.
void InitDiscord() {
start_time = time(nullptr);
DiscordEventHandlers handlers;
memset(&handlers, 0, sizeof(handlers));
handlers.ready = HandleDiscordReady;
handlers.errored = HandleDiscordError;
handlers.disconnected = HandleDiscordDisconnected;
Discord_Initialize(application_id, &handlers, 1, steam_appid);
_MESSAGE("Discord RPC Init OK.");
}
//Updates rich presence in Discord.
void UpdatePresence(const char * current_state,
const char * current_details) {
if (is_user_connected) {
DiscordRichPresence discord_presence;
memset(&discord_presence, 0, sizeof(discord_presence));
discord_presence.state = current_state;
discord_presence.details = current_details;
discord_presence.startTimestamp = start_time;
discord_presence.largeImageKey = "skyrim_logo";
discord_presence.largeImageText = "The Elder Scrolls V: Skyrim";
Discord_UpdatePresence(&discord_presence);
Discord_RunCallbacks();
_MESSAGE("Updated presence.");
}
}
#pragma endregion
#pragma region ReceiveEvent
EventResult DiscordMenuEventHandler::ReceiveEvent(
MenuOpenCloseEvent * evn,
EventDispatcher<MenuOpenCloseEvent> * dispatcher) {
if (evn->opening && evn->menuName == BSFixedString("Main Menu")) {
DiscordState::dispatch(GoToMainMenu());
} else if (evn->menuName == BSFixedString("Loading Menu")) {
if (evn->opening)
DiscordState::dispatch(GoToLoading());
else
DiscordState::dispatch(GoToPlaying());
} else if (evn->menuName == BSFixedString("RaceSex Menu")) {
if (evn->opening)
DiscordState::dispatch(GoToEditingCharacter());
else
DiscordState::dispatch(GoToPlaying());
}
return kEvent_Continue;
}
#pragma endregion
#pragma region Event Handlers
DiscordMenuEventHandler g_discordMenuEventHandler;
#pragma endregion
#pragma region Event registration
void RegisterGameEventHandlers() {
_MESSAGE("Registering game event handlers...");
DiscordState::start();
auto mm = MenuManager::GetSingleton();
if (mm) {
mm->MenuOpenCloseEventDispatcher()->AddEventSink(
&g_discordMenuEventHandler);
} else { _MESSAGE("Failed to register SKSE menuEventHandler!"); }
}
#pragma endregion
#pragma region Papyrus functions
void UpdatePresenceData(StaticFunctionTag * base, BSFixedString new_position,
BSFixedString new_player_info) {
_MESSAGE("CURRENT POSITION: %s", new_position.data);
current_position = is_valid_utf8(new_position.data)
? new_position.data
: Cp1251ToUtf8(new_position.data);
current_player_info = is_valid_utf8(new_player_info.data)
? new_player_info.data
: Cp1251ToUtf8(new_player_info.data);
if (DiscordState::is_in_state<PlayingState>()) {
UpdatePresence(current_position, current_player_info);
}
}
void SetGameLoaded(StaticFunctionTag * base) {
DiscordState::dispatch(GoToPlaying());
UpdatePresence(current_position, current_player_info);
}
bool RegisterFuncs(VMClassRegistry * registry) {
registry->RegisterFunction(
new NativeFunction2<StaticFunctionTag, void, BSFixedString, BSFixedString>
("UpdatePresenceData", "DragonbornPresence", UpdatePresenceData,
registry));
registry->RegisterFunction(new NativeFunction0<StaticFunctionTag, void>(
"SetGameLoaded", "DragonbornPresence", SetGameLoaded, registry));
InitDiscord();
return true;
}
#pragma endregion
}