-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveLimbusData.cpp
More file actions
553 lines (504 loc) · 18.2 KB
/
MoveLimbusData.cpp
File metadata and controls
553 lines (504 loc) · 18.2 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// g++ -o MoveLimbusData.exe MoveLimbusData.cpp resource.o -static-libgcc -static-libstdc++ -static -O2 -s -lshlwapi -lshell32
#include <windows.h>
#include <shellapi.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <string>
#include <vector>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <sstream>
#include <cctype>
#pragma comment(lib, "Shell32.lib")
#pragma comment(lib, "Shlwapi.lib")
// 控制台颜色常量
enum Color {
BLACK = 0,
DARK_BLUE = 1,
DARK_GREEN = 2,
DARK_CYAN = 3,
DARK_RED = 4,
DARK_MAGENTA = 5,
DARK_YELLOW = 6,
LIGHT_GRAY = 7,
DARK_GRAY = 8,
BLUE = 9,
GREEN = 10,
CYAN = 11,
RED = 12,
MAGENTA = 13,
YELLOW = 14,
WHITE = 15
};
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
WORD originalColor = 7; // 默认浅灰色
void SetColor(WORD color) {
SetConsoleTextAttribute(hConsole, color);
}
void ResetColor() {
SetConsoleTextAttribute(hConsole, originalColor);
}
// 初始化保存原始颜色
void InitColor() {
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (GetConsoleScreenBufferInfo(hConsole, &csbi)) {
originalColor = csbi.wAttributes;
}
}
// 检查管理员权限
bool IsAdmin() {
BOOL isAdmin = FALSE;
PSID adminGroup = NULL;
SID_IDENTIFIER_AUTHORITY ntAuthority = SECURITY_NT_AUTHORITY;
if (AllocateAndInitializeSid(&ntAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &adminGroup)) {
CheckTokenMembership(NULL, adminGroup, &isAdmin);
FreeSid(adminGroup);
}
return isAdmin == TRUE;
}
// 请求 UAC 提权
void ElevateNow() {
wchar_t exePath[MAX_PATH];
GetModuleFileNameW(NULL, exePath, MAX_PATH);
HINSTANCE result = ShellExecuteW(NULL, L"runas", exePath, NULL, NULL, SW_SHOWNORMAL);
if ((INT_PTR)result <= 32) {
MessageBoxW(NULL, L"需要管理员权限才能运行。\n请右键点击程序,选择“以管理员身份运行”。", L"权限不足", MB_ICONERROR);
}
ExitProcess(0);
}
// 字符串转换
std::wstring toWide(const std::string& str) {
int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0);
std::wstring wstr(len, 0);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &wstr[0], len);
return wstr;
}
std::string toUtf8(const std::wstring& wstr) {
int len = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
std::string str(len, 0);
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, &str[0], len, nullptr, nullptr);
return str;
}
// 获取环境变量
std::wstring GetEnvVar(const std::wstring& name) {
DWORD len = GetEnvironmentVariableW(name.c_str(), nullptr, 0);
if (len == 0) return L"";
std::vector<wchar_t> buffer(len);
GetEnvironmentVariableW(name.c_str(), buffer.data(), len);
return std::wstring(buffer.data());
}
// 路径是否存在
bool PathExists(const std::wstring& path) {
DWORD attr = GetFileAttributesW(path.c_str());
return (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY));
}
// 路径是否为符号链接/目录链接
bool IsSymLink(const std::wstring& path) {
DWORD attr = GetFileAttributesW(path.c_str());
if (attr == INVALID_FILE_ATTRIBUTES) return false;
return (attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0;
}
// 删除符号链接(不删除目标)
bool DeleteSymLink(const std::wstring& link) {
if (RemoveDirectoryW(link.c_str())) return true;
DWORD err = GetLastError();
SetColor(RED);
std::cerr << "删除符号链接失败: " << toUtf8(link) << " 错误码: " << err << "\n";
ResetColor();
return false;
}
// 获取磁盘可用空间(GB)
__int64 GetFreeSpaceGB(const std::wstring& rootPath) {
ULARGE_INTEGER freeBytesAvailable, totalBytes, totalFreeBytes;
if (!GetDiskFreeSpaceExW(rootPath.c_str(), &freeBytesAvailable, &totalBytes, &totalFreeBytes))
return -1;
return static_cast<__int64>(totalFreeBytes.QuadPart / (1024LL * 1024 * 1024));
}
// 移动文件夹(跨卷)
bool MoveFolder(const std::wstring& src, const std::wstring& dst) {
std::wstring dstParent = dst;
PathRemoveFileSpecW(&dstParent[0]);
int ret = SHCreateDirectoryExW(NULL, dstParent.c_str(), NULL);
if (ret != ERROR_SUCCESS && ret != ERROR_FILE_EXISTS && ret != ERROR_ALREADY_EXISTS) {
SetColor(RED);
std::cerr << "创建目录失败: " << toUtf8(dstParent) << " 错误码: " << ret << "\n";
ResetColor();
return false;
}
std::wstring from = src + L'\0';
std::wstring to = dst + L'\0';
SHFILEOPSTRUCTW op = {0};
op.wFunc = FO_MOVE;
op.pFrom = from.c_str();
op.pTo = to.c_str();
op.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMMKDIR;
int result = SHFileOperationW(&op);
if (result != 0) {
SetColor(RED);
std::cerr << "SHFileOperation 失败,返回值: " << result;
if (op.fAnyOperationsAborted) {
std::cerr << " (操作被用户取消)";
}
std::cerr << "\n";
ResetColor();
return false;
}
return true;
}
// 创建目录符号链接
bool CreateSymLink(const std::wstring& link, const std::wstring& target) {
return CreateSymbolicLinkW(link.c_str(), target.c_str(), SYMBOLIC_LINK_FLAG_DIRECTORY) != 0;
}
// 去除字符串首尾空白
std::string trim(const std::string& str) {
size_t first = str.find_first_not_of(" \t\r\n");
if (first == std::string::npos) return "";
size_t last = str.find_last_not_of(" \t\r\n");
return str.substr(first, last - first + 1);
}
// 定义文件夹对
struct FolderPair {
std::wstring appdataPath;
std::wstring relPath;
bool exists;
};
// 扫描可用盘符(排除 C 盘)
std::vector<std::pair<wchar_t, __int64>> ScanDrives() {
std::vector<std::pair<wchar_t, __int64>> drives;
for (wchar_t drive = L'C'; drive <= L'Z'; ++drive) {
if (drive == L'C') continue;
std::wstring root = std::wstring(1, drive) + L":\\";
UINT type = GetDriveTypeW(root.c_str());
if (type == DRIVE_FIXED || type == DRIVE_REMOVABLE || type == DRIVE_RAMDISK) {
__int64 freeGB = GetFreeSpaceGB(root);
if (freeGB >= 20) {
drives.emplace_back(drive, freeGB);
}
}
}
return drives;
}
// 让用户选择盘符(支持手动输入)
wchar_t SelectDrive(const std::vector<std::pair<wchar_t, __int64>>& drives) {
if (!drives.empty()) {
int index = 1;
for (const auto& d : drives) {
SetColor(WHITE);
std::cout << "[" << index++ << "] ";
SetColor(CYAN);
std::cout << static_cast<char>(d.first) << ": 盘";
ResetColor();
std::cout << " - 可用空间: " << d.second << " GB\n";
}
SetColor(YELLOW);
std::cout << "\n请选择目标盘符 [1-" << drives.size() << "]: ";
ResetColor();
int choice;
std::cin >> choice;
if (choice >= 1 && choice <= static_cast<int>(drives.size())) {
return drives[choice - 1].first;
}
SetColor(RED);
std::cerr << "选择无效,将尝试手动输入。\n";
ResetColor();
}
SetColor(YELLOW);
std::cout << "请输入目标盘符(例如 D): ";
ResetColor();
char driveChar;
std::cin >> driveChar;
driveChar = toupper(driveChar);
if (driveChar >= 'C' && driveChar <= 'Z') {
return driveChar;
}
return 0;
}
int main() {
InitColor();
if (!IsAdmin()) {
ElevateNow();
return 0;
}
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
SetColor(CYAN);
std::cout << "\n";
std::cout << "================================================\n";
std::cout << " Limbus Company 文件夹迁移工具\n";
std::cout << " By Bilibili@都市零协会汉化组 曾小皮-ZengXiaoPi\n";
std::cout << "================================================\n\n";
ResetColor();
std::wstring userProfile = GetEnvVar(L"USERPROFILE");
if (userProfile.empty()) {
SetColor(RED);
std::cerr << "X 错误: 无法获取 USERPROFILE 环境变量\n";
ResetColor();
system("pause");
return 1;
}
std::vector<FolderPair> folders = {
{ userProfile + L"\\AppData\\LocalLow\\ProjectMoon\\LimbusCompany", L"LimbusCompanyData\\LimbusCompany", false },
{ userProfile + L"\\AppData\\LocalLow\\Unity", L"UnityData\\Unity", false }
};
SetColor(YELLOW);
std::cout << "请选择操作:\n";
std::cout << " [1] 迁移数据到目标盘(将 C盘 数据移动到目标盘,比如 D盘)\n";
std::cout << " [2] 恢复数据到原位置(将目标盘数据移回 C盘)\n";
ResetColor();
std::cout << "请输入 [1/2]: ";
int mode;
std::cin >> mode;
if (mode != 1 && mode != 2) {
SetColor(RED);
std::cerr << "X 选择无效\n";
ResetColor();
system("pause");
return 1;
}
std::cout << "\n";
// ---------- 迁移模式 ----------
if (mode == 1) {
int existCount = 0;
for (auto& f : folders) {
f.exists = PathExists(f.appdataPath);
if (f.exists) existCount++;
}
if (existCount == 0) {
SetColor(RED);
std::cerr << "X 错误: 没有找到需要迁移的文件夹\n";
ResetColor();
system("pause");
return 1;
}
SetColor(WHITE);
std::cout << "将迁移以下文件夹:\n";
ResetColor();
for (const auto& f : folders) {
if (f.exists) {
std::cout << " - " << toUtf8(f.appdataPath) << "\n";
}
}
std::cout << "\n";
auto drives = ScanDrives();
if (drives.empty()) {
SetColor(RED);
std::cerr << "X 错误: 没有找到可用空间 ≥20GB 的盘符\n";
ResetColor();
system("pause");
return 1;
}
wchar_t targetDrive = SelectDrive(drives);
if (targetDrive == 0) {
SetColor(RED);
std::cerr << "X 无效盘符\n";
ResetColor();
system("pause");
return 1;
}
std::wstring baseTarget = std::wstring(1, targetDrive) + L":\\MoveData\\";
for (const auto& f : folders) {
if (!f.exists) continue;
std::wstring dst = baseTarget + f.relPath;
if (PathExists(dst)) {
SetColor(RED);
std::cerr << "X 目标路径已存在: " << toUtf8(dst) << "\n";
std::cerr << " 请手动处理后再试。\n";
ResetColor();
system("pause");
return 1;
}
}
// ----- 新增检测:检查是否已经搬迁过(原位置存在符号链接)-----
std::vector<std::wstring> checkPaths = {
userProfile + L"\\AppData\\LocalLow\\ProjectMoon\\LimbusCompany",
userProfile + L"\\AppData\\LocalLow\\Unity",
userProfile + L"\\AppData\\LocalLow\\Unity\\ProjectMoon_LimbusCompany"
};
bool alreadyLinked = false;
for (const auto& p : checkPaths) {
if (IsSymLink(p)) {
if (!alreadyLinked) {
SetColor(RED);
std::cout << "警告: 以下路径已经是符号链接,可能已经搬迁过:\n";
alreadyLinked = true;
}
std::cout << " - " << toUtf8(p) << "\n";
}
}
if (alreadyLinked) {
ResetColor(); // 恢复颜色以便后续提示使用黄色
SetColor(YELLOW);
std::cout << "\n如果你确定要重新搬迁(覆盖现有符号链接),请输入 IMSURE 继续: ";
ResetColor();
std::string sureInput;
std::cin >> sureInput;
if (sureInput != "IMSURE") {
SetColor(RED);
std::cerr << "X 输入错误,操作已取消\n";
ResetColor();
system("pause");
return 1;
}
}
// -------------------------------------------------------------
std::string confirmText = "FACETHEFEARSAVETHEEGO";
std::string input;
SetColor(YELLOW);
std::cout << "请输入指定文字确认迁移 [输入 '" << confirmText << "']: ";
ResetColor();
std::cin.ignore(); // 忽略之前输入中的换行符
std::getline(std::cin, input);
if (trim(input) != confirmText) {
SetColor(RED);
std::cerr << "X 确认文字错误,操作已取消\n";
ResetColor();
system("pause");
return 1;
}
SetColor(CYAN);
std::cout << "开始迁移...\n";
ResetColor();
bool allSuccess = true;
for (const auto& f : folders) {
if (!f.exists) continue;
std::wstring dst = baseTarget + f.relPath;
std::cout << "移动 " << toUtf8(f.appdataPath) << " -> " << toUtf8(dst) << " ...\n";
if (!MoveFolder(f.appdataPath, dst)) {
SetColor(RED);
std::cerr << "X 移动失败: " << toUtf8(f.appdataPath) << "\n";
ResetColor();
allSuccess = false;
continue;
}
std::cout << "创建符号链接 " << toUtf8(f.appdataPath) << " -> " << toUtf8(dst) << " ...(杀毒软件可能会弹窗,允许即可!)\n";
if (!CreateSymLink(f.appdataPath, dst)) {
DWORD err = GetLastError();
SetColor(RED);
std::cerr << "X 创建符号链接失败 (错误码: " << err << "),可能权限不足\n";
ResetColor();
allSuccess = false;
} else {
SetColor(GREEN);
std::cout << "[OK] " << toUtf8(f.appdataPath) << " 迁移完成\n\n";
ResetColor();
}
}
if (allSuccess) {
SetColor(GREEN);
std::cout << "[OK] 迁移完成!\n";
ResetColor();
} else {
SetColor(RED);
std::cout << "部分操作失败,请检查以上错误信息。\n";
ResetColor();
}
system("pause");
return allSuccess ? 0 : 1;
}
// ---------- 恢复模式 ----------
else {
auto drives = ScanDrives();
wchar_t targetDrive = SelectDrive(drives);
if (targetDrive == 0) {
SetColor(RED);
std::cerr << "X 无效盘符\n";
ResetColor();
system("pause");
return 1;
}
std::wstring baseTarget = std::wstring(1, targetDrive) + L":\\MoveData\\";
int existCount = 0;
for (auto& f : folders) {
std::wstring targetPath = baseTarget + f.relPath;
f.exists = PathExists(targetPath);
if (f.exists) existCount++;
}
if (existCount == 0) {
SetColor(RED);
std::cerr << "X 错误: 在目标盘上没有找到需要恢复的文件夹\n";
ResetColor();
system("pause");
return 1;
}
SetColor(WHITE);
std::cout << "将恢复以下文件夹:\n";
ResetColor();
for (const auto& f : folders) {
if (f.exists) {
std::wstring targetPath = baseTarget + f.relPath;
std::cout << " - " << toUtf8(targetPath) << " -> " << toUtf8(f.appdataPath) << "\n";
}
}
std::cout << "\n";
for (const auto& f : folders) {
if (!f.exists) continue;
if (PathExists(f.appdataPath)) {
if (IsSymLink(f.appdataPath)) {
SetColor(YELLOW);
std::cout << "原位置存在符号链接,将删除: " << toUtf8(f.appdataPath) << "\n";
ResetColor();
if (!DeleteSymLink(f.appdataPath)) {
SetColor(RED);
std::cerr << "X 无法删除符号链接,请手动处理\n";
ResetColor();
system("pause");
return 1;
}
} else {
SetColor(RED);
std::cerr << "X 原位置已存在真实文件夹,请手动处理后再恢复: " << toUtf8(f.appdataPath) << "\n";
ResetColor();
system("pause");
return 1;
}
}
}
std::string confirmText = "FACETHEFEARSAVETHEEGO";
std::string input;
SetColor(YELLOW);
std::cout << "请输入指定文字确认恢复 [输入 '" << confirmText << "']: ";
ResetColor();
std::cin.ignore();
std::getline(std::cin, input);
if (trim(input) != confirmText) {
SetColor(RED);
std::cerr << "X 确认文字错误,操作已取消\n";
ResetColor();
system("pause");
return 1;
}
SetColor(CYAN);
std::cout << "开始恢复...\n";
ResetColor();
bool allSuccess = true;
for (const auto& f : folders) {
if (!f.exists) continue;
std::wstring src = baseTarget + f.relPath;
std::wstring dst = f.appdataPath;
std::cout << "移动 " << toUtf8(src) << " -> " << toUtf8(dst) << " ...\n";
if (!MoveFolder(src, dst)) {
SetColor(RED);
std::cerr << "X 移动失败: " << toUtf8(src) << "\n";
ResetColor();
allSuccess = false;
continue;
}
SetColor(GREEN);
std::cout << "[OK] " << toUtf8(src) << " 恢复完成\n\n";
ResetColor();
}
if (allSuccess) {
SetColor(GREEN);
std::cout << "[OK] 恢复完成!\n";
ResetColor();
} else {
SetColor(RED);
std::cout << "部分操作失败,请检查以上错误信息。\n";
ResetColor();
}
system("pause");
return allSuccess ? 0 : 1;
}
}