forked from kubik-jaroslav/ddda-dinput8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveBackup.cpp
More file actions
151 lines (137 loc) · 3.74 KB
/
Copy pathSaveBackup.cpp
File metadata and controls
151 lines (137 loc) · 3.74 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
#include "stdafx.h"
#include "SaveBackup.h"
string saveDir, savePath;
int saveLimit;
void printError(LPCSTR msg, DWORD error)
{
logFile << msg << ": ";
if (error == ERROR_FILE_NOT_FOUND)
logFile << "file not found";
else if (error == ERROR_PATH_NOT_FOUND)
logFile << "path not found";
else if (error == ERROR_INVALID_NAME)
logFile << "invalid path";
else
logFile << (LPVOID)error;
logFile << std::endl;
}
void clearBackups()
{
WIN32_FIND_DATA ffd;
HANDLE hFind = FindFirstFile((saveDir + "ddda_*.sav").c_str(), &ffd);
if (hFind == INVALID_HANDLE_VALUE)
return;
std::vector<std::pair<UINT64, string>> files;
do
{
SYSTEMTIME t;
if (sscanf_s(ffd.cFileName, "ddda_%04hd-%02hd-%02hd_%02hd-%02hd-%02hd.sav",
&t.wYear, &t.wMonth, &t.wDay, &t.wHour, &t.wMinute, &t.wSecond) == 6)
{
UINT64 time;
SystemTimeToFileTime(&t, (LPFILETIME)&time);
files.push_back({ time, ffd.cFileName });
}
} while (FindNextFile(hFind, &ffd));
FindClose(hFind);
sort(files.begin(), files.end());
for (int i = 0; i < (int)files.size() - saveLimit; i++)
{
string file = saveDir + files[i].second;
DeleteFile(file.c_str());
logFile << "Backup deleted: " << file << std::endl;
}
}
void __stdcall handleSave()
{
WIN32_FILE_ATTRIBUTE_DATA fileData;
if (GetFileAttributesEx(savePath.c_str(), GetFileExInfoStandard, &fileData))
{
SYSTEMTIME systemTime, t;
FileTimeToSystemTime(&fileData.ftLastWriteTime, &systemTime);
SystemTimeToTzSpecificLocalTime(nullptr, &systemTime, &t);
CHAR str[64];
sprintf_s(str, "ddda_%04hd-%02hd-%02hd_%02hd-%02hd-%02hd.sav", t.wYear, t.wMonth, t.wDay, t.wHour, t.wMinute, t.wSecond);
string backupPath = saveDir + str;
if (CopyFile(savePath.c_str(), backupPath.c_str(), FALSE))
{
logFile << "Backup created: " << backupPath.c_str() << std::endl;
if (saveLimit > 0)
clearBackups();
}
else
printError("Creating backup", GetLastError());
}
else
printError("Finding save", GetLastError());
}
LPVOID oSaveGame = nullptr;
void __declspec(naked) HSaveGame()
{
__asm
{
pushad;
call handleSave;
popad;
jmp oSaveGame;
}
}
bool findSavePath()
{
string configDir = config.getStr("main", "savePath");
if (configDir.empty())
{
HMODULE hModule = GetModuleHandle("steam_api.dll");
if (hModule)
{
char buf[1024];
tSteamUser pSteamUser = (tSteamUser)GetProcAddress(hModule, "SteamUser");
if (pSteamUser && pSteamUser() && pSteamUser()->GetUserDataFolder(buf, 1024))
{
saveDir = string(buf);
size_t index = saveDir.rfind("local");
if (index != string::npos)
{
saveDir.erase(index);
saveDir += "remote";
}
}
}
}
else
saveDir = configDir;
saveDir.erase(saveDir.find_last_not_of('\\') + 1);
DWORD attributes = GetFileAttributes(saveDir.c_str());
if (attributes == INVALID_FILE_ATTRIBUTES || (attributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
{
if (configDir.empty())
logFile << "SaveBackup path: NOT FOUND, SET MANUALLY IN DINPUT8.INI" << std::endl;
else
logFile << "SaveBackup path: INVALID PATH - " << saveDir << std::endl;
return false;
}
saveDir.push_back('\\');
savePath = saveDir + "ddda.sav";
logFile << "SaveBackup path: " << savePath << std::endl;
return true;
}
void Hooks::SaveBackup()
{
if (!config.getBool("main", "backupSaves", false) || !findSavePath())
{
logFile << "SaveBackup: disabled" << std::endl;
return;
}
saveLimit = config.getInt("main", "saveLimit", -1);
BYTE *pSaveName;
BYTE saveName[] = "DDDA.sav";
if (FindData("SaveBackup", saveName, &pSaveName))
{
BYTE sig[] = { 0x8D, 0x93, 0xE6, 0x09, 0x00, 0x00,
0x68, 0x00, 0x00, 0x00, 0x00 };
*(LPDWORD)(sig + 7) = (DWORD)pSaveName;
BYTE *pOffset;
if (FindSignature("SaveBackup", sig, &pOffset))
CreateHook("SaveBackup", pOffset, &HSaveGame, &oSaveGame);
}
}