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
@@ -18,3 +18,4 @@ target_sources(${DUSK_BINARY_TARGET_NAME}
add_subdirectory(asset)
add_subdirectory(input)
add_subdirectory(log)
add_subdirectory(save)
+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
savevita.c
savestreamvita.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/savevita.h"
#include "save/savestreamvita.h"
typedef savevita_t saveplatform_t;
typedef savestreamvita_t saveplatformstream_t;
#define saveInitPlatform saveInitVita
#define saveDisposePlatform saveDisposeVita
#define saveDeletePlatform saveDeleteVita
#define saveStreamOpenReadPlatform(stream, slot) \
saveStreamOpenReadVita(&(stream)->platform, &(stream)->found, slot)
#define saveStreamOpenWritePlatform(stream, slot) \
saveStreamOpenWriteVita(&(stream)->platform, slot)
#define saveStreamClosePlatform(stream) \
saveStreamCloseVita(&(stream)->platform)
#define saveStreamReadBytesPlatform(stream, buf, len) \
saveStreamReadBytesVita(&(stream)->platform, buf, len)
#define saveStreamWriteBytesPlatform(stream, buf, len) \
saveStreamWriteBytesVita(&(stream)->platform, buf, len)
#define saveStreamSeekPlatform(stream, pos) \
saveStreamSeekVita(&(stream)->platform, pos)
+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/savestreamvita.h"
errorret_t saveStreamOpenReadVita(
savestreamvita_t *p, bool_t *found, const uint8_t slot
) {
char_t path[SAVE_VITA_PATH_MAX];
snprintf(path, SAVE_VITA_PATH_MAX, SAVE_VITA_FILE_FORMAT,
SAVE_VITA_TITLE_ID, (uint32_t)slot
);
p->fd = sceIoOpen(path, SCE_O_RDONLY, 0);
*found = (p->fd >= 0);
errorOk();
}
errorret_t saveStreamOpenWriteVita(savestreamvita_t *p, const uint8_t slot) {
char_t dir[SAVE_VITA_PATH_MAX];
snprintf(dir, SAVE_VITA_PATH_MAX, SAVE_VITA_DIR_FORMAT,
SAVE_VITA_TITLE_ID, (uint32_t)slot
);
sceIoMkdir(dir, 0777);
char_t path[SAVE_VITA_PATH_MAX];
snprintf(path, SAVE_VITA_PATH_MAX, SAVE_VITA_FILE_FORMAT,
SAVE_VITA_TITLE_ID, (uint32_t)slot
);
p->fd = sceIoOpen(path, SCE_O_WRONLY | SCE_O_CREAT | SCE_O_TRUNC, 0777);
if(p->fd < 0) {
errorThrow(
"Failed to open Vita save file for writing: slot %u", (uint32_t)slot
);
}
errorOk();
}
void saveStreamCloseVita(savestreamvita_t *p) {
if(p->fd >= 0) {
sceIoClose(p->fd);
p->fd = -1;
}
}
errorret_t saveStreamReadBytesVita(
savestreamvita_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 Vita save file");
}
errorOk();
}
errorret_t saveStreamWriteBytesVita(
savestreamvita_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 Vita save data");
}
errorOk();
}
errorret_t saveStreamSeekVita(savestreamvita_t *p, const size_t pos) {
if(sceIoLseek(p->fd, (SceOff)pos, SCE_SEEK_SET) < 0) {
errorThrow("Failed to seek in Vita save file");
}
errorOk();
}
+78
View File
@@ -0,0 +1,78 @@
/**
* 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 <psp2/io/fcntl.h>
#include <psp2/io/stat.h>
#include <stddef.h>
typedef struct {
SceUID fd;
} savestreamvita_t;
/**
* Opens a Vita 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 saveStreamOpenReadVita(
savestreamvita_t *p, bool_t *found, const uint8_t slot
);
/**
* Opens a Vita 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 saveStreamOpenWriteVita(savestreamvita_t *p, const uint8_t slot);
/**
* Closes the file descriptor held by the stream.
*
* @param p Stream to close.
*/
void saveStreamCloseVita(savestreamvita_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 saveStreamReadBytesVita(
savestreamvita_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 saveStreamWriteBytesVita(
savestreamvita_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 saveStreamSeekVita(savestreamvita_t *p, const size_t pos);