Application Context Implementation #4
							
								
								
									
										62
									
								
								src/sys.zig
									
									
									
									
									
								
							
							
						
						
									
										62
									
								
								src/sys.zig
									
									
									
									
									
								
							| @ -6,14 +6,14 @@ const io = @import("./io.zig"); | ||||
| const mem = @import("./mem.zig"); | ||||
| const stack = @import("./stack.zig"); | ||||
| const std = @import("std"); | ||||
| const tar = @import("./tar.zig"); | ||||
| 
 | ||||
| /// | ||||
| /// A thread-safe platform abstraction over multiplexing system I/O processing and event handling. | ||||
| /// | ||||
| pub const EventLoop = opaque { | ||||
| pub const App = opaque { | ||||
|     /// | ||||
|     /// Linked list of messages chained together to be processed by the internal file system message | ||||
|     /// processor of an [EventLoop]. | ||||
|     /// Linked list of tasks chained together to be processed by the work processor. | ||||
|     /// | ||||
|     const FileSystemMessage = struct { | ||||
|         next: ?*FileSystemMessage = null, | ||||
| @ -34,7 +34,7 @@ pub const EventLoop = opaque { | ||||
|             open: struct { | ||||
|                 mode: OpenMode, | ||||
|                 file_system_path: *const FileSystem.Path, | ||||
|                 result: OpenError!*FileAccess = error.NotFound, | ||||
|                 result: OpenError!*FileAccess = error.FileNotFound, | ||||
|             }, | ||||
| 
 | ||||
|             read_file: struct { | ||||
| @ -436,11 +436,8 @@ pub const FileSystem = struct { | ||||
|     /// | ||||
|     /// | ||||
|     pub const Root = union(enum) { | ||||
|         native: struct { | ||||
|             prefix: []const u8, | ||||
|         }, | ||||
| 
 | ||||
|         tar: struct {}, | ||||
|         native: []const u8, | ||||
|         archive: *tar.Archive, | ||||
| 
 | ||||
|         /// | ||||
|         /// | ||||
| @ -552,12 +549,12 @@ pub const LogKind = enum(u32) { | ||||
| }; | ||||
| 
 | ||||
| /// | ||||
| /// [OpenError.NotFound] is a catch-all for when a file could not be located to be opened. This | ||||
| /// [OpenError.FileNotFound] is a catch-all for when a file could not be located to be opened. This | ||||
| /// may be as simple as it doesn't exist or the because the underlying file-system will not / | ||||
| /// cannot give access to it at this time. | ||||
| /// | ||||
| pub const OpenError = error { | ||||
|     NotFound, | ||||
|     FileNotFound, | ||||
| }; | ||||
| 
 | ||||
| /// | ||||
| @ -625,24 +622,26 @@ 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.root.*) { | ||||
|         .tar => { | ||||
|         .archive => |archive| { | ||||
|             // TODO: Implement. | ||||
|             return error.NotFound; | ||||
|             _ = archive; | ||||
| 
 | ||||
|             return error.FileNotFound; | ||||
|         }, | ||||
| 
 | ||||
|         .native => |native| { | ||||
|             var path_buffer = std.mem.zeroes([4096]u8); | ||||
|             var path = stack.Fixed(u8){.buffer = path_buffer[0 .. ]}; | ||||
| 
 | ||||
|             path.pushAll(native.prefix) catch return error.NotFound; | ||||
|             path.pushAll(native) catch return error.FileNotFound; | ||||
| 
 | ||||
|             if (file_system_path.write(path.writer())) return error.NotFound; | ||||
|             if (file_system_path.write(path.writer())) return error.FileNotFound; | ||||
| 
 | ||||
|             return @ptrCast(?*FileAccess, ext.SDL_RWFromFile(&path_buffer, switch (mode) { | ||||
|                 .readonly => "rb", | ||||
|                 .overwrite => "wb", | ||||
|                 .append => "ab", | ||||
|             })) orelse error.NotFound; | ||||
|             })) orelse error.FileNotFound; | ||||
|         }, | ||||
|     } | ||||
| } | ||||
| @ -650,7 +649,9 @@ pub fn open(mode: OpenMode, file_system_path: FileSystem.Path) OpenError!*FileAc | ||||
| /// | ||||
| /// | ||||
| /// | ||||
| pub fn runGraphics(comptime Errors: anytype, comptime run: GraphicsRunner(Errors)) (RunError || Errors)!void { | ||||
| pub fn runGraphics(comptime Errors: anytype, allocator: std.mem.Allocator, | ||||
|     comptime run: GraphicsRunner(Errors)) (RunError || Errors)!void { | ||||
| 
 | ||||
|     if (ext.SDL_Init(ext.SDL_INIT_EVERYTHING) != 0) { | ||||
|         ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize runtime"); | ||||
| 
 | ||||
| @ -693,22 +694,25 @@ pub fn runGraphics(comptime Errors: anytype, comptime run: GraphicsRunner(Errors | ||||
| 
 | ||||
|     defer ext.SDL_free(user_prefix); | ||||
| 
 | ||||
|     var data_archive = tar.Archive.init(allocator); | ||||
| 
 | ||||
|     data_archive.load("./data.tar") catch |err| switch (err) { | ||||
|         error.FileNotFound => { | ||||
| 
 | ||||
|         }, | ||||
|     }; | ||||
| 
 | ||||
|     var file_system = FileSystem{ | ||||
|         .user = .{.native = .{.prefix = user_prefix[0 .. std.mem.len(user_prefix)]}}, | ||||
|         .data = .{.tar = .{}}, | ||||
|         .user = .{.native = user_prefix[0 .. std.mem.len(user_prefix)]}, | ||||
|         .data = .{.archive = &data_archive}, | ||||
|     }; | ||||
| 
 | ||||
|     var event_loop = EventLoop.Implementation.init() catch |err| { | ||||
|         switch (err) { | ||||
|             error.OutOfMemory => ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, | ||||
|                 "Failed to allocate necessary memory"), | ||||
| 
 | ||||
|             error.OutOfMutexes => ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, | ||||
|                 "Failed to create file-system work lock"), | ||||
| 
 | ||||
|             error.OutOfSemaphores => ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, | ||||
|                 "Failed to create file-system work scheduler"), | ||||
|         } | ||||
|         ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, switch (err) { | ||||
|             error.OutOfMemory => "Failed to allocate necessary memory", | ||||
|             error.OutOfMutexes => "Failed to create file-system work lock", | ||||
|             error.OutOfSemaphores => "Failed to create file-system work scheduler", | ||||
|         }); | ||||
| 
 | ||||
|         return error.InitFailure; | ||||
|     }; | ||||
|  | ||||
							
								
								
									
										29
									
								
								src/tar.zig
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								src/tar.zig
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,29 @@ | ||||
| const std = @import("std"); | ||||
| 
 | ||||
| /// | ||||
| /// | ||||
| /// | ||||
| pub const Archive = struct { | ||||
|     allocator: std.mem.Allocator, | ||||
| 
 | ||||
|     pub const LoadError = error { | ||||
|         FileNotFound, | ||||
|     }; | ||||
| 
 | ||||
|     /// | ||||
|     /// | ||||
|     /// | ||||
|     pub fn init(allocator: std.mem.Allocator) Archive { | ||||
|         return Archive{ | ||||
|             .allocator = allocator, | ||||
|         }; | ||||
|     } | ||||
| 
 | ||||
|     /// | ||||
|     /// | ||||
|     /// | ||||
|     pub fn load(archive: *Archive, file_path: []const u8) LoadError!void { | ||||
|         _ = file_path; | ||||
|         _ = archive; | ||||
|     } | ||||
| }; | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user