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>
|
2022-01-19 16:43:21 -06:00
|
|
|
#include <unistd.h>
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2022-07-03 00:43:42 -05:00
|
|
|
#include <ftw.h>
|
|
|
|
|
2023-02-13 21:02:14 -06:00
|
|
|
#include "stb_ds.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
|
|
|
|
|
|
|
const char *EXT_PREFAB = ".prefab";
|
|
|
|
const char *EXT_LEVEL = ".level";
|
2022-08-24 12:24:21 -05:00
|
|
|
const char *EXT_ASSET = ".asset";
|
2021-11-30 21:29:18 -06:00
|
|
|
int stemlen = 0;
|
|
|
|
|
|
|
|
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-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/");
|
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-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-08-31 17:23:24 -05:00
|
|
|
nftw(".", ext_check, 10, 0);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void findPrefabs() {
|
|
|
|
fill_extensions(prefabs, DATA_PATH, EXT_PREFAB);
|
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-05-12 13:22:05 -05:00
|
|
|
char *strdup(const char *s) {
|
|
|
|
char *new = malloc(sizeof(char) * (strlen(s) + 1));
|
|
|
|
strcpy(new, s);
|
|
|
|
return new;
|
2022-08-25 15:48:15 -05:00
|
|
|
}
|