Rework file system API to support different backends
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
979c2a73f3
commit
1289e9634b
61
src/sys.zig
61
src/sys.zig
|
@ -70,8 +70,6 @@ pub const EventLoop = opaque {
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
const InitError = error {
|
const InitError = error {
|
||||||
DataFileNotFound,
|
|
||||||
DataFileInvalid,
|
|
||||||
OutOfSemaphores,
|
OutOfSemaphores,
|
||||||
OutOfMutexes,
|
OutOfMutexes,
|
||||||
OutOfMemory,
|
OutOfMemory,
|
||||||
|
@ -118,7 +116,6 @@ pub const EventLoop = opaque {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ext.SDL_free(implementation.user_prefix.ptr);
|
|
||||||
ext.SDL_DestroyMutex(implementation.file_system_mutex);
|
ext.SDL_DestroyMutex(implementation.file_system_mutex);
|
||||||
ext.SDL_DestroySemaphore(implementation.file_system_semaphore);
|
ext.SDL_DestroySemaphore(implementation.file_system_semaphore);
|
||||||
}
|
}
|
||||||
|
@ -148,19 +145,11 @@ pub const EventLoop = opaque {
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
fn init() InitError!Implementation {
|
fn init() InitError!Implementation {
|
||||||
const data_file_access = @ptrCast(*FileAccess,
|
|
||||||
ext.SDL_RWFromFile("./data.tar", "r+") orelse return error.DataFileNotFound);
|
|
||||||
|
|
||||||
return Implementation{
|
return Implementation{
|
||||||
.data_archive = tar.Archive.init(data_file_access) catch |err| switch (err) {
|
|
||||||
error.Invalid, error.Inaccessible => return error.DataFileInvalid,
|
|
||||||
},
|
|
||||||
|
|
||||||
.file_system_semaphore = ext.SDL_CreateSemaphore(0)
|
.file_system_semaphore = ext.SDL_CreateSemaphore(0)
|
||||||
orelse return error.OutOfSemaphores,
|
orelse return error.OutOfSemaphores,
|
||||||
|
|
||||||
.file_system_mutex = ext.SDL_CreateMutex() orelse return error.OutOfMutexes,
|
.file_system_mutex = ext.SDL_CreateMutex() orelse return error.OutOfMutexes,
|
||||||
.data_file = data_file_access,
|
|
||||||
.file_system_thread = null,
|
.file_system_thread = null,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -177,14 +166,16 @@ pub const EventLoop = opaque {
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
while (implementation.file_system_messages) |messages| {
|
while (implementation.file_system_messages) |messages| {
|
||||||
|
const root = @import("./sys.zig");
|
||||||
|
|
||||||
switch (messages.request) {
|
switch (messages.request) {
|
||||||
.exit => return 0,
|
.exit => return 0,
|
||||||
.log => |*log_request| .log(log_request.kind, log_request.message),
|
.log => |*log_request| root.log(log_request.kind, log_request.message),
|
||||||
|
|
||||||
.open => |*open_request| open_request.result =
|
.open => |*open_request| open_request.result =
|
||||||
.open(open_request.mode, open_request.file_system_path),
|
root.open(open_request.mode, open_request.file_system_path.*),
|
||||||
|
|
||||||
.close => |*close_request| .close(close_request.file_access),
|
.close => |*close_request| root.close(close_request.file_access),
|
||||||
|
|
||||||
.read_file => |read_request| {
|
.read_file => |read_request| {
|
||||||
// TODO: Implement.
|
// TODO: Implement.
|
||||||
|
@ -444,13 +435,17 @@ pub const FileSystem = struct {
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
pub const Root = struct {
|
pub const Root = union(enum) {
|
||||||
|
native: struct {
|
||||||
prefix: []const u8,
|
prefix: []const u8,
|
||||||
|
},
|
||||||
|
|
||||||
|
tar: struct {},
|
||||||
|
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
pub fn joinedPath(root: Root, sequences: []const []const u8) PathError!Path {
|
pub fn joinedPath(root: *const Root, sequences: []const []const u8) PathError!Path {
|
||||||
var path = Path{
|
var path = Path{
|
||||||
.root = root,
|
.root = root,
|
||||||
.buffer = std.mem.zeroes([Path.max]u8),
|
.buffer = std.mem.zeroes([Path.max]u8),
|
||||||
|
@ -609,8 +604,8 @@ pub const SeekOrigin = enum {
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
pub fn close(file_access: *FileAccess) void {
|
pub fn close(file_access: *FileAccess) void {
|
||||||
if (!ext.SDL_RWclose(@ptrCast(*ext.SDL_RWops,
|
if (ext.SDL_RWclose(@ptrCast(*ext.SDL_RWops,
|
||||||
@alignCast(@alignOf(ext.SDL_RWops), file_access)))) {
|
@alignCast(@alignOf(ext.SDL_RWops), file_access))) != 0) {
|
||||||
|
|
||||||
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION,
|
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION,
|
||||||
"Failed to close file - may have been already closed");
|
"Failed to close file - may have been already closed");
|
||||||
|
@ -629,21 +624,21 @@ pub fn log(kind: LogKind, message: []const u8) void {
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
pub fn open(mode: OpenMode, file_system_path: FileSystem.Path) OpenError!*FileAccess {
|
pub fn open(mode: OpenMode, file_system_path: FileSystem.Path) OpenError!*FileAccess {
|
||||||
switch (file_system_path.file_system) {
|
switch (file_system_path.root.*) {
|
||||||
.data => {
|
.tar => {
|
||||||
// TODO: Implement
|
// TODO: Implement.
|
||||||
return error.NotFound;
|
return error.NotFound;
|
||||||
},
|
},
|
||||||
|
|
||||||
.user => {
|
.native => |native| {
|
||||||
var path_buffer = std.mem.zeroes([4096]u8);
|
var path_buffer = std.mem.zeroes([4096]u8);
|
||||||
var path = stack.Fixed(u8){.buffer = path_buffer[0 .. ]};
|
var path = stack.Fixed(u8){.buffer = path_buffer[0 .. ]};
|
||||||
|
|
||||||
path.pushAll("/home/kayomn/.local/share") catch return error.NotFound;
|
path.pushAll(native.prefix) catch return error.NotFound;
|
||||||
|
|
||||||
if (file_system_path.write(path.writer())) return error.NotFound;
|
if (file_system_path.write(path.writer())) return error.NotFound;
|
||||||
|
|
||||||
return @ptrCast(*FileAccess, ext.SDL_RWFromFile(&path_buffer, switch (mode) {
|
return @ptrCast(?*FileAccess, ext.SDL_RWFromFile(&path_buffer, switch (mode) {
|
||||||
.readonly => "rb",
|
.readonly => "rb",
|
||||||
.overwrite => "wb",
|
.overwrite => "wb",
|
||||||
.append => "ab",
|
.append => "ab",
|
||||||
|
@ -689,25 +684,20 @@ pub fn runGraphics(comptime Errors: anytype, comptime run: GraphicsRunner(Errors
|
||||||
|
|
||||||
defer ext.SDL_DestroyRenderer(renderer);
|
defer ext.SDL_DestroyRenderer(renderer);
|
||||||
|
|
||||||
var file_system = FileSystem{
|
const user_prefix = ext.SDL_GetPrefPath("ona", "ona") orelse {
|
||||||
.user = .{.prefix = create_pref_path: {
|
|
||||||
const path = ext.SDL_GetPrefPath("ona", "ona") orelse {
|
|
||||||
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION,
|
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION,
|
||||||
"Failed to load user path");
|
"Failed to load user path");
|
||||||
|
|
||||||
return error.InitFailure;
|
return error.InitFailure;
|
||||||
};
|
};
|
||||||
|
|
||||||
break: create_pref_path path[0 .. std.mem.len(path)];
|
defer ext.SDL_free(user_prefix);
|
||||||
}},
|
|
||||||
|
|
||||||
.data = .{.prefix = "./"},
|
var file_system = FileSystem{
|
||||||
|
.user = .{.native = .{.prefix = user_prefix[0 .. std.mem.len(user_prefix)]}},
|
||||||
|
.data = .{.tar = .{}},
|
||||||
};
|
};
|
||||||
|
|
||||||
defer {
|
|
||||||
ext.SDL_free(file_system.user.prefix);
|
|
||||||
}
|
|
||||||
|
|
||||||
var event_loop = EventLoop.Implementation.init() catch |err| {
|
var event_loop = EventLoop.Implementation.init() catch |err| {
|
||||||
switch (err) {
|
switch (err) {
|
||||||
error.OutOfMemory => ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION,
|
error.OutOfMemory => ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION,
|
||||||
|
@ -743,5 +733,6 @@ pub fn runGraphics(comptime Errors: anytype, comptime run: GraphicsRunner(Errors
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
return run(@ptrCast(*EventLoop, &event_loop), @ptrCast(*GraphicsContext, &graphics_context));
|
return run(@ptrCast(*EventLoop, &event_loop), &file_system,
|
||||||
|
@ptrCast(*GraphicsContext, &graphics_context));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue