Compare commits
2 Commits
677e7be0a2
...
88ef9078e0
Author | SHA1 | Date |
---|---|---|
kayomn | 88ef9078e0 | |
kayomn | d6782d12d2 |
74
src/main.zig
74
src/main.zig
|
@ -11,76 +11,26 @@ const sys = @import("./sys.zig");
|
||||||
/// Entry point.
|
/// Entry point.
|
||||||
///
|
///
|
||||||
pub fn main() anyerror!void {
|
pub fn main() anyerror!void {
|
||||||
if (ext.SDL_Init(ext.SDL_INIT_EVERYTHING) != 0) {
|
return sys.runGraphics(anyerror, run);
|
||||||
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize SDL2 runtime");
|
|
||||||
|
|
||||||
return error.InitFailure;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
defer ext.SDL_Quit();
|
fn run(event_loop: *sys.EventLoop, graphics: *sys.GraphicalContext) anyerror!void {
|
||||||
|
var gpa = std.heap.GeneralPurposeAllocator(){};
|
||||||
|
|
||||||
const pref_path = create_pref_path: {
|
{
|
||||||
const path = ext.SDL_GetPrefPath("ona", "ona") orelse {
|
const file = event_loop.open(.readonly, sys.Path.get(.data, &.{"ona.lua"}));
|
||||||
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to load user path");
|
|
||||||
|
|
||||||
return error.InitFailure;
|
defer _ = event_loop.closeFile(file);
|
||||||
};
|
|
||||||
|
|
||||||
break: create_pref_path path[0 .. std.mem.len(path)];
|
const file_size = try file.size(event_loop);
|
||||||
};
|
var buffer = try gpa.allocator().alloc(u8, file_size);
|
||||||
|
|
||||||
defer ext.SDL_free(pref_path.ptr);
|
if (try event_loop.readFile(buffer) != file_size) return error.ScriptLoadError;
|
||||||
|
|
||||||
const window = create_window: {
|
sys.log(buffer);
|
||||||
const pos = ext.SDL_WINDOWPOS_UNDEFINED;
|
|
||||||
var flags = @as(u32, 0);
|
|
||||||
|
|
||||||
break: create_window ext.SDL_CreateWindow("Ona", pos, pos, 640, 480, flags) orelse {
|
|
||||||
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to load SDL2 window");
|
|
||||||
|
|
||||||
return error.InitFailure;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
defer ext.SDL_DestroyWindow(window);
|
|
||||||
|
|
||||||
const renderer = create_renderer: {
|
|
||||||
var flags = @as(u32, 0);
|
|
||||||
|
|
||||||
break: create_renderer ext.SDL_CreateRenderer(window, -1, flags) orelse {
|
|
||||||
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to load SDL2 renderer");
|
|
||||||
|
|
||||||
return error.InitFailure;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
defer ext.SDL_DestroyRenderer(renderer);
|
|
||||||
|
|
||||||
var event_loop = sys.EventLoop{};
|
|
||||||
var is_running = true;
|
|
||||||
|
|
||||||
while (is_running) {
|
|
||||||
var event = std.mem.zeroes(ext.SDL_Event);
|
|
||||||
|
|
||||||
while (ext.SDL_PollEvent(&event) != 0) {
|
|
||||||
switch (event.type) {
|
|
||||||
ext.SDL_QUIT => is_running = false,
|
|
||||||
else => {},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ext.SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255) != 0) {
|
while (graphics.poll()) |_| {
|
||||||
ext.SDL_LogError(ext.SDL_LOG_CATEGORY_VIDEO, ext.SDL_GetError());
|
graphics.present();
|
||||||
ext.SDL_ClearError();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ext.SDL_RenderClear(renderer) != 0) {
|
|
||||||
ext.SDL_LogError(ext.SDL_LOG_CATEGORY_VIDEO, ext.SDL_GetError());
|
|
||||||
ext.SDL_ClearError();
|
|
||||||
}
|
|
||||||
|
|
||||||
ext.SDL_RenderPresent(renderer);
|
|
||||||
event_loop.tick();
|
|
||||||
ext.SDL_Delay(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,6 @@
|
||||||
const io = @import("./io.zig");
|
const io = @import("./io.zig");
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
///
|
|
||||||
/// Potential errors that may occur while trying to push one or more elements into a stack of a
|
|
||||||
/// known maximum size.
|
|
||||||
///
|
|
||||||
/// [FinitePushError.Overflow] is returned if the stack does not have sufficient capacity to hold a
|
|
||||||
/// given set of elements.
|
|
||||||
///
|
|
||||||
pub const FixedPushError = error {
|
|
||||||
Overflow,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub fn Fixed(comptime Element: type) type {
|
pub fn Fixed(comptime Element: type) type {
|
||||||
return struct {
|
return struct {
|
||||||
filled: usize = 0,
|
filled: usize = 0,
|
||||||
|
@ -24,7 +13,7 @@ pub fn Fixed(comptime Element: type) type {
|
||||||
///
|
///
|
||||||
/// Note that this will raise a compilation error if [Element] is not `u8`.
|
/// Note that this will raise a compilation error if [Element] is not `u8`.
|
||||||
///
|
///
|
||||||
pub fn asWriter(self: *Self) io.Writer {
|
pub fn writer(self: *Self) io.Writer {
|
||||||
if (Element != u8) @compileError("Cannot coerce fixed stack of type " ++
|
if (Element != u8) @compileError("Cannot coerce fixed stack of type " ++
|
||||||
@typeName(Element) ++ " into a Writer");
|
@typeName(Element) ++ " into a Writer");
|
||||||
|
|
||||||
|
@ -91,6 +80,17 @@ pub fn Fixed(comptime Element: type) type {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Potential errors that may occur while trying to push one or more elements into a stack of a
|
||||||
|
/// known maximum size.
|
||||||
|
///
|
||||||
|
/// [FinitePushError.Overflow] is returned if the stack does not have sufficient capacity to hold a
|
||||||
|
/// given set of elements.
|
||||||
|
///
|
||||||
|
pub const FixedPushError = error {
|
||||||
|
Overflow,
|
||||||
|
};
|
||||||
|
|
||||||
test "fixed stack" {
|
test "fixed stack" {
|
||||||
const testing = @import("std").testing;
|
const testing = @import("std").testing;
|
||||||
const expectError = testing.expectError;
|
const expectError = testing.expectError;
|
||||||
|
@ -112,7 +112,7 @@ test "fixed stack" {
|
||||||
|
|
||||||
try expectEqual(stack.count(), 0);
|
try expectEqual(stack.count(), 0);
|
||||||
|
|
||||||
const writer = stack.asWriter();
|
const writer = stack.writer();
|
||||||
|
|
||||||
try expectEqual(writer.write(&.{0, 0, 0, 0}), 4);
|
try expectEqual(writer.write(&.{0, 0, 0, 0}), 4);
|
||||||
try expectEqual(writer.writeByte(0), false);
|
try expectEqual(writer.writeByte(0), false);
|
||||||
|
|
263
src/sys.zig
263
src/sys.zig
|
@ -10,163 +10,31 @@ const std = @import("std");
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
pub const EventLoop = packed struct {
|
pub const EventLoop = struct {
|
||||||
current_request: ?*Request = null,
|
|
||||||
|
|
||||||
///
|
|
||||||
/// [OpenError.NotFound] is used as a catch-all for any hardware or software-specific reason for
|
|
||||||
/// failing to open a given file. This includes file-system restrictions surrounding a specific
|
|
||||||
/// file as well as it simply not existing.
|
|
||||||
///
|
|
||||||
/// [OpenError.OutOfFiles] occurs when there are no more resources available to open further
|
|
||||||
/// files. As a result, some open files must be closed before more may be opened.
|
|
||||||
///
|
|
||||||
pub const OpenError = error {
|
|
||||||
NotFound,
|
|
||||||
OutOfFiles,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Indicates what kind of access the consumer logic has to a file.
|
|
||||||
///
|
|
||||||
/// [OpenMode.read] is for reading from an existing file from the start.
|
|
||||||
///
|
|
||||||
/// [OpenMode.overwrite] is for deleting the contents of a file, or creating an empty one if no
|
|
||||||
/// such file exists, and writing to it from the start.
|
|
||||||
///
|
|
||||||
/// [OpenMode.append] is for writing additional contents to a file, creating an empty one if no
|
|
||||||
/// such file exists, on the end of whatever it already contains.
|
|
||||||
///
|
|
||||||
pub const OpenMode = enum {
|
|
||||||
readonly,
|
|
||||||
overwrite,
|
|
||||||
append,
|
|
||||||
};
|
|
||||||
|
|
||||||
const Request = struct {
|
|
||||||
next: ?*Request = null,
|
|
||||||
frame: anyframe,
|
|
||||||
|
|
||||||
message: union(enum) {
|
|
||||||
close: struct {
|
|
||||||
file: *FileAccess,
|
|
||||||
},
|
|
||||||
|
|
||||||
open: struct {
|
|
||||||
file_system: FileSystem,
|
|
||||||
path: *const Path,
|
|
||||||
mode: OpenMode,
|
|
||||||
file: OpenError!*FileAccess = error.OutOfFiles,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const max_files = 512;
|
|
||||||
|
|
||||||
///
|
|
||||||
/// Asynchronously closes `file_access` via `event_loop`.
|
|
||||||
///
|
|
||||||
/// *Note* that `file_access` must have been opened by `event_loop` for it to be closed by it,
|
|
||||||
/// otherwise it will cause undefined behavior.
|
|
||||||
///
|
|
||||||
pub fn close(event_loop: *EventLoop, file_access: *FileAccess) void {
|
|
||||||
var request = Request{
|
|
||||||
.frame = @frame(),
|
|
||||||
.message = .{.close = @ptrCast(*ext.SDL_RWops, file_access)},
|
|
||||||
};
|
|
||||||
|
|
||||||
suspend if (event_loop.current_request) |current_request| {
|
|
||||||
current_request.next = &request;
|
|
||||||
} else {
|
|
||||||
event_loop.current_request = &request;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
|
pub const FileAccess = opaque {
|
||||||
///
|
///
|
||||||
pub fn open(event_loop: *EventLoop, file_system: FileSystem,
|
/// Scans the number of bytes in the file referenced by `file_access` via `event_loop`, returing
|
||||||
path: Path, mode: OpenMode) OpenError!*FileAccess {
|
/// its byte size or a [FileError] if it failed.
|
||||||
|
|
||||||
var request = Request{
|
|
||||||
.frame = @frame(),
|
|
||||||
|
|
||||||
.message = .{
|
|
||||||
.open = .{
|
|
||||||
.file_system = file_system,
|
|
||||||
.path = &path,
|
|
||||||
.mode = mode,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
suspend if (event_loop.current_request) |current_request| {
|
|
||||||
current_request.next = &request;
|
|
||||||
} else {
|
|
||||||
event_loop.current_request = &request;
|
|
||||||
};
|
|
||||||
|
|
||||||
return @ptrCast(*FileAccess, try request.message.open.file);
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
///
|
pub fn size(file_access: *FileAccess, event_loop: *EventLoop) FileError!usize {
|
||||||
///
|
const origin_cursor = try app.tellFile(file_access);
|
||||||
pub fn tick(event_loop: *EventLoop) void {
|
|
||||||
while (event_loop.current_request) |request| {
|
|
||||||
switch (request.message) {
|
|
||||||
.close => |*close| {
|
|
||||||
// Swallow file close errors.
|
|
||||||
_ = ext.SDL_RWclose(@ptrCast(*ext.SDL_RWops, @alignCast(@alignOf(ext.SDL_RWops), close.file)));
|
|
||||||
},
|
|
||||||
|
|
||||||
.open => |*open| {
|
try app.seekFile(file_access, .tail, 0);
|
||||||
switch (open.file_system) {
|
|
||||||
.data => {
|
|
||||||
var zeroed_path_buffer = std.mem.zeroes([4096]u8);
|
|
||||||
var zeroed_path = stack.Fixed(u8){.buffer = &zeroed_path_buffer};
|
|
||||||
|
|
||||||
zeroed_path.push('.') catch continue;
|
const ending_cursor = try app.tellFile(file_access);
|
||||||
|
|
||||||
_ = open.path.write(zeroed_path.asWriter());
|
try app.seekFile(file_access, .head, origin_cursor);
|
||||||
|
|
||||||
zeroed_path.push(0) catch continue;
|
return ending_cursor;
|
||||||
|
|
||||||
// TODO: Access TAR file.
|
|
||||||
},
|
|
||||||
|
|
||||||
.user => {
|
|
||||||
var zeroed_path_buffer = std.mem.zeroes([4096]u8);
|
|
||||||
var zeroed_path = stack.Fixed(u8){.buffer = &zeroed_path_buffer};
|
|
||||||
|
|
||||||
zeroed_path.pushAll("/home/kayomn/.local/share") catch continue;
|
|
||||||
|
|
||||||
_ = open.path.write(zeroed_path.asWriter());
|
|
||||||
|
|
||||||
zeroed_path.push(0) catch continue;
|
|
||||||
|
|
||||||
open.file = @ptrCast(?*FileAccess, ext.SDL_RWFromFile(&zeroed_path_buffer, switch (open.mode) {
|
|
||||||
.readonly => "rb",
|
|
||||||
.overwrite => "wb",
|
|
||||||
.append => "ab",
|
|
||||||
})) orelse error.NotFound;
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
resume request.frame;
|
|
||||||
|
|
||||||
event_loop.current_request = request.next;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
///
|
|
||||||
///
|
|
||||||
///
|
|
||||||
pub const FileAccess = opaque {};
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// With files typically being backed by a block device, they can produce a variety of errors -
|
/// With files typically being backed by a block device, they can produce a variety of errors -
|
||||||
/// from physical to virtual errors - these are all encapsulated by the API as general
|
/// from physical to virtual errors - these are all encapsulated by the API as general
|
||||||
|
@ -176,14 +44,6 @@ pub const FileError = error {
|
||||||
Inaccessible,
|
Inaccessible,
|
||||||
};
|
};
|
||||||
|
|
||||||
///
|
|
||||||
///
|
|
||||||
///
|
|
||||||
pub const FileSystem = enum {
|
|
||||||
data,
|
|
||||||
user,
|
|
||||||
};
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Platform-agnostic mechanism for accessing files on any of the virtual file-systems supported by
|
/// Platform-agnostic mechanism for accessing files on any of the virtual file-systems supported by
|
||||||
/// Ona.
|
/// Ona.
|
||||||
|
@ -199,6 +59,13 @@ pub const Path = struct {
|
||||||
TooLong,
|
TooLong,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
///
|
||||||
|
///
|
||||||
|
///
|
||||||
|
pub fn isComplete(path: Path) bool {
|
||||||
|
return (path.length != 0) and (path.buffer[0] == '/');
|
||||||
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Returns `true` if the length of `path` is empty, otherwise `false`.
|
/// Returns `true` if the length of `path` is empty, otherwise `false`.
|
||||||
///
|
///
|
||||||
|
@ -213,6 +80,80 @@ pub const Path = struct {
|
||||||
return std.mem.eql(u8, this.buffer[0 .. this.length], that.buffer[0 .. that.length]);
|
return std.mem.eql(u8, this.buffer[0 .. this.length], that.buffer[0 .. that.length]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Creates and returns a [Path] value in the file system indicated by `file_system` from the
|
||||||
|
/// path components in `component_groups`.
|
||||||
|
///
|
||||||
|
pub fn from(file_system: FileSystem, component_groups: []const []const u8) Error!Path {
|
||||||
|
var path = Path{
|
||||||
|
.file_system = file_system,
|
||||||
|
.buffer = std.mem.zeroes([max]u8),
|
||||||
|
.length = 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
for (component_groups) |first_component_group,
|
||||||
|
first_component_group_index| if (first_component_group.len != 0) {
|
||||||
|
|
||||||
|
var first_component_root_offset = @as(usize, 0);
|
||||||
|
|
||||||
|
if (component_group[0] == '/') {
|
||||||
|
comptime if (max < 1) unreachable;
|
||||||
|
|
||||||
|
path.buffer[path.length] = '/';
|
||||||
|
path.length += 1;
|
||||||
|
first_component_root_offset = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
var first_components = mem.Spliterator(u8){
|
||||||
|
.source = first_component_group[first_component_root_offset ..
|
||||||
|
(first_component_group.len - first_component_root_offset)],
|
||||||
|
|
||||||
|
.delimiter = "/",
|
||||||
|
};
|
||||||
|
|
||||||
|
while (first_components.next()) |component| if (component.len != 0) {
|
||||||
|
for (component) |byte| {
|
||||||
|
if (path.length == max) return error.TooLong;
|
||||||
|
|
||||||
|
path.buffer[path.length] = byte;
|
||||||
|
path.length += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (path.length == max) return error.TooLong;
|
||||||
|
|
||||||
|
path.buffer[path.length] = '/';
|
||||||
|
path.length += 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
const second_component_group_index = (first_component_group_index + 1);
|
||||||
|
|
||||||
|
for (component_groups[second_component_group_index .. (component_groups.len -
|
||||||
|
second_component_group_index)]) |component_group| if (component_group.len != 0) {
|
||||||
|
|
||||||
|
var components = mem.Spliterator(u8){
|
||||||
|
.source = component_group,
|
||||||
|
.delimiter = "/",
|
||||||
|
};
|
||||||
|
|
||||||
|
while (components.next()) |component| if (component.len != 0) {
|
||||||
|
for (component) |byte| {
|
||||||
|
if (path.length == max) return error.TooLong;
|
||||||
|
|
||||||
|
path.buffer[path.length] = byte;
|
||||||
|
path.length += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (path.length == max) return error.TooLong;
|
||||||
|
|
||||||
|
path.buffer[path.length] = '/';
|
||||||
|
path.length += 1;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
/// The maximum possible byte-length of a [Path].
|
/// The maximum possible byte-length of a [Path].
|
||||||
///
|
///
|
||||||
|
@ -222,31 +163,17 @@ pub const Path = struct {
|
||||||
///
|
///
|
||||||
pub const max = 1000;
|
pub const max = 1000;
|
||||||
|
|
||||||
///
|
|
||||||
///
|
|
||||||
///
|
|
||||||
pub fn parseJoins(path: Path, joins: []const []const u8) Error!Path {
|
|
||||||
_ = path;
|
|
||||||
_ = joins;
|
|
||||||
|
|
||||||
return Path{
|
|
||||||
.buffer = std.mem.zeroes([max]u8),
|
|
||||||
.length = 0
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
pub fn write(path: Path, writer: io.Writer) bool {
|
pub fn write(path: Path, writer: io.Writer) bool {
|
||||||
return (writer.write(path.buffer[0 .. path.length]) == path.length);
|
return (writer.write(path.buffer[0 .. path.length]) == path.length);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
pub const root = Path{
|
pub fn runGraphics(comptime ErrorUnion: anytype, run: fn (*App) ErrorUnion!void) void {
|
||||||
.buffer = [_]u8{'/'} ** max,
|
|
||||||
.length = 1,
|
}
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
Loading…
Reference in New Issue