Merge branch 'file_functions' into 'master'

File functions

See merge request banjo.decomp/banjo-kazooie!67
This commit is contained in:
Banjo Kazooie
2024-09-13 23:38:30 +00:00
16 changed files with 336 additions and 286 deletions

View File

@@ -843,7 +843,7 @@ segments:
# - [0x1018B00, c, code_C31A0] #DONE
# - [0x1018D60, c, rand] #DONE
# - [0x10193A0, c, code_C3A40] #DONE
# - [0x1019480, c, code_C3B20] #DONE
# - [0x1019480, c, file] #DONE
# - [0x1019C80, c, code_C4320] #DONE
# - [0x101A8A0, c, code_C4F40] #DONE
# - [0x101ADA0, c, code_C5440] #DONE

View File

@@ -843,7 +843,7 @@ segments:
- [0x1018B00, c, code_C31A0] #DONE
- [0x1018D60, c, rand] #DONE
- [0x10193A0, c, code_C3A40] #DONE
- [0x1019480, c, code_C3B20] #DONE
- [0x1019480, c, file] #DONE
- [0x1019C80, c, code_C4320] #DONE
- [0x101A8A0, c, code_C4F40] #DONE
- [0x101ADA0, c, code_C5440] #DONE

View File

@@ -4798,4 +4798,12 @@ enum marker_collision_func_type_e {
MARKER_COLLISION_FUNC_2_DIE
};
enum file_mode_e {
FILE_MODE_0_UNKNOWN,
FILE_MODE_1_UNKNOWN,
FILE_MODE_2_FROM_ASSET,
FILE_MODE_3_FROM_MEMORY,
FILE_MODE_4_ALLOCATED
};
#endif

View File

@@ -460,7 +460,6 @@ void func_8033A280(f32);
void func_80346C10(enum bs_e *retVal, enum bs_e fail_state, enum bs_e success_state, enum item_e item_id, int use_item);
void func_80347A14(s32);
void func_8034A174(struct5Bs *this, s32 indx,f32 dst[3]);
Struct61s *func_8034AB6C(enum map_e map_id);
Struct70s *func_8034C528(s32);
Struct70s *func_8034C5AC(s32);
void func_8034DC08(Struct6Ds *, f32[3], f32[3], f32, s32);
@@ -582,4 +581,31 @@ extern void miscFlag_set(enum misc_flag_e arg0);
extern void miscFlag_clear(enum misc_flag_e arg0);
extern void miscFlag_toggle(enum misc_flag_e arg0);
extern void file_close(File* file);
extern File *file_open(enum asset_e asset_id);
extern File *file_openMap(enum map_e map_id);
extern File *file_openWithBaseIndex(s32 indx, enum asset_e base_indx);
extern File *file_openFromMem(void *ptr, s32 size);
extern File *file_allocNew();
extern void file_realloc(File *file, void **arg1, s32 *size);
extern void file_getByte(File *file, u8 *dst);
extern void file_getNBytes(File *file, u8 *dst, s32 cnt);
extern void file_getFloat(File *file, f32 *dst);
extern void file_getNFloat(File *file, f32 *dst, s32 cnt);
extern void file_getWord(File *file, s32 *dst);
extern void file_getNWords(File *file, s32 *dst, s32 cnt);
extern void file_read(File *file, void *dst, s32 len);
extern void file_getShort(File *file, s16 *dst);
extern void file_getNShorts(File *file, s16 *dst, s32 cnt);
extern bool file_isNextByteExpected(File *file, s32 expected);
extern bool file_getByte_ifExpected(File *file, s32 expected, u8 *dst);
extern bool file_getNBytes_ifExpected(File *file, s32 expected, u8 *dst, s32 cnt);
extern bool file_getFloat_ifExpected(File *file, s32 expected, f32 *dst);
extern bool file_getNFloats_ifExpected(File *file, s32 expected, f32 *dst, s32 cnt);
extern bool file_getWord_ifExpected(File *file, s32 expected, s32 *dst);
extern bool file_getNWords_ifExpected(File *file, s32 expected, s32 *dst, s32 cnt);
extern bool file_get_ifExpected(File *file, s32 expected, void *dst, s32 len);
extern bool file_getShort_ifExpected(File *file, s32 expected, s16 *dst);
extern bool file_getNShorts_ifExpected(File *file, s32 expected, s16 *dst, s32 cnt);
#endif

View File

@@ -4,6 +4,8 @@
#include <ultra64.h>
#include "model.h"
#include "core2/vla.h"
#include "enums.h"
#define MERGE(a, b) a ## b
#define UNK_TYPE(t) t
@@ -549,17 +551,17 @@ typedef struct {
//Struct60s moved to top
typedef struct{
void *unk0;
void *unk4;
void *unk8; //start_ptr
void *unkC; //current_ptr
void *unk10; //end_ptr
s32 unk14;
typedef struct file_s {
void *asset_base_ptr;
void *asset_current_ptr;
void *base_ptr;
void *current_ptr;
void *end_ptr;
enum file_mode_e mode;
u8 pad18[0x64];
s32 unk7C;
s32 unk80;
}Struct61s; //file stream
s32 last_expected; // used in file_isNextByteExpected
s32 unk80; // always set to -1 and never used
} File;
typedef struct {
s16 unk0;

View File

@@ -25,7 +25,7 @@ void func_802BA23C(s32 *arg0, s32 arg1){
*arg0 = arg1;
}
void func_802BA244(Struct61s *file_ptr, s32 *arg1){
void func_802BA244(File *file_ptr, s32 *arg1){
while(!file_isNextByteExpected(file_ptr, 0)){
file_getWord_ifExpected(file_ptr, 1, arg1);
}

View File

@@ -129,7 +129,7 @@ void func_802BA530(Struct_core2_33310 *arg0, bool arg1){
func_802BA2A0(arg0, arg1, 1);
}
void func_802BA550(Struct61s *file_ptr, Struct_core2_33310 *arg1){
void func_802BA550(File *file_ptr, Struct_core2_33310 *arg1){
while(!file_isNextByteExpected(file_ptr, 0)){
if(!file_getNFloats_ifExpected(file_ptr, 1, arg1->unk0, 3)){
if(file_isNextByteExpected(file_ptr, 2)){

View File

@@ -114,7 +114,7 @@ void func_802BA91C(Struct_core2_336F0 *arg0, s32 arg1){
func_802BA680(arg0, arg1, 2);
}
void func_802BA93C(Struct61s *file_ptr, Struct_core2_336F0 *arg1){
void func_802BA93C(File *file_ptr, Struct_core2_336F0 *arg1){
while(!file_isNextByteExpected(file_ptr, 0)){
if(!file_getNFloats_ifExpected(file_ptr, 1, arg1->unk0, 3)){
if(file_isNextByteExpected(file_ptr, 2)){

View File

@@ -43,7 +43,7 @@ void ncCameraNodeType2_setRotation(CameraNodeType2 *this, f32 src[3]){
ml_vec3f_copy(this->rotation, src);
}
void ncCameraNodeType2_fromFile(Struct61s *file_ptr, CameraNodeType2 *arg1){
void ncCameraNodeType2_fromFile(File *file_ptr, CameraNodeType2 *arg1){
while(!file_isNextByteExpected(file_ptr, 0)){
if(!file_getNFloats_ifExpected(file_ptr, 1, arg1->position, 3)){
file_getNFloats_ifExpected(file_ptr, 2, arg1->rotation, 3);

View File

@@ -873,7 +873,7 @@ void func_803045CC(s32 arg0, s32 arg1){}
void func_803045D8(void){}
void func_803045E0(Cube *cube, Struct61s* file_ptr) {
void func_803045E0(Cube *cube, File* file_ptr) {
s32 sp2C[3];
while(!file_isNextByteExpected(file_ptr, 1)) {
@@ -886,7 +886,7 @@ void func_803045E0(Cube *cube, Struct61s* file_ptr) {
}
}
void cubeList_fromFile(Struct61s *file_ptr) {
void cubeList_fromFile(File *file_ptr) {
s32 sp5C[3];
s32 sp50[3];
s32 sp44[3];

View File

@@ -856,7 +856,7 @@ void func_8032E7E8(NodeProp *node, Cube *cube, s32 cnt) {
}
}
void cube_fromFile(Struct61s *file_ptr, Cube *cube) {
void cube_fromFile(File *file_ptr, Cube *cube) {
u8 sp47;
u8 sp46;
NodeProp *temp_v0;

View File

@@ -184,7 +184,7 @@ void func_80333B28(s32 index , s32 *arg1){
v0->unk28[2] = arg1[2];
}
void func_80333B78(Struct61s *file_ptr){
void func_80333B78(File *file_ptr){
f32 sp4C[3];
f32 sp44[2];
s32 sp38[3];

View File

@@ -382,10 +382,10 @@ s32 func_80335134(){
}
void func_80335140(enum map_e map_id) {
Struct61s *fp;
File *fp;
func_80254008();
fp = func_8034AB6C(map_id); //LevelSetupFile_Open
fp = file_openMap(map_id); //LevelSetupFile_Open
while (file_isNextByteExpected(fp, 0) == 0) {
if (file_isNextByteExpected(fp, 2)) {

View File

@@ -1,261 +0,0 @@
#include <ultra64.h>
#include "functions.h"
#include "variables.h"
#define CORE2_C3B20_DEFAULT_SIZE 0x20
void __file_read(Struct61s *file, void *arg1, s32 arg2);
/* .code */
void file_close(Struct61s * file){
if(file->unk14 == 2){
assetcache_release(file->unk0);
}
free(file);
}
Struct61s *func_8034AAF4(enum asset_e asset_id) {
Struct61s * this;
this = (Struct61s *) malloc(sizeof(Struct61s));
if (this == NULL) {
return NULL;
}
this->unk14 = 2;
this->unk7C = -1;
this->unk80 = -1;
this->unk0 = assetcache_get(asset_id);
this->unk4 = this->unk0;
if (this->unk4 != NULL) {
return this;
}
free(this);
return NULL;
}
//open map file stream
Struct61s *func_8034AB6C(enum map_e map_id){
return func_8034AAF4(map_id + 0x71C);
}
Struct61s *func_8034AB8C(s32 indx, enum asset_e base_indx){
indx += base_indx;
return func_8034AAF4(indx);
}
Struct61s *func_8034ABAC(void *ptr, s32 size){
Struct61s * this;
this = (Struct61s *) malloc(sizeof(Struct61s));
this->unk14 = 3;
this->unk7C = -1;
this->unk80 = -1;
this->unk8 = ptr;
this->unkC = this->unk8;
this->unk10 = (u32)this->unk8 + size;
return this;
}
Struct61s *func_8034AC04(void){
Struct61s *this;
this = (Struct61s *) malloc(sizeof(Struct61s));
this->unk14 = 4;
this->unk7C = -1;
this->unk80 = -1;
this->unk8 = malloc(CORE2_C3B20_DEFAULT_SIZE);
this->unkC = this->unk8;
this->unk10 = (u32)this->unk8 + CORE2_C3B20_DEFAULT_SIZE;
return this;
}
void file_realloc(Struct61s *file, void **arg1, s32 *size){
*size = ((u32)file->unkC - (u32)file->unk8);
*arg1 = realloc(file->unk8, *size);
file->unk8 = NULL;
file_close(file);
}
void file_getByte(Struct61s *file, u8 *arg1){
__file_read(file, arg1, 1);
}
void file_getNBytes(Struct61s *file, u8 *arg1, s32 cnt){
while(cnt > 0){
file_getByte(file, arg1);
cnt--;
arg1++;
}
}
void file_getFloat(Struct61s *file, f32 *arg1){
__file_read(file, arg1, 4);
}
void file_getNFloat(Struct61s *file, f32 *arg1, s32 cnt){
while(cnt > 0){
file_getFloat(file, arg1);
cnt--;
arg1++;
}
}
void file_getWord(Struct61s *file, s32 *arg1){
__file_read(file, arg1, 4);
}
void file_getNWords(Struct61s *file, s32 *arg1, s32 cnt){
while(cnt > 0){
file_getWord(file, arg1);
cnt--;
arg1++;
}
}
void __file_read(Struct61s *file, void *dst, s32 len) {
u32 curr_offset;
u32 capacity;
u32 end_ptr;
u32 var_v0;
if (file->unk14 == 2) { //read asset
memcpy(dst, file->unk4, len);
file->unk4 = (void *) ((u32)file->unk4 + len);
}
else if (file->unk14 == 3) { //read bin???
memcpy(dst, file->unkC, len);
file->unkC = (void *) ((u32)file->unkC + len);
}
else if (file->unk14 == 4) { // write bin???
if ((u8*)file->unk10 < (u8*)file->unkC + len) {
curr_offset = (u8*)file->unkC - (u8*)file->unk8;
capacity = ((u8*)file->unk10 - (u8*)file->unk8)*2;
while (((u8*)file->unk8 + capacity) < (u8*)file->unkC + len) {
capacity *= 2;
}
var_v0 = realloc(file->unk8, capacity);
file->unk8 = var_v0;
file->unkC = var_v0 + curr_offset;
file->unk10 = var_v0 + capacity;
}
memcpy(file->unkC, dst, len);
file->unkC = (u32)file->unkC + len;
}
}
void file_getShort(Struct61s *file, s16 *arg1){
__file_read(file, arg1, sizeof(s16));
}
void file_getNShorts(Struct61s *file, s16 *arg1, s32 cnt){
while(cnt > 0){
file_getShort(file, arg1);
cnt--;
arg1++;
}
}
bool file_isNextByteExpected(Struct61s *file, s32 arg1) {
u8 sp1F;
sp1F = arg1;
if ((file->unk14 == 1) || (file->unk14 == 4)) {
file_getByte(file, &sp1F);
return 1;
}
if (file->unk7C == -1) {
file_getByte(file, &sp1F);
if (arg1 == sp1F) {
return 1;
}
file->unk7C = sp1F;
return 0;
}
if (arg1 == file->unk7C) {
file->unk7C = -1;
return 1;
}
return 0;
}
bool file_getByte_ifExpected(Struct61s * file, s32 arg1, u8 *arg2){
if(!file_isNextByteExpected(file, arg1)){
return FALSE;
} else{
file_getByte(file, arg2);
return TRUE;
}
}
bool file_getNBytes_ifExpected(Struct61s * file, s32 arg1, u8 *arg2, s32 n){
if(!file_isNextByteExpected(file, arg1)){
return FALSE;
} else{
file_getNBytes(file, arg2, n);
return TRUE;
}
}
bool file_getFloat_ifExpected(Struct61s * file, s32 arg1, f32 *arg2){
if(!file_isNextByteExpected(file, arg1)){
return FALSE;
} else{
file_getFloat(file, arg2);
return TRUE;
}
}
bool file_getNFloats_ifExpected(Struct61s * file, s32 arg1, f32 *arg2, s32 n){
if(!file_isNextByteExpected(file, arg1)){
return FALSE;
} else{
file_getNFloat(file, arg2, n);
return TRUE;
}
}
bool file_getWord_ifExpected(Struct61s * file, s32 arg1, s32 *arg2){
if(!file_isNextByteExpected(file, arg1)){
return FALSE;
} else{
file_getWord(file, arg2);
return TRUE;
}
}
bool file_getNWords_ifExpected(Struct61s * file, s32 arg1, s32 *arg2, s32 n){
if(!file_isNextByteExpected(file, arg1)){
return FALSE;
} else{
file_getNWords(file, arg2, n);
return TRUE;
}
}
bool file_get_ifExpected(Struct61s * file, s32 arg1, void *dst, s32 size){
if(!file_isNextByteExpected(file, arg1)){
return FALSE;
} else{
__file_read(file, dst, size);
return TRUE;
}
}
bool file_getShort_ifExpected(Struct61s * file, s32 expected, s16 *dst){
if(!file_isNextByteExpected(file, expected)){
return FALSE;
} else{
file_getShort(file, dst);
return TRUE;
}
}
bool file_getNShorts_ifExpected(Struct61s * file, s32 expected, s16 *dst, s32 n){
if(!file_isNextByteExpected(file, expected)){
return FALSE;
} else{
file_getNShorts(file, dst, n);
return TRUE;
}
}

275
src/core2/file.c Normal file
View File

@@ -0,0 +1,275 @@
#include <ultra64.h>
#include "functions.h"
#include "variables.h"
#define FILE_DEFAULT_SIZE 0x20
void file_read(File *file, void *arg1, s32 arg2);
void file_close(File *file) {
if (file->mode == FILE_MODE_2_FROM_ASSET){
assetcache_release(file->asset_base_ptr);
}
free(file);
}
File *file_open(enum asset_e asset_id) {
File * this;
this = (File *) malloc(sizeof(File));
if (this == NULL) {
return NULL;
}
this->mode = FILE_MODE_2_FROM_ASSET;
this->last_expected = -1;
this->unk80 = -1;
this->asset_base_ptr = assetcache_get(asset_id);
this->asset_current_ptr = this->asset_base_ptr;
if (this->asset_current_ptr != NULL) {
return this;
}
free(this);
return NULL;
}
File *file_openMap(enum map_e map_id) {
return file_open(map_id + 0x71C);
}
File *file_openWithBaseIndex(s32 indx, enum asset_e base_indx) {
indx += base_indx;
return file_open(indx);
}
File *file_openFromMem(void *ptr, s32 size) {
File * this;
this = (File *) malloc(sizeof(File));
this->mode = FILE_MODE_3_FROM_MEMORY;
this->last_expected = -1;
this->unk80 = -1;
this->base_ptr = ptr;
this->current_ptr = this->base_ptr;
this->end_ptr = (u8 *) this->base_ptr + size;
return this;
}
File *file_allocNew() {
File *this;
this = (File *) malloc(sizeof(File));
this->mode = FILE_MODE_4_ALLOCATED;
this->last_expected = -1;
this->unk80 = -1;
this->base_ptr = malloc(FILE_DEFAULT_SIZE);
this->current_ptr = this->base_ptr;
this->end_ptr = (u8 *) this->base_ptr + FILE_DEFAULT_SIZE;
return this;
}
void file_realloc(File *file, void **arg1, s32 *size) {
*size = ((u32)file->current_ptr - (u32)file->base_ptr);
*arg1 = realloc(file->base_ptr, *size);
file->base_ptr = NULL;
file_close(file);
}
void file_getByte(File *file, u8 *dst) {
file_read(file, dst, sizeof(u8));
}
void file_getNBytes(File *file, u8 *dst, s32 cnt) {
while (cnt > 0) {
file_getByte(file, dst);
cnt--;
dst++;
}
}
void file_getFloat(File *file, f32 *dst) {
file_read(file, dst, sizeof(f32));
}
void file_getNFloat(File *file, f32 *dst, s32 cnt) {
while (cnt > 0) {
file_getFloat(file, dst);
cnt--;
dst++;
}
}
void file_getWord(File *file, s32 *dst) {
file_read(file, dst, sizeof(s32));
}
void file_getNWords(File *file, s32 *dst, s32 cnt) {
while (cnt > 0) {
file_getWord(file, dst);
cnt--;
dst++;
}
}
void file_read(File *file, void *dst, s32 len) {
u32 curr_offset;
u32 capacity;
void *new_base_ptr;
if (file->mode == FILE_MODE_2_FROM_ASSET) {
memcpy(dst, file->asset_current_ptr, len);
file->asset_current_ptr = (void *) ((u32)file->asset_current_ptr + len);
}
else if (file->mode == FILE_MODE_3_FROM_MEMORY) {
memcpy(dst, file->current_ptr, len);
file->current_ptr = (void *) ((u32)file->current_ptr + len);
}
else if (file->mode == FILE_MODE_4_ALLOCATED) { // why does it write in read function?
if ((u8 *) file->end_ptr < (u8 *) file->current_ptr + len) {
curr_offset = (u8 *) file->current_ptr - (u8 *) file->base_ptr;
capacity = ((u8 *) file->end_ptr - (u8 *) file->base_ptr) * 2;
while (((u8 *) file->base_ptr + capacity) < ((u8*) file->current_ptr + len)) {
capacity *= 2;
}
new_base_ptr = realloc(file->base_ptr, capacity);
file->base_ptr = new_base_ptr;
file->current_ptr = (u8 *) new_base_ptr + curr_offset;
file->end_ptr = (u8 *) new_base_ptr + capacity;
}
memcpy(file->current_ptr, dst, len);
file->current_ptr = (u8 *) file->current_ptr + len;
}
}
void file_getShort(File *file, s16 *dst) {
file_read(file, dst, sizeof(s16));
}
void file_getNShorts(File *file, s16 *dst, s32 cnt) {
while (cnt > 0) {
file_getShort(file, dst);
cnt--;
dst++;
}
}
bool file_isNextByteExpected(File *file, s32 expected) {
u8 saved_expected;
saved_expected = expected;
if ((file->mode == FILE_MODE_1_UNKNOWN) || (file->mode == FILE_MODE_4_ALLOCATED)) {
file_getByte(file, &saved_expected);
return TRUE;
}
if (file->last_expected == -1) {
file_getByte(file, &saved_expected);
if (expected == saved_expected) {
return TRUE;
}
file->last_expected = saved_expected;
return FALSE;
}
if (expected == file->last_expected) {
file->last_expected = -1;
return TRUE;
}
return FALSE;
}
bool file_getByte_ifExpected(File *file, s32 expected, u8 *dst) {
if (!file_isNextByteExpected(file, expected)) {
return FALSE;
} else {
file_getByte(file, dst);
return TRUE;
}
}
bool file_getNBytes_ifExpected(File *file, s32 expected, u8 *dst, s32 cnt) {
if (!file_isNextByteExpected(file, expected)) {
return FALSE;
} else {
file_getNBytes(file, dst, cnt);
return TRUE;
}
}
bool file_getFloat_ifExpected(File *file, s32 expected, f32 *dst) {
if (!file_isNextByteExpected(file, expected)) {
return FALSE;
} else {
file_getFloat(file, dst);
return TRUE;
}
}
bool file_getNFloats_ifExpected(File *file, s32 expected, f32 *dst, s32 cnt) {
if (!file_isNextByteExpected(file, expected)) {
return FALSE;
} else {
file_getNFloat(file, dst, cnt);
return TRUE;
}
}
bool file_getWord_ifExpected(File *file, s32 expected, s32 *dst) {
if (!file_isNextByteExpected(file, expected)) {
return FALSE;
} else {
file_getWord(file, dst);
return TRUE;
}
}
bool file_getNWords_ifExpected(File *file, s32 expected, s32 *dst, s32 cnt) {
if (!file_isNextByteExpected(file, expected)) {
return FALSE;
} else {
file_getNWords(file, dst, cnt);
return TRUE;
}
}
bool file_get_ifExpected(File *file, s32 expected, void *dst, s32 len) {
if (!file_isNextByteExpected(file, expected)) {
return FALSE;
} else{
file_read(file, dst, len);
return TRUE;
}
}
bool file_getShort_ifExpected(File *file, s32 expected, s16 *dst) {
if (!file_isNextByteExpected(file, expected)) {
return FALSE;
} else {
file_getShort(file, dst);
return TRUE;
}
}
bool file_getNShorts_ifExpected(File *file, s32 expected, s16 *dst, s32 cnt) {
if (!file_isNextByteExpected(file, expected)) {
return FALSE;
} else {
file_getNShorts(file, dst, cnt);
return TRUE;
}
}

View File

@@ -120,7 +120,7 @@ void func_802B9EBC(s32 camera_node_index, s32 arg1){
}
void __ncCameraNodeList_nodeFromFile(Struct61s *file_ptr, s32 arg1){
void __ncCameraNodeList_nodeFromFile(File *file_ptr, s32 arg1){
u8 sp27;
__ncCameraNodeList_addNode(arg1);
file_getByte_ifExpected(file_ptr, 2, &sp27);
@@ -143,7 +143,7 @@ void __ncCameraNodeList_nodeFromFile(Struct61s *file_ptr, s32 arg1){
}
}
void ncCameraNodeList_fromFile(Struct61s *file_ptr){
void ncCameraNodeList_fromFile(File *file_ptr){
s16 sp26;
ncCameraNodeList_free();
ncCameraNodeList_init();