Skip to content

Commit

Permalink
update generic hl2 launcher files
Browse files Browse the repository at this point in the history
  • Loading branch information
darealshinji committed Mar 16, 2019
1 parent 9c9cc88 commit f25a91a
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 153 deletions.
43 changes: 0 additions & 43 deletions COPYING.gcrypt

This file was deleted.

1 change: 0 additions & 1 deletion MINERVA/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ steamcmd +login USERNAME PASSWORD +force_install_dir $HOME/MINERVA +app_update 2
steamcmd +login USERNAME PASSWORD +@sSteamCmdForcePlatformType windows +force_install_dir $HOME/MINERVA/MINERVA +app_license_request 235780 +app_update 235780 validate +quit

cp -rf metastasis -t $HOME/MINERVA/MINERVA
cp ../libgcrypt.so.11 $HOME/MINERVA/bin
cd $HOME/MINERVA
mv MINERVA/metastasis .
mv sourcetest/bin ep2
Expand Down
60 changes: 25 additions & 35 deletions hl2_launcher_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,53 @@

#include <dlfcn.h>
#include <libgen.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#define PRINT_ERROR(msg) fprintf(stderr, "error: %s\n", msg)
typedef int (*LauncherMain_t)(int, char**);

int main(int argc, char **argv)
int main(int argc, char *argv[])
{
char buf[PATH_MAX + 1] = {0};
ssize_t ssize;
void *handle;
char *error;
int rv, rv_dlclose;
int (*pLauncherMain) (int, char**);
char *self, *dn, *error;
void *hinstLauncher;
LauncherMain_t pLauncherMain;
int resultLauncherMain;

ssize = readlink("/proc/self/exe", buf, PATH_MAX);

if (ssize < 1)
{
PRINT_ERROR("readlink()");
if ((self = realpath("/proc/self/exe", NULL)) == NULL) {
perror("realpath()");
return 1;
}

if (chdir(dirname(buf)) != 0)
{
PRINT_ERROR("chdir()");
dn = dirname(self);
free(self);

if (chdir(dn) != 0) {
perror("chdir()");
return 1;
}

handle = dlopen("./bin/launcher.so", RTLD_LAZY);

if (!handle)
{
PRINT_ERROR(dlerror());
if ((hinstLauncher = dlopen("./bin/launcher.so", RTLD_LAZY)) == NULL) {
fprintf(stderr, "error: %s\n", dlerror());
return 1;
}

dlerror();
*(void **) (&pLauncherMain) = dlsym(handle, "LauncherMain");
error = dlerror();
pLauncherMain = dlsym(hinstLauncher, "LauncherMain");

if (error)
{
PRINT_ERROR(error);
dlclose(handle);
if ((error = dlerror()) != NULL) {
fprintf(stderr, "error: %s\n", error);
dlclose(hinstLauncher);
return 1;
}

rv = (*pLauncherMain)(argc, argv);
rv_dlclose = dlclose(handle);
resultLauncherMain = pLauncherMain(argc, argv);

if (rv_dlclose != 0)
{
PRINT_ERROR(dlerror());
rv = rv_dlclose;
if (dlclose(hinstLauncher) != 0) {
fprintf(stderr, "error: %s\n", dlerror());
return 1;
}

return rv;
return resultLauncherMain;
}

97 changes: 97 additions & 0 deletions hl2_launcher_win32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* HL2 launcher for Windows (hl2.exe) */

// windres hl2.res hl2_res.o
// gcc -m32 -Wall -O3 -mwindows hl2_launcher_win32.c -o hl2.exe hl2_res.o -s

// cl /Ox /EHsc /GS /guard:cf hl2_launcher_win32.c /link /out:hl2.exe hl2.res user32.lib

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFSIZE 4096
#define LONG_MAX_PATH 32767

typedef int (*LauncherMain_t)(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);

static void show_error_message(int errCode, const char *title, const char *str)
{
int rv;
char buf[BUFSIZE];
char err[BUFSIZE];

rv = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
errCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buf,
sizeof(buf),
NULL);

if (rv == 0) {
_snprintf_s(err, sizeof(err), _TRUNCATE, "%s\n\nerror code %ld", str, errCode);
} else {
_snprintf_s(err, sizeof(err), _TRUNCATE, "%s\n\n%s", str, buf);
}

MessageBoxA(NULL, err, title, MB_ICONERROR|MB_OK);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
const char *title = "Launcher Error";
char modulePath[LONG_MAX_PATH];
char *p, *env, *path;
size_t len, modulePathLen;
HINSTANCE hinstLauncher;
LauncherMain_t pLauncherMain;
int resultLauncherMain;

// get exe path
if (!GetModuleFileNameA(hInstance, modulePath, sizeof(modulePath))) {
show_error_message(GetLastError(), title, "Failed calling GetModuleFileName():");
return 1;
}

// get dirname
p = strrchr(modulePath, '\\');
if (!p) {
MessageBoxA(NULL, "Cannot get executable path!", title, MB_ICONERROR|MB_OK);
return 1;
}
*p = 0;
modulePathLen = strlen(modulePath);

// add .\bin to PATH
env = getenv("PATH");
len = modulePathLen + strlen(env) + 6;
path = malloc(len);
_snprintf_s(path, len, _TRUNCATE, "%s\\bin;%s", modulePath, env);
_putenv_s("PATH", path);

// load launcher.dll
len = strlen(modulePath) + 18;
path = realloc(path, len);
_snprintf_s(path, len, _TRUNCATE, "%s\\bin\\launcher.dll", modulePath);

hinstLauncher = LoadLibraryExA(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
free(path);

if (!hinstLauncher) {
show_error_message(GetLastError(), title, "Failed to load launcher.dll:");
return 1;
}

// call LauncherMain()
pLauncherMain = (LauncherMain_t) GetProcAddress(hinstLauncher, "LauncherMain");
resultLauncherMain = pLauncherMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);

if (FreeLibrary(hinstLauncher) == 0) {
show_error_message(GetLastError(), title, "Failed to free launcher.dll:");
return 1;
}

return resultLauncherMain;
}

74 changes: 0 additions & 74 deletions hl2_launcher_win32.cpp

This file was deleted.

0 comments on commit f25a91a

Please sign in to comment.