-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMGS2.SaveLocation.cpp
More file actions
282 lines (224 loc) · 7.64 KB
/
Copy pathMGS2.SaveLocation.cpp
File metadata and controls
282 lines (224 loc) · 7.64 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
#include "MGS2.framework.h"
#include <shlobj.h>
#include <shlwapi.h>
#include <string>
#include <locale>
#include <filesystem>
#pragma comment(lib, "shlwapi.lib")
namespace MGS2::SaveLocation {
const char* Category = "SaveLocation";
struct Profile {
char* Location = nullptr;
char DefaultIndex = -1; // not in use
};
char MainSavePath[MAX_PATH];
size_t CurrentLocationIndex = 0;
size_t TotalSaveLocations = 0;
size_t LastListedIndex = 0;
int TimeoutFrames = INT_MAX;
std::vector<Profile*> SaveProfiles;
HMENU ContextMenu;
void SetSaveLocation(char* savePath) {
memset(Mem::SaveGameBasePath, 0, MAX_PATH);
memcpy(Mem::SaveGameBasePath, savePath, MAX_PATH - 1);
}
tFUN_Void oFUN_0076f8cb;
void __cdecl hkFUN_0076f8cb() {
// don't let the game overwrite our location
if (!TotalSaveLocations) {
oFUN_0076f8cb();
}
}
void SetSaveLocationIndex(size_t index) {
if (!TotalSaveLocations) {
return;
}
SetSaveLocation(SaveProfiles[index]->Location);
CurrentLocationIndex = index;
std::string path = SaveProfiles[index]->Location;
size_t i = path.rfind('\\', path.length() - 2);
if (i != std::string::npos) {
path = path.substr(i + 1, path.length() - i - 2);
}
TimeoutFrames = *Mem::RenderedFrames + 48;
Log::DisplayText("Save Location [" + std::to_string(index + 1) + '/' + std::to_string(TotalSaveLocations) + ']' + '\n' + path);
}
void NextSaveLocation() {
size_t nextIndex = CurrentLocationIndex + 1;
if (nextIndex >= TotalSaveLocations) {
nextIndex = 0;
}
SetSaveLocationIndex(nextIndex);
}
void PreviousSaveLocation() {
SetSaveLocationIndex(
(CurrentLocationIndex == 0) ?
TotalSaveLocations - 1 :
CurrentLocationIndex - 1);
}
void NextSaveLocation(Actions::Action action) {
NextSaveLocation();
}
void PreviousSaveLocation(Actions::Action action) {
PreviousSaveLocation();
}
void OpenExplorer(Actions::Action action) {
Actions::OpenExplorer((const char*)Mem::SaveGameBasePath);
}
void OpenDirectory(Actions::Action action) {
ShowCursor(CURSOR_SHOWING);
IFileDialog* pfd;
if (SUCCEEDED(CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd))))
{
DWORD dwOptions;
if (SUCCEEDED(pfd->GetOptions(&dwOptions)))
{
pfd->SetTitle(L"Select MGS2 Saves Directory");
pfd->SetOptions(dwOptions | FOS_PICKFOLDERS);
}
if (SUCCEEDED(pfd->Show(NULL)))
{
IShellItem* psi;
if (SUCCEEDED(pfd->GetResult(&psi)))
{
LPWSTR wPath[MAX_PATH * 2];
if (SUCCEEDED(psi->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, wPath)))
{
PathAddBackslashW(*wPath);
wcstombs((char*)wPath, *wPath, MAX_PATH - 1);
SetSaveLocation((char*)wPath);
CurrentLocationIndex = -1;
if (LastListedIndex == -1) {
LastListedIndex = -2;
}
TimeoutFrames = 0;
}
psi->Release();
}
}
pfd->Release();
}
ShowCursor(CURSOR_SUPPRESSED);
}
void ReloadSaveGameList(uintptr_t param_1) {
uintptr_t p2 = *(uintptr_t*)(param_1 + 0x5c);
int val = 1;
if (val == 7) {
*(int*)(param_1 + 0x48) = 0;
}
((void(*)(int))0x761fc5)(2);
((void(*)(int))0x761fc5)(4);
((void(*)(int))0x761f77)(p2);
((void(*)(int, int))0x761ef1)(p2, val);
TimeoutFrames = INT_MAX;
}
tFUN_007902a5 oFUN_007902a5;
void __cdecl hkFUN_007902a5(int param_1, int param_2, unsigned int param_3) {
try_mgs2
bool doAction = true;
if (Actions::ShortcutActive("SaveLocation.PreviousLocation")) {
PreviousSaveLocation();
}
else if (Actions::ShortcutActive("SaveLocation.NextLocation")) {
NextSaveLocation();
}
if ((LastListedIndex != CurrentLocationIndex) && (*Mem::RenderedFrames > TimeoutFrames)) {
ReloadSaveGameList(param_1);
}
catch_mgs2(Category, "7902A5");
oFUN_007902a5(param_1, param_2, param_3);
}
tFUN_Void_Int oFUN_00761e37;
void __cdecl hkFUN_00761e37(int param_1) {
LastListedIndex = CurrentLocationIndex;
oFUN_00761e37(param_1);
}
void AddLocationToContextMenu(const char* name) {
AppendMenuA(ContextMenu, MF_STRING, 5000 + TotalSaveLocations, name);
}
void Run(CSimpleIniA& ini) {
if (ini.IsEmpty() || (!ini.GetBoolValue(Category, "Enabled", false))) {
return;
}
const char* contextMenuText = ini.GetValue(Category, "ContextMenu", "");
bool useContextMenu = (strcmp(contextMenuText, "") != 0);
if (useContextMenu) {
ContextMenu = CreatePopupMenu();
}
const char* str = ini.GetValue(Category, "BasePath", "");
FilesystemHelper::SetCurrentPath();
char* basePath = nullptr;
auto dirEntry = std::filesystem::directory_entry(str);
if (dirEntry.exists() && dirEntry.is_directory()) {
basePath = _strdup(std::filesystem::absolute(dirEntry.path()).string().c_str());
}
std::list<CSimpleIniA::Entry> paths;
bool anyPathsFound = ini.GetAllValues(Category, "Path", paths);
paths.sort(typename CSimpleIniA::Entry::LoadOrder());
if (anyPathsFound) {
CSimpleIniA::TNamesDepend::const_iterator it;
for (it = paths.begin(); it != paths.end(); ++it) {
char* path = _strdup(it->pItem);
PathRemoveBackslashA(path);
PathAddBackslashA(path);
if (char* fullSavePath = (char*)calloc(MAX_PATH, 1)) {
if (PathIsDirectoryA(path)) {
strncpy(fullSavePath, path, MAX_PATH - 2);
}
else if (basePath != nullptr) {
strncpy(fullSavePath, basePath, MAX_PATH - 2);
PathAppendA(fullSavePath, path);
if (!PathIsDirectoryA(fullSavePath)) {
free(fullSavePath);
fullSavePath = nullptr;
}
}
if (fullSavePath != nullptr) {
SaveProfiles.push_back(new Profile{ fullSavePath });
TotalSaveLocations++;
if (useContextMenu) {
AddLocationToContextMenu(fullSavePath);
}
}
}
}
}
else if (basePath != nullptr) {
using it = std::filesystem::directory_iterator;
for (const auto& path : it(basePath)) {
if (path.is_directory()) {
std::filesystem::path canonicalPath = std::filesystem::canonical(path);
char* fullSavePath = new char[MAX_PATH]{ 0 };
wcstombs(fullSavePath, canonicalPath.c_str(), MAX_PATH - 2);
PathAddBackslashA(fullSavePath);
SaveProfiles.push_back(new Profile{ fullSavePath });
TotalSaveLocations++;
if (useContextMenu) {
AddLocationToContextMenu(fullSavePath);
}
}
}
}
FilesystemHelper::RevertCurrentPath();
free(basePath);
if (TotalSaveLocations) {
SetSaveLocation(SaveProfiles[0]->Location);
if (useContextMenu) {
ContextMenuItem* contextMenuItem = new ContextMenuItem;
contextMenuItem->Enabled = true;
contextMenuItem->FriendlyName = contextMenuText;
contextMenuItem->ContextMenu = ContextMenu;
Actions::AddContextMenuItem(contextMenuItem);
}
}
Actions::RegisterAction(ini, "SaveLocation.OpenLocationDialog", &OpenDirectory);
Actions::RegisterAction(ini, "SaveLocation.OpenExplorer", &OpenExplorer);
Actions::RegisterAction(ini, "SaveLocation.PreviousLocationGlobal", &PreviousSaveLocation);
Actions::RegisterAction(ini, "SaveLocation.NextLocationGlobal", &NextSaveLocation);
Actions::RegisterShortcut(ini, "SaveLocation.PreviousLocation");
Actions::RegisterShortcut(ini, "SaveLocation.NextLocation");
oFUN_007902a5 = (tFUN_007902a5)mem::TrampHook32((BYTE*)0x7902A5, (BYTE*)hkFUN_007902a5, 7);
oFUN_0076f8cb = (tFUN_Void)mem::TrampHook32((BYTE*)0x76F8CB, (BYTE*)hkFUN_0076f8cb, 9);
oFUN_00761e37 = (tFUN_Void_Int)mem::TrampHook32((BYTE*)0x761E37, (BYTE*)hkFUN_00761e37, 5);
}
}