I'm starting with basics and trying to make a "create" savefile function without zlib compression (That will be easy to add later).
//insert the includes here
typedef struct SaveFile{
uint32_t var1;
uint8_t var2;
} SaveFile_t;
SaveFile_t save;
unsigned char * icon; //uint8_t
unsigned short * palette; //uint16_t
int DC_CreateVMUSaveFile(char *src){
char dst[32];
vmu_pkg_t pkg;
uint8_t *pkg_out;
int pkg_size;
//Setup the save file ready to go into a VMU header
uint8_t * data;
int filesize = sizeof(SaveFile_t);
data = (uint8_t *) malloc(filesize);
memcpy(data, &save, filesize);
// Our VMU + full save name
strcpy(dst, "/vmu/a1/");
strcat(dst, src);
// Required VMU header
strcpy(pkg.desc_short, "Crayon\\DC"); //I think the \\DC is redundant
strcpy(pkg.desc_long, "Game Save");
strcpy(pkg.app_id, "CRAYON_VMU_TEST\\DC");
memcpy(&pkg.icon_pal[0], palette, 32);
pkg.icon_data = icon;
pkg.data_len = filesize;
pkg.data = data;
vmu_pkg_build(&pkg, &pkg_out, &pkg_size);
pkg.icon_cnt = 1;
pkg.icon_anim_speed = 0;
pkg.eyecatch_type = VMUPKG_EC_NONE;
// Save the newly created VMU save to the VMU
file_t file = fs_open(dst, O_WRONLY); //Errors
if(file == -1){
printf("File DNE\n");
}
printf("%d\n", file);
fs_write(file, pkg_out, pkg_size);
fs_close(file); //Errors
// Free unused memory
free(pkg_out);
free(data);
return 0;
}
// Romdisk
extern uint8 romdisk_boot[];
KOS_INIT_ROMDISK(romdisk_boot);
int main(void){
pvr_init_defaults();
save.var1 = 20000;
save.var2 = 41;
file_t file;
file = fs_open("/rd/IMAGE.BIN", O_RDONLY);
int filesize = fs_total(file);
icon = (unsigned char *) malloc(filesize);
fs_read(file, icon, filesize);
fs_close(file);
file = fs_open("/rd/PALLETTE.BIN", O_RDONLY);
int filesize2 = fs_total(file);
palette = (unsigned short *) malloc(filesize2);
fs_read(file, palette, filesize2);
fs_close(file);
DC_CreateVMUSaveFile("hii");
// main loop goes here
return 0;
}
When I run this code I get two read errors on the lines with the "Errors" comments on them (As is it continues on, but if I add some printf's for the file descriptors when loading IMAGE.BIN and PALLETTE.BIN then it crashes after the second "Errors" read fails...weird).
I think the main problem is that I'm trying to open a file that doesn't exist yet, being "/vmu/a1/hii" (I'm not even sure if that's a valid name or if it needs to have a certain extension) and I don't see a function that makes a new file in the VFS (Aside from `fs_load()` which I don't think I want for this). The docs for `fs_open()` don't mention whether its supposed to return -1 if the file DNE or it makes a new empty file. It doesn't even make sense for `fs_open()` to make a new file because it wouldn't know how large you want it to be. The file descriptor when printed is 3, not -1 so I don't know what to make of this.