2021-11-30 21:29:18 -06:00
|
|
|
#include "resources.h"
|
|
|
|
|
|
|
|
#include "config.h"
|
2023-05-12 13:22:05 -05:00
|
|
|
#include "log.h"
|
|
|
|
#include <dirent.h>
|
2021-11-30 21:29:18 -06:00
|
|
|
#include <stdarg.h>
|
2023-05-12 13:22:05 -05:00
|
|
|
#include <stddef.h>
|
2021-11-30 21:29:18 -06:00
|
|
|
#include <stdio.h>
|
2023-05-12 13:22:05 -05:00
|
|
|
#include <stdlib.h>
|
2022-01-19 16:43:21 -06:00
|
|
|
#include <string.h>
|
2023-05-12 13:22:05 -05:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2023-09-26 17:07:51 -05:00
|
|
|
#include <errno.h>
|
2022-01-19 16:43:21 -06:00
|
|
|
#include <unistd.h>
|
2023-09-04 09:48:44 -05:00
|
|
|
#include "font.h"
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-09-05 10:44:52 -05:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include "cdb.h"
|
2023-09-04 09:48:44 -05:00
|
|
|
|
|
|
|
#ifndef __EMSCRIPTEN__
|
2022-07-03 00:43:42 -05:00
|
|
|
#include <ftw.h>
|
2023-09-04 09:48:44 -05:00
|
|
|
#endif
|
2022-07-03 00:43:42 -05:00
|
|
|
|
2023-02-13 21:02:14 -06:00
|
|
|
#include "stb_ds.h"
|
|
|
|
|
2023-09-18 07:36:07 -05:00
|
|
|
#include "core.cdb.h"
|
|
|
|
|
2021-11-30 21:29:18 -06:00
|
|
|
char *DATA_PATH = NULL;
|
|
|
|
char *PREF_PATH = NULL;
|
|
|
|
|
2023-08-31 17:23:24 -05:00
|
|
|
char **prefabs;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
|
|
|
static const char *cur_ext = NULL;
|
|
|
|
struct dirent *c_dirent = NULL;
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
char pathbuf[MAXPATH + 1];
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-09-04 09:48:44 -05:00
|
|
|
const char *DB_NAME = "test.db";
|
|
|
|
|
2023-09-18 07:36:07 -05:00
|
|
|
static struct cdb corecdb;
|
2023-09-05 10:44:52 -05:00
|
|
|
static struct cdb game_cdb;
|
2023-09-04 09:48:44 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void resources_init() {
|
|
|
|
DATA_PATH = malloc(MAXPATH);
|
|
|
|
getcwd(DATA_PATH, MAXPATH);
|
|
|
|
strncat(DATA_PATH, "/", MAXPATH);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
if (!PREF_PATH)
|
|
|
|
PREF_PATH = strdup("./tmp/");
|
2023-09-04 09:48:44 -05:00
|
|
|
|
2023-09-18 07:36:07 -05:00
|
|
|
int fd = open("test.cdb", O_RDONLY);
|
2023-09-05 10:44:52 -05:00
|
|
|
cdb_init(&game_cdb, fd);
|
2023-09-18 07:36:07 -05:00
|
|
|
cdb_initf(&corecdb, core_cdb, core_cdb_len);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
char *get_filename_from_path(char *path, int extension) {
|
|
|
|
char *dirpos = strrchr(path, '/');
|
|
|
|
if (!dirpos)
|
|
|
|
dirpos = path;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
char *end = strrchr(path, '\0');
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
int offset = 0;
|
|
|
|
if (!extension) {
|
|
|
|
char *ext = strrchr(path, '.');
|
|
|
|
offset = end - ext;
|
|
|
|
YughInfo("Making %s without extension ...");
|
|
|
|
}
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
char *filename = malloc(sizeof(char) * (end - dirpos - offset + 1));
|
|
|
|
strncpy(filename, dirpos, end - dirpos - offset);
|
|
|
|
return filename;
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
char *get_directory_from_path(char *path) {
|
|
|
|
const char *dirpos = strrchr(path, '/');
|
|
|
|
char *directory = (char *)malloc(sizeof(char) * (dirpos - path + 1));
|
|
|
|
strncpy(directory, path, dirpos - path);
|
|
|
|
return directory;
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
FILE *res_open(char *path, const char *tag) {
|
|
|
|
strncpy(pathbuf, DATA_PATH, MAXPATH);
|
|
|
|
strncat(pathbuf, path, MAXPATH);
|
|
|
|
FILE *f = fopen(pathbuf, tag);
|
|
|
|
return f;
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-09-18 07:36:07 -05:00
|
|
|
char *seprint(char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start (args, fmt);
|
|
|
|
char test[128];
|
|
|
|
int len = vsnprintf(test, 128, fmt, args);
|
|
|
|
if (len > 128) {
|
2023-09-18 10:45:51 -05:00
|
|
|
char *test = malloc(len+1);
|
2023-09-18 07:36:07 -05:00
|
|
|
vsnprintf(test, len+1, fmt, args);
|
|
|
|
return strdup(test);
|
|
|
|
}
|
|
|
|
|
|
|
|
return strdup(test);
|
|
|
|
}
|
|
|
|
|
2023-09-04 09:48:44 -05:00
|
|
|
static char *ext_paths = NULL;
|
|
|
|
|
|
|
|
#ifndef __EMSCRIPTEN__
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
static int ext_check(const char *path, const struct stat *sb, int typeflag) {
|
|
|
|
if (typeflag == FTW_F) {
|
|
|
|
const char *ext = strrchr(path, '.');
|
2023-08-27 21:57:19 -05:00
|
|
|
if (ext != NULL && !strcmp(ext, cur_ext)) {
|
|
|
|
char newstr[255];
|
|
|
|
strncpy(newstr, path, 255);
|
|
|
|
arrput(prefabs, newstr);
|
|
|
|
}
|
2023-05-12 13:22:05 -05:00
|
|
|
}
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
return 0;
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-08-27 21:57:19 -05:00
|
|
|
void fill_extensions(char *paths, const char *path, const char *ext) {
|
2023-05-12 13:22:05 -05:00
|
|
|
cur_ext = ext;
|
2023-08-27 21:57:19 -05:00
|
|
|
arrfree(paths);
|
2023-09-04 09:48:44 -05:00
|
|
|
ext_paths = paths;
|
2023-09-04 01:20:55 -05:00
|
|
|
ftw(".", ext_check, 10);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
2023-09-13 01:08:32 -05:00
|
|
|
|
|
|
|
static char **ls_paths = NULL;
|
|
|
|
|
|
|
|
static int ls_ftw(const char *path, const struct stat *sb, int typeflag)
|
|
|
|
{
|
|
|
|
if (typeflag == FTW_F && strlen(path) > 2)
|
|
|
|
arrpush(ls_paths, strdup(&path[2]));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char **ls(char *path)
|
|
|
|
{
|
|
|
|
if (ls_paths) {
|
|
|
|
for (int i = 0; i < arrlen(ls_paths); i++)
|
|
|
|
free(ls_paths[i]);
|
|
|
|
|
|
|
|
arrfree(ls_paths);
|
|
|
|
}
|
|
|
|
ftw(".", ls_ftw, 10);
|
|
|
|
return ls_paths;
|
|
|
|
}
|
|
|
|
|
2023-09-04 09:48:44 -05:00
|
|
|
#else
|
|
|
|
void fill_extensions(char *paths, const char *path, const char *ext)
|
|
|
|
{};
|
|
|
|
#endif
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2022-08-24 12:24:21 -05:00
|
|
|
char *str_replace_ext(const char *s, const char *newext) {
|
2023-05-12 13:22:05 -05:00
|
|
|
static char ret[256];
|
2022-08-24 12:24:21 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
strncpy(ret, s, 256);
|
|
|
|
char *ext = strrchr(ret, '.');
|
|
|
|
strncpy(ext, newext, 10);
|
2022-08-24 12:24:21 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
return ret;
|
2022-08-24 12:24:21 -05:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
FILE *path_open(const char *tag, const char *fmt, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsprintf(pathbuf, fmt, args);
|
|
|
|
va_end(args);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
FILE *f = fopen(pathbuf, tag);
|
|
|
|
return f;
|
2022-01-19 16:43:21 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
char *make_path(const char *file) {
|
|
|
|
strncpy(pathbuf, DATA_PATH, MAXPATH);
|
|
|
|
strncat(pathbuf, file, MAXPATH);
|
|
|
|
return pathbuf;
|
2022-02-06 10:14:57 -06:00
|
|
|
}
|
2022-08-25 15:48:15 -05:00
|
|
|
|
2023-09-18 07:36:07 -05:00
|
|
|
void *cdb_slurp(struct cdb *cdb, const char *file, size_t *size)
|
2023-09-04 09:48:44 -05:00
|
|
|
{
|
2023-09-05 10:44:52 -05:00
|
|
|
unsigned vlen, vpos;
|
2023-09-18 07:36:07 -05:00
|
|
|
vpos = cdb_datapos(cdb);
|
|
|
|
vlen = cdb_datalen(cdb);
|
2023-10-03 17:16:38 -05:00
|
|
|
char *data = malloc(vlen+1);
|
2023-09-18 07:36:07 -05:00
|
|
|
cdb_read(cdb, data, vlen, vpos);
|
2023-09-06 12:17:16 -05:00
|
|
|
if (size) *size = vlen;
|
2023-09-11 02:46:12 -05:00
|
|
|
return data;
|
2023-09-18 07:36:07 -05:00
|
|
|
}
|
|
|
|
|
2023-09-18 10:45:51 -05:00
|
|
|
int fexists(char *path)
|
|
|
|
{
|
|
|
|
return !access(path,R_OK);
|
|
|
|
|
|
|
|
int len = strlen(path);
|
|
|
|
if (cdb_find(&game_cdb, path,len)) return 1;
|
|
|
|
else if (cdb_find(&core_cdb, path, len)) return 1;
|
|
|
|
else if (!access(path, R_OK)) return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-09-23 12:35:02 -05:00
|
|
|
void *os_slurp(const char *file, size_t *size)
|
2023-09-18 07:36:07 -05:00
|
|
|
{
|
2023-09-04 09:48:44 -05:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
jump:
|
2023-09-23 12:35:02 -05:00
|
|
|
f = fopen(file, "rb");
|
2023-09-04 09:48:44 -05:00
|
|
|
|
|
|
|
if (!f) return NULL;
|
|
|
|
|
|
|
|
fseek(f, 0, SEEK_END);
|
2023-09-06 12:17:16 -05:00
|
|
|
size_t fsize = ftell(f);
|
|
|
|
rewind(f);
|
|
|
|
void *slurp = malloc(fsize);
|
2023-09-04 09:48:44 -05:00
|
|
|
fread(slurp, fsize, 1, f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
if (size) *size = fsize;
|
|
|
|
|
|
|
|
return slurp;
|
|
|
|
}
|
|
|
|
|
2023-09-23 12:35:02 -05:00
|
|
|
void *slurp_file(const char *filename, size_t *size)
|
|
|
|
{
|
|
|
|
if (!access(filename, R_OK))
|
|
|
|
return os_slurp(filename, size);
|
|
|
|
else if (cdb_find(&game_cdb, filename, strlen(filename)))
|
|
|
|
return cdb_slurp(&game_cdb, filename, size);
|
|
|
|
else if (cdb_find(&corecdb, filename, strlen(filename)))
|
|
|
|
return cdb_slurp(&corecdb, filename, size);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-09-07 16:46:35 -05:00
|
|
|
char *slurp_text(const char *filename, size_t *size)
|
2023-09-05 10:44:52 -05:00
|
|
|
{
|
2023-09-06 12:17:16 -05:00
|
|
|
size_t len;
|
2023-09-18 07:36:07 -05:00
|
|
|
unsigned char *str = slurp_file(filename, &len);
|
2023-09-13 01:08:32 -05:00
|
|
|
if (!str) return NULL;
|
2023-09-06 12:17:16 -05:00
|
|
|
char *retstr = malloc(len+1);
|
|
|
|
memcpy(retstr, str, len);
|
2023-09-11 02:46:12 -05:00
|
|
|
retstr[len] = '\0';
|
2023-09-06 12:17:16 -05:00
|
|
|
free(str);
|
2023-09-07 16:46:35 -05:00
|
|
|
if (size) *size = len;
|
2023-09-06 12:17:16 -05:00
|
|
|
return retstr;
|
2023-09-04 09:48:44 -05:00
|
|
|
}
|
|
|
|
|
2023-11-17 15:16:13 -06:00
|
|
|
int cp(char *p1, char *p2)
|
|
|
|
{
|
|
|
|
long len;
|
|
|
|
void *data = slurp_file(p1, &len);
|
|
|
|
|
|
|
|
FILE *f = fopen_mkdir(p2, "w");
|
|
|
|
if (!f) return 1;
|
|
|
|
fwrite(data, len, 1, f);
|
|
|
|
free(data);
|
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-09-26 17:07:51 -05:00
|
|
|
void rek_mkdir(char *path) {
|
|
|
|
char *sep = strrchr(path, '/');
|
|
|
|
if(sep != NULL) {
|
|
|
|
*sep = 0;
|
|
|
|
rek_mkdir(path);
|
|
|
|
*sep = '/';
|
|
|
|
}
|
|
|
|
#if defined __WIN32
|
|
|
|
if(mkdir(path) && errno != EEXIST)
|
|
|
|
#else
|
|
|
|
if (mkdir(path, 0777) && errno != EEXIST)
|
|
|
|
#endif
|
|
|
|
printf("error while trying to create '%s'\n%m\n", path);
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *fopen_mkdir(char *path, char *mode) {
|
|
|
|
char *sep = strrchr(path, '/');
|
|
|
|
if(sep) {
|
|
|
|
char *path0 = strdup(path);
|
|
|
|
path0[ sep - path ] = 0;
|
|
|
|
rek_mkdir(path0);
|
|
|
|
free(path0);
|
|
|
|
}
|
|
|
|
return fopen(path,mode);
|
|
|
|
}
|
|
|
|
|
2023-09-04 09:48:44 -05:00
|
|
|
int slurp_write(const char *txt, const char *filename) {
|
2023-09-26 17:07:51 -05:00
|
|
|
FILE *f = fopen_mkdir(filename, "w");
|
2023-09-04 09:48:44 -05:00
|
|
|
if (!f) return 1;
|
|
|
|
|
|
|
|
fputs(txt, f);
|
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef __EMSCRIPTEN__
|
2023-09-05 10:44:52 -05:00
|
|
|
static struct cdb_make cdbm;
|
2023-09-04 09:48:44 -05:00
|
|
|
|
2023-09-11 02:46:12 -05:00
|
|
|
static const char *pack_ext[] = {".qoi", ".qoa", ".js", ".wav", ".mp3", ".png", ".sf2", ".midi", ".lvl", ".glsl", ".ttf"};
|
2023-09-04 09:48:44 -05:00
|
|
|
|
|
|
|
static int ftw_pack(const char *path, const struct stat *sb, int flag)
|
|
|
|
{
|
|
|
|
if (flag != FTW_F) return 0;
|
|
|
|
int pack = 0;
|
|
|
|
char *ext = strrchr(path, '.');
|
2023-09-05 09:38:52 -05:00
|
|
|
|
2023-09-04 09:48:44 -05:00
|
|
|
if (!ext)
|
|
|
|
return 0;
|
|
|
|
|
2023-09-11 02:46:12 -05:00
|
|
|
for (int i = 0; i < 11; i++) {
|
2023-09-04 09:48:44 -05:00
|
|
|
if (!strcmp(ext, pack_ext[i])) {
|
|
|
|
pack = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pack) return 0;
|
|
|
|
|
2023-09-06 12:17:16 -05:00
|
|
|
size_t len;
|
2023-09-04 09:48:44 -05:00
|
|
|
void *file = slurp_file(path, &len);
|
2023-09-05 10:44:52 -05:00
|
|
|
cdb_make_add(&cdbm, &path[2], strlen(&path[2]), file, len);
|
2023-09-04 09:48:44 -05:00
|
|
|
|
|
|
|
free(file);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-09-05 17:09:25 -05:00
|
|
|
void pack_engine(const char *fname)
|
2023-09-04 09:48:44 -05:00
|
|
|
{
|
2023-09-05 10:44:52 -05:00
|
|
|
int fd;
|
|
|
|
char *key, *va;
|
|
|
|
unsigned klen, vlen;
|
2023-09-05 17:09:25 -05:00
|
|
|
fd = open(fname, O_RDWR|O_CREAT);
|
2023-09-05 10:44:52 -05:00
|
|
|
cdb_make_start(&cdbm, fd);
|
2023-09-04 09:48:44 -05:00
|
|
|
ftw(".", ftw_pack, 20);
|
2023-09-05 10:44:52 -05:00
|
|
|
cdb_make_finish(&cdbm);
|
2023-09-04 09:48:44 -05:00
|
|
|
}
|
|
|
|
#else
|
2023-09-05 17:09:25 -05:00
|
|
|
void pack_engine(const char *fname){
|
2023-09-04 09:48:44 -05:00
|
|
|
YughError("Cannot pack engine on a web build.");
|
|
|
|
}
|
2023-09-18 12:35:40 -05:00
|
|
|
|
|
|
|
char **ls(char *path) { return NULL; }
|
2023-09-04 09:48:44 -05:00
|
|
|
#endif
|