Asset compartmentalized

This commit is contained in:
2026-03-08 13:29:40 -05:00
parent a3c2e37b17
commit e984b9f5d7
13 changed files with 375 additions and 282 deletions

View File

@@ -10,4 +10,5 @@ target_include_directories(${DUSK_LIBRARY_TARGET_NAME}
)
# Subdirs
add_subdirectory(asset)
add_subdirectory(debug)

View File

@@ -0,0 +1,10 @@
# Copyright (c) 2026 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
assetlinux.c
)

View File

@@ -0,0 +1,81 @@
/**
* 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[FILENAME_MAX];
stringCopy(buffer, ENGINE.argv[0], FILENAME_MAX);
size_t len = strlen(buffer);
// Normalize slashes
for(size_t i = 0; i < FILENAME_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, FILENAME_MAX);
}
}
// Default system path, intended to be overridden by the platform
stringCopy(ASSET.platform.systemPath, ".", FILENAME_MAX);
// Open zip file
char_t searchPath[FILENAME_MAX];
const char_t **path = ASSET_LINUX_SEARCH_PATHS;
int32_t error;
do {
sprintf(
searchPath,
*path,
ASSET.platform.systemPath,
ASSET_FILE_NAME
);
// Try open
ASSET.zip = zip_open(searchPath, ZIP_RDONLY, &error);
if(ASSET.zip == NULL) continue;
if(error != 0) {
printf("Warning: 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();
}

View File

@@ -0,0 +1,37 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "error/error.h"
static const char_t *ASSET_LINUX_SEARCH_PATHS[] = {
"%s/%s",
"%s",
"../%s",
"../../%s",
"data/%s",
"../data/%s",
NULL
};
typedef struct {
char_t systemPath[FILENAME_MAX];
} assetlinux_t;
/**
* Initializes the asset system on Linux.
*
* @return Error state if failed, otherwise returns success.
*/
errorret_t assetInitLinux(void);
/**
* Disposes the asset system on Linux.
*
* @return Error state if failed, otherwise returns success.
*/
errorret_t assetDisposeLinux(void);

View File

@@ -0,0 +1,13 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "asset/assetlinux.h"
typedef assetlinux_t assetplatform_t;
#define assetInitPlatform assetInitLinux
#define assetDisposePlatform assetDisposeLinux