Keep all baked assets in one place

This commit is contained in:
kamkow1
2025-06-10 00:19:02 +02:00
parent f864f92d94
commit fbe749a13e
9 changed files with 140 additions and 150 deletions

56
baked.c
View File

@ -1,10 +1,56 @@
#define INCBIN_PREFIX
#define INCBIN_STYLE INCBIN_STYLE_SNAKE
#include <sys/mman.h>
#include "gebs/gebs.h"
#include "incbin/incbin.h"
#include "stb/stb_ds.h"
#include "baked.h"
INCBIN(gpp1, "gpp1");
INCBIN(gpp1, "./gpp1");
INCBIN(home_t, "./tmpls/home.t");
INCBIN(page_missing_t, "./tmpls/page-missing.t");
INCBIN(simple_min_css, "./etc/simple.min.css");
INCBIN(favicon_ico, "./etc/favicon.ico");
Baked_Resource *baked_resources = NULL;
void add_baked_resource(char *key, const uchar *data, size_t size)
{
int fd = memfd_create(key, 0);
if (fd < 0) {
LOGE("Could not create resource %s. Aborting...\n", key);
abort();
}
write(fd, data, size);
shput(baked_resources, key, fd);
}
void init_baked_resources(void)
{
add_baked_resource("home.t", home_t_data, home_t_size);
add_baked_resource("page-missing.t", page_missing_t_data, page_missing_t_size);
add_baked_resource("gpp1", gpp1_data, gpp1_size);
add_baked_resource("simple.min.css", simple_min_css_data, simple_min_css_size);
add_baked_resource("favicon.ico", favicon_ico_data, favicon_ico_size);
}
void free_baked_resources(void)
{
for (size_t i = 0; i < shlen(baked_resources); i++) {
close(baked_resources[i].value);
}
shfree(baked_resources);
}
bool get_baked_resource_path(char *key, char *buf, size_t size)
{
if (shgeti(baked_resources, key) != -1) {
int fd = shget(baked_resources, key);
snprintf(buf, size, "/proc/%d/fd/%d", getpid(), fd);
return true;
}
return false;
}
INCBIN(tmpl_home, "./tmpls/home.t");
INCBIN(tmpl_page_missing, "./tmpls/page-missing.t");