From 816f97ab41b730b1515747685f51f602bca56d8f Mon Sep 17 00:00:00 2001 From: xianii Date: Wed, 27 Sep 2023 03:05:45 +0800 Subject: [PATCH 1/4] add tcc toolchain --- ahk-compile-toolset | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ahk-compile-toolset b/ahk-compile-toolset index d7ca4a7..1989c4d 160000 --- a/ahk-compile-toolset +++ b/ahk-compile-toolset @@ -1 +1 @@ -Subproject commit d7ca4a70e681af190018dd5be43a01c0cc9c48ca +Subproject commit 1989c4d13337a1027761823ea1251be1373e1346 From 54e4c33751a0b0c74cfeb73b67989bd0e95587b8 Mon Sep 17 00:00:00 2001 From: xianii Date: Wed, 27 Sep 2023 03:14:43 +0800 Subject: [PATCH 2/4] rewrite updater in C --- .gitignore | 3 ++ distribution.ahk | 4 +- update.ahk | 11 +++++- updater.ahk | 10 ----- updater.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 113 insertions(+), 14 deletions(-) delete mode 100644 updater.ahk create mode 100644 updater.c diff --git a/.gitignore b/.gitignore index 0b2bbaf..2871ca5 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,6 @@ dist/ *.ini compile_prop.ahk + +*.exe +*.zip diff --git a/distribution.ahk b/distribution.ahk index 9b9238c..77ffd15 100644 --- a/distribution.ahk +++ b/distribution.ahk @@ -45,11 +45,11 @@ DirCreate("dist") try { - RunWait("./ahk-compile-toolset/ahk2exe.exe /in updater.ahk /out updater.exe /base `"" A_AhkPath "`" /compress 1") + RunWait("./ahk-compile-toolset/tcc/tcc.exe ./updater.c -luser32") } catch as e { - MsgBox("updater.ahk`nERROR CODE=" . e.Message) + MsgBox("updater compile`nERROR CODE=" . e.Message) ExitApp } diff --git a/update.ahk b/update.ahk index 56f11f6..534db47 100644 --- a/update.ahk +++ b/update.ahk @@ -1,7 +1,14 @@ #include meta.ahk if FileExist("updater.exe") { - FileDelete("updater.exe") + try { + FileDelete("updater.exe") + } +} +if FileExist("___delete_me___.exe") { + try { + FileDelete("___delete_me___.exe") + } } if !FileExist("setting.ini") { IniWrite(version, "setting.ini", "update", "ver") @@ -108,7 +115,7 @@ updateReady() { MsgBox("Download finished`nProgram will restart now", , "T3") todayUpdated() FileInstall("updater.exe", "updater.exe", 1) - Run("updater.exe") + Run("updater.exe ___ORIGIN_ME___ " downloadFilename " " binaryFilename) ExitApp } catch as e { TrayTip "An exception was thrown!`nSpecifically: " . e.Message, "upgrade failed", 0x3 diff --git a/updater.ahk b/updater.ahk deleted file mode 100644 index bfecdf4..0000000 --- a/updater.ahk +++ /dev/null @@ -1,10 +0,0 @@ -#SingleInstance Ignore -SetWorkingDir(A_ScriptDir) - -#include meta.ahk - -RunWait('powershell -command "Expand-Archive -Force ' downloadFilename ' ."', , "Hide") -FileDelete(downloadFilename) -Run(binaryFilename) - -ExitApp diff --git a/updater.c b/updater.c new file mode 100644 index 0000000..7140d86 --- /dev/null +++ b/updater.c @@ -0,0 +1,99 @@ + +#include +#include +#include +#include +#include +#include +#include + +#define ORIGIN_ME "___ORIGIN_ME___" +#define KILL_ME "___KILL_ME___" +#define DELETE_ME "___delete_me___.exe" + +// USAGE: +// .\updater.exe ___ORIGIN_ME___ app.zip app.exe + +// Compile: +// tcc .\updater.c -luser32 + +void unzip(const char *path) { + TCHAR wpath[128]; + TCHAR cmd[128]; + + sprintf(wpath, "%s", path); + sprintf(cmd, "powershell -command \"Expand-Archive -Force %s .\"", path); + printf("cmd=%s\n", cmd); + printf("Sleep 200ms\n"); + Sleep(200); + system(cmd); + DeleteFile(wpath); +} + +void runNewBinary(const char *path) { + TCHAR cmd[128]; + STARTUPINFO si = {sizeof(si)}; + PROCESS_INFORMATION pi; + sprintf(cmd, "%s", path); + CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); +} + +void cloneSelf() { + TCHAR selfPath[_MAX_PATH]; + GetModuleFileName(NULL, selfPath, _MAX_PATH); + printf("%d = Copy %s to %s\n", CopyFile(selfPath, DELETE_ME, FALSE), selfPath, DELETE_ME); + HANDLE hfile = CreateFile(DELETE_ME, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, NULL); + printf("HANDLE(%d) = Create %s in FILE_FLAG_DELETE_ON_CLOSE\n", hfile, DELETE_ME); + HANDLE hProcessOrig = OpenProcess(SYNCHRONIZE, TRUE, GetCurrentProcessId()); + printf("Origin HANDLE = %d\n", hProcessOrig); + + // Create clone process + TCHAR cmd[256]; + wsprintf(cmd, __T("%s " KILL_ME " %d \"%s\""), DELETE_ME, hProcessOrig, selfPath); + printf("%s\n", cmd); + STARTUPINFO si; + ZeroMemory(&si, sizeof(si)); + si.cb = sizeof(si); + PROCESS_INFORMATION pi; + CreateProcess(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi); + + printf("Close(%d)\n", hProcessOrig); + CloseHandle(hProcessOrig); + printf("Close(%d)\n", hfile); + CloseHandle(hfile); +} + +void delete_original(const char *handle, const char *path) { + HANDLE hProcessOrig = (HANDLE)_ttoi(handle); + printf("[k]Wait(%d)\n", hProcessOrig); + WaitForSingleObject(hProcessOrig, INFINITE); + printf("[k]Close(%d)\n", hProcessOrig); + CloseHandle(hProcessOrig); + printf("[k]DeleteFile(%s)\n", path); + DeleteFile(path); +} + +int main(int argc, const char *argv[]) { + + // printf("argc=%d\n", argc); + // for (size_t i = 0; i < argc; i++) { + // printf("argv[%d]=%s\n", i, argv[i]); + // } + + if (argc != 4) { + return -1; + } + + if (strcmp(argv[1], ORIGIN_ME) == 0) { + printf("--> unzip\n"); + unzip(argv[2]); + printf("--> runNewBinary\n"); + runNewBinary(argv[3]); + printf("--> cloneSelf\n"); + cloneSelf(); + } else if (strcmp(argv[1], KILL_ME) == 0) { + printf("-->[k] killSelf\n"); + delete_original(argv[2], argv[3]); + } + return 0; +} From 8d5fbb387a1e7bedb4f89c1a54b85730dcea4b7d Mon Sep 17 00:00:00 2001 From: xianii Date: Wed, 27 Sep 2023 22:39:33 +0800 Subject: [PATCH 3/4] run updater in hide mode --- update.ahk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/update.ahk b/update.ahk index 534db47..9bd7dec 100644 --- a/update.ahk +++ b/update.ahk @@ -115,7 +115,7 @@ updateReady() { MsgBox("Download finished`nProgram will restart now", , "T3") todayUpdated() FileInstall("updater.exe", "updater.exe", 1) - Run("updater.exe ___ORIGIN_ME___ " downloadFilename " " binaryFilename) + Run("updater.exe ___ORIGIN_ME___ " downloadFilename " " binaryFilename, A_ScriptDir, "Hide") ExitApp } catch as e { TrayTip "An exception was thrown!`nSpecifically: " . e.Message, "upgrade failed", 0x3 From f3f5fa18788bfe7d9ef02d0525c406b6950fbfd3 Mon Sep 17 00:00:00 2001 From: xianii Date: Thu, 28 Sep 2023 00:31:47 +0800 Subject: [PATCH 4/4] v0.4.0 update --- README.md | 3 ++- meta.ahk | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f387932..2bb2e2d 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,8 @@ Then the script would download the file and check if there is newer version. If - set `version` - set `ahkFilename` to your script name, it would compile `%ahkFilename%.ahk` to `%binaryFilename%.exe` -- set `downloadUrl` to your github release URL +- ~~set `downloadUrl` to your github release URL~~ +- set your GitHub id(`GitHubID`) and Repo name (`repoName`) ### `tray.ahk` diff --git a/meta.ahk b/meta.ahk index 8cb7ab6..01bec39 100644 --- a/meta.ahk +++ b/meta.ahk @@ -1,14 +1,14 @@ FileEncoding("UTF-8") appName := "auto upgrader" -version := "0.3.4" +version := "0.4.0" versionFilename := "version.txt" ahkFilename := "app.ahk" binaryFilename := "app.exe" downloadFilename := "app.zip" -downloadUrl := "/Nigh/ahk-autoupdate-template/releases/latest/download/" +GitHubID := "Nigh" +repoName := "ahk-autoupdate-template" +downloadUrl := "/" GitHubID "/" repoName "/releases/latest/download/" update_log := " ( -Replaced compiler binary source -Removed dead GitHub mirror -Fixed property set in the compiled executable's version information +Rewrite updater.ahk with C, that will reduce the compiled binary by half. )"