ruen-ui/Tools/Ruen-Setup.c

37 lines
2.6 KiB
C
Raw Normal View History

/* Tiny native Windows entry point: uses the installed Windows PowerShell.
No runtime download, no elevation, no background service. Source is bundled. */
typedef unsigned short W; typedef unsigned long D; typedef int B; typedef void *H;
typedef struct {D cb;W *reserved,*desktop,*title;D x,y,cx,cy,xc,yc,fill,flags;unsigned short show,reserved2;void *r2;H in,out,err;} STARTUP;
typedef struct {H process,thread;D pid,tid;} PROCESS;
#define API __declspec(dllimport)
API D GetModuleFileNameW(H,W*,D);API W* GetCommandLineW(void);API D GetWindowsDirectoryW(W*,D);
API B CreateProcessW(const W*,W*,void*,void*,B,D,void*,const W*,STARTUP*,PROCESS*);
API D WaitForSingleObject(H,D);API B GetExitCodeProcess(H,D*);API B CloseHandle(H);API void ExitProcess(D);
API B ReadFile(H,void*,D,D*,void*);
API H GetStdHandle(D);API B WriteFile(H,const void*,D,D*,void*);
static W module[32768],exe[32768],cmd[32768];static unsigned pos;
static STARTUP si;static PROCESS pi;
static void add(const W *s){while(*s && pos<32766)cmd[pos++]=*s++;cmd[pos]=0;}
static int starts(const W *p,const W *s){while(*s)if(*p++!=*s++)return 0;return !*p||*p==32;}
void entry(void){
D n=GetModuleFileNameW(0,module,32768),code=1;
if(!n||n>=32768)ExitProcess(1);
while(n && module[n-1]!=92)--n;module[n]=0;
D m=GetWindowsDirectoryW(exe,32000);const W *suffix=L"\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
if(!m||m>=32000)ExitProcess(1);while(*suffix)exe[m++]=*suffix++;exe[m]=0;
W *args=GetCommandLineW();if(*args==34){++args;while(*args&&*args!=34)++args;if(*args)++args;}else while(*args&&*args!=32)++args;
while(*args==32)++args;
int optimize=starts(args,L"--optimize");if(optimize){args+=10;while(*args==32)++args;}
add(L"\"");add(exe);add(L"\" -NoLogo -NoProfile -ExecutionPolicy Bypass -File \"");add(module);
add(optimize?L"Optimize-Ruen-UI.ps1":L"Install-Ruen-UI.ps1");add(L"\" ");
/* --optimize [FPS] is shared with Linux; named PowerShell options still work. */
if(optimize && *args && (*args!=45 || (args[1]>=48 && args[1]<=57)))add(L"-FPS ");
add(args);
si.cb=sizeof(si);
if(CreateProcessW(exe,cmd,0,0,1,0,0,module,&si,&pi)){
WaitForSingleObject(pi.process,0xffffffff);GetExitCodeProcess(pi.process,&code);CloseHandle(pi.thread);CloseHandle(pi.process);
}else{const char msg[]="Unable to start Windows PowerShell. Run Install-Ruen-UI.cmd from the extracted package.\r\n";D written;WriteFile(GetStdHandle((D)-12),msg,sizeof(msg)-1,&written,0);}
if(!*args){const char msg[]="\r\nPress Enter to close this window.\r\n";char key;D n;WriteFile(GetStdHandle((D)-11),msg,sizeof(msg)-1,&n,0);ReadFile(GetStdHandle((D)-10),&key,1,&n,0);}
ExitProcess(code);
}