diff --git a/LunaTranslator/LunaTranslator/LunaTranslator_main.py b/LunaTranslator/LunaTranslator/LunaTranslator_main.py index 51676dc2..9aa45b2a 100644 --- a/LunaTranslator/LunaTranslator/LunaTranslator_main.py +++ b/LunaTranslator/LunaTranslator/LunaTranslator_main.py @@ -2,6 +2,7 @@ import sys, windows import platform, os if __name__ == "__main__": + _lock = windows.AutoHandle(windows.CreateMutex(False, "LUNA_UPDATER_BLOCK")) dirname = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) os.chdir(dirname) windows.addenvpath("./LunaTranslator/runtime/") # win7 no vcredist2015 diff --git a/LunaTranslator/LunaTranslator/myutils/githubupdate.py b/LunaTranslator/LunaTranslator/myutils/githubupdate.py index ec989dfe..0df1c3d0 100644 --- a/LunaTranslator/LunaTranslator/myutils/githubupdate.py +++ b/LunaTranslator/LunaTranslator/myutils/githubupdate.py @@ -5,12 +5,10 @@ from myutils.proxy import getproxy from traceback import print_exc import zipfile, os from myutils.config import globalconfig -import windows +import subprocess def getvesionmethod(): - url = "https://github.com/HIllya51/LunaTranslator/releases/" - try: headers = { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", @@ -36,34 +34,13 @@ def getvesionmethod(): def update(): if platform.architecture()[0] == "64bit": bit = "" + _6432 = "64" elif platform.architecture()[0] == "32bit": bit = "_x86" - with open("./cache/update/update.bat", "w", encoding="utf8") as ff: - - ff.write( - r""" -@echo off -:waitloop -tasklist | find "LunaTranslator_main.exe" > nul -if %errorlevel%==0 ( - timeout /t 1 > nul - goto waitloop -) -timeout 1 -xcopy .\cache\update\LunaTranslator""" - + bit - + r""" .\ /s /e /c /y /h /r -exit - """ - ) - windows.ShellExecute( - None, - "open", - "cache\\update\\update.bat", - "", - os.path.dirname("."), - windows.SW_HIDE, - ) + _6432 = "32" + os.makedirs("./cache", exist_ok=True) + shutil.copy(rf".\files\plugins\shareddllproxy{_6432}.exe", rf".\cache\Updater.exe") + subprocess.Popen(rf".\cache\Updater.exe update .\cache\update\LunaTranslator{bit}") def updatemethod(_version, progresscallback): diff --git a/plugins/shareddllproxy/CMakeLists.txt b/plugins/shareddllproxy/CMakeLists.txt index c8302758..59be03b3 100644 --- a/plugins/shareddllproxy/CMakeLists.txt +++ b/plugins/shareddllproxy/CMakeLists.txt @@ -12,7 +12,7 @@ generate_product_version( ) -add_executable(shareddllproxy shareddllproxy.cpp dllinject.cpp ntleas.cpp aspatch.cpp ${versioninfo}) +add_executable(shareddllproxy shareddllproxy.cpp dllinject.cpp ntleas.cpp aspatch.cpp update.cpp ${versioninfo}) target_precompile_headers(shareddllproxy REUSE_FROM pch) if(${CMAKE_SIZEOF_VOID_P} EQUAL 8) diff --git a/plugins/shareddllproxy/shareddllproxy.cpp b/plugins/shareddllproxy/shareddllproxy.cpp index c3ebf555..58fb1d87 100644 --- a/plugins/shareddllproxy/shareddllproxy.cpp +++ b/plugins/shareddllproxy/shareddllproxy.cpp @@ -2,6 +2,7 @@ int dllinjectwmain(int argc, wchar_t *argv[]); int ntleaswmain(int argc, wchar_t *wargv[]); +int updatewmain(int argc, wchar_t *wargv[]); bool checkisapatch(); #ifndef _WIN64 int LRwmain(int argc, wchar_t *argv[]); @@ -61,6 +62,8 @@ int wmain(int argc, wchar_t *argv[]) return ntleaswmain(argc - 1, argv + 1); if (argv0 == L"listpm") return listprocessmodule(argc - 1, argv + 1); + if (argv0 == L"update") + return updatewmain(argc - 1, argv + 1); #ifndef _WIN64 else if (argv0 == L"LR") diff --git a/plugins/shareddllproxy/update.cpp b/plugins/shareddllproxy/update.cpp new file mode 100644 index 00000000..afb6e036 --- /dev/null +++ b/plugins/shareddllproxy/update.cpp @@ -0,0 +1,36 @@ +#pragma comment(linker, "/subsystem:windows /entry:wmainCRTStartup") + +int updatewmain(int argc, wchar_t *argv[]) +{ + if (argc <= 1) + return 0; + 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); + try + { + std::filesystem::copy(argv[1], L".\\", std::filesystem::copy_options::recursive | std::filesystem::copy_options::overwrite_existing); + MessageBoxW(0, L"Update success", L"Success", 0); + } + catch (std::exception &e) + { + MessageBoxW(0, (StringToWideString(e.what()) + L"\r\nUpdate failed, maybe you should download again to fix errors").c_str(), L"Error", 0); + ShellExecute(0, L"open", L"https://github.com/HIllya51/LunaTranslator/releases", NULL, NULL, SW_SHOWNORMAL); + } + return 0; +} \ No newline at end of file