Skip to content

Commit 85db8dd

Browse files
committed
Refactor SteamAuth Caching System
+ Add dw_cacheTicket dvar + Automatically delete bad cache files
1 parent f783fa9 commit 85db8dd

5 files changed

Lines changed: 119 additions & 25 deletions

File tree

components/game_mod/com_files.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,39 @@ int __cdecl FS_FOpenFileWriteToDir(const char *filename, const char *dir, const
151151

152152
return FS_FOpenFileWriteToDir_o(filename, dir, osbasepath);
153153
}
154+
155+
unsigned __int64 FS_FileAge(const char* path)
156+
{
157+
// Quick lambda for converting filetimes to ull
158+
auto ft2uint64 = [](const FILETIME& ft)
159+
{
160+
ULARGE_INTEGER ul;
161+
ul.LowPart = ft.dwLowDateTime;
162+
ul.HighPart = ft.dwHighDateTime;
163+
return ul.QuadPart;
164+
};
165+
166+
HANDLE h = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
167+
if (h == INVALID_HANDLE_VALUE)
168+
return UINT64_MAX;
169+
170+
// Retrieve the file times for the file
171+
FILETIME create, access, write;
172+
auto err = GetFileTime(h, &create, &access, &write);
173+
CloseHandle(h);
174+
175+
// Abort if we couldn't get the filetime
176+
if (!err)
177+
return UINT64_MAX;
178+
179+
FILETIME sys_time;
180+
GetSystemTimeAsFileTime(&sys_time);
181+
182+
auto age = (ft2uint64(sys_time) - ft2uint64(write)) / 10000;
183+
return __int64(age);
184+
}
185+
186+
unsigned __int64 FS_FileAge_Sec(const char* path)
187+
{
188+
return FS_FileAge(path) / 1000;
189+
}

components/game_mod/com_files.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,15 @@ int __cdecl FS_GetModList(char *listbuf, int bufsize);
9393
// Used only by Com_WriteConfigToFile and Com_WriteKeyConfigToFile
9494
//
9595
int __cdecl FS_FOpenFileWriteToDir(const char *filename, const char *dir, const char *osbasepath);
96+
97+
//
98+
// Get the age of a file in milliseconds
99+
// returns UINT64_MAX if the file doesn't exist
100+
//
101+
unsigned __int64 FS_FileAge(const char* path);
102+
103+
//
104+
// Get the age of a file in seconds
105+
// returns UINT64_MAX if the file doesn't exist
106+
//
107+
unsigned __int64 FS_FileAge_Sec(const char* path);

components/game_mod/dvar.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ dvar_s* com_developer_print = NULL;
1919

2020
dvar_s* sv_showCommands = NULL;
2121

22+
dvar_s* dw_cacheTicket = NULL;
23+
2224
dvar_s* radiant_live = NULL;
2325
dvar_s* radiant_livePort = NULL;
2426
dvar_s* radiant_liveDebug = NULL;
@@ -92,6 +94,8 @@ void R_RegisterCustomDvars()
9294
sv_showCommands = Dvar_RegisterBool("sv_showCommands", 0, 0, "Print client commands in the log file");
9395

9496
r_streamCheckAabb = Dvar_RegisterBool("r_streamCheckAabb", false, DVAR_CHEAT, "Enables runtime checking of the stream aabb tree");
97+
98+
dw_cacheTicket = Dvar_RegisterBool("dw_cacheTicket", true, DVAR_ARCHIVE, "Cache the SteamAuth ticket to the disk");
9599
}
96100

97101
void* rtn_R_RegisterDvars = (void*)0x006CA283;

components/game_mod/dvar.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ extern dvar_s* com_developer_print;
208208

209209
extern dvar_s* sv_showCommands;
210210

211+
extern dvar_s* dw_cacheTicket;
212+
211213
extern dvar_s* radiant_live;
212214
extern dvar_s* radiant_livePort;
213215
extern dvar_s* radiant_liveDebug;

components/game_mod/live_steam_client.cpp

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,31 @@ namespace LiveSteam
7070
return 1;
7171
}
7272

73+
//
74+
// Error handler lambda that automatically:
75+
// + frees the cache file handle
76+
// + deletes the cache file
77+
// + clears the memory cache
78+
//
79+
auto DeleteBadCache = [this, h](void)
80+
{
81+
// Close the file handle so we can actually delete the file
82+
fclose(h);
83+
84+
DeleteFileA(AUTHCACHE_FILEPATH);
85+
ClearCache();
86+
};
87+
88+
//
89+
// Check the age of the cache file
90+
// If it's too old we need to delete it and clear the cache
91+
//
92+
if (FS_FileAge_Sec(AUTHCACHE_FILEPATH) > 60)
93+
{
94+
DeleteBadCache();
95+
return 5;
96+
}
97+
7398
// We only need to allocate a buffer for m_steamCookieKey if it doesn't already have one.
7499
// Since m_steamCookieKeySize is constant, we don't need to worry about resizing the buffer
75100
if (m_steamCookieKey == nullptr)
@@ -78,8 +103,7 @@ namespace LiveSteam
78103
if (fread(m_steamCookieKey, 1, m_steamCookieKeySize, h) != m_steamCookieKeySize)
79104
{
80105
// Unable to read the correct number of bytes for the cookie key
81-
fclose(h);
82-
ClearCache();
106+
DeleteBadCache();
83107
return 2;
84108
}
85109

@@ -104,8 +128,7 @@ namespace LiveSteam
104128
if (fread(m_steamAppTicket, 1, m_steamAppTicketSize, h) != m_steamAppTicketSize)
105129
{
106130
// Unable to read the correct number of bytes for the app ticket
107-
fclose(h);
108-
ClearCache();
131+
DeleteBadCache();
109132
return 3;
110133
}
111134

@@ -193,25 +216,36 @@ bool LiveSteamClient::GetRetrievedEncryptedAppTicket(void *ticketBuf, const unsi
193216
auto steamUser = (*(void *(__cdecl **)())0x009A356C)();
194217
auto getEncryptedAppTicket = *(bool(__thiscall **)(void *, void *, int, unsigned int *))(*(DWORD *)steamUser + 80);
195218

196-
// Use the real function if Steam had a good ticket
197-
if (this->rawTicketResult == k_EResultOK)
219+
bool useCache = dw_cacheTicket && dw_cacheTicket->current.enabled;
220+
221+
//
222+
// Use the real function if one of the following conditions are met:
223+
// + Steam had a good ticket
224+
// + The caching subsystem is disabled
225+
//
226+
// Otherwise we fallback to using the cached ticket
227+
//
228+
if (this->rawTicketResult == k_EResultOK || !useCache)
198229
{
199230
if (getEncryptedAppTicket(steamUser, ticketBuf, ticketBufSize, ticketSize))
200231
{
201-
DBG_ASSERT(g_authService);
202-
if (!g_authService)
232+
if (useCache)
203233
{
204-
Com_DPrintf(1, "STEAM: Couldn't cache recieved auth ticket - g_authService is NULL\n");
205-
return true;
206-
}
207-
208-
// Update the cache with the generated ticket & DW cookie
209-
g_authCache.UpdateCache(ticketBuf, *ticketSize, g_authService->m_steamCookieKey);
210-
211-
// Commit the new cache contents to the disk
212-
if (g_authCache.CommitCache() == 0)
213-
{
214-
Com_DPrintf(1, "STEAM: Wrote cached auth ticket\n");
234+
DBG_ASSERT(g_authService);
235+
if (!g_authService)
236+
{
237+
Com_DPrintf(1, "STEAM: Couldn't cache recieved auth ticket - g_authService is NULL\n");
238+
return true;
239+
}
240+
241+
// Update the cache with the generated ticket & DW cookie
242+
g_authCache.UpdateCache(ticketBuf, *ticketSize, g_authService->m_steamCookieKey);
243+
244+
// Commit the new cache contents to the disk
245+
if (g_authCache.CommitCache() == 0)
246+
{
247+
Com_DPrintf(1, "STEAM: Wrote cached auth ticket\n");
248+
}
215249
}
216250

217251
Com_DPrintf(1, "STEAM: Retrieved auth ticket from Steam, sending to DemonWare\n");
@@ -266,15 +300,21 @@ void LiveSteamClient::OnRequestEncryptedAppTicket(EncryptedAppTicketResponse_t *
266300
break;
267301
}
268302

269-
// If the request was rate limited, check if there's a cached ticket
270-
if (pEncryptedAppTicketResponse->m_eResult == k_EResultLimitExceeded)
303+
//
304+
// Only use the caching subsystem if we explicitly have it enabled
305+
//
306+
if (dw_cacheTicket && dw_cacheTicket->current.enabled)
271307
{
272-
if (g_authCache.ValidateCache())
308+
// If the request was rate limited, check if there's a cached ticket
309+
if (pEncryptedAppTicketResponse->m_eResult == k_EResultLimitExceeded)
273310
{
274-
Com_DPrintf(1, "STEAM: Attempting to use cached auth ticket...\n");
311+
if (g_authCache.ValidateCache())
312+
{
313+
Com_DPrintf(1, "STEAM: Attempting to use cached auth ticket...\n");
275314

276-
pEncryptedAppTicketResponse->m_eResult = k_EResultOK;
277-
dwLogonSeAcquiredSteamTicket();
315+
pEncryptedAppTicketResponse->m_eResult = k_EResultOK;
316+
dwLogonSeAcquiredSteamTicket();
317+
}
278318
}
279319
}
280320

0 commit comments

Comments
 (0)