Files
dusk/src/dusklinux/asset/assetlinux.c
T
2026-05-01 14:00:24 -05:00

96 lines
2.3 KiB
C

/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "asset/asset.h"
#include "engine/engine.h"
#include "util/string.h"
#include "assert/assert.h"
errorret_t assetInitLinux(void) {
// Engine may have been provided the launch path
if(ENGINE.argc > 0) {
// Get the directory of the executable
char_t buffer[ASSET_FILE_PATH_MAX];
stringCopy(buffer, ENGINE.argv[0], ASSET_FILE_PATH_MAX);
size_t len = strlen(buffer);
// Normalize slashes
for(size_t i = 0; i < ASSET_FILE_PATH_MAX; i++) {
if(buffer[i] == '\0') break;
if(buffer[i] == '\\') buffer[i] = '/';
}
// Now find the last slash
char_t *end = buffer + len - 1;
do {
end--;
if(*end == '/') {
*end = '\0';
break;
}
} while(end != buffer);
// Did we find a slash?
if(end != buffer) {
// We found the directory, set as system path
stringCopy(ASSET.platform.systemPath, buffer, ASSET_FILE_PATH_MAX);
}
}
// Default system path, intended to be overridden by the platform
stringCopy(ASSET.platform.systemPath, ".", ASSET_FILE_PATH_MAX);
// Open zip file
char_t searchPath[ASSET_FILE_PATH_MAX];
const char_t **path = ASSET_LINUX_SEARCH_PATHS;
int32_t error;
do {
char_t temp[ASSET_FILE_PATH_MAX];
snprintf(
temp,
ASSET_FILE_PATH_MAX,
*path,
ASSET_FILE_NAME
);
// Ensure combined length does not exceed ASSET_FILE_PATH_MAX
size_t syslen = strlen(ASSET.platform.systemPath);
size_t slashlen = 1; // for '/'
size_t max_temp = ASSET_FILE_PATH_MAX - syslen - slashlen - 1; // -1 for null terminator
if(strlen(temp) > max_temp) {
temp[max_temp] = '\0';
}
snprintf(
searchPath,
ASSET_FILE_PATH_MAX,
"%s/%s",
ASSET.platform.systemPath,
temp
);
// Try open
ASSET.zip = zip_open(searchPath, ZIP_RDONLY, &error);
if(ASSET.zip == NULL) continue;
if(error != 0) {
printf("Opened asset file with non-zero error code: %d\n", error);
ASSET.zip = NULL;
continue;
}
break;// Found!
} while(*(++path) != NULL);
// Did we open the asset?
if(ASSET.zip == NULL) {
errorThrow("Failed to open asset file.");
}
errorOk();
}
errorret_t assetDisposeLinux(void) {
errorOk();
}