From 1289e9634b1dc8d913d1fc81ab68c9d190ad85d4 Mon Sep 17 00:00:00 2001 From: kayomn Date: Thu, 6 Oct 2022 23:22:07 +0100 Subject: [PATCH] Rework file system API to support different backends --- src/sys.zig | 73 +++++++++++++++++++++++------------------------------ 1 file changed, 32 insertions(+), 41 deletions(-) diff --git a/src/sys.zig b/src/sys.zig index 973a81c..06cd249 100644 --- a/src/sys.zig +++ b/src/sys.zig @@ -70,8 +70,6 @@ pub const EventLoop = opaque { /// /// const InitError = error { - DataFileNotFound, - DataFileInvalid, OutOfSemaphores, OutOfMutexes, 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_DestroySemaphore(implementation.file_system_semaphore); } @@ -148,19 +145,11 @@ pub const EventLoop = opaque { /// /// fn init() InitError!Implementation { - const data_file_access = @ptrCast(*FileAccess, - ext.SDL_RWFromFile("./data.tar", "r+") orelse return error.DataFileNotFound); - 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) orelse return error.OutOfSemaphores, .file_system_mutex = ext.SDL_CreateMutex() orelse return error.OutOfMutexes, - .data_file = data_file_access, .file_system_thread = null, }; } @@ -177,14 +166,16 @@ pub const EventLoop = opaque { while (true) { while (implementation.file_system_messages) |messages| { + const root = @import("./sys.zig"); + switch (messages.request) { .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.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| { // TODO: Implement. @@ -444,13 +435,17 @@ pub const FileSystem = struct { /// /// /// - pub const Root = struct { - prefix: []const u8, + pub const Root = union(enum) { + native: struct { + 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{ .root = root, .buffer = std.mem.zeroes([Path.max]u8), @@ -609,8 +604,8 @@ pub const SeekOrigin = enum { /// /// pub fn close(file_access: *FileAccess) void { - if (!ext.SDL_RWclose(@ptrCast(*ext.SDL_RWops, - @alignCast(@alignOf(ext.SDL_RWops), file_access)))) { + if (ext.SDL_RWclose(@ptrCast(*ext.SDL_RWops, + @alignCast(@alignOf(ext.SDL_RWops), file_access))) != 0) { ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "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 { - switch (file_system_path.file_system) { - .data => { - // TODO: Implement + switch (file_system_path.root.*) { + .tar => { + // TODO: Implement. return error.NotFound; }, - .user => { + .native => |native| { var path_buffer = std.mem.zeroes([4096]u8); 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; - return @ptrCast(*FileAccess, ext.SDL_RWFromFile(&path_buffer, switch (mode) { + return @ptrCast(?*FileAccess, ext.SDL_RWFromFile(&path_buffer, switch (mode) { .readonly => "rb", .overwrite => "wb", .append => "ab", @@ -689,24 +684,19 @@ pub fn runGraphics(comptime Errors: anytype, comptime run: GraphicsRunner(Errors defer ext.SDL_DestroyRenderer(renderer); - var file_system = FileSystem{ - .user = .{.prefix = create_pref_path: { - const path = ext.SDL_GetPrefPath("ona", "ona") orelse { - ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, - "Failed to load user path"); + const user_prefix = ext.SDL_GetPrefPath("ona", "ona") orelse { + ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, + "Failed to load user path"); - return error.InitFailure; - }; - - break: create_pref_path path[0 .. std.mem.len(path)]; - }}, - - .data = .{.prefix = "./"}, + return error.InitFailure; }; - defer { - ext.SDL_free(file_system.user.prefix); - } + defer ext.SDL_free(user_prefix); + + var file_system = FileSystem{ + .user = .{.native = .{.prefix = user_prefix[0 .. std.mem.len(user_prefix)]}}, + .data = .{.tar = .{}}, + }; var event_loop = EventLoop.Implementation.init() catch |err| { switch (err) { @@ -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)); }