mach: remove ResourceManager for now

needs much more thought

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2023-06-30 20:40:29 -07:00
parent 923cce1fc3
commit 0e1b79969f
3 changed files with 0 additions and 150 deletions

View file

@ -15,7 +15,6 @@ pub const gfx2d = struct {
pub const Sprite2D = @import("gfx2d/Sprite2D.zig"); pub const Sprite2D = @import("gfx2d/Sprite2D.zig");
}; };
pub const math = @import("math.zig"); pub const math = @import("math.zig");
pub const ResourceManager = @import("resource/ResourceManager.zig");
// Engine exports // Engine exports
pub const App = @import("engine.zig").App; pub const App = @import("engine.zig").App;
@ -24,7 +23,6 @@ pub const Module = @import("engine.zig").Module;
const std = @import("std"); const std = @import("std");
test { test {
std.testing.refAllDeclsRecursive(ResourceManager);
std.testing.refAllDeclsRecursive(gfx); std.testing.refAllDeclsRecursive(gfx);
_ = ecs; _ = ecs;
_ = earcut; _ = earcut;

View file

@ -1,102 +0,0 @@
const std = @import("std");
const uri_parser = @import("uri_parser.zig");
const ResourceManager = @This();
allocator: std.mem.Allocator,
paths: []const []const u8,
// TODO: Use comptime hash map for resource_types
resource_map: std.StringArrayHashMapUnmanaged(ResourceType) = .{},
resources: std.StringHashMapUnmanaged(Resource) = .{},
cwd: std.fs.Dir,
context: ?*anyopaque = null,
pub fn init(allocator: std.mem.Allocator, paths: []const []const u8, resource_types: []const ResourceType) !ResourceManager {
var cwd = try std.fs.openDirAbsolute(try std.fs.selfExeDirPathAlloc(allocator), .{});
errdefer cwd.close();
var resource_map: std.StringArrayHashMapUnmanaged(ResourceType) = .{};
for (resource_types) |res| {
try resource_map.put(allocator, res.name, res);
}
return ResourceManager{
.allocator = allocator,
.paths = paths,
.resource_map = resource_map,
.cwd = cwd,
};
}
pub const ResourceType = struct {
name: []const u8,
load: *const fn (context: ?*anyopaque, mem: []const u8) error{ InvalidResource, CorruptData }!*anyopaque,
unload: *const fn (context: ?*anyopaque, resource: *anyopaque) void,
};
pub fn setLoadContext(self: *ResourceManager, ctx: anytype) void {
var context = self.allocator.create(@TypeOf(ctx)) catch unreachable;
context.* = ctx;
self.context = context;
}
pub fn getResource(self: *ResourceManager, uri: []const u8) !Resource {
if (self.resources.get(uri)) |res|
return res;
var file: ?std.fs.File = null;
const uri_data = try uri_parser.parseUri(uri);
for (self.paths) |path| {
var dir = try self.cwd.openDir(path, .{});
defer dir.close();
file = dir.openFile(uri_data.path, .{}) catch |err| switch (err) {
error.FileNotFound => continue,
else => return err,
};
errdefer file.deinit();
}
if (file) |f| {
if (self.resource_map.get(uri_data.scheme)) |res_type| {
var data = try f.reader().readAllAlloc(self.allocator, std.math.maxInt(usize));
errdefer self.allocator.free(data);
const resource = try res_type.load(self.context, data);
errdefer res_type.unload(self.context, resource);
const res = Resource{
.uri = try self.allocator.dupe(u8, uri),
.resource = resource,
.size = data.len,
};
try self.resources.putNoClobber(self.allocator, uri, res);
return res;
}
return error.UnknownResourceType;
}
return error.ResourceNotFound;
}
pub fn unloadResource(self: *ResourceManager, res: Resource) void {
const uri_data = uri_parser.parseUri(res.uri) catch unreachable;
if (self.resource_map.get(uri_data.scheme)) |res_type| {
res_type.unload(self.context, res.resource);
}
_ = self.resources.remove(res.uri);
}
pub const Resource = struct {
uri: []const u8,
resource: *anyopaque,
size: u64,
// Returns the raw data, which you can use in any ways. Internally it is stored
// as an *anyopaque
pub fn getData(res: *const Resource, comptime T: type) *T {
return @as(*T, @ptrCast(@alignCast(std.meta.alignment(*T), res.resource)));
}
};

View file

@ -1,46 +0,0 @@
const std = @import("std");
const mem = std.mem;
pub const Uri = struct {
scheme: []const u8,
path: []const u8,
};
pub const Error = error{InvalidUri};
pub fn parseUri(uri: []const u8) Error!Uri {
const scheme_end = mem.indexOfScalar(u8, uri, ':');
if (scheme_end == null)
return error.InvalidUri;
const scheme = uri[0..scheme_end.?];
if (scheme_end.? + 3 >= uri.len)
return error.InvalidUri;
if (uri[scheme_end.? + 1] != '/' or uri[scheme_end.? + 2] != '/')
return error.InvalidUri;
const path = uri[scheme_end.? + 3 ..];
return Uri{
.scheme = scheme,
.path = path,
};
}
const testing = std.testing;
const expectError = testing.expectError;
const expectEqualStrings = testing.expectEqualStrings;
test "invalid" {
try expectError(error.InvalidUri, parseUri("xyz"));
try expectError(error.InvalidUri, parseUri("xyz:"));
try expectError(error.InvalidUri, parseUri("xyz:/"));
try expectError(error.InvalidUri, parseUri("xyz://"));
}
test "path" {
try expectEqualStrings("xyz", (try parseUri("xyz://abc")).scheme);
try expectEqualStrings("abc", (try parseUri("xyz://abc")).path);
}