GBFileOpen can open resource now

This commit is contained in:
Nishi 2026-01-10 01:40:21 +09:00
commit cc46e0fc32
Signed by: nishi
GPG key ID: 27EF69B208EB9343
8 changed files with 174 additions and 38 deletions

View file

@ -8,9 +8,11 @@
extern "C" {
#endif
GBDECL GBFile GBFileOpen(GBEngine engine, const char* path);
GBDECL int GBFileRead(GBFile file, void* out, int size);
GBDECL void GBFileClose(GBFile file);
GBDECL GBFile GBFileOpen(GBEngine engine, const char* path);
GBDECL int GBFileRead(GBFile file, void* out, int size);
GBDECL void GBFileSeek(GBFile file, int pos);
GBDECL unsigned int GBFileSize(GBFile file);
GBDECL void GBFileClose(GBFile file);
#ifdef __cplusplus
}

View file

@ -11,7 +11,7 @@ extern "C" {
#endif
/* gl_shader.c */
GBDECL int GBGLShaderPrepare(GLuint* shader, const char* vs, const char* fs);
GBDECL int GBGLShaderPrepare(GBEngine engine, GLuint* shader, const char* vs, const char* fs);
#ifdef __cplusplus
}

View file

@ -9,6 +9,7 @@ extern "C" {
#endif
GBDECL GBResource GBResourceOpen(GBEngine engine, const char* path);
GBDECL void* GBResourceGet(GBResource resource, const char* name, unsigned int* size);
GBDECL void GBResourceClose(GBResource resource);
#ifdef __cplusplus

View file

@ -48,7 +48,10 @@ struct _GBEngine {
};
struct _GBFile {
FILE* fp;
FILE* fp;
unsigned char* data;
unsigned int size;
unsigned int seek;
};
struct _GBResource {

View file

@ -2,6 +2,9 @@
#include <GearBox/String.h>
#include <GearBox/Log.h>
#include <GearBox/Resource.h>
#include <stb_ds.h>
GBFile GBFileOpen(GBEngine engine, const char* path) {
GBFile file = malloc(sizeof(*file));
@ -12,31 +15,70 @@ GBFile GBFileOpen(GBEngine engine, const char* path) {
GBLog(GBLogInfo, "Opening %s", path);
file->seek = 0;
if((file->fp = fopen(path, "rb")) != NULL) {
fseek(file->fp, 0, SEEK_END);
file->size = ftell(file->fp);
fseek(file->fp, 0, SEEK_SET);
return file;
}
s = GBStringDuplicate(path);
if(engine != NULL && ((nam = strstr(s, ":/")) != NULL || (nam = strstr(s, ":\\")) != NULL)) {
int i;
nam[0] = 0;
nam[1] = 0;
nam += 2;
printf("%s\n", nam);
for(i = 0; i < shlen(engine->resource); i++) {
if(strcmp(engine->resource[i].key, s) == 0) {
if((file->data = GBResourceGet(engine->resource[i].value, nam, &file->size)) != NULL) {
free(s);
return file;
}
break;
}
}
}
free(s);
if((file->fp = fopen(path, "rb")) != NULL) {
return file;
}
GBLog(GBLogWarn, "Could not open %s", path);
GBLog(GBLogError, "Could not open %s", path);
GBFileClose(file);
return NULL;
}
int GBFileRead(GBFile file, void* out, int size) {
if(file->fp != NULL) return fread(out, 1, size, file->fp);
if(file->data != NULL) {
int len = 0;
if((file->seek + size) > file->size) {
len = file->size - file->seek;
} else {
len = size;
}
memcpy(out, file->data + file->seek, len);
file->seek += len;
}
}
void GBFileSeek(GBFile file, int pos) {
if(file->fp != NULL) fseek(file->fp, pos, SEEK_SET);
file->seek = pos;
}
unsigned int GBFileSize(GBFile file) {
return file->size;
}
void GBFileClose(GBFile file) {
if(file->fp != NULL) fclose(file->fp);
if(file->data != NULL) free(file->data);
free(file);
}

View file

@ -1,5 +1,7 @@
#include <GearBox/GL.h>
#include <GearBox/File.h>
static void err(const char* path, GLuint shader) {
GLint len, ret;
char* e;
@ -16,40 +18,36 @@ static void err(const char* path, GLuint shader) {
free(e);
}
int GBGLShaderPrepare(GLuint* shader, const char* vs, const char* fs) {
int GBGLShaderPrepare(GBEngine engine, GLuint* shader, const char* vs, const char* fs) {
GLuint vsi;
GLuint fsi;
FILE* f;
GBFile f;
int vsl = 0;
int fsl = 0;
char* vss;
char* fss;
GLint st;
if((f = fopen(vs, "rb")) != NULL) {
fseek(f, 0, SEEK_END);
vsl = ftell(f);
fseek(f, 0, SEEK_SET);
if((f = GBFileOpen(engine, vs)) != NULL) {
int sz = GBFileSize(f);
vss = malloc(vsl + 1);
fread(vss, vsl, 1, f);
vss[vsl] = 0;
vss = malloc(sz + 1);
GBFileRead(f, vss, sz);
vss[sz] = 0;
fclose(f);
GBFileClose(f);
} else {
return 0;
}
if((f = fopen(fs, "rb")) != NULL) {
fseek(f, 0, SEEK_END);
fsl = ftell(f);
fseek(f, 0, SEEK_SET);
if((f = GBFileOpen(engine, fs)) != NULL) {
int sz = GBFileSize(f);
fss = malloc(fsl + 1);
fread(fss, fsl, 1, f);
fss[fsl] = 0;
fss = malloc(sz + 1);
GBFileRead(f, fss, sz);
fss[sz] = 0;
fclose(f);
GBFileClose(f);
} else {
free(vss);

View file

@ -1,9 +1,19 @@
#include <GearBox/Resource.h>
#include <GearBox/File.h>
#include <GearBox/Log.h>
#include <lz4.h>
static unsigned char res_sig[4] = {
0x7f,
'P',
'A',
'K'};
GBResource GBResourceOpen(GBEngine engine, const char* path) {
GBResource resource = malloc(sizeof(*resource));
GBResource resource = malloc(sizeof(*resource));
unsigned char sig[4];
memset(resource, 0, sizeof(*resource));
@ -12,9 +22,70 @@ GBResource GBResourceOpen(GBEngine engine, const char* path) {
return NULL;
}
if(GBFileRead(resource->file, sig, 4) < 4) {
GBResourceClose(resource);
return NULL;
}
if(memcmp(sig, res_sig, 4) != 0) {
GBLog(GBLogError, "%s: Resource has wrong signature", path);
GBResourceClose(resource);
return NULL;
}
return resource;
}
void* GBResourceGet(GBResource resource, const char* name, unsigned int* size) {
char fn[129];
fn[128] = 0;
GBFileSeek(resource->file, 4);
while(!(GBFileRead(resource->file, fn, 128) < 128 || fn[0] == 0)) {
unsigned int actsz = 0;
unsigned int cmpsz = 0;
unsigned char c;
int i;
void* d;
for(i = 0; i < 4; i++) {
if(GBFileRead(resource->file, &c, 1) < 1) return NULL;
actsz = actsz << 8;
actsz = actsz | c;
}
for(i = 0; i < 4; i++) {
if(GBFileRead(resource->file, &c, 1) < 1) return NULL;
cmpsz = cmpsz << 8;
cmpsz = cmpsz | c;
}
*size = actsz;
d = malloc(cmpsz);
if(GBFileRead(resource->file, d, cmpsz) < cmpsz) {
free(d);
return NULL;
}
if(strcmp(fn, name) == 0) {
unsigned char* r = malloc(actsz);
LZ4_decompress_safe(d, r, cmpsz, actsz);
free(d);
return r;
} else {
free(d);
}
}
return NULL;
}
void GBResourceClose(GBResource resource) {
if(resource->file != NULL) GBFileClose(resource->file);

View file

@ -42,15 +42,25 @@ static void pack(const char* fullpath, const char* path) {
outsz = LZ4_compress_default(input, output, insz, m);
if(outsz > 0) {
int sz = outsz;
int i;
char fn[128];
unsigned int sz;
int i;
char fn[128];
memset(fn, 0, 128);
memcpy(fn, path, strlen(path));
fwrite(fn, 1, 128, out);
sz = insz;
for(i = 0; i < 4; i++) {
unsigned char n = (sz >> 24) & 0xff;
fwrite(&n, 1, 1, out);
sz = sz << 8;
}
sz = outsz;
for(i = 0; i < 4; i++) {
unsigned char n = (sz >> 24) & 0xff;
@ -76,7 +86,7 @@ static int is_dir(const char* path) {
#ifdef _WIN32
DWORD dw;
if((dw = GetFileAttributes(path)) == INVALID_FILE_ATTRIBUTES && !(dw & FILE_ATTRIBUTE_DIRECTORY)) return 0;
if((dw = GetFileAttributes(path)) == INVALID_FILE_ATTRIBUTES || !(dw & FILE_ATTRIBUTE_DIRECTORY)) return 0;
return 1;
#else
@ -151,9 +161,18 @@ static void scan(const char* path, const char* pre) {
}
int main(int argc, char** argv) {
unsigned char nul[128];
unsigned char dat[128];
if(argc >= 2) out = fopen(argv[1], "wb");
if(argc >= 2) {
out = fopen(argv[1], "wb");
dat[0] = 0x7f;
dat[1] = 'P';
dat[2] = 'A';
dat[3] = 'K';
fwrite(dat, 1, 4, out);
}
if(argc == 2) {
scan(".", "");
} else if(argc > 2) {
@ -166,8 +185,8 @@ int main(int argc, char** argv) {
return 1;
}
memset(nul, 0, 128);
fwrite(nul, 1, 128, out);
memset(dat, 0, 128);
fwrite(dat, 1, 128, out);
fclose(out);
}