Skip to content

Commit

Permalink
Merge pull request #5 from Nigh/c-updater
Browse files Browse the repository at this point in the history
rewrite updater with C
  • Loading branch information
Nigh authored Sep 27, 2023
2 parents 58997a6 + f3f5fa1 commit d8b2973
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ dist/

*.ini
compile_prop.ahk

*.exe
*.zip
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
2 changes: 1 addition & 1 deletion ahk-compile-toolset
Submodule ahk-compile-toolset updated 94 files
+ tcc/i386-win32-tcc.exe
+170 −0 tcc/include/_mingw.h
+57 −0 tcc/include/assert.h
+409 −0 tcc/include/conio.h
+281 −0 tcc/include/ctype.h
+31 −0 tcc/include/dir.h
+68 −0 tcc/include/direct.h
+135 −0 tcc/include/dirent.h
+55 −0 tcc/include/dos.h
+75 −0 tcc/include/errno.h
+123 −0 tcc/include/excpt.h
+52 −0 tcc/include/fcntl.h
+108 −0 tcc/include/fenv.h
+57 −0 tcc/include/float.h
+297 −0 tcc/include/inttypes.h
+418 −0 tcc/include/io.h
+111 −0 tcc/include/limits.h
+91 −0 tcc/include/locale.h
+181 −0 tcc/include/malloc.h
+737 −0 tcc/include/math.h
+13 −0 tcc/include/mem.h
+40 −0 tcc/include/memory.h
+176 −0 tcc/include/process.h
+42 −0 tcc/include/sec_api/conio_s.h
+19 −0 tcc/include/sec_api/crtdbg_s.h
+33 −0 tcc/include/sec_api/io_s.h
+52 −0 tcc/include/sec_api/mbstring_s.h
+25 −0 tcc/include/sec_api/search_s.h
+145 −0 tcc/include/sec_api/stdio_s.h
+67 −0 tcc/include/sec_api/stdlib_s.h
+30 −0 tcc/include/sec_api/stralign_s.h
+41 −0 tcc/include/sec_api/string_s.h
+34 −0 tcc/include/sec_api/sys/timeb_s.h
+266 −0 tcc/include/sec_api/tchar_s.h
+61 −0 tcc/include/sec_api/time_s.h
+128 −0 tcc/include/sec_api/wchar_s.h
+160 −0 tcc/include/setjmp.h
+28 −0 tcc/include/share.h
+63 −0 tcc/include/signal.h
+79 −0 tcc/include/stdarg.h
+11 −0 tcc/include/stdbool.h
+54 −0 tcc/include/stddef.h
+212 −0 tcc/include/stdint.h
+429 −0 tcc/include/stdio.h
+580 −0 tcc/include/stdlib.h
+164 −0 tcc/include/string.h
+13 −0 tcc/include/sys/fcntl.h
+14 −0 tcc/include/sys/file.h
+30 −0 tcc/include/sys/locking.h
+290 −0 tcc/include/sys/stat.h
+69 −0 tcc/include/sys/time.h
+133 −0 tcc/include/sys/timeb.h
+118 −0 tcc/include/sys/types.h
+14 −0 tcc/include/sys/unistd.h
+146 −0 tcc/include/sys/utime.h
+201 −0 tcc/include/tcc/tcc_libm.h
+80 −0 tcc/include/tcclib.h
+1,102 −0 tcc/include/tchar.h
+287 −0 tcc/include/time.h
+11 −0 tcc/include/vadefs.h
+4 −0 tcc/include/values.h
+12 −0 tcc/include/varargs.h
+873 −0 tcc/include/wchar.h
+172 −0 tcc/include/wctype.h
+149 −0 tcc/include/winapi/basetsd.h
+85 −0 tcc/include/winapi/basetyps.h
+156 −0 tcc/include/winapi/guiddef.h
+8 −0 tcc/include/winapi/poppack.h
+8 −0 tcc/include/winapi/pshpack1.h
+8 −0 tcc/include/winapi/pshpack2.h
+8 −0 tcc/include/winapi/pshpack4.h
+8 −0 tcc/include/winapi/pshpack8.h
+2,951 −0 tcc/include/winapi/winbase.h
+301 −0 tcc/include/winapi/wincon.h
+293 −0 tcc/include/winapi/windef.h
+127 −0 tcc/include/winapi/windows.h
+3,166 −0 tcc/include/winapi/winerror.h
+4,080 −0 tcc/include/winapi/wingdi.h
+5,835 −0 tcc/include/winapi/winnt.h
+272 −0 tcc/include/winapi/winreg.h
+5,651 −0 tcc/include/winapi/winuser.h
+160 −0 tcc/include/winapi/winver.h
+337 −0 tcc/lib/gdi32.def
+770 −0 tcc/lib/kernel32.def
+ tcc/lib/libtcc1-32.a
+ tcc/lib/libtcc1-64.a
+1,399 −0 tcc/lib/msvcrt.def
+658 −0 tcc/lib/user32.def
+ tcc/libtcc.dll
+37 −0 tcc/libtcc/libtcc.def
+100 −0 tcc/libtcc/libtcc.h
+295 −0 tcc/sha1/sha1.c
+52 −0 tcc/sha1/sha1.h
+ tcc/tcc.exe
4 changes: 2 additions & 2 deletions distribution.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
10 changes: 5 additions & 5 deletions meta.ahk
Original file line number Diff line number Diff line change
@@ -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.
)"
11 changes: 9 additions & 2 deletions update.ahk
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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, A_ScriptDir, "Hide")
ExitApp
} catch as e {
TrayTip "An exception was thrown!`nSpecifically: " . e.Message, "upgrade failed", 0x3
Expand Down
10 changes: 0 additions & 10 deletions updater.ahk

This file was deleted.

99 changes: 99 additions & 0 deletions updater.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@

#include <process.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <windows.h>
#include <winuser.h>

#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;
}

0 comments on commit d8b2973

Please sign in to comment.