First pass of save

This commit is contained in:
2026-08-14 14:19:44 -05:00
parent 33f50a2c69
commit 45331c2a60
23 changed files with 577 additions and 510 deletions
+3 -1
View File
@@ -16,5 +16,7 @@ add_subdirectory(input)
if(DUSK_NETWORK)
add_subdirectory(network)
endif()
add_subdirectory(save)
add_subdirectory(system)
add_subdirectory(time)
add_subdirectory(time)
add_subdirectory(util)
+9
View File
@@ -0,0 +1,9 @@
# Copyright (c) 2026 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
savedevicelinux.c
)
+51
View File
@@ -0,0 +1,51 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "save/savedevice.h"
#include "save/savedevicelinux.h"
#include "assert/assert.h"
#include "util/mkdirp.h"
errorret_t saveDeviceLinuxInit(savedevice_t *device) {
assertNotNull(device, "device cannot be null");
device->state = SAVE_DEVICE_STATE_UNKNOWN;
errorOk();
}
errorret_t saveDeviceLinuxUpdate(savedevice_t *device) {
assertNotNull(device, "device cannot be null");
assertIsMainThread("Invalid thread");
errorOk();
}
void saveDeviceLinuxCheckAvailability(savedevice_t *device) {
assertNotNull(device, "device cannot be null");
assertIsMainThread("Invalid thread");
assertTrue(
device->state == SAVE_DEVICE_STATE_CHECKING_AVAILABILITY,
"Device state incorrect?"
);
// Mkdirp the save directory.
int mkdirpResult = mkdirp(SAVE_DEVICE_LINUX_DIRECTORY, 0700);
if(mkdirpResult != 0) {
device->state = SAVE_DEVICE_STATE_UNAVAILABLE;
device->reasonKey = "save.linux.mkdirp_failed";
return saveDeviceFireCallback(device);
}
// The save device is available.
device->state = SAVE_DEVICE_STATE_AVAILABLE;
device->reasonKey = NULL;
saveDeviceFireCallback(device);
}
errorret_t saveDeviceLinuxDispose(savedevice_t *device) {
errorOk();
}
+50
View File
@@ -0,0 +1,50 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "error/error.h"
#define SAVE_DEVICE_LINUX_DIRECTORY "~/.dusk/saves"
#define SAVE_DEVICE_LINUX_FILE "~/.dusk/saves/save.dat"
typedef struct {
void *nothing;
} savedeviceplatform_t;
typedef struct savedevice_s savedevice_t;
/**
* Initializes the save device platform.
*
* @param device The save device platform to initialize.
* @return Error state if any.
*/
errorret_t saveDeviceLinuxInit(savedevice_t *device);
/**
* Updates the save device platform.
*
* @param device The save device platform to update.
* @return Error state if any.
*/
errorret_t saveDeviceLinuxUpdate(savedevice_t *device);
/**
* Requests the device to check its availability, this will call the callback
* whence completed.
*
* @param device The save device platform to check availability.
*/
void saveDeviceLinuxCheckAvailability(savedevice_t *device);
/**
* Disposes of the save device platform.
*
* @param device The save device platform to dispose.
* @return Error state if any.
*/
errorret_t saveDeviceLinuxDispose(savedevice_t *device);
+16
View File
@@ -0,0 +1,16 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "savedevicelinux.h"
#define SAVE_DEVICE_COUNT 1
#define saveDevicePlatformInit saveDeviceLinuxInit
#define saveDevicePlatformUpdate saveDeviceLinuxUpdate
#define saveDeviceCheckAvailabilityPlatform saveDeviceLinuxCheckAvailability
#define saveDevicePlatformDispose saveDeviceLinuxDispose
+9
View File
@@ -0,0 +1,9 @@
# Copyright (c) 2026 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
mkdirp.c
)
+38
View File
@@ -0,0 +1,38 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "util/mkdirp.h"
#include "util/string.h"
#include "assert/assert.h"
#include <sys/stat.h>
#include <limits.h>
int_t mkdirp(const char_t *path, const int_t mode) {
assertNotNull(path, "path cannot be null");
assertStrLenMax(path, PATH_MAX, "path is too long");
char_t buffer[PATH_MAX];
stringCopy(buffer, path, sizeof(buffer) - 1);
size_t len = strlen(buffer);
if(len == 0) return -1;
// Strip a trailing slash so the loop below doesn't try to create the same
// directory twice.
if(buffer[len - 1] == '/') buffer[len - 1] = '\0';
for(char_t *p = buffer + 1; *p != '\0'; p++) {
if(*p != '/') continue;
*p = '\0';
if(mkdir(buffer, mode) != 0 && errno != EEXIST) return -1;
*p = '/';
}
if(mkdir(buffer, mode) != 0 && errno != EEXIST) return -1;
return 0;
}
+20
View File
@@ -0,0 +1,20 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
/**
* Recursively creates a directory and any missing parent directories, similar
* to the shell command `mkdir -p`. If the directory already exists, this is
* considered a success.
*
* @param path The path of the directory to create.
* @param mode The permissions to create the directories with.
* @return 0 on success, -1 on failure (errno will be set).
*/
int_t mkdirp(const char_t *path, const int_t mode);