-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdllmain.cpp
290 lines (243 loc) · 8.5 KB
/
dllmain.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
#pragma once
#pragma comment(linker,"/export:DWriteCreateFactory=C:\\Windows\\SysWOW64\\DWrite.DWriteCreateFactory,@1")
#include "windows.h"
#include "ios"
#include "fstream"
#include "Discord.h"
#include "ini.h"
#include "MemoryUtility.h"
#include <mutex>
#include <psapi.h>
#include <format>
#include "Video.h"
#include <iostream>
using namespace mINI;
static Discord* g_discord;
static MemoryUtility* g_memoryUtility;
static bool threadStarted = false;
static HANDLE hMainThread = NULL;
static std::mutex initMutex;
// Remove this line if you aren't proxying any functions.
HMODULE hModule = LoadLibrary(L"C:\\Windows\\SysWOW64\\DWrite.dll");
static bool show_buttons = true;
static std::string link_target = "app";
static bool disable_in_menu = false;
static bool disable_when_paused = false;
static bool enable_console = false;
static int refresh_interval = 10;
static time_t g_startTime;
static std::string toLowerCase(const std::string& str) {
std::string result = str;
std::transform(result.begin(), result.end(), result.begin(), ::tolower);
return result;
}
static void ReadConfig()
{
INIFile config(".\\RPCconfig.ini");
INIStructure ini;
if (config.read(ini)) {
// Successfully read the INI file, load the values
show_buttons = ini["Buttons"]["show_buttons"] == "true";
link_target = toLowerCase(ini["Buttons"]["link_target"]);
if (link_target != "app" && link_target != "web") {
link_target = "app"; // Default to "app" if invalid
}
disable_in_menu = ini["Activity"]["disable_in_menu"] == "true";
disable_when_paused = ini["Activity"]["disable_when_paused"] == "true";
enable_console = ini["Debug"]["enable_console"] == "true";
refresh_interval = std::stoi(ini["Activity"]["refresh_interval"]);
if (refresh_interval < 1 || refresh_interval > 60) {
refresh_interval = 10; // Default to 10 seconds if invalid
}
}
else {
// Failed to read the INI file, initialize with default values and create the file
ini["Buttons"]["show_buttons"] = show_buttons ? "true" : "false";
ini["Buttons"]["link_target"] = link_target;
ini["Activity"]["disable_in_menu"] = disable_in_menu ? "true" : "false";
ini["Activity"]["disable_when_paused"] = disable_when_paused ? "true" : "false";
ini["Debug"]["enable_console"] = enable_console ? "true" : "false";
ini["Activity"]["refresh_interval"] = std::to_string(refresh_interval);
// Write default configuration to file
if (!config.write(ini, true)) {
std::cerr << "Error: Unable to write default configuration to RPCconfig.ini" << std::endl;
}
}
}
static void UpdateDiscordPresence() {
std::string oldinfo = "";
double previousVideoTime = -1;
bool isPaused = false;
while (true) {
std::this_thread::sleep_for(std::chrono::seconds(refresh_interval));
double videoTime = g_memoryUtility->ReadVideoTime();
if (videoTime > 0) {
Video vid;
DiscordRichPresence discordRichPresence;
memset(&discordRichPresence, 0, sizeof(discordRichPresence));
std::vector<std::string> videoId = vid.getvidinfo();
std::string currentInfoStr = vid.join(videoId, ":");
if (oldinfo != currentInfoStr) {
oldinfo = currentInfoStr;
vid = Video(); // Reinitialize video object with new video info
}
if (videoTime == previousVideoTime) {
isPaused = true;
}
else {
isPaused = false;
previousVideoTime = videoTime;
}
std::time_t currentTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
int difference = static_cast<int>(currentTime) - static_cast<int>(videoTime);
std::time_t endTime = currentTime + static_cast<int>(vid.getmaxtime() - vid.gettime());
std::string state;
std::string details = vid.name;
std::string largeImageKey = vid.poster;
std::string largeImageText = vid.name + " " + vid.year;
std::string smallImageKey;
std::string smallImageText;
std::string IMBdUrl = "https://www.imdb.com/title/" + vid.id;
std::string StremioUrl = "";
if (link_target == "web") {
StremioUrl = "https://web.stremio.com/#/detail/" + vid.type + "/" + vid.id + "/" + currentInfoStr;
}
else {
StremioUrl = "stremio:///detail/" + vid.type + "/" + vid.id + "/" + currentInfoStr;
}
if (isPaused) {
if (disable_when_paused) {
g_discord->clearPresence();
continue;
}
smallImageText = "Paused";
smallImageKey = "https://i.imgur.com/eCUJpm9.png";
}
else {
if (vid.type == "series") {
smallImageKey = vid.thumbnail;
}
else {
smallImageKey = "https://raw.githubusercontent.com/Stremio/stremio-web/refs/heads/development/images/icon.png";
}
smallImageText = "Playing";
discordRichPresence.startTimestamp = difference;
discordRichPresence.endTimestamp = endTime;
}
if (vid.type == "series") {
state = vid.epname + " (S" + vid.s + "-E" + vid.ep + ")";
}
else {
state = vid.year;
}
discordRichPresence.type = DISCORD_ACTIVITY_TYPE_WATCHING;
discordRichPresence.state = state.c_str();
discordRichPresence.details = details.c_str();
discordRichPresence.largeImageKey = largeImageKey.c_str();
discordRichPresence.largeImageText = largeImageText.c_str();
discordRichPresence.smallImageKey = smallImageKey.c_str();
discordRichPresence.smallImageText = smallImageText.c_str();
if (show_buttons) {
discordRichPresence.button1Label = "More details";
discordRichPresence.button1Url = IMBdUrl.c_str();
discordRichPresence.button2Label = "Watch on Stremio";
discordRichPresence.button2Url = StremioUrl.c_str();
}
g_discord->update(discordRichPresence);
}
else if (!disable_in_menu) {
DiscordRichPresence discordRichPresence;
memset(&discordRichPresence, 0, sizeof(discordRichPresence));
discordRichPresence.type = DISCORD_ACTIVITY_TYPE_WATCHING;
discordRichPresence.state = "In Menu";
discordRichPresence.details = "Browsing catalog";
discordRichPresence.largeImageKey = "https://raw.githubusercontent.com/Stremio/stremio-web/refs/heads/development/images/icon.png";
discordRichPresence.largeImageText = "Stremio";
discordRichPresence.startTimestamp = g_startTime;
g_discord->update(discordRichPresence);
}
else {
g_discord->clearPresence();
}
}
}
DWORD WINAPI MainThread(LPVOID param) {
g_discord->initialize();
g_memoryUtility->initialize();
// Create a thread to update Discord presence
HANDLE hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
[](LPVOID) -> DWORD {
UpdateDiscordPresence();
return 0;
},
NULL, // argument to thread function
0, // use default creation flags
NULL); // returns the thread identifier
if (hThread == NULL) {
DWORD dwError = GetLastError();
}
else {
CloseHandle(hThread);
}
return 0;
}
std::string GetProcessName()
{
wchar_t processName[MAX_PATH] = { 0 };
DWORD processId = GetCurrentProcessId();
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
if (hProcess)
{
HMODULE hMod;
DWORD cbNeeded;
if (EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded))
{
GetModuleBaseNameW(hProcess, hMod, processName, sizeof(processName) / sizeof(wchar_t));
}
CloseHandle(hProcess); // Move CloseHandle inside the if block
}
// Convert wide string to narrow string using wcstombs_s
char narrowProcessName[MAX_PATH];
size_t convertedChars = 0;
wcstombs_s(&convertedChars, narrowProcessName, MAX_PATH, processName, _TRUNCATE);
// Ensure null termination
narrowProcessName[MAX_PATH - 1] = '\0';
return std::string(narrowProcessName);
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
std::lock_guard<std::mutex> lock(initMutex);
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
if (GetProcessName() == "stremio.exe") {
ReadConfig();
g_startTime = std::time(0);
if (enable_console) {
AllocConsole(); // Allocate a console
freopen_s((FILE**)stdout, "CONOUT$", "w", stdout); // Redirect stdout to console
freopen_s((FILE**)stderr, "CONOUT$", "w", stderr); // Redirect stderr to console
freopen_s((FILE**)stdin, "CONIN$", "r", stdin); // Redirect stdin to console
std::cout << "DLL attached to process: " << GetProcessName() << std::endl;
}
if (!threadStarted) {
hMainThread = CreateThread(NULL, 0, MainThread, NULL, 0, NULL);
if (hMainThread != NULL) {
threadStarted = true;
}
}
}
break;
case DLL_PROCESS_DETACH:
if (hMainThread != NULL) {
CloseHandle(hMainThread);
hMainThread = NULL;
threadStarted = false;
}
FreeConsole();
break;
}
return TRUE;
}