Skip to content

Commit

Permalink
chdir to exe path (#345)
Browse files Browse the repository at this point in the history
* using OS_Basename to get root directory from argv[0]

* remove call to old GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS since opengl guarantees at least 16
  • Loading branch information
dethrace-labs committed Aug 9, 2023
1 parent dc135e4 commit 511d827
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 22 deletions.
16 changes: 9 additions & 7 deletions src/harness/harness.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,17 @@ void Harness_Init(int* argc, char* argv[]) {
}

char* root_dir = getenv("DETHRACE_ROOT_DIR");
if (root_dir == NULL) {
LOG_INFO("DETHRACE_ROOT_DIR is not set, assuming '.'");
if (root_dir != NULL) {
LOG_INFO("DETHRACE_ROOT_DIR is set to '%s'", root_dir);
} else {
printf("Data directory: %s\n", root_dir);
result = chdir(root_dir);
if (result != 0) {
LOG_PANIC("Failed to chdir. Error is %s", strerror(errno));
}
root_dir = OS_Dirname(argv[0]);
}
printf("Using root directory: %s\n", root_dir);
result = chdir(root_dir);
if (result != 0) {
LOG_PANIC("Failed to chdir. Error is %s", strerror(errno));
}

if (harness_game_info.mode == eGame_none) {
Harness_DetectGameMode();
}
Expand Down
4 changes: 4 additions & 0 deletions src/harness/include/harness/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ FILE* OS_fopen(const char* pathname, const char* mode);

size_t OS_ConsoleReadPassword(char* pBuffer, size_t pBufferLen);

char* OS_Dirname(const char* path);

char* OS_Basename(const char* path);

#endif
18 changes: 15 additions & 3 deletions src/harness/os/linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
#include <unistd.h>

#define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
#define MAX_STACK_FRAMES 64
#define TRACER_PID_STRING "TracerPid:"

static int stack_nbr = 0;
static char _program_name[1024];
#define MAX_STACK_FRAMES 64

static void* stack_traces[MAX_STACK_FRAMES];
#define TRACER_PID_STRING "TracerPid:"
static char name_buf[4096];

struct dl_iterate_callback_data {
int initialized;
Expand Down Expand Up @@ -176,7 +178,7 @@ static void signal_handler(int sig, siginfo_t* siginfo, void* context) {
void resolve_full_path(char* path, const char* argv0) {
if (argv0[0] == '/') { // run with absolute path
strcpy(path, argv0);
} else { // run with relative path
} else { // run with relative path
if (NULL == getcwd(path, PATH_MAX)) {
perror("getcwd error");
return;
Expand Down Expand Up @@ -301,3 +303,13 @@ size_t OS_ConsoleReadPassword(char* pBuffer, size_t pBufferLen) {
tcsetattr(STDIN_FILENO, TCSANOW, &old);
return len;
}

char* OS_Dirname(const char* path) {
strcpy(name_buf, path);
return dirname(name_buf);
}

char* OS_Basename(const char* path) {
strcpy(name_buf, path);
return basename(name_buf);
}
11 changes: 11 additions & 0 deletions src/harness/os/macos.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ static int stack_nbr = 0;
static char _program_name[1024];
#define MAX_STACK_FRAMES 64
static void* stack_traces[MAX_STACK_FRAMES];
static char name_buf[4096];

// Resolve symbol name and source location given the path to the executable and an address
int addr2line(char const* const program_name, intptr_t slide, void const* const addr) {
Expand Down Expand Up @@ -228,3 +229,13 @@ size_t OS_ConsoleReadPassword(char* pBuffer, size_t pBufferLen) {
fgets(pBuffer, pBufferLen, stdin);
return strlen(pBuffer);
}

char* OS_Dirname(const char* path) {
strcpy(name_buf, path);
return dirname(name_buf);
}

char* OS_Basename(const char* path) {
strcpy(name_buf, path);
return basename(name_buf);
}
13 changes: 13 additions & 0 deletions src/harness/os/windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ void dr_dprintf(char* fmt_string, ...);
static int stack_nbr = 0;
static char _program_name[1024];

static char dirname_buf[_MAX_DIR];
static char fname_buf[_MAX_FNAME];

int addr2line(char const* const program_name, void const* const addr) {
char addr2line_cmd[512] = { 0 };

Expand Down Expand Up @@ -169,3 +172,13 @@ size_t OS_ConsoleReadPassword(char* pBuffer, size_t pBufferLen) {
fgets(pBuffer, pBufferLen, stdin);
return strlen(pBuffer);
}

char* OS_Dirname(const char* path) {
_splitpath(path, NULL, dirname_buf, NULL, NULL);
return dirname_buf;
}

char* OS_Basename(const char* path) {
_splitpath(path, NULL, NULL, fname_buf, NULL);
return fname_buf;
}
6 changes: 0 additions & 6 deletions src/harness/renderers/gl/gl_renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,6 @@ void GLRenderer_Init(int pRender_width, int pRender_height) {
LOG_INFO("OpenGL version string: %s", glGetString(GL_VERSION));
LOG_INFO("OpenGL shading language version string: %s", glGetString(GL_SHADING_LANGUAGE_VERSION));

int maxTextureImageUnits;
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureImageUnits);
if (maxTextureImageUnits < 3) {
LOG_PANIC("GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS is %d. Need at least 3", maxTextureImageUnits);
}

LoadShaders();
SetupFullScreenRectGeometry();

Expand Down
12 changes: 6 additions & 6 deletions src/harness/win95/polyfill.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "harness/hooks.h"
#include "harness/os.h"
#include "harness/win95_polyfill.h"

#include <assert.h>
Expand Down Expand Up @@ -237,12 +238,11 @@ void DirectDrawDevice_SetPaletteEntries(PALETTEENTRY_* palette, int pFirst_colou
}

void _splitpath_(char* path, char* drive, char* dir, char* fname, char* ext) {
#ifdef _WIN32
_splitpath(path, NULL, NULL, fname, NULL);
#else
char* base = basename(path);
strcpy(fname, base);
#endif
assert(dir == NULL);
assert(fname != NULL);

char* result = OS_Basename(path);
strcpy(fname, result);
}

int _CrtDbgReport_(int reportType, const char* filename, int linenumber, const char* moduleName, const char* format, ...) {
Expand Down

0 comments on commit 511d827

Please sign in to comment.