Save file update (incomplete)

This commit is contained in:
2026-05-10 11:20:09 -05:00
parent d7f515575a
commit a8fd55cb38
42 changed files with 2678 additions and 1 deletions
+1
View File
@@ -19,5 +19,6 @@ add_subdirectory(asset)
add_subdirectory(input)
add_subdirectory(log)
add_subdirectory(network)
add_subdirectory(save)
add_subdirectory(system)
add_subdirectory(time)
+11
View File
@@ -0,0 +1,11 @@
# Copyright (c) 2026 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
savepsp.c
savestreampsp.c
)
+30
View File
@@ -0,0 +1,30 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "save/savepsp.h"
#include "save/savestreampsp.h"
typedef savepsp_t saveplatform_t;
typedef savestreampsp_t saveplatformstream_t;
#define saveInitPlatform saveInitPSP
#define saveDisposePlatform saveDisposePSP
#define saveDeletePlatform saveDeletePSP
#define saveStreamOpenReadPlatform(stream, slot) \
saveStreamOpenReadPSP(&(stream)->platform, &(stream)->found, slot)
#define saveStreamOpenWritePlatform(stream, slot) \
saveStreamOpenWritePSP(&(stream)->platform, slot)
#define saveStreamClosePlatform(stream) \
saveStreamClosePSP(&(stream)->platform)
#define saveStreamReadBytesPlatform(stream, buf, len) \
saveStreamReadBytesPSP(&(stream)->platform, buf, len)
#define saveStreamWriteBytesPlatform(stream, buf, len) \
saveStreamWriteBytesPSP(&(stream)->platform, buf, len)
#define saveStreamSeekPlatform(stream, pos) \
saveStreamSeekPSP(&(stream)->platform, pos)
+81
View File
@@ -0,0 +1,81 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "save/save.h"
errorret_t saveInitPSP(void) {
errorOk();
}
errorret_t saveDisposePSP(void) {
errorOk();
}
errorret_t saveLoadPSP(const uint8_t slot, savefile_t *file) {
char_t path[SAVE_PSP_PATH_MAX];
snprintf(path, SAVE_PSP_PATH_MAX, SAVE_PSP_FILE_FORMAT,
SAVE_PSP_TITLE_ID, (uint32_t)slot
);
SceUID fd = sceIoOpen(path, PSP_O_RDONLY, 0);
if(fd < 0) {
file->exists = false;
errorOk();
}
int32_t read = sceIoRead(fd, file, sizeof(savefile_t));
sceIoClose(fd);
if(read != (int32_t)sizeof(savefile_t)) {
file->exists = false;
errorThrow("Failed to read save data for slot %u", (uint32_t)slot);
}
file->exists = true;
errorOk();
}
errorret_t saveWritePSP(const uint8_t slot, const savefile_t *file) {
char_t dir[SAVE_PSP_PATH_MAX];
snprintf(dir, SAVE_PSP_PATH_MAX, SAVE_PSP_DIR_FORMAT,
SAVE_PSP_TITLE_ID, (uint32_t)slot
);
sceIoMkdir(dir, 0777);
char_t path[SAVE_PSP_PATH_MAX];
snprintf(path, SAVE_PSP_PATH_MAX, SAVE_PSP_FILE_FORMAT,
SAVE_PSP_TITLE_ID, (uint32_t)slot
);
SceUID fd = sceIoOpen(path, PSP_O_WRONLY | PSP_O_CREAT | PSP_O_TRUNC, 0777);
if(fd < 0) {
errorThrow("Failed to open save file for writing: slot %u", (uint32_t)slot);
}
int32_t written = sceIoWrite(fd, file, sizeof(savefile_t));
sceIoClose(fd);
if(written != (int32_t)sizeof(savefile_t)) {
errorThrow("Failed to write save data for slot %u", (uint32_t)slot);
}
errorOk();
}
errorret_t saveDeletePSP(const uint8_t slot) {
char_t path[SAVE_PSP_PATH_MAX];
snprintf(path, SAVE_PSP_PATH_MAX, SAVE_PSP_FILE_FORMAT,
SAVE_PSP_TITLE_ID, (uint32_t)slot
);
int32_t result = sceIoRemove(path);
if(result < 0 && result != (int32_t)0x80010002) {
errorThrow("Failed to delete save file for slot %u", (uint32_t)slot);
}
errorOk();
}
+63
View File
@@ -0,0 +1,63 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "error/error.h"
#include "save/savefile.h"
#include <pspiofilemgr.h>
#define SAVE_PSP_PATH_MAX 256
#define SAVE_PSP_FILE_FORMAT "ms0:/PSP/SAVEDATA/%s%02u/save.dat"
#define SAVE_PSP_DIR_FORMAT "ms0:/PSP/SAVEDATA/%s%02u"
#ifndef SAVE_PSP_TITLE_ID
#define SAVE_PSP_TITLE_ID "DUSK00001"
#endif
typedef struct {
uint8_t unused;
} savepsp_t;
/**
* Initializes the save system on PSP.
*
* @return An error code if initialization fails.
*/
errorret_t saveInitPSP(void);
/**
* Disposes of the save system on PSP.
*
* @return An error code if disposal fails.
*/
errorret_t saveDisposePSP(void);
/**
* Loads a save file from PSP save data for the given slot.
*
* @param slot The save slot index.
* @param file Output save file data.
* @return An error code if the load fails.
*/
errorret_t saveLoadPSP(const uint8_t slot, savefile_t *file);
/**
* Writes a save file to PSP save data for the given slot.
*
* @param slot The save slot index.
* @param file Save file data to write.
* @return An error code if the write fails.
*/
errorret_t saveWritePSP(const uint8_t slot, const savefile_t *file);
/**
* Deletes the save file for the given slot from PSP save data.
*
* @param slot The save slot index.
* @return An error code if the delete fails.
*/
errorret_t saveDeletePSP(const uint8_t slot);
+77
View File
@@ -0,0 +1,77 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "save/save.h"
#include "save/savestreampsp.h"
errorret_t saveStreamOpenReadPSP(
savestreampsp_t *p, bool_t *found, const uint8_t slot
) {
char_t path[SAVE_PSP_PATH_MAX];
snprintf(path, SAVE_PSP_PATH_MAX, SAVE_PSP_FILE_FORMAT,
SAVE_PSP_TITLE_ID, (uint32_t)slot
);
p->fd = sceIoOpen(path, PSP_O_RDONLY, 0);
*found = (p->fd >= 0);
errorOk();
}
errorret_t saveStreamOpenWritePSP(savestreampsp_t *p, const uint8_t slot) {
char_t dir[SAVE_PSP_PATH_MAX];
snprintf(dir, SAVE_PSP_PATH_MAX, SAVE_PSP_DIR_FORMAT,
SAVE_PSP_TITLE_ID, (uint32_t)slot
);
sceIoMkdir(dir, 0777);
char_t path[SAVE_PSP_PATH_MAX];
snprintf(path, SAVE_PSP_PATH_MAX, SAVE_PSP_FILE_FORMAT,
SAVE_PSP_TITLE_ID, (uint32_t)slot
);
p->fd = sceIoOpen(path, PSP_O_WRONLY | PSP_O_CREAT | PSP_O_TRUNC, 0777);
if(p->fd < 0) {
errorThrow(
"Failed to open PSP save file for writing: slot %u", (uint32_t)slot
);
}
errorOk();
}
void saveStreamClosePSP(savestreampsp_t *p) {
if(p->fd >= 0) {
sceIoClose(p->fd);
p->fd = -1;
}
}
errorret_t saveStreamReadBytesPSP(
savestreampsp_t *p, void *buf, const size_t len
) {
int32_t read = sceIoRead(p->fd, buf, (SceSize)len);
if(read != (int32_t)len) {
errorThrow("Unexpected end of PSP save file");
}
errorOk();
}
errorret_t saveStreamWriteBytesPSP(
savestreampsp_t *p, const void *buf, const size_t len
) {
int32_t written = sceIoWrite(p->fd, buf, (SceSize)len);
if(written != (int32_t)len) {
errorThrow("Failed to write PSP save data");
}
errorOk();
}
errorret_t saveStreamSeekPSP(savestreampsp_t *p, const size_t pos) {
if(sceIoLseek(p->fd, (SceOff)pos, PSP_SEEK_SET) < 0) {
errorThrow("Failed to seek in PSP save file");
}
errorOk();
}
+77
View File
@@ -0,0 +1,77 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "error/error.h"
#include <pspiofilemgr.h>
#include <stddef.h>
typedef struct {
SceUID fd;
} savestreampsp_t;
/**
* Opens a PSP save data file for reading.
*
* @param p Stream to initialize.
* @param found Set to true if the file exists, false if it does not.
* @param slot Save slot index.
* @return An error if the open fails for a reason other than missing file.
*/
errorret_t saveStreamOpenReadPSP(
savestreampsp_t *p, bool_t *found, const uint8_t slot
);
/**
* Opens a PSP save data file for writing, creating or truncating it.
* Creates the save data directory if it does not already exist.
*
* @param p Stream to initialize.
* @param slot Save slot index.
* @return An error if the file cannot be opened for writing.
*/
errorret_t saveStreamOpenWritePSP(savestreampsp_t *p, const uint8_t slot);
/**
* Closes the file descriptor held by the stream.
*
* @param p Stream to close.
*/
void saveStreamClosePSP(savestreampsp_t *p);
/**
* Reads len bytes from the stream into buf.
*
* @param p Active stream.
* @param buf Destination buffer.
* @param len Number of bytes to read.
* @return An error if fewer than len bytes are available.
*/
errorret_t saveStreamReadBytesPSP(
savestreampsp_t *p, void *buf, const size_t len
);
/**
* Writes len bytes from buf into the stream.
*
* @param p Active stream.
* @param buf Source buffer.
* @param len Number of bytes to write.
* @return An error if the write fails.
*/
errorret_t saveStreamWriteBytesPSP(
savestreampsp_t *p, const void *buf, const size_t len
);
/**
* Seeks to an absolute byte position within the stream.
*
* @param p Active stream.
* @param pos Target byte offset from the start of the file.
* @return An error if the seek fails.
*/
errorret_t saveStreamSeekPSP(savestreampsp_t *p, const size_t pos);