power_play/src/resource.c
2024-02-29 16:01:51 -06:00

72 lines
1.7 KiB
C

#include "resource.h"
#include "arena.h"
#include "tar.h"
#include "incbin.h"
#if RESOURCES_EMBEDDED
#include "inc.h"
/* Add resource data to binary */
GLOBAL struct {
struct arena arena;
struct tar_archive archive;
} L = { 0 } DEBUG_LVAR(L_resource);
#endif
void resource_startup(void)
{
#if RESOURCES_EMBEDDED
struct buffer embedded_data = inc_res_tar();
//struct buffer embedded_data = ((struct buffer) { (u8 *)(_incbin_res_tar_end) - (u8 *)(_incbin_res_tar_start), (u8 *)_incbin_res_tar_start });;
L.arena = arena_alloc(GIGABYTE(64));
if (embedded_data.size <= 0) {
sys_panic(STR("No embedded resources found"));
}
L.archive = tar_parse(&L.arena, embedded_data, STR("res/"));
#else
/* Ensure we have the right working directory */
if (!sys_is_dir(STR("res"))) {
sys_panic(STR("Resource directory \"res\" not found"));
}
#endif
}
b32 resource_exists(struct string path)
{
#if RESOURCES_EMBEDDED
struct tar_entry *entry = tar_get(&L.archive, path);
return entry && !entry->is_dir;
#else
return sys_is_file(path);
#endif
}
struct resource resource_open(struct string path)
{
#if RESOURCES_EMBEDDED
struct tar_entry *entry = tar_get(&L.archive, path);
return (struct resource) {
.bytes = entry ? entry->buff : BUFFER(0, 0)
};
#else
struct sys_file file = sys_file_open_read(path);
struct sys_file_map file_map = sys_file_map_open_read(file);
struct buffer bytes = sys_file_map_data(file_map);
return (struct resource) {
.bytes = bytes,
.file = file,
.file_map = file_map
};
#endif
}
#if !RESOURCES_EMBEDDED
void resource_close(struct resource res)
{
sys_file_map_close(res.file_map);
sys_file_close(res.file);
}
#endif