Add id check for each chunk load
parent
20d5bd4ac7
commit
5cfa0c0fc0
|
@ -40,107 +40,119 @@ bool load_from_rres(const char* file, Assets_t* assets)
|
|||
rres_file.dir = rresLoadCentralDirectory(file);
|
||||
rres_file.fname = file;
|
||||
|
||||
rresResourceChunk chunk = rresLoadResourceChunk(file, rresGetResourceId(rres_file.dir, "assets.info")); // Hardcoded
|
||||
FILE* in_file = fmemopen(chunk.data.raw, chunk.info.baseSize, "rb");
|
||||
|
||||
char buffer[256];
|
||||
char* tmp;
|
||||
size_t line_num = 0;
|
||||
AssetInfoType_t info_type = INVALID_INFO;
|
||||
|
||||
while (true)
|
||||
if (rres_file.dir.count == 0)
|
||||
{
|
||||
tmp = fgets(buffer, 256, in_file);
|
||||
if (tmp == NULL) break;
|
||||
tmp[strcspn(tmp, "\r\n")] = '\0';
|
||||
puts("Empty central directory");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tmp[0] == '-')
|
||||
int res_id = rresGetResourceId(rres_file.dir, "assets.info");
|
||||
rresResourceChunk chunk = rresLoadResourceChunk(file, res_id); // Hardcoded
|
||||
bool okay = false;
|
||||
|
||||
if (chunk.info.id == res_id)
|
||||
{
|
||||
FILE* in_file = fmemopen(chunk.data.raw, chunk.info.baseSize, "rb");
|
||||
|
||||
char buffer[256];
|
||||
char* tmp;
|
||||
size_t line_num = 0;
|
||||
AssetInfoType_t info_type = INVALID_INFO;
|
||||
|
||||
while (true)
|
||||
{
|
||||
tmp++;
|
||||
if (strcmp(tmp, "Texture") == 0)
|
||||
tmp = fgets(buffer, 256, in_file);
|
||||
if (tmp == NULL) break;
|
||||
tmp[strcspn(tmp, "\r\n")] = '\0';
|
||||
|
||||
if (tmp[0] == '-')
|
||||
{
|
||||
info_type = TEXTURE_INFO;
|
||||
}
|
||||
else if (strcmp(tmp, "Sprite") == 0)
|
||||
{
|
||||
info_type = SPRITE_INFO;
|
||||
}
|
||||
else if (strcmp(tmp, "LevelPack") == 0)
|
||||
{
|
||||
info_type = LEVELPACK_INFO;
|
||||
tmp++;
|
||||
if (strcmp(tmp, "Texture") == 0)
|
||||
{
|
||||
info_type = TEXTURE_INFO;
|
||||
}
|
||||
else if (strcmp(tmp, "Sprite") == 0)
|
||||
{
|
||||
info_type = SPRITE_INFO;
|
||||
}
|
||||
else if (strcmp(tmp, "LevelPack") == 0)
|
||||
{
|
||||
info_type = LEVELPACK_INFO;
|
||||
}
|
||||
else
|
||||
{
|
||||
info_type = INVALID_INFO;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
info_type = INVALID_INFO;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
char* name = strtok(buffer, ":");
|
||||
char* info_str = strtok(NULL, ":");
|
||||
if (name == NULL || info_str == NULL) continue;
|
||||
char* name = strtok(buffer, ":");
|
||||
char* info_str = strtok(NULL, ":");
|
||||
if (name == NULL || info_str == NULL) continue;
|
||||
|
||||
while(*name == ' ' || *name == '\t') name++;
|
||||
while(*info_str == ' ' || *info_str == '\t') info_str++;
|
||||
switch(info_type)
|
||||
{
|
||||
case TEXTURE_INFO:
|
||||
while(*name == ' ' || *name == '\t') name++;
|
||||
while(*info_str == ' ' || *info_str == '\t') info_str++;
|
||||
switch(info_type)
|
||||
{
|
||||
//if (add_texture(assets, name, info_str) == NULL)
|
||||
if (add_texture_rres(assets, name, info_str, &rres_file) == NULL)
|
||||
case TEXTURE_INFO:
|
||||
{
|
||||
printf("Unable to add texture at line %lu\n", line_num);
|
||||
break;
|
||||
//if (add_texture(assets, name, info_str) == NULL)
|
||||
if (add_texture_rres(assets, name, info_str, &rres_file) == NULL)
|
||||
{
|
||||
printf("Unable to add texture at line %lu\n", line_num);
|
||||
break;
|
||||
}
|
||||
printf("Added texture %s as %s\n", info_str, name);
|
||||
//strcpy(tmp2, name);
|
||||
}
|
||||
printf("Added texture %s as %s\n", info_str, name);
|
||||
//strcpy(tmp2, name);
|
||||
break;
|
||||
case LEVELPACK_INFO:
|
||||
{
|
||||
//if (add_level_pack(assets, name, info_str) == NULL)
|
||||
if (add_level_pack_rres(assets, name, info_str, &rres_file) == NULL)
|
||||
{
|
||||
printf("Unable to add level pack at line %lu\n", line_num);
|
||||
break;
|
||||
}
|
||||
printf("Added level pack %s as %s\n", info_str, name);
|
||||
}
|
||||
break;
|
||||
case SPRITE_INFO:
|
||||
{
|
||||
SpriteInfo_t spr_info = {0};
|
||||
if (!parse_sprite_info(info_str, &spr_info))
|
||||
{
|
||||
printf("Unable to parse info for sprite at line %lu\n", line_num);
|
||||
break;
|
||||
}
|
||||
//printf("Compare %s,%s = %d\n", tmp2, spr_info.tex, strcmp(tmp2, spr_info.tex));
|
||||
Texture2D* tex = get_texture(assets, spr_info.tex);
|
||||
if (tex == NULL)
|
||||
{
|
||||
printf("Unable to get texture info %s for sprite %s\n", spr_info.tex, name);
|
||||
break;
|
||||
}
|
||||
printf("Added Sprite %s from texture %s\n", name, spr_info.tex);
|
||||
Sprite_t* spr = add_sprite(assets, name, tex);
|
||||
spr->origin = spr_info.origin;
|
||||
spr->frame_size = spr_info.frame_size;
|
||||
spr->frame_count = spr_info.frame_count;
|
||||
spr->speed = spr_info.speed;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case LEVELPACK_INFO:
|
||||
{
|
||||
//if (add_level_pack(assets, name, info_str) == NULL)
|
||||
if (add_level_pack_rres(assets, name, info_str, &rres_file) == NULL)
|
||||
{
|
||||
printf("Unable to add level pack at line %lu\n", line_num);
|
||||
break;
|
||||
}
|
||||
printf("Added level pack %s as %s\n", info_str, name);
|
||||
}
|
||||
break;
|
||||
case SPRITE_INFO:
|
||||
{
|
||||
SpriteInfo_t spr_info = {0};
|
||||
if (!parse_sprite_info(info_str, &spr_info))
|
||||
{
|
||||
printf("Unable to parse info for sprite at line %lu\n", line_num);
|
||||
break;
|
||||
}
|
||||
//printf("Compare %s,%s = %d\n", tmp2, spr_info.tex, strcmp(tmp2, spr_info.tex));
|
||||
Texture2D* tex = get_texture(assets, spr_info.tex);
|
||||
if (tex == NULL)
|
||||
{
|
||||
printf("Unable to get texture info %s for sprite %s\n", spr_info.tex, name);
|
||||
break;
|
||||
}
|
||||
printf("Added Sprite %s from texture %s\n", name, spr_info.tex);
|
||||
Sprite_t* spr = add_sprite(assets, name, tex);
|
||||
spr->origin = spr_info.origin;
|
||||
spr->frame_size = spr_info.frame_size;
|
||||
spr->frame_count = spr_info.frame_count;
|
||||
spr->speed = spr_info.speed;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
line_num++;
|
||||
}
|
||||
line_num++;
|
||||
fclose(in_file);
|
||||
okay = true;
|
||||
}
|
||||
fclose(in_file);
|
||||
|
||||
rresUnloadResourceChunk(chunk);
|
||||
rresUnloadCentralDirectory(rres_file.dir);
|
||||
return true;
|
||||
return okay;
|
||||
}
|
||||
|
||||
bool load_from_infofile(const char* file, Assets_t* assets)
|
||||
|
|
|
@ -87,19 +87,25 @@ Texture2D* add_texture_rres(Assets_t* assets, const char* name, const char* file
|
|||
uint8_t tex_idx = n_loaded[0];
|
||||
assert(tex_idx < MAX_TEXTURES);
|
||||
|
||||
rresResourceChunk chunk = rresLoadResourceChunk(rres_file->fname, rresGetResourceId(rres_file->dir, filename));
|
||||
int res_id = rresGetResourceId(rres_file->dir, filename);
|
||||
rresResourceChunk chunk = rresLoadResourceChunk(rres_file->fname, res_id);
|
||||
|
||||
//Expect RAW type of png extension
|
||||
Image image = LoadImageFromMemory(GetFileExtension(filename), chunk.data.raw, chunk.info.baseSize);
|
||||
Texture2D tex = LoadTextureFromImage(image);
|
||||
UnloadImage(image);
|
||||
Texture2D* out_tex = NULL;
|
||||
if (chunk.info.id == res_id)
|
||||
{
|
||||
//Expect RAW type of png extension
|
||||
Image image = LoadImageFromMemory(GetFileExtension(filename), chunk.data.raw, chunk.info.baseSize);
|
||||
Texture2D tex = LoadTextureFromImage(image);
|
||||
UnloadImage(image);
|
||||
|
||||
textures[tex_idx].texture = tex;
|
||||
strncpy(textures[tex_idx].name, name, MAX_NAME_LEN);
|
||||
sc_map_put_s64(&assets->m_textures, textures[tex_idx].name, tex_idx);
|
||||
n_loaded[0]++;
|
||||
out_tex = &textures[tex_idx].texture;
|
||||
}
|
||||
rresUnloadResourceChunk(chunk);
|
||||
|
||||
textures[tex_idx].texture = tex;
|
||||
strncpy(textures[tex_idx].name, name, MAX_NAME_LEN);
|
||||
sc_map_put_s64(&assets->m_textures, textures[tex_idx].name, tex_idx);
|
||||
n_loaded[0]++;
|
||||
return &textures[tex_idx].texture;
|
||||
return out_tex;
|
||||
}
|
||||
|
||||
Sprite_t* add_sprite(Assets_t* assets, const char* name, Texture2D* texture)
|
||||
|
@ -312,14 +318,18 @@ LevelPack_t* uncompress_level_pack(Assets_t* assets, const char* name, const cha
|
|||
|
||||
LevelPack_t* add_level_pack_rres(Assets_t* assets, const char* name, const char* filename, const RresFileInfo_t* rres_file)
|
||||
{
|
||||
int res_id = rresGetResourceId(rres_file->dir, filename);
|
||||
|
||||
rresResourceChunk chunk = rresLoadResourceChunk(rres_file->fname, rresGetResourceId(rres_file->dir, filename));
|
||||
FILE* f_in = fmemopen(chunk.data.raw, chunk.info.baseSize, "rb");
|
||||
rresResourceChunk chunk = rresLoadResourceChunk(rres_file->fname, res_id);
|
||||
|
||||
LevelPack_t* pack = add_level_pack_zst(assets, name, f_in);
|
||||
fclose(f_in);
|
||||
LevelPack_t* pack = NULL;
|
||||
if ( chunk.info.id == res_id)
|
||||
{
|
||||
FILE* f_in = fmemopen(chunk.data.raw, chunk.info.baseSize, "rb");
|
||||
pack = add_level_pack_zst(assets, name, f_in);
|
||||
fclose(f_in);
|
||||
}
|
||||
rresUnloadResourceChunk(chunk);
|
||||
|
||||
return pack;
|
||||
}
|
||||
|
||||
|
|
|
@ -183,11 +183,22 @@ bool init_player_creation_rres(const char* rres_fname, const char* file, Assets_
|
|||
rres_file.dir = rresLoadCentralDirectory(rres_fname);
|
||||
rres_file.fname = rres_fname;
|
||||
|
||||
rresResourceChunk chunk = rresLoadResourceChunk(rres_file.fname, rresGetResourceId(rres_file.dir, file)); // Hardcoded
|
||||
FILE* in_file = fmemopen(chunk.data.raw, chunk.info.baseSize, "rb");
|
||||
if (rres_file.dir.count == 0)
|
||||
{
|
||||
puts("Empty central directory");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool okay = init_player_file(in_file, assets);
|
||||
fclose(in_file);
|
||||
int res_id = rresGetResourceId(rres_file.dir, file);
|
||||
rresResourceChunk chunk = rresLoadResourceChunk(rres_file.fname, res_id);
|
||||
|
||||
bool okay = false;
|
||||
if (chunk.info.id == res_id)
|
||||
{
|
||||
FILE* in_file = fmemopen(chunk.data.raw, chunk.info.baseSize, "rb");
|
||||
okay = init_player_file(in_file, assets);
|
||||
fclose(in_file);
|
||||
}
|
||||
|
||||
rresUnloadResourceChunk(chunk);
|
||||
rresUnloadCentralDirectory(rres_file.dir);
|
||||
|
|
Loading…
Reference in New Issue