Split archive FS logic into its own file

This commit is contained in:
kayomn 2022-10-19 11:21:54 +01:00
parent 6769ea92af
commit 174b16199e
2 changed files with 36 additions and 31 deletions

View File

@ -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 {

View File

@ -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,
});