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>
|
2024-10-19 18:37:27 -05:00
|
|
|
|
2024-03-28 17:40:14 -05:00
|
|
|
#include "miniz.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
|
|
|
|
2024-03-04 11:15:55 -06:00
|
|
|
#include "sokol/sokol_fetch.h"
|
2023-02-13 21:02:14 -06:00
|
|
|
#include "stb_ds.h"
|
|
|
|
|
2024-03-05 00:11:34 -06:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <direct.h>
|
|
|
|
#define mkdir(x,y) _mkdir(x)
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
2024-10-19 18:37:27 -05:00
|
|
|
static mz_zip_archive core_res;
|
2023-09-04 09:48:44 -05:00
|
|
|
|
2024-03-11 22:23:02 -05:00
|
|
|
int LOADED_GAME = 0;
|
2024-03-04 11:15:55 -06:00
|
|
|
uint8_t *gamebuf;
|
2024-04-17 06:32:25 -05:00
|
|
|
void *zipbuf;
|
|
|
|
|
|
|
|
sfetch_handle_t game_h;
|
2024-03-04 11:15:55 -06:00
|
|
|
|
2024-10-19 18:37:27 -05:00
|
|
|
void *os_slurp(const char *file, size_t *size)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
jump:
|
|
|
|
f = fopen(file, "rb");
|
|
|
|
|
|
|
|
if (!f) return NULL;
|
|
|
|
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
size_t fsize = ftell(f);
|
|
|
|
rewind(f);
|
|
|
|
void *slurp = malloc(fsize);
|
|
|
|
fread(slurp, fsize, 1, f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
if (size) *size = fsize;
|
|
|
|
|
|
|
|
return slurp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-08 02:46:59 -05:00
|
|
|
#define MAXGAMESIZE 15*1024*1024
|
|
|
|
|
2024-03-04 11:15:55 -06:00
|
|
|
static void response_cb(const sfetch_response_t *r)
|
|
|
|
{
|
|
|
|
if (r->fetched) {
|
2024-10-08 02:46:59 -05:00
|
|
|
YughInfo("FINISHED FETCH\n");
|
2024-04-17 06:32:25 -05:00
|
|
|
zipbuf = malloc(r->data.size);
|
|
|
|
memcpy(zipbuf, r->data.ptr, r->data.size);
|
|
|
|
|
2024-03-04 11:15:55 -06:00
|
|
|
}
|
|
|
|
if (r->finished) {
|
2024-10-08 02:46:59 -05:00
|
|
|
YughInfo("FINISHED RESPONSE\n");
|
2024-04-17 06:32:25 -05:00
|
|
|
LOADED_GAME = 1;
|
|
|
|
void *buf = sfetch_unbind_buffer(r->handle);
|
|
|
|
free(buf);
|
|
|
|
if (r->failed)
|
2024-03-04 11:15:55 -06:00
|
|
|
LOADED_GAME = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-19 18:37:27 -05:00
|
|
|
#if defined(__linux__)
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
#include <mach-o/dyld.h>
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int get_executable_path(char *buffer, unsigned int buffer_size) {
|
|
|
|
#if defined(__linux__)
|
|
|
|
ssize_t len = readlink("/proc/self/exe", buffer, buffer_size - 1);
|
|
|
|
if (len == -1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
buffer[len] = '\0';
|
|
|
|
return len;
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
if (_NSGetExecutablePath(buffer, &buffer_size) == 0) {
|
|
|
|
return buffer_size;
|
|
|
|
}
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
return GetModuleFileName(NULL, buffer, buffer_size);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2024-04-16 07:51:22 -05:00
|
|
|
void *gamedata;
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void resources_init() {
|
2024-03-04 11:15:55 -06:00
|
|
|
sfetch_setup(&(sfetch_desc_t){
|
|
|
|
.max_requests = 1024,
|
|
|
|
.num_channels = 4,
|
|
|
|
.num_lanes = 8,
|
|
|
|
.logger = { .func = sg_logging },
|
|
|
|
});
|
2024-10-19 18:37:27 -05:00
|
|
|
|
|
|
|
size_t coresize;
|
|
|
|
void *core = os_slurp("core.zip", &coresize);
|
|
|
|
mz_zip_reader_init_mem(&core_res, core, coresize, 0);
|
|
|
|
|
2024-04-17 06:32:25 -05:00
|
|
|
#ifdef __EMSCRIPTEN__
|
2024-10-08 02:46:59 -05:00
|
|
|
gamebuf = malloc(MAXGAMESIZE);
|
2024-04-17 06:32:25 -05:00
|
|
|
game_h = sfetch_send(&(sfetch_request_t){
|
2024-04-16 22:27:29 -05:00
|
|
|
.path="game.zip",
|
2024-03-04 11:15:55 -06:00
|
|
|
.callback = response_cb,
|
|
|
|
.buffer = {
|
|
|
|
.ptr = gamebuf,
|
2024-10-08 02:46:59 -05:00
|
|
|
.size = MAXGAMESIZE
|
2024-03-04 11:15:55 -06:00
|
|
|
}
|
|
|
|
});
|
2024-04-17 06:32:25 -05:00
|
|
|
#endif
|
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-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;
|
2024-06-07 00:43:15 -05:00
|
|
|
}
|
2023-09-13 01:08:32 -05:00
|
|
|
|
2024-03-15 10:51:04 -05:00
|
|
|
time_t file_mod_secs(const char *file) {
|
|
|
|
struct stat attr;
|
2024-10-19 18:37:27 -05:00
|
|
|
|
2024-06-07 10:38:21 -05:00
|
|
|
mz_uint index;
|
|
|
|
mz_zip_archive_file_stat pstat;
|
2024-10-19 18:37:27 -05:00
|
|
|
|
2024-08-07 16:55:08 -05:00
|
|
|
if (!stat(file,&attr))
|
|
|
|
return attr.st_mtime;
|
2024-10-19 18:37:27 -05:00
|
|
|
else if ((index = mz_zip_reader_locate_file(&core_res, file, NULL, 0)) != -1) {
|
|
|
|
mz_zip_reader_file_stat(&core_res, index, &pstat);
|
2024-06-07 10:38:21 -05:00
|
|
|
return pstat.m_time;
|
|
|
|
}
|
2024-06-07 00:43:15 -05:00
|
|
|
|
2024-08-07 16:55:08 -05:00
|
|
|
return -1;
|
2024-03-15 10:51:04 -05:00
|
|
|
}
|
|
|
|
|
2024-02-27 10:09:15 -06:00
|
|
|
// TODO: Not reentrant
|
2023-12-21 17:21:01 -06:00
|
|
|
char **ls(const char *path)
|
2023-09-13 01:08:32 -05:00
|
|
|
{
|
|
|
|
if (ls_paths) {
|
|
|
|
for (int i = 0; i < arrlen(ls_paths); i++)
|
|
|
|
free(ls_paths[i]);
|
|
|
|
|
|
|
|
arrfree(ls_paths);
|
|
|
|
}
|
2024-03-04 11:15:55 -06:00
|
|
|
|
|
|
|
ftw(path, ls_ftw, 10);
|
2023-09-13 01:08:32 -05:00
|
|
|
return ls_paths;
|
|
|
|
}
|
|
|
|
|
2023-09-04 09:48:44 -05:00
|
|
|
#else
|
|
|
|
void fill_extensions(char *paths, const char *path, const char *ext)
|
|
|
|
{};
|
2024-03-28 17:40:14 -05:00
|
|
|
char **ls(const char *path) { return NULL; }
|
2024-04-17 06:32:25 -05:00
|
|
|
void pack(const char *name, const char *dir) {}
|
2023-09-04 09:48:44 -05:00
|
|
|
#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-12-21 17:21:01 -06:00
|
|
|
int fexists(const char *path)
|
2023-09-18 10:45:51 -05:00
|
|
|
{
|
|
|
|
int len = strlen(path);
|
2024-10-19 18:37:27 -05:00
|
|
|
if (mz_zip_reader_locate_file(&core_res, path, NULL, 0) != -1) return 1;
|
2023-09-18 10:45:51 -05:00
|
|
|
else if (!access(path, R_OK)) return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-09-04 09:48:44 -05:00
|
|
|
|
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);
|
2024-10-19 18:37:27 -05:00
|
|
|
else
|
|
|
|
return mz_zip_reader_extract_file_to_heap(&core_res, filename, size, 0);
|
2023-09-23 12:35:02 -05:00
|
|
|
}
|
|
|
|
|
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-12-21 17:21:01 -06:00
|
|
|
int cp(const char *p1, const char *p2)
|
2023-11-17 15:16:13 -06:00
|
|
|
{
|
2023-12-20 17:20:29 -06:00
|
|
|
size_t len;
|
2023-11-17 15:16:13 -06:00
|
|
|
void *data = slurp_file(p1, &len);
|
|
|
|
|
2024-03-02 23:34:41 -06:00
|
|
|
FILE *f = fopen(p2, "w");
|
2023-11-17 15:16:13 -06:00
|
|
|
if (!f) return 1;
|
|
|
|
fwrite(data, len, 1, f);
|
|
|
|
free(data);
|
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-03-05 00:11:34 -06:00
|
|
|
int mkpath(char *path, mode_t mode)
|
2024-03-02 23:34:41 -06:00
|
|
|
{
|
2024-03-05 00:11:34 -06:00
|
|
|
char tmp[256];
|
|
|
|
char *p = NULL;
|
|
|
|
size_t len;
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
strncpy(tmp, path, sizeof(tmp));
|
|
|
|
len = strlen(tmp);
|
|
|
|
if (len > 0 && tmp[len - 1] == '/')
|
|
|
|
tmp[len - 1] = 0;
|
|
|
|
|
|
|
|
for (p = tmp + 1; *p; p++) {
|
|
|
|
if (*p == '/') {
|
|
|
|
*p = 0;
|
|
|
|
if (stat(tmp, &sb) != 0) {
|
|
|
|
if (errno != ENOENT || mkdir(tmp, mode) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (!S_ISDIR(sb.st_mode)) {
|
|
|
|
errno = ENOTDIR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*p = '/';
|
|
|
|
}
|
|
|
|
}
|
2024-03-02 23:34:41 -06:00
|
|
|
|
2024-03-05 00:11:34 -06:00
|
|
|
if (stat(tmp, &sb) != 0) {
|
|
|
|
if (errno != ENOENT || mkdir(tmp, mode) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (!S_ISDIR(sb.st_mode)) {
|
|
|
|
errno = ENOTDIR;
|
|
|
|
return -1;
|
|
|
|
}
|
2024-03-02 23:34:41 -06:00
|
|
|
|
2024-03-05 00:11:34 -06:00
|
|
|
return 0;
|
2023-09-26 17:07:51 -05:00
|
|
|
}
|
|
|
|
|
2024-05-20 13:50:57 -05:00
|
|
|
int slurp_write(void *txt, const char *filename, size_t len) {
|
2024-03-02 23:34:41 -06:00
|
|
|
FILE *f = fopen(filename, "w");
|
2023-09-04 09:48:44 -05:00
|
|
|
if (!f) return 1;
|
|
|
|
|
2024-01-31 02:42:15 -06:00
|
|
|
if (len < 0) len = strlen(txt);
|
|
|
|
fwrite(txt, len, 1, f);
|
2023-09-04 09:48:44 -05:00
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|