forked from diozdeath19/pes6stat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscordrcp.cpp
More file actions
315 lines (261 loc) · 10.5 KB
/
discordrcp.cpp
File metadata and controls
315 lines (261 loc) · 10.5 KB
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
315
// discordrcp.cpp
#include "discordrcp.h"
#include <iostream>
#include <sstream>
#include <ctime>
#include <string>
#include <tchar.h>
#include <chrono>
KMOD k_discord = {MODID, _T(NAMELONG), _T(NAMESHORT), DEFAULT_DEBUG};
HINSTANCE hInst;
static struct IDiscordCore *g_core = nullptr;
static struct IDiscordActivityManager *g_activity = nullptr;
static struct IDiscordApplicationManager *g_application = nullptr;
static bool g_discordInitResult;
// Game state tracking
static __int8 matchStarted = 0;
static __int8 matchStartedBuf = 0;
static __int8 matchStadyBuf = 0;
static int teamHomeScore = 0;
static int teamGuestScore = 0;
static int teamHomeEXScore = 0;
static int teamGuestEXScore = 0;
static int teamHomePenaltiesScore = 0;
static int teamGuestPenaltiesScore = 0;
static int matchMinute = 0;
static int matchStady = 0;
static TCHAR teamNameHome[50];
static TCHAR teamNameGuest[50];
static std::basic_string<TCHAR> matchStartTime;
static int64_t matchStartTimestamp = 0;
// Rate limiting
static int64_t lastUpdateTime = 0;
static const int64_t UPDATE_INTERVAL = 2; // Update every 2 seconds
// Memory addresses
const DWORD MATCH_STARTED_ADDR = 0x010CF2EC;
const DWORD TEAM_NAME_HOME_ADDR = 0x010D3C16;
const DWORD TEAM_NAME_GUEST_ADDR = 0x010D6E3E;
const DWORD TEAM_HOME_SCORE_ADDR = 0x01017B38;
const DWORD TEAM_GUEST_SCORE_ADDR = 0x01017E2C;
const DWORD MATCH_MINUTE_ADDR = 0x010D2986;
const DWORD MATCH_STADY_ADDR = 0x010D298C;
// Function declarations
bool InitDiscord();
void InitModule();
void DiscordRCPPresent(IDirect3DDevice8 *self, CONST RECT *src, CONST RECT *dest, HWND hWnd, LPVOID unused);
void UpdateGameState();
void UpdateDiscordActivity();
std::basic_string<TCHAR> GetCurrentDateTime();
bool ReadMemory(HANDLE process, DWORD address, LPVOID buffer, SIZE_T size);
// Memory reading helper function
bool ReadMemory(HANDLE process, DWORD address, LPVOID buffer, SIZE_T size) {
SIZE_T bytesRead;
return ReadProcessMemory(process, (LPCVOID)address, buffer, size, &bytesRead) && bytesRead == size;
}
std::basic_string<TCHAR> GetCurrentDateTime() {
time_t now = time(0);
struct tm timeinfo;
TCHAR buffer[80];
localtime_s(&timeinfo, &now);
_tcsftime(buffer, sizeof(buffer)/sizeof(TCHAR), _T("%Y-%m-%d %H:%M:%S"), &timeinfo);
return std::basic_string<TCHAR>(buffer);
}
void DISCORD_CALLBACK OnUpdateActivity(void *data, enum EDiscordResult result)
{
if (result == DiscordResult_Ok)
{
Log(&k_discord, "OnUpdateActivity::Discord activity OK");
}
else
{
LogWithNumber(&k_discord, "OnUpdateActivity::Error updating Discord activity, error code (%d)", result);
}
}
EXTERN_C BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
hInst = hInstance;
Log(&k_discord, "DllMain::Attaching DiscordPresence module...");
switch (GetPESInfo()->GameVersion)
{
case gvPES6PC:
break;
default:
Log(&k_discord, "DllMain::Your game version is currently not supported!");
return false;
}
RegisterKModule(&k_discord);
g_discordInitResult = InitDiscord();
if (!g_discordInitResult)
{
Log(&k_discord, "DllMain::Error with discord initialization!");
return false;
}
HookFunction(hk_D3D_Create, (DWORD)InitModule);
HookFunction(hk_D3D_Present, (DWORD)DiscordRCPPresent);
}
else if (dwReason == DLL_PROCESS_DETACH)
{
Log(&k_discord, "DllMain::Detaching DiscordPresence module...");
if (g_discordInitResult)
{
UnhookFunction(hk_D3D_Present, (DWORD)DiscordRCPPresent);
}
if (g_core)
{
g_core->destroy(g_core);
g_core = nullptr;
g_activity = nullptr;
Log(&k_discord, "DllMain::Discord Core destroyed.");
}
Log(&k_discord, "DllMain::Detached.");
}
return TRUE;
}
bool InitDiscord()
{
struct DiscordCreateParams params;
DiscordCreateParamsSetDefault(¶ms);
params.client_id = 1193291233565618326;
params.flags = DiscordCreateFlags_NoRequireDiscord;
params.event_data = nullptr;
EDiscordResult res = DiscordCreate(DISCORD_VERSION, ¶ms, &g_core);
if (res != DiscordResult_Ok)
{
LogWithNumber(&k_discord, "DiscordCreate::Error trying to create Discord Core, code (%d)", res);
return false;
}
g_activity = g_core->get_activity_manager(g_core);
g_application = g_core->get_application_manager(g_core);
if (!g_activity || !g_application)
{
Log(&k_discord, "DiscordCreate::Error trying to get activity o application manager");
g_core->destroy(g_core);
g_core = nullptr;
return false;
}
DiscordLocale locale;
g_application->get_current_locale(g_application, &locale);
LogWithString(&k_discord, "DiscordCreate::Discord client locale: %s", locale);
if (!locale)
{
strncpy(locale, "en-US", sizeof(locale) - 1);
locale[sizeof(locale) - 1] = '\0';
LogWithString(&k_discord, "DiscordCreate::Error trying to get locale from client, using default (%s)", locale);
}
return true;
}
void InitModule()
{
UnhookFunction(hk_D3D_Create, (DWORD)InitModule);
Log(&k_discord, "InitModule::Initializing Discord Rich Presence...");
struct DiscordActivity activity;
memset(&activity, 0, sizeof(activity));
strncpy(activity.details, "Playing prueba discord rcp", sizeof(activity.details) - 1);
strncpy(activity.state, "hk_D3D_Create", sizeof(activity.state) - 1);
g_activity->update_activity(g_activity, &activity, nullptr, OnUpdateActivity);
}
void UpdateGameState() {
HANDLE pHandle = GetCurrentProcess();
// Read match state
__int8 prevMatchStarted = matchStarted;
ReadMemory(pHandle, MATCH_STARTED_ADDR, &matchStarted, sizeof(matchStarted));
// Set timestamp only when match first starts
if (matchStarted == 1 && prevMatchStarted == 0) {
auto now = std::chrono::system_clock::now();
matchStartTimestamp = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count();
matchStartTime = GetCurrentDateTime();
}
// Reset timestamp when match ends
else if (matchStarted == 0 && prevMatchStarted == 1) {
matchStartTimestamp = 0;
}
// Read scores and match info
ReadMemory(pHandle, TEAM_HOME_SCORE_ADDR, &teamHomeScore, sizeof(teamHomeScore));
ReadMemory(pHandle, TEAM_GUEST_SCORE_ADDR, &teamGuestScore, sizeof(teamGuestScore));
// Read match minute as a byte first
__int8 matchMinuteBuf;
ReadMemory(pHandle, MATCH_MINUTE_ADDR, &matchMinuteBuf, sizeof(matchMinuteBuf));
// Read match stady
ReadMemory(pHandle, MATCH_STADY_ADDR, &matchStady, sizeof(matchStady));
// Convert match minute based on match stady
if (matchStady <= 1) {
matchMinute = (int)matchMinuteBuf;
} else if (matchStady < 4) {
// Extra time
matchMinute = (int)matchMinuteBuf + 90;
} else if (matchStady == 4) {
// Penalties
matchMinute = 120;
}
// Read team names
ReadMemory(pHandle, TEAM_NAME_HOME_ADDR, teamNameHome, sizeof(teamNameHome));
ReadMemory(pHandle, TEAM_NAME_GUEST_ADDR, teamNameGuest, sizeof(teamNameGuest));
}
void UpdateDiscordActivity() {
if (!g_activity) return;
struct DiscordActivity activity;
memset(&activity, 0, sizeof(activity));
std::basic_stringstream<TCHAR> details;
std::basic_stringstream<TCHAR> state;
std::string detailsStr;
std::string stateStr;
if (matchStarted) {
details << teamNameHome << _T(" ") << teamHomeScore << _T(" - ") << teamGuestScore << _T(" ") << teamNameGuest;
if (matchStady == 0) { // Regular time
state << _T("Match Time: ") << matchMinute << _T("'");
} else if (matchStady == 1) { // Extra time
state << _T("Extra Time: ") << matchMinute << _T("'");
} else if (matchStady == 2) { // Penalties
state << _T("Penalties: ") << teamHomePenaltiesScore << _T(" - ") << teamGuestPenaltiesScore;
}
} else {
details << _T("In Menus");
state << _T("Setting up match");
}
// Convert wide strings to narrow strings for Discord API
#ifdef UNICODE
std::wstring wDetails = details.str();
std::wstring wState = state.str();
detailsStr = std::string(wDetails.begin(), wDetails.end());
stateStr = std::string(wState.begin(), wState.end());
#else
detailsStr = details.str();
stateStr = state.str();
#endif
strncpy(activity.details, detailsStr.c_str(), sizeof(activity.details) - 1);
strncpy(activity.state, stateStr.c_str(), sizeof(activity.state) - 1);
// Set timestamps only if we have a valid match timestamp
if (matchStartTimestamp > 0) {
activity.timestamps.start = matchStartTimestamp;
}
// Set assets with appropriate keys and hover text
if (matchStarted) {
strncpy(activity.assets.large_image, "large_image", sizeof(activity.assets.large_image) - 1);
strncpy(activity.assets.large_text, "Pro Evolution Soccer 6", sizeof(activity.assets.large_text) - 1);
strncpy(activity.assets.small_image, "match_icon", sizeof(activity.assets.small_image) - 1);
strncpy(activity.assets.small_text, "In Match", sizeof(activity.assets.small_text) - 1);
} else {
strncpy(activity.assets.large_image, "large_image", sizeof(activity.assets.large_image) - 1);
strncpy(activity.assets.large_text, "Pro Evolution Soccer 6", sizeof(activity.assets.large_text) - 1);
strncpy(activity.assets.small_image, "menu_icon", sizeof(activity.assets.small_image) - 1);
strncpy(activity.assets.small_text, "In Menus", sizeof(activity.assets.small_text) - 1);
}
g_activity->update_activity(g_activity, &activity, nullptr, OnUpdateActivity);
}
void DiscordRCPPresent(IDirect3DDevice8 *self, CONST RECT *src, CONST RECT *dest, HWND hWnd, LPVOID unused)
{
if (!g_core || !g_activity) return;
// Get current time
auto now = std::chrono::system_clock::now();
int64_t currentTime = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count();
// Only update if enough time has passed
if (currentTime - lastUpdateTime >= UPDATE_INTERVAL) {
UpdateGameState();
UpdateDiscordActivity();
lastUpdateTime = currentTime;
}
// Always run callbacks as they're lightweight
g_core->run_callbacks(g_core);
}