From 0e1b79969f729264520d839e444a0adc38ecd24c Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Fri, 30 Jun 2023 20:40:29 -0700 Subject: [PATCH] mach: remove ResourceManager for now needs much more thought Signed-off-by: Stephen Gutekanst --- src/main.zig | 2 - src/resource/ResourceManager.zig | 102 ------------------------------- src/resource/uri_parser.zig | 46 -------------- 3 files changed, 150 deletions(-) delete mode 100644 src/resource/ResourceManager.zig delete mode 100644 src/resource/uri_parser.zig diff --git a/src/main.zig b/src/main.zig index 019ede88..48877134 100644 --- a/src/main.zig +++ b/src/main.zig @@ -15,7 +15,6 @@ pub const gfx2d = struct { pub const Sprite2D = @import("gfx2d/Sprite2D.zig"); }; pub const math = @import("math.zig"); -pub const ResourceManager = @import("resource/ResourceManager.zig"); // Engine exports pub const App = @import("engine.zig").App; @@ -24,7 +23,6 @@ pub const Module = @import("engine.zig").Module; const std = @import("std"); test { - std.testing.refAllDeclsRecursive(ResourceManager); std.testing.refAllDeclsRecursive(gfx); _ = ecs; _ = earcut; diff --git a/src/resource/ResourceManager.zig b/src/resource/ResourceManager.zig deleted file mode 100644 index 7dcd484b..00000000 --- a/src/resource/ResourceManager.zig +++ /dev/null @@ -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))); - } -}; diff --git a/src/resource/uri_parser.zig b/src/resource/uri_parser.zig deleted file mode 100644 index 8756678f..00000000 --- a/src/resource/uri_parser.zig +++ /dev/null @@ -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); -}