Untested BFile version for saves (#1)

Pas testé parce que j'ai pas la bonne calto sous la main, mais c'est un point de départ.

Reviewed-on: Shadow15510/CalcCity#1
Co-authored-by: Lephenixnoir <sebastien.michelland@protonmail.com>
Co-committed-by: Lephenixnoir <sebastien.michelland@protonmail.com>
This commit is contained in:
Lephenixnoir 2022-06-23 13:44:40 +02:00 committed by Shadow15510
parent d716218997
commit 1ef42ee837
1 changed files with 63 additions and 21 deletions

View File

@ -1,41 +1,83 @@
#include <stdio.h>
#include <gint/bfile.h>
#include <gint/hardware.h>
#include "save.h"
const char *filename = "/CalcCity.sav";
char const *filename_u8 = "/CalcCity.sav";
uint16_t const *filename_u16 = u"\\\\fls0\\CalcCity.sav";
void read_save(struct calccity *calccity, struct camera *camera, struct map *map)
{
FILE *fp;
fp = fopen(filename, "rb");
if(gint[HWCALC] == HWCALC_G35PE2)
{
FILE *fp;
fp = fopen(filename_u8, "rb");
// If the backup doesn't exist
if (fp == NULL)
default_values(calccity, camera, map);
// If the backup doesn't exist
if (fp == NULL)
default_values(calccity, camera, map);
else
{
fread(calccity, 1, sizeof *calccity, fp);
fread(camera, 1, sizeof *camera, fp);
fread(map, 1, sizeof *map, fp);
fclose(fp);
}
}
else
{
fread(calccity, 1, sizeof *calccity, fp);
fread(camera, 1, sizeof *camera, fp);
fread(map, 1, sizeof *map, fp);
fclose(fp);
int fd = BFile_Open(filename_u16, BFile_ReadOnly);
if(fd < 0)
default_values(calccity, camera, map);
else
{
BFile_Read(fd, calccity, sizeof *calccity, -1);
BFile_Read(fd, camera, sizeof *camera, -1);
BFile_Read(fd, map, sizeof *map, -1);
BFile_Close(fd);
}
}
}
void write_save(const struct calccity *calccity, const struct camera *camera, const struct map *map)
{
FILE *fp;
fp = fopen(filename, "wb");
if (fp != NULL)
if(gint[HWCALC] == HWCALC_G35PE2)
{
fwrite(calccity, sizeof *calccity, 1, fp);
fwrite(camera, sizeof *camera, 1, fp);
fwrite(map, sizeof *map, 1, fp);
FILE *fp;
fp = fopen(filename_u8, "wb");
fclose(fp);
if (fp != NULL)
{
fwrite(calccity, sizeof *calccity, 1, fp);
fwrite(camera, sizeof *camera, 1, fp);
fwrite(map, sizeof *map, 1, fp);
fclose(fp);
}
}
else
{
// Only even-size writes
if((sizeof *calccity) & 1 ||
(sizeof *camera) & 1 ||
(sizeof *map) & 1)
return;
BFile_Remove(filename_u16);
int size = sizeof *calccity + sizeof *camera + sizeof *map;
int rc = BFile_Create(filename_u16, BFile_File, &size);
if(rc < 0) return;
int fd = BFile_Open(filename_u16, BFile_WriteOnly);
if(fd < 0) return;
BFile_Write(fd, calccity, sizeof *calccity);
BFile_Write(fd, camera, sizeof *camera);
BFile_Write(fd, map, sizeof *map);
BFile_Close(fd);
}
}