66 lines
1.9 KiB
C++
Raw Normal View History

2024-05-03 22:31:17 +08:00
#pragma comment(linker, "/subsystem:windows /entry:wmainCRTStartup")
int updatewmain(int argc, wchar_t *argv[])
{
if (argc <= 1)
return 0;
2024-05-24 00:56:39 +08:00
SetProcessDPIAware();
2024-05-03 22:31:17 +08:00
AutoHandle hMutex = CreateMutex(NULL, FALSE, L"LUNA_UPDATER_SINGLE");
if (GetLastError() == ERROR_ALREADY_EXISTS)
return 0;
while (true)
{
AutoHandle semaphore = CreateMutex(NULL, FALSE, L"LUNA_UPDATER_BLOCK");
if (GetLastError() != ERROR_ALREADY_EXISTS)
break;
Sleep(1000);
}
WCHAR path[MAX_PATH];
GetModuleFileNameW(GetModuleHandle(0), path, MAX_PATH);
*(wcsrchr(path, '\\')) = 0;
*(wcsrchr(path, '\\')) = 0;
SetCurrentDirectory(path);
2024-09-04 11:21:26 +08:00
int needreload = std::stoi(argv[1]);
2024-05-03 22:31:17 +08:00
try
{
2024-11-21 16:20:54 +08:00
std::filesystem::remove_all(L".\\files_old");
std::filesystem::rename(L".\\files", L".\\files_old");
2024-09-04 11:21:26 +08:00
std::filesystem::copy(argv[2], L".\\", std::filesystem::copy_options::recursive | std::filesystem::copy_options::overwrite_existing);
2024-11-21 16:20:54 +08:00
try
{
std::filesystem::remove_all(L".\\files_old");
}
catch (...)
{
}
2024-05-03 22:31:17 +08:00
}
catch (std::exception &e)
{
2024-11-21 16:20:54 +08:00
try
{
std::filesystem::rename(L".\\files_old", L".\\files");
}
catch (...)
{
}
2024-05-24 00:56:39 +08:00
MessageBoxA(GetForegroundWindow(), (std::string("Update failed!\r\n") + e.what()).c_str(), "Error", 0);
2024-09-04 11:21:26 +08:00
ShellExecute(0, L"open", (std::wstring(argv[3]) + L"/Github/LunaTranslator/releases").c_str(), NULL, NULL, SW_SHOWNORMAL);
2024-05-29 13:41:03 +08:00
return 0;
}
try
{
2024-11-21 16:20:54 +08:00
std::filesystem::remove_all(argv[2]);
2024-05-29 13:41:03 +08:00
}
catch (std::exception &e)
{
2024-05-03 22:31:17 +08:00
}
2024-09-04 11:21:26 +08:00
MessageBoxW(GetForegroundWindow(), L"Update success", L"Success", 0);
if (needreload)
{
ShellExecute(0, L"open", L".\\LunaTranslator.exe", NULL, NULL, SW_SHOWNORMAL);
}
2024-05-03 22:31:17 +08:00
return 0;
}