From 174b16199e6d893b3baefac7f8ecc243d5f032a8 Mon Sep 17 00:00:00 2001 From: kayomn Date: Wed, 19 Oct 2022 11:21:54 +0100 Subject: [PATCH] Split archive FS logic into its own file --- src/engine/sys.zig | 38 +++++++------------------------------- src/engine/sys/Archive.zig | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 31 deletions(-) create mode 100644 src/engine/sys/Archive.zig diff --git a/src/engine/sys.zig b/src/engine/sys.zig index 427ac7c..63a01c5 100644 --- a/src/engine/sys.zig +++ b/src/engine/sys.zig @@ -1,3 +1,5 @@ +const Archive = @import("./sys/Archive.zig"); + const ext = @cImport({ @cInclude("SDL2/SDL.h"); }); @@ -148,7 +150,7 @@ pub const AppContext = opaque { .user_path_prefix = user_path_prefix, .data_file_system = .{.archive = .{ - .index_cache = try FileSystem.ArchiveIndexCache.init(allocator), + .index_cache = try Archive.IndexCache.init(allocator), .file_access = data_archive_file_access, }}, @@ -269,33 +271,7 @@ pub const AppContext = opaque { /// pub const FileSystem = union(enum) { native: []const u8, - - archive: struct { - file_access: ona.io.FileAccess, - index_cache: ArchiveIndexCache, - - entry_table: [max_open_entries]ArchiveEntry = - std.mem.zeroes([max_open_entries]ArchiveEntry), - - const max_open_entries = 16; - }, - - /// - /// - /// - const ArchiveEntry = struct { - owner: ?*ona.io.FileAccess, - cursor: u64, - header: oar.Entry, - }; - - /// - /// - /// - const ArchiveIndexCache = ona.table.Hashed(oar.Path, u64, .{ - .equals = oar.Path.equals, - .hash = oar.Path.hash, - }); + archive: Archive, /// /// Platform-agnostic mechanism for referencing files and directories on a [FileSystem]. @@ -379,9 +355,9 @@ pub const FileSystem = union(enum) { for (archive.entry_table) |*entry| if (entry.owner == null) { const Implementation = struct { - fn archiveEntryCast(context: *anyopaque) *ArchiveEntry { - return @ptrCast(*ArchiveEntry, @alignCast( - @alignOf(ArchiveEntry), context)); + fn archiveEntryCast(context: *anyopaque) *Archive.Entry { + return @ptrCast(*Archive.Entry, @alignCast( + @alignOf(Archive.Entry), context)); } fn close(context: *anyopaque) void { diff --git a/src/engine/sys/Archive.zig b/src/engine/sys/Archive.zig new file mode 100644 index 0000000..500ff4a --- /dev/null +++ b/src/engine/sys/Archive.zig @@ -0,0 +1,29 @@ +const oar = @import("oar"); +const ona = @import("ona"); +const std = @import("std"); + +file_access: ona.io.FileAccess, +index_cache: IndexCache, +entry_table: [max_open_entries]Entry = std.mem.zeroes([max_open_entries]Entry), + +/// +/// Hard limit on the maximum number of entries open at once. +/// +const max_open_entries = 16; + +/// +/// Stateful extension of an [oar.Entry]. +/// +pub const Entry = struct { + owner: ?*ona.io.FileAccess, + cursor: u64, + header: oar.Entry, +}; + +/// +/// Table cache for associating [oar.Path] values with offsets to entries in a given file. +/// +pub const IndexCache = ona.table.Hashed(oar.Path, u64, .{ + .equals = oar.Path.equals, + .hash = oar.Path.hash, +});