Save file update (incomplete)
This commit is contained in:
@@ -14,5 +14,6 @@ add_subdirectory(asset)
|
||||
add_subdirectory(log)
|
||||
add_subdirectory(input)
|
||||
add_subdirectory(network)
|
||||
add_subdirectory(save)
|
||||
add_subdirectory(system)
|
||||
add_subdirectory(time)
|
||||
@@ -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
|
||||
savelinux.c
|
||||
savestreamlinux.c
|
||||
)
|
||||
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "save/save.h"
|
||||
#include "util/string.h"
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
|
||||
errorret_t saveInitLinux(void) {
|
||||
stringCopy(SAVE.platform.savePath, SAVE_LINUX_PATH, SAVE_LINUX_PATH_MAX);
|
||||
|
||||
if(mkdir(SAVE.platform.savePath, 0755) != 0 && errno != EEXIST) {
|
||||
errorThrow("Failed to create save directory: %s", SAVE.platform.savePath);
|
||||
}
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t saveDisposeLinux(void) {
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t saveLoadLinux(const uint8_t slot, savefile_t *file) {
|
||||
char_t path[SAVE_LINUX_PATH_MAX];
|
||||
snprintf(path, SAVE_LINUX_PATH_MAX, SAVE_LINUX_FILE_FORMAT,
|
||||
SAVE.platform.savePath, (uint32_t)slot
|
||||
);
|
||||
|
||||
FILE *f = fopen(path, "rb");
|
||||
if(!f) {
|
||||
file->exists = false;
|
||||
errorOk();
|
||||
}
|
||||
|
||||
size_t read = fread(file, sizeof(savefile_t), 1, f);
|
||||
fclose(f);
|
||||
|
||||
if(read != 1) {
|
||||
file->exists = false;
|
||||
errorThrow("Failed to read save data for slot %u", (uint32_t)slot);
|
||||
}
|
||||
|
||||
file->exists = true;
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t saveWriteLinux(const uint8_t slot, const savefile_t *file) {
|
||||
char_t path[SAVE_LINUX_PATH_MAX];
|
||||
snprintf(path, SAVE_LINUX_PATH_MAX, SAVE_LINUX_FILE_FORMAT,
|
||||
SAVE.platform.savePath, (uint32_t)slot
|
||||
);
|
||||
|
||||
FILE *f = fopen(path, "wb");
|
||||
if(!f) {
|
||||
errorThrow("Failed to open save file for writing: slot %u", (uint32_t)slot);
|
||||
}
|
||||
|
||||
size_t written = fwrite(file, sizeof(savefile_t), 1, f);
|
||||
fclose(f);
|
||||
|
||||
if(written != 1) {
|
||||
errorThrow("Failed to write save data for slot %u", (uint32_t)slot);
|
||||
}
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t saveDeleteLinux(const uint8_t slot) {
|
||||
char_t path[SAVE_LINUX_PATH_MAX];
|
||||
snprintf(path, SAVE_LINUX_PATH_MAX, SAVE_LINUX_FILE_FORMAT,
|
||||
SAVE.platform.savePath, (uint32_t)slot
|
||||
);
|
||||
|
||||
if(remove(path) != 0 && errno != ENOENT) {
|
||||
errorThrow("Failed to delete save file for slot %u", (uint32_t)slot);
|
||||
}
|
||||
|
||||
errorOk();
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* 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"
|
||||
|
||||
#define SAVE_LINUX_PATH_MAX FILENAME_MAX
|
||||
#define SAVE_LINUX_FILE_FORMAT "%s/save_%u.dat"
|
||||
|
||||
#ifndef SAVE_LINUX_PATH
|
||||
#define SAVE_LINUX_PATH "./saves"
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
char_t savePath[SAVE_LINUX_PATH_MAX];
|
||||
} savelinux_t;
|
||||
|
||||
/**
|
||||
* Initializes the save system on Linux.
|
||||
*
|
||||
* @return An error code if initialization fails.
|
||||
*/
|
||||
errorret_t saveInitLinux(void);
|
||||
|
||||
/**
|
||||
* Disposes of the save system on Linux.
|
||||
*
|
||||
* @return An error code if disposal fails.
|
||||
*/
|
||||
errorret_t saveDisposeLinux(void);
|
||||
|
||||
/**
|
||||
* Loads a save file from disk 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 saveLoadLinux(const uint8_t slot, savefile_t *file);
|
||||
|
||||
/**
|
||||
* Writes a save file to disk 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 saveWriteLinux(const uint8_t slot, const savefile_t *file);
|
||||
|
||||
/**
|
||||
* Deletes the save file for the given slot from disk.
|
||||
*
|
||||
* @param slot The save slot index.
|
||||
* @return An error code if the delete fails.
|
||||
*/
|
||||
errorret_t saveDeleteLinux(const uint8_t slot);
|
||||
@@ -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/savelinux.h"
|
||||
#include "save/savestreamlinux.h"
|
||||
|
||||
typedef savelinux_t saveplatform_t;
|
||||
typedef savestreamlinux_t saveplatformstream_t;
|
||||
|
||||
#define saveInitPlatform saveInitLinux
|
||||
#define saveDisposePlatform saveDisposeLinux
|
||||
#define saveDeletePlatform saveDeleteLinux
|
||||
|
||||
#define saveStreamOpenReadPlatform(stream, slot) \
|
||||
saveStreamOpenReadLinux(&(stream)->platform, &(stream)->found, slot)
|
||||
#define saveStreamOpenWritePlatform(stream, slot) \
|
||||
saveStreamOpenWriteLinux(&(stream)->platform, slot)
|
||||
#define saveStreamClosePlatform(stream) \
|
||||
saveStreamCloseLinux(&(stream)->platform)
|
||||
#define saveStreamReadBytesPlatform(stream, buf, len) \
|
||||
saveStreamReadBytesLinux(&(stream)->platform, buf, len)
|
||||
#define saveStreamWriteBytesPlatform(stream, buf, len) \
|
||||
saveStreamWriteBytesLinux(&(stream)->platform, buf, len)
|
||||
#define saveStreamSeekPlatform(stream, pos) \
|
||||
saveStreamSeekLinux(&(stream)->platform, pos)
|
||||
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "save/save.h"
|
||||
#include "save/savestreamlinux.h"
|
||||
#include "util/string.h"
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
|
||||
static void _saveStreamGetPath(
|
||||
char_t *out, const size_t max, const uint8_t slot
|
||||
) {
|
||||
snprintf(
|
||||
out, max, SAVE_LINUX_FILE_FORMAT,
|
||||
SAVE.platform.savePath, (uint32_t)slot
|
||||
);
|
||||
}
|
||||
|
||||
errorret_t saveStreamOpenReadLinux(
|
||||
savestreamlinux_t *p, bool_t *found, const uint8_t slot
|
||||
) {
|
||||
char_t path[SAVE_LINUX_PATH_MAX];
|
||||
_saveStreamGetPath(path, SAVE_LINUX_PATH_MAX, slot);
|
||||
|
||||
p->file = fopen(path, "rb");
|
||||
*found = (p->file != NULL);
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t saveStreamOpenWriteLinux(savestreamlinux_t *p, const uint8_t slot) {
|
||||
char_t path[SAVE_LINUX_PATH_MAX];
|
||||
_saveStreamGetPath(path, SAVE_LINUX_PATH_MAX, slot);
|
||||
|
||||
p->file = fopen(path, "wb");
|
||||
if(!p->file) {
|
||||
errorThrow("Failed to open save file for writing: slot %u", (uint32_t)slot);
|
||||
}
|
||||
errorOk();
|
||||
}
|
||||
|
||||
void saveStreamCloseLinux(savestreamlinux_t *p) {
|
||||
if(p->file) {
|
||||
fclose(p->file);
|
||||
p->file = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
errorret_t saveStreamReadBytesLinux(
|
||||
savestreamlinux_t *p, void *buf, const size_t len
|
||||
) {
|
||||
if(fread(buf, 1, len, p->file) != len) {
|
||||
errorThrow("Unexpected end of save file");
|
||||
}
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t saveStreamWriteBytesLinux(
|
||||
savestreamlinux_t *p, const void *buf, const size_t len
|
||||
) {
|
||||
if(fwrite(buf, 1, len, p->file) != len) {
|
||||
errorThrow("Failed to write save data");
|
||||
}
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t saveStreamSeekLinux(savestreamlinux_t *p, const size_t pos) {
|
||||
if(fseek(p->file, (long)pos, SEEK_SET) != 0) {
|
||||
errorThrow("Failed to seek in save file");
|
||||
}
|
||||
errorOk();
|
||||
}
|
||||
@@ -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 <stdio.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct {
|
||||
FILE *file;
|
||||
} savestreamlinux_t;
|
||||
|
||||
/**
|
||||
* Opens a save slot 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 saveStreamOpenReadLinux(
|
||||
savestreamlinux_t *p, bool_t *found, const uint8_t slot
|
||||
);
|
||||
|
||||
/**
|
||||
* Opens a save slot file for writing, creating or truncating it.
|
||||
*
|
||||
* @param p Stream to initialize.
|
||||
* @param slot Save slot index.
|
||||
* @return An error if the file cannot be opened for writing.
|
||||
*/
|
||||
errorret_t saveStreamOpenWriteLinux(
|
||||
savestreamlinux_t *p, const uint8_t slot
|
||||
);
|
||||
|
||||
/**
|
||||
* Closes the file handle held by the stream.
|
||||
*
|
||||
* @param p Stream to close.
|
||||
*/
|
||||
void saveStreamCloseLinux(savestreamlinux_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 saveStreamReadBytesLinux(
|
||||
savestreamlinux_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 saveStreamWriteBytesLinux(
|
||||
savestreamlinux_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 saveStreamSeekLinux(savestreamlinux_t *p, const size_t pos);
|
||||
Reference in New Issue
Block a user