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

@ -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);