Implement more stub functions / structures
continuous-integration/drone/push Build is passing Details

This commit is contained in:
kayomn 2022-09-30 16:07:18 +01:00
parent 88ef9078e0
commit 67768eba7f
2 changed files with 295 additions and 105 deletions

View File

@ -14,20 +14,21 @@ pub fn main() anyerror!void {
return sys.runGraphics(anyerror, run);
}
fn run(event_loop: *sys.EventLoop, graphics: *sys.GraphicalContext) anyerror!void {
var gpa = std.heap.GeneralPurposeAllocator(){};
fn run(event_loop: *sys.EventLoop, graphics: *sys.GraphicsContext) anyerror!void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
{
const file = event_loop.open(.readonly, sys.Path.get(.data, &.{"ona.lua"}));
const file_access =
try event_loop.open(.readonly, try sys.FileSystem.data.pathJoin(&.{"data", "ona.lua"}));
defer _ = event_loop.closeFile(file);
defer _ = event_loop.close(file_access);
const file_size = try file.size(event_loop);
const file_size = try file_access.size(event_loop);
var buffer = try gpa.allocator().alloc(u8, file_size);
if (try event_loop.readFile(buffer) != file_size) return error.ScriptLoadError;
if ((try event_loop.readFile(file_access, buffer)) != file_size) return error.ScriptLoadError;
sys.log(buffer);
event_loop.log(buffer);
}
while (graphics.poll()) |_| {

View File

@ -10,8 +10,136 @@ const std = @import("std");
///
///
///
pub const EventLoop = struct {
pub const EventLoop = opaque {
const Implementation = struct {
current_message: ?*Message,
};
///
///
///
const Message = struct {
next: ?*Message,
frame: anyframe,
request: union(enum) {
open: struct {
mode: OpenMode,
path: *const FileSystem.Path,
},
},
response: union(enum) {
none,
file_access: *FileAccess,
file_error: FileError,
} = .none,
};
///
///
///
pub const OpenError = error {
NotFound,
};
///
///
///
pub const OpenMode = enum {
readonly,
overwrite,
append,
};
///
///
///
pub fn close(event_loop: *EventLoop, file_access: *FileAccess) void {
var message = Message{
.frame = @frame(),
.request = .{
.close = .{
.file_access = file_access,
},
},
};
suspend event_loop.enqueueMessage(&message);
}
///
///
///
fn enqueueMessage(event_loop: *EventLoop, message: *Message) void {
const implementation =
@ptrCast(*Implementation, @alignCast(@alignOf(Implementation), event_loop));
if (implementation.current_message) |current_message| {
current_message.next = message;
} else {
implementation.current_message = message;
}
}
pub fn log(event_loop: *EventLoop, message: []const u8) void {
// TODO: Implement.
_ = event_loop;
_ = message;
}
///
///
///
pub fn open(event_loop: *EventLoop, mode: OpenMode,
path: FileSystem.Path) OpenError!*FileAccess {
var message = Message{
.frame = @frame(),
.request = .{
.open = .{
.mode = mode,
.path = &path,
},
},
};
suspend event_loop.enqueueMessage(&message);
switch (message.response) {
.file_access => |file_access| return file_access,
.open_error => |open_error| return open_error,
else => unreachable,
}
}
///
///
///
pub fn readFile(event_loop: *EventLoop, file_access: *FileAccess,
buffer: []const u8) FileError!usize {
var message = Message{
.frame = @frame(),
.request = .{
.read_file = .{
.file_access = file_access,
.buffer = buffer,
},
},
};
suspend event_loop.enqueueMessage(&message);
switch (message.response) {
.size => |size| return size,
.file_error => |file_error| return file_error,
else => unreachable,
}
}
};
///
@ -23,13 +151,15 @@ pub const FileAccess = opaque {
/// its byte size or a [FileError] if it failed.
///
pub fn size(file_access: *FileAccess, event_loop: *EventLoop) FileError!usize {
const origin_cursor = try app.tellFile(file_access);
// Save cursor to return to it later.
const origin_cursor = try event_loop.tellFile(file_access);
try app.seekFile(file_access, .tail, 0);
try event_loop.seekFile(file_access, .tail, 0);
const ending_cursor = try app.tellFile(file_access);
const ending_cursor = try event_loop.tellFile(file_access);
try app.seekFile(file_access, .head, origin_cursor);
// Return to original cursor.
try event_loop.seekFile(file_access, .head, origin_cursor);
return ending_cursor;
}
@ -45,135 +175,194 @@ pub const FileError = error {
};
///
/// Platform-agnostic mechanism for accessing files on any of the virtual file-systems supported by
/// Ona.
///
pub const Path = struct {
length: u16,
buffer: [max]u8,
///
pub const FileSystem = enum {
data,
user,
///
/// Platform-agnostic mechanism for referencing files and directories on a [FileSystem]
///
pub const Path = struct {
file_system: FileSystem,
length: u16,
buffer: [max]u8,
///
/// Returns `true` if the length of `path` is empty, otherwise `false`.
///
pub fn isEmpty(path: Path) bool {
return (path.length == 0);
}
///
/// Returns `true` if `this` is equal to `that`, otherwise `false`.
///
pub fn equals(this: Path, that: Path) bool {
return std.mem.eql(u8, this.buffer[0 .. this.length], that.buffer[0 .. that.length]);
}
///
/// The maximum possible byte-length of a [Path].
///
/// Note that paths are encoded using UTF-8, meaning that a character may be bigger than one
/// byte. Because of this, it is not safe to asume that a path may hold [max] individual
/// characters.
///
pub const max = 1000;
///
///
///
pub fn write(path: Path, writer: io.Writer) bool {
return (writer.write(path.buffer[0 .. path.length]) == path.length);
}
};
///
pub const Error = error {
/// [PathError.TooLong] occurs when creating a path that is greater than the maximum size **in
/// bytes**.
///
pub const PathError = error {
TooLong,
};
///
/// Creates and returns a [Path] value in the file system to the location specified by the
/// joining of the `component_groups` path values.
///
///
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`.
///
pub fn isEmpty(path: Path) bool {
return (path.length == 0);
}
///
/// Returns `true` if `this` is equal to `that`, otherwise `false`.
///
pub fn equals(this: Path, that: Path) bool {
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 {
pub fn pathJoin(file_system: FileSystem, component_groups: []const []const u8) PathError!Path {
var path = Path{
.file_system = file_system,
.buffer = std.mem.zeroes([max]u8),
.buffer = std.mem.zeroes([Path.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)],
for (component_groups) |component_group| if (component_group.len != 0) {
var components = mem.Spliterator(u8){
.source = component_group,
.delimiter = "/",
};
while (first_components.next()) |component| if (component.len != 0) {
while (components.next()) |component| if (component.len != 0) {
for (component) |byte| {
if (path.length == max) return error.TooLong;
if (path.length == Path.max) return error.TooLong;
path.buffer[path.length] = byte;
path.length += 1;
}
if (path.length == max) return error.TooLong;
if (path.length == Path.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].
///
/// Note that paths are encoded using UTF-8, meaning that a character may be bigger than one
/// byte. Because of this, it is not safe to asume that a path may hold [max] individual
/// characters.
///
pub const max = 1000;
///
///
///
pub fn write(path: Path, writer: io.Writer) bool {
return (writer.write(path.buffer[0 .. path.length]) == path.length);
}
};
///
///
///
pub fn runGraphics(comptime ErrorUnion: anytype, run: fn (*App) ErrorUnion!void) void {
pub const GraphicsContext = opaque {
///
///
///
pub const Event = struct {
};
const Implementation = struct {
event: Event,
};
///
///
///
pub fn poll(graphics_context: *GraphicsContext) ?*const Event {
// TODO: Implement.
_ = graphics_context;
return null;
}
///
///
///
pub fn present(graphics_context: *GraphicsContext) void {
// TODO: Implement;
_ = graphics_context;
}
};
///
///
///
pub fn GraphicsRunner(comptime Errors: type) type {
return fn (*EventLoop, *GraphicsContext) Errors!void;
}
///
///
///
pub fn runGraphics(comptime Errors: anytype, run: GraphicsRunner(Errors)) Errors!void {
if (ext.SDL_Init(ext.SDL_INIT_EVERYTHING) != 0) {
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize SDL2 runtime");
return error.InitFailure;
}
defer ext.SDL_Quit();
const pref_path = create_pref_path: {
const path = ext.SDL_GetPrefPath("ona", "ona") orelse {
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to load user path");
return error.InitFailure;
};
break: create_pref_path path[0 .. std.mem.len(path)];
};
defer ext.SDL_free(pref_path.ptr);
const window = create_window: {
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 = EventLoop.Implementation{
.current_message = null,
};
var graphics_context = GraphicsContext.Implementation{
.event = .{
},
};
return run(@ptrCast(*EventLoop, &event_loop), @ptrCast(*GraphicsContext, &graphics_context));
}