Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing the absolute path loader [fix #224] #227

Merged
merged 8 commits into from
Dec 21, 2021
Merged
61 changes: 38 additions & 23 deletions src/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,12 @@ getColorComponents(uint32_t color, uint8_t *r, uint8_t *g, uint8_t *b) {

internal void
ENGINE_openLogFile(ENGINE* engine) {
// DOME-2020-02-02-090000.log
char* filename = "DOME-out.log";
engine->debug.logFile = fopen(filename, "w+");
}

internal void
ENGINE_printLogVariadic(ENGINE* engine, const char* line, va_list argList) {
va_list args;
va_copy(args, argList);
size_t bufSize = vsnprintf(NULL, 0, line, args) + 1;
va_end(args);

char buffer[bufSize];
buffer[0] = '\0';
va_copy(args, argList);
vsnprintf(buffer, bufSize, line, args);
va_end(args);

ENGINE_writeToLog(ENGINE* engine, const char* buffer) {
// Output to console
printf("%s", buffer);

Expand All @@ -38,6 +26,24 @@ ENGINE_printLogVariadic(ENGINE* engine, const char* line, va_list argList) {
}
}

#define RENDER_VARIADIC_STRING(buffer, line, argList) \
va_list args;\
va_copy(args, argList);\
size_t bufSize = vsnprintf(NULL, 0, line, args) + 1;\
va_end(args);\
char buffer[bufSize];\
buffer[0] = '\0';\
va_copy(args, argList);\
vsnprintf(buffer, bufSize, line, args);\
va_end(args);

internal void
ENGINE_printLogVariadic(ENGINE* engine, const char* line, va_list argList) {
RENDER_VARIADIC_STRING(buffer, line, argList);
ENGINE_writeToLog(engine, buffer);
}


internal void
ENGINE_printLog(ENGINE* engine, char* line, ...) {
// Args is mutated by each vsnprintf call,
Expand Down Expand Up @@ -1164,16 +1170,25 @@ ENGINE_takeScreenshot(ENGINE* engine) {
stbi_write_png("screenshot.png", canvas.width, canvas.height, 4, canvas.pixels, canvas.width * 4);
}


internal void
ENGINE_reportError(ENGINE* engine) {
if (engine->debug.errorBuf != NULL) {
ENGINE_printLog(engine, engine->debug.errorBuf);
if (engine->debug.errorDialog) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
"DOME - Error",
engine->debug.errorBuf,
NULL);
}
ENGINE_reportErrorVariadic(ENGINE* engine, const char* line, va_list argList) {
if (line == NULL) {
return;
}
RENDER_VARIADIC_STRING(buffer, line, argList);
ENGINE_writeToLog(engine, buffer);
if (engine->debug.errorDialog) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
"DOME - Error",
buffer,
NULL);
}
}

internal void
ENGINE_reportError(ENGINE* engine, const char* line, ...) {
va_list args;
va_start(args, line);
ENGINE_reportErrorVariadic(engine, line, args);
va_end(args);
}
6 changes: 3 additions & 3 deletions src/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,9 @@ int DOME_begin(ENGINE* engine, char* entryPath) {
gameFile = ENGINE_readFile(engine, entryPath, &gameFileLength, NULL);
if (gameFile == NULL) {
if (engine->tar != NULL) {
ENGINE_printLog(engine, "Error: Could not load %s in bundle.\n", entryPath);
ENGINE_reportError(engine, "Error: Could not load %s in bundle.\n", entryPath);
} else {
ENGINE_printLog(engine, "Error: Could not load %s.\n", engine->argv[1]);
ENGINE_reportError(engine, "Error: Could not load %s\n", engine->argv[1]);
}
result = EXIT_FAILURE;
goto cleanup;
Expand Down Expand Up @@ -415,7 +415,7 @@ int DOME_begin(ENGINE* engine, char* entryPath) {
}

// Free resources
ENGINE_reportError(engine);
ENGINE_reportError(engine, engine->debug.errorBuf);

if (initMethod != NULL) {
wrenReleaseHandle(vm, initMethod);
Expand Down
15 changes: 14 additions & 1 deletion src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ doesFileExist(char* path) {
return access(path, F_OK) != -1;
}

internal bool
isPathAbsolute(const char* path) {

#ifdef _WIN32
return (path[0] == '/' || (strlen(path) > 3 &&
isalpha(path[0]) &&
path[1] == ':' &&
(path[2] == '/' || path[2] == '\\')));
#else
return (path[0] == '/');
#endif

}

internal int
readFileFromTar(mtar_t* tar, char* path, size_t* lengthPtr, char** data) {
// We assume the tar open has been done already
Expand Down Expand Up @@ -232,4 +246,3 @@ readEntireFile(char* path, size_t* lengthPtr, char** error) {
return source;
}


11 changes: 8 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,13 @@ char* resolveEntryPath(ENGINE* engine, char* entryArgument, bool autoResolve) {
} else {
if (entryArgument != NULL) {
// Set the basepath according to the incoming argument
strcpy(entryPath, base);
strcat(entryPath, entryArgument);
if (isPathAbsolute(entryArgument)) {
strcpy(entryPath, entryArgument);
} else {
strcpy(entryPath, base);
strcat(entryPath, entryArgument);
}

if (isDirectory(entryPath)) {
autoResolve = true;
BASEPATH_set(entryPath);
Expand Down Expand Up @@ -247,7 +252,7 @@ char* resolveEntryPath(ENGINE* engine, char* entryArgument, bool autoResolve) {
}

if (!engine->fused && !resolved) {
ENGINE_printLog(engine, "Error: Could not find an entry point at: %s\n", dirname(entryPath));
ENGINE_reportError(engine, "Error: Could not find an entry point at: %s\n", dirname(entryPath));
printUsage(engine);
return NULL;
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/tools/embed.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,8 @@ EMBED_perform(ENGINE* engine, char** argv) {

int result = EMBED_encode(fileToConvert, length, moduleName, destination);
free(fileToConvert);
if (shouldFree) {
free(destination);
}
return result;
}
2 changes: 1 addition & 1 deletion src/tools/nest.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ NEST_writeFile(ENGINE* engine, mtar_t* tar, char* filePath, char* tarPath) {

internal int
NEST_packDirectory(ENGINE* engine, mtar_t* tar, char* directory, size_t start) {
tinydir_dir dir;
INIT_TO_ZERO(tinydir_dir, dir);
tinydir_open(&dir, directory);

while (dir.has_next) {
Expand Down