2022-09-24 23:09:02 +01:00
|
|
|
const ext = @cImport({
|
|
|
|
@cInclude("SDL2/SDL.h");
|
|
|
|
});
|
|
|
|
|
2022-09-27 21:45:32 +01:00
|
|
|
const io = @import("./io.zig");
|
2022-10-09 23:10:13 +01:00
|
|
|
const math = @import("./math.zig");
|
2022-09-24 23:09:02 +01:00
|
|
|
const mem = @import("./mem.zig");
|
2022-10-09 23:10:13 +01:00
|
|
|
const oar = @import("./oar.zig");
|
2022-09-24 23:09:02 +01:00
|
|
|
const stack = @import("./stack.zig");
|
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
///
|
2022-10-03 22:32:16 +01:00
|
|
|
/// A thread-safe platform abstraction over multiplexing system I/O processing and event handling.
|
2022-09-24 23:09:02 +01:00
|
|
|
///
|
2022-10-10 10:15:45 +01:00
|
|
|
pub const AppContext = opaque {
|
2022-09-30 16:07:18 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// Linked list of asynchronous messages chained together to be processed by the work processor.
|
2022-09-30 16:07:18 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
const Message = struct {
|
|
|
|
next: ?*Message = null,
|
2022-09-30 16:07:18 +01:00
|
|
|
frame: anyframe,
|
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
kind: union(enum) {
|
|
|
|
quit,
|
2022-10-01 21:12:12 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
task: struct {
|
|
|
|
data: *anyopaque,
|
|
|
|
action: fn (*anyopaque) void,
|
2022-09-30 16:07:18 +01:00
|
|
|
},
|
|
|
|
},
|
2022-10-01 17:46:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
///
|
2022-10-03 21:30:02 +01:00
|
|
|
/// Internal state of the event loop hidden from the API consumer.
|
2022-10-01 17:46:21 +01:00
|
|
|
///
|
|
|
|
const Implementation = struct {
|
2022-10-09 23:10:13 +01:00
|
|
|
data_file_system: FileSystem,
|
|
|
|
user_file_system: FileSystem,
|
|
|
|
message_semaphore: *ext.SDL_sem,
|
|
|
|
message_mutex: *ext.SDL_mutex,
|
|
|
|
message_thread: ?*ext.SDL_Thread,
|
|
|
|
messages: ?*Message = null,
|
2022-09-30 16:07:18 +01:00
|
|
|
|
2022-10-04 13:51:46 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// [StartError.OutOfSemaphores] indicates that the process has no more semaphores available
|
|
|
|
/// to it for use, meaning an [Implementation] may not be initialized at this time.
|
|
|
|
///
|
|
|
|
/// [StartError.OutOfMutexes] indicates that the process has no more mutexes available to it
|
|
|
|
/// for use, meaning an [Implementation] may not be initialized at this time.
|
2022-10-04 13:51:46 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// [StartError.OutOfMemory] indicates that the process has no more memory available to it
|
|
|
|
/// for use, meaning an [Implementation] may not be initialized at this time.
|
2022-10-04 13:51:46 +01:00
|
|
|
///
|
|
|
|
const InitError = error {
|
|
|
|
OutOfSemaphores,
|
|
|
|
OutOfMutexes,
|
|
|
|
OutOfMemory,
|
|
|
|
};
|
|
|
|
|
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// [StartError.OutOfThreads] indicates that the process has no more threads available to it
|
|
|
|
/// to use, meaning that no asynchronous work may be started on an [Implementation] at this
|
|
|
|
/// time.
|
2022-10-04 13:51:46 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// [StartError.AlreadyStarted] is occurs when a request to start work processing happens on
|
|
|
|
/// an [Implementation] that is already processing work.
|
2022-10-04 13:51:46 +01:00
|
|
|
///
|
|
|
|
const StartError = error {
|
|
|
|
OutOfThreads,
|
|
|
|
AlreadyStarted,
|
|
|
|
};
|
|
|
|
|
2022-10-03 21:30:02 +01:00
|
|
|
///
|
2022-10-10 10:15:45 +01:00
|
|
|
/// Casts `app_context` to a [Implementation] reference.
|
2022-10-03 21:30:02 +01:00
|
|
|
///
|
2022-10-10 10:15:45 +01:00
|
|
|
/// *Note* that if `app_context` does not have the same alignment as [Implementation], safety-
|
2022-10-09 23:10:13 +01:00
|
|
|
/// checked undefined behavior will occur.
|
2022-10-03 21:30:02 +01:00
|
|
|
///
|
2022-10-10 10:15:45 +01:00
|
|
|
fn cast(app_context: *AppContext) *Implementation {
|
|
|
|
return @ptrCast(*Implementation, @alignCast(@alignOf(Implementation), app_context));
|
2022-10-01 17:46:21 +01:00
|
|
|
}
|
2022-10-04 13:51:46 +01:00
|
|
|
|
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// Deinitializes the `implementation`, requesting any running asynchronous workload
|
|
|
|
/// processes quit and waits for them to do so before freeing any resources.
|
2022-10-04 13:51:46 +01:00
|
|
|
///
|
|
|
|
fn deinit(implementation: *Implementation) void {
|
2022-10-09 23:10:13 +01:00
|
|
|
var message = Message{
|
2022-10-04 13:51:46 +01:00
|
|
|
.frame = @frame(),
|
2022-10-09 23:10:13 +01:00
|
|
|
.kind = .quit,
|
2022-10-04 13:51:46 +01:00
|
|
|
};
|
|
|
|
|
2022-10-10 10:15:45 +01:00
|
|
|
@ptrCast(*AppContext, implementation).schedule(&message);
|
2022-10-04 13:51:46 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
var status = @as(c_int, 0);
|
|
|
|
|
2022-10-04 13:54:17 +01:00
|
|
|
// SDL2 defines waiting on a null thread reference as a no-op. See
|
|
|
|
// https://wiki.libsdl.org/SDL_WaitThread for more information
|
2022-10-09 23:10:13 +01:00
|
|
|
ext.SDL_WaitThread(implementation.message_thread, &status);
|
2022-10-04 13:51:46 +01:00
|
|
|
|
|
|
|
if (status != 0) {
|
|
|
|
// TODO: Error check this.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
ext.SDL_DestroyMutex(implementation.message_mutex);
|
|
|
|
ext.SDL_DestroySemaphore(implementation.message_semaphore);
|
2022-10-04 13:51:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// Initializes a new [Implemenation] with `data_archive_path` as the read-only data archive
|
|
|
|
/// to read from and `user_path_prefix` as the native writable user data directory.
|
2022-10-04 13:51:46 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// Returns the created [Implementation] value on success or [InitError] on failure.
|
2022-10-04 13:51:46 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
fn init(data_archive_path: []const u8, user_path_prefix: []const u8) InitError!Implementation {
|
2022-10-06 10:24:39 +01:00
|
|
|
return Implementation{
|
2022-10-09 23:10:13 +01:00
|
|
|
.message_semaphore = ext.SDL_CreateSemaphore(0) orelse return error.OutOfSemaphores,
|
|
|
|
.message_mutex = ext.SDL_CreateMutex() orelse return error.OutOfMutexes,
|
|
|
|
.data_file_system = .{.archive = data_archive_path},
|
|
|
|
.user_file_system = .{.native = user_path_prefix},
|
|
|
|
.message_thread = null,
|
2022-10-04 13:51:46 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// [FileSystemMessage] processing function used by a dedicated worker thread, where `data`
|
|
|
|
/// is a type-erased reference to a [EventLoop].
|
|
|
|
///
|
|
|
|
/// The processor returns `0` if it exited normally or any other value if an erroneous exit
|
|
|
|
/// occured.
|
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
fn processTasks(userdata: ?*anyopaque) callconv(.C) c_int {
|
2022-10-10 10:15:45 +01:00
|
|
|
const implementation = Implementation.cast(@ptrCast(*AppContext, userdata orelse unreachable));
|
2022-10-04 13:51:46 +01:00
|
|
|
|
|
|
|
while (true) {
|
2022-10-09 23:10:13 +01:00
|
|
|
_ = ext.SDL_LockMutex(implementation.message_mutex);
|
2022-10-04 13:51:46 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
defer _ = ext.SDL_UnlockMutex(implementation.message_mutex);
|
2022-10-04 13:51:46 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
while (implementation.messages) |messages| {
|
|
|
|
switch (messages.kind) {
|
|
|
|
.quit => return 0,
|
|
|
|
.task => |task| task.action(task.data),
|
2022-10-04 13:51:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resume messages.frame;
|
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
implementation.messages = messages.next;
|
2022-10-04 13:51:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Error check this.
|
2022-10-09 23:10:13 +01:00
|
|
|
_ = ext.SDL_SemWait(implementation.message_semaphore);
|
2022-10-04 13:51:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// Attempts to start the asynchronous worker thread of `implementation` if it hasn't been
|
|
|
|
/// already.
|
2022-10-04 13:51:46 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// [StartError] is returned on failure.
|
2022-10-04 13:51:46 +01:00
|
|
|
///
|
|
|
|
fn start(implementation: *Implementation) StartError!void {
|
2022-10-09 23:10:13 +01:00
|
|
|
if (implementation.message_thread != null) return error.AlreadyStarted;
|
2022-10-04 13:51:46 +01:00
|
|
|
|
2022-10-10 10:15:45 +01:00
|
|
|
implementation.message_thread = ext.SDL_CreateThread(processTasks,
|
|
|
|
"File System Worker", implementation) orelse {
|
|
|
|
|
2022-10-04 13:51:46 +01:00
|
|
|
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Failed to create file-system work processor");
|
|
|
|
|
|
|
|
return error.OutOfThreads;
|
|
|
|
};
|
|
|
|
}
|
2022-09-30 16:07:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// Returns a reference to the currently loaded data file-system.
|
2022-09-30 16:07:18 +01:00
|
|
|
///
|
2022-10-10 10:15:45 +01:00
|
|
|
pub fn data(app_context: *AppContext) *const FileSystem {
|
|
|
|
return &Implementation.cast(app_context).data_file_system;
|
2022-10-09 23:10:13 +01:00
|
|
|
}
|
|
|
|
|
2022-10-03 22:32:16 +01:00
|
|
|
///
|
2022-10-10 10:15:45 +01:00
|
|
|
/// Enqueues `message` to the message processor of `app_context` to be processed at a later, non-
|
2022-10-09 23:10:13 +01:00
|
|
|
/// deterministic point.
|
|
|
|
///
|
2022-10-10 10:15:45 +01:00
|
|
|
pub fn schedule(app_context: *AppContext, message: *Message) void {
|
|
|
|
const implementation = Implementation.cast(app_context);
|
2022-10-09 23:10:13 +01:00
|
|
|
|
|
|
|
// TODO: Error check these.
|
|
|
|
_ = ext.SDL_LockMutex(implementation.message_mutex);
|
|
|
|
|
|
|
|
defer _ = ext.SDL_UnlockMutex(implementation.message_mutex);
|
|
|
|
|
|
|
|
if (implementation.messages) |messages| {
|
|
|
|
messages.next = message;
|
|
|
|
} else {
|
|
|
|
implementation.messages = message;
|
|
|
|
}
|
2022-09-30 16:07:18 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
// TODO: Error check this.
|
|
|
|
_ = ext.SDL_SemPost(implementation.message_semaphore);
|
2022-09-30 16:07:18 +01:00
|
|
|
}
|
|
|
|
|
2022-10-01 17:46:21 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// Returns a reference to the currently loaded user file-system.
|
|
|
|
///
|
2022-10-10 10:15:45 +01:00
|
|
|
pub fn user(app_context: *AppContext) *const FileSystem {
|
|
|
|
return &Implementation.cast(app_context).user_file_system;
|
2022-10-09 23:10:13 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
///
|
|
|
|
/// File-system agnostic abstraction for manipulating a file.
|
|
|
|
///
|
|
|
|
pub const FileAccess = opaque {
|
2022-10-01 17:46:21 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// [Error.FileInaccessible] is a generic catch-all for a [FileAccess] reference no longer
|
|
|
|
/// pointing to a file or the file becomming invalid for whatever reason.
|
2022-10-01 17:46:21 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
pub const Error = error {
|
|
|
|
FileInaccessible,
|
|
|
|
};
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Returns `file_access` casted to a [ext.SDL_RWops].
|
|
|
|
///
|
|
|
|
fn asRwOps(file_access: *FileAccess) *ext.SDL_RWops {
|
|
|
|
return @ptrCast(*ext.SDL_RWops, @alignCast(@alignOf(ext.SDL_RWops), file_access));
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Close the file referenced by `file_access`, invalidating the reference to it and releasing
|
|
|
|
/// any associated resources.
|
|
|
|
///
|
|
|
|
/// Freeing an invalid `file_access` has no effect on the file and logs a warning over the
|
|
|
|
/// wasted effort.
|
|
|
|
///
|
2022-10-10 10:15:45 +01:00
|
|
|
pub fn close(file_access: *FileAccess, app_context: *AppContext) void {
|
2022-10-09 23:10:13 +01:00
|
|
|
const Task = struct {
|
|
|
|
file_access: *FileAccess,
|
|
|
|
|
|
|
|
const Task = @This();
|
|
|
|
|
|
|
|
fn process(data: *anyopaque) void {
|
|
|
|
const task = @ptrCast(*Task, @alignCast(@alignOf(Task), data));
|
|
|
|
|
|
|
|
if (ext.SDL_RWclose(task.file_access.asRwOps()) != 0)
|
|
|
|
ext.SDL_LogWarn(ext.SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Closed an invalid file reference");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var task = Task{.file_access = file_access};
|
|
|
|
|
2022-10-10 10:15:45 +01:00
|
|
|
var message = AppContext.Message{
|
2022-10-03 21:30:02 +01:00
|
|
|
.frame = @frame(),
|
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
.kind = .{.task = .{
|
|
|
|
.data = &task,
|
|
|
|
.action = Task.process,
|
2022-10-03 21:30:02 +01:00
|
|
|
}},
|
|
|
|
};
|
|
|
|
|
2022-10-10 10:15:45 +01:00
|
|
|
suspend app_context.schedule(&message);
|
2022-09-30 16:07:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// Attempts to query the current cursor position for the file referenced by `file_access`.
|
2022-10-03 21:30:02 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// Returns the number of bytes into the file that the cursor is relative to its beginning or a
|
|
|
|
/// [Error] on failure.
|
2022-09-30 16:07:18 +01:00
|
|
|
///
|
2022-10-10 10:15:45 +01:00
|
|
|
pub fn queryCursor(file_access: *FileAccess, app_context: *AppContext) Error!u64 {
|
2022-10-09 23:10:13 +01:00
|
|
|
const Task = struct {
|
|
|
|
file_access: *FileAccess,
|
|
|
|
result: Error!u64,
|
|
|
|
|
|
|
|
const Task = @This();
|
|
|
|
|
|
|
|
fn process(data: *anyopaque) void {
|
|
|
|
const task = @ptrCast(*Task, @alignCast(@alignOf(Task), data));
|
2022-09-30 16:07:18 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
ext.SDL_ClearError();
|
|
|
|
|
|
|
|
const sought = ext.SDL_RWtell(task.file_access.asRwOps());
|
|
|
|
|
|
|
|
if (sought < 0) {
|
|
|
|
task.result = error.FileInaccessible;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
task.result = @intCast(u64, sought);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var task = Task{
|
|
|
|
.file_access = file_access,
|
|
|
|
.result = error.FileInaccessible,
|
|
|
|
};
|
|
|
|
|
2022-10-10 10:15:45 +01:00
|
|
|
var message = AppContext.Message{
|
2022-09-30 16:07:18 +01:00
|
|
|
.frame = @frame(),
|
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
.kind = .{.task = .{
|
|
|
|
.data = &task,
|
|
|
|
.action = Task.process,
|
2022-10-03 22:32:16 +01:00
|
|
|
}},
|
2022-09-30 16:07:18 +01:00
|
|
|
};
|
|
|
|
|
2022-10-10 10:15:45 +01:00
|
|
|
suspend app_context.schedule(&message);
|
2022-10-01 17:46:21 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
return task.result;
|
2022-10-01 17:46:21 +01:00
|
|
|
}
|
|
|
|
|
2022-09-30 16:07:18 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// Attempts to query the current length for the file referenced by `file_access`.
|
2022-09-30 16:07:18 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// Returns the current length of the file at the time of the operation or a [Error] if the file
|
|
|
|
/// failed to be queried.
|
2022-09-30 16:07:18 +01:00
|
|
|
///
|
2022-10-10 10:15:45 +01:00
|
|
|
pub fn queryLength(file_access: *FileAccess, app_context: *AppContext) Error!u64 {
|
2022-10-09 23:10:13 +01:00
|
|
|
const Task = struct {
|
|
|
|
file_access: *FileAccess,
|
|
|
|
result: Error!usize,
|
|
|
|
|
|
|
|
const Task = @This();
|
|
|
|
|
|
|
|
fn process(data: *anyopaque) void {
|
|
|
|
const task = @ptrCast(*Task, @alignCast(@alignOf(Task), data));
|
|
|
|
|
|
|
|
ext.SDL_ClearError();
|
2022-09-30 16:07:18 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
const sought = ext.SDL_RWsize(task.file_access.asRwOps());
|
|
|
|
|
|
|
|
if (sought < 0) {
|
|
|
|
task.result = error.FileInaccessible;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
task.result = @intCast(u64, sought);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var task = Task{
|
|
|
|
.file_access = file_access,
|
|
|
|
.result = error.FileInaccessible,
|
|
|
|
};
|
|
|
|
|
2022-10-10 10:15:45 +01:00
|
|
|
var message = AppContext.Message{
|
2022-09-30 16:07:18 +01:00
|
|
|
.frame = @frame(),
|
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
.kind = .{.task = .{
|
|
|
|
.data = &task,
|
|
|
|
.action = Task.process,
|
2022-10-03 22:32:16 +01:00
|
|
|
}},
|
2022-09-30 16:07:18 +01:00
|
|
|
};
|
|
|
|
|
2022-10-10 10:15:45 +01:00
|
|
|
suspend app_context.schedule(&message);
|
2022-09-30 16:07:18 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
return task.result;
|
2022-10-01 17:46:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// Attempts to read `file_access` from the its current position into `buffer`, while using
|
2022-10-10 10:15:45 +01:00
|
|
|
/// `app_context` as the execution context.
|
2022-10-03 22:32:16 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// Returns the number of bytes that were available to be read, otherwise an [Error] on failure.
|
2022-10-03 22:32:16 +01:00
|
|
|
///
|
2022-10-10 10:15:45 +01:00
|
|
|
pub fn read(file_access: *FileAccess, app_context: *AppContext, buffer: []u8) Error!usize {
|
2022-10-09 23:10:13 +01:00
|
|
|
const Task = struct {
|
|
|
|
file_access: *FileAccess,
|
|
|
|
buffer: []u8,
|
|
|
|
result: Error!usize,
|
|
|
|
|
|
|
|
const Task = @This();
|
|
|
|
|
|
|
|
fn process(data: *anyopaque) void {
|
|
|
|
const task = @ptrCast(*Task, @alignCast(@alignOf(Task), data));
|
|
|
|
|
|
|
|
ext.SDL_ClearError();
|
|
|
|
|
|
|
|
const buffer_read = ext.SDL_RWread(task.file_access.asRwOps(),
|
|
|
|
task.buffer.ptr, @sizeOf(u8), task.buffer.len);
|
|
|
|
|
|
|
|
if ((buffer_read == 0) and (ext.SDL_GetError() != null)) {
|
|
|
|
task.result = error.FileInaccessible;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
task.result = buffer_read;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var task = Task{
|
|
|
|
.file_access = file_access,
|
|
|
|
.buffer = buffer,
|
|
|
|
.result = error.FileInaccessible,
|
|
|
|
};
|
|
|
|
|
2022-10-10 10:15:45 +01:00
|
|
|
var message = AppContext.Message{
|
2022-10-03 22:32:16 +01:00
|
|
|
.frame = @frame(),
|
2022-10-09 23:10:13 +01:00
|
|
|
|
|
|
|
.kind = .{.task = .{
|
|
|
|
.data = &task,
|
|
|
|
.action = Task.process,
|
|
|
|
}},
|
2022-10-03 22:32:16 +01:00
|
|
|
};
|
|
|
|
|
2022-10-10 10:15:45 +01:00
|
|
|
suspend app_context.schedule(&message);
|
2022-10-03 22:32:16 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
return task.result;
|
2022-10-03 22:32:16 +01:00
|
|
|
}
|
|
|
|
|
2022-10-01 17:46:21 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// Attempts to seek `file_access` from the beginning of the file to `cursor` bytes while using
|
2022-10-10 10:15:45 +01:00
|
|
|
/// `app_context` as the execution context.
|
2022-10-09 23:10:13 +01:00
|
|
|
///
|
|
|
|
/// Returns [Error] on failure.
|
2022-10-01 17:46:21 +01:00
|
|
|
///
|
2022-10-10 10:15:45 +01:00
|
|
|
pub fn seek(file_access: *FileAccess, app_context: *AppContext, cursor: u64) Error!void {
|
2022-10-09 23:10:13 +01:00
|
|
|
const Task = struct {
|
|
|
|
file_access: *FileAccess,
|
|
|
|
cursor: u64,
|
|
|
|
result: Error!void,
|
|
|
|
|
|
|
|
const Task = @This();
|
|
|
|
|
|
|
|
fn process(data: *anyopaque) void {
|
|
|
|
const task = @ptrCast(*Task, @alignCast(@alignOf(Task), data));
|
|
|
|
|
|
|
|
if (task.cursor >= std.math.maxInt(i64)) {
|
|
|
|
task.result = error.OutOfRange;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ext.SDL_ClearError();
|
|
|
|
|
|
|
|
if (ext.SDL_RWseek(task.file_access.asRwOps(),
|
|
|
|
@intCast(i64, task.cursor), ext.RW_SEEK_SET) < 0) {
|
2022-10-01 17:46:21 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
task.result = error.FileInaccessible;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
task.result = {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var task = Task{
|
|
|
|
.file_access = file_access,
|
|
|
|
.cursor = cursor,
|
|
|
|
.result = error.FileInaccessible,
|
|
|
|
};
|
|
|
|
|
2022-10-10 10:15:45 +01:00
|
|
|
var message = AppContext.Message{
|
2022-10-01 17:46:21 +01:00
|
|
|
.frame = @frame(),
|
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
.kind = .{.task = .{
|
|
|
|
.data = &task,
|
|
|
|
.action = Task.process,
|
|
|
|
}},
|
2022-10-01 17:46:21 +01:00
|
|
|
};
|
|
|
|
|
2022-10-10 10:15:45 +01:00
|
|
|
suspend app_context.schedule(&message);
|
2022-10-01 17:46:21 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
return task.result;
|
2022-09-30 16:07:18 +01:00
|
|
|
}
|
2022-09-24 23:09:02 +01:00
|
|
|
|
|
|
|
///
|
2022-10-10 10:15:45 +01:00
|
|
|
/// Attempts to seek `file_access` to the end of the file while using `app_context` as the execution
|
2022-10-09 23:10:13 +01:00
|
|
|
/// context.
|
2022-09-24 23:09:02 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// Returns [Error] on failure.
|
|
|
|
///
|
2022-10-10 10:15:45 +01:00
|
|
|
pub fn seekToEnd(file_access: *FileAccess, app_context: *AppContext) Error!void {
|
2022-10-09 23:10:13 +01:00
|
|
|
const Task = struct {
|
|
|
|
file_access: *FileAccess,
|
|
|
|
result: Error!void,
|
2022-09-27 21:45:32 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
const Task = @This();
|
2022-09-27 21:45:32 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
fn process(data: *anyopaque) void {
|
|
|
|
const task = @ptrCast(*Task, @alignCast(@alignOf(Task), data));
|
2022-09-27 21:45:32 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
ext.SDL_ClearError();
|
2022-09-27 21:45:32 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
if (ext.SDL_RWseek(task.file_access.asRwOps(), 0, ext.RW_SEEK_END) < 0) {
|
|
|
|
task.result = error.FileInaccessible;
|
2022-09-24 23:09:02 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
task.result = {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var task = Task{
|
|
|
|
.file_access = file_access,
|
|
|
|
.result = error.FileInaccessible,
|
|
|
|
};
|
|
|
|
|
2022-10-10 10:15:45 +01:00
|
|
|
var message = AppContext.Message{
|
2022-10-09 23:10:13 +01:00
|
|
|
.frame = @frame(),
|
|
|
|
|
|
|
|
.kind = .{.task = .{
|
|
|
|
.data = &task,
|
|
|
|
.action = Task.process,
|
|
|
|
}},
|
|
|
|
};
|
|
|
|
|
2022-10-10 10:15:45 +01:00
|
|
|
suspend app_context.schedule(&message);
|
2022-10-09 23:10:13 +01:00
|
|
|
|
|
|
|
return task.result;
|
|
|
|
}
|
2022-09-27 21:45:32 +01:00
|
|
|
};
|
2022-09-24 23:09:02 +01:00
|
|
|
|
|
|
|
///
|
2022-10-03 22:32:16 +01:00
|
|
|
/// Platform-agnostic mechanism for working with an abstraction of the underlying file-system(s)
|
|
|
|
/// available to the application in a sandboxed environment.
|
2022-09-30 16:07:18 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
pub const FileSystem = union(enum) {
|
|
|
|
native: []const u8,
|
|
|
|
archive: []const u8,
|
2022-09-24 23:09:02 +01:00
|
|
|
|
|
|
|
///
|
2022-10-03 22:32:16 +01:00
|
|
|
/// Platform-agnostic mechanism for referencing files and directories on a [FileSystem].
|
2022-09-24 23:09:02 +01:00
|
|
|
///
|
2022-09-30 16:07:18 +01:00
|
|
|
pub const Path = struct {
|
2022-10-09 23:10:13 +01:00
|
|
|
file_system: *const FileSystem,
|
|
|
|
length: u8,
|
2022-09-30 16:07:18 +01:00
|
|
|
buffer: [max]u8,
|
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
///
|
|
|
|
/// 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 [OpenError.FileNotFound] errors.
|
|
|
|
///
|
|
|
|
/// When a given [FileSystem] does not support a specified [OpenMode],
|
|
|
|
/// [OpenError.ModeUnsupported] is used to inform the consuming code that another [OpenMode]
|
|
|
|
/// should be tried or, if no mode other is suitable, that the resource is effectively
|
|
|
|
/// unavailable.
|
|
|
|
///
|
|
|
|
pub const OpenError = error {
|
|
|
|
FileNotFound,
|
|
|
|
ModeUnsupported,
|
|
|
|
};
|
|
|
|
|
|
|
|
///
|
|
|
|
/// [OpenMode.readonly] indicates that an existing file is opened in a read-only state,
|
|
|
|
/// disallowing write access.
|
|
|
|
///
|
|
|
|
/// [OpenMode.overwrite] indicates that an empty file has been created or an existing file
|
|
|
|
/// has been completely overwritten into.
|
|
|
|
///
|
|
|
|
/// [OpenMode.append] indicates that an existing file that has been opened for reading from
|
|
|
|
/// and writing to on the end of existing data.
|
|
|
|
///
|
|
|
|
pub const OpenMode = enum {
|
|
|
|
readonly,
|
|
|
|
overwrite,
|
|
|
|
append,
|
|
|
|
};
|
|
|
|
|
2022-09-30 16:07:18 +01:00
|
|
|
///
|
|
|
|
/// 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 {
|
2022-10-09 23:10:13 +01:00
|
|
|
return (this.file_system == that.file_system) and
|
|
|
|
std.mem.eql(u8, this.buffer[0 .. this.length], that.buffer[0 .. that.length]);
|
2022-09-30 16:07:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
pub const max = 255;
|
2022-09-30 16:07:18 +01:00
|
|
|
|
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// Attempts to open the file identified by `path` with `mode` as the mode for opening the
|
2022-10-10 10:15:45 +01:00
|
|
|
/// file and `app_context` as the execution context.
|
2022-09-30 16:07:18 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// Returns a [FileAccess] reference that provides access to the file referenced by `path`
|
|
|
|
/// or a [OpenError] if it failed.
|
2022-09-30 16:07:18 +01:00
|
|
|
///
|
2022-10-10 10:15:45 +01:00
|
|
|
pub fn open(path: Path, app_context: *AppContext, mode: OpenMode) OpenError!*FileAccess {
|
2022-10-09 23:10:13 +01:00
|
|
|
const Task = struct {
|
|
|
|
path: *const FileSystem.Path,
|
2022-10-10 10:15:45 +01:00
|
|
|
app_context: *AppContext,
|
2022-10-09 23:10:13 +01:00
|
|
|
mode: OpenMode,
|
|
|
|
result: OpenError!*FileAccess,
|
|
|
|
|
|
|
|
const Task = @This();
|
|
|
|
|
|
|
|
fn process(data: *anyopaque) void {
|
|
|
|
const task = @ptrCast(*Task, @alignCast(@alignOf(Task), data));
|
|
|
|
|
|
|
|
switch (task.path.file_system.*) {
|
|
|
|
.archive => |archive| {
|
|
|
|
if (archive.len == 0) {
|
|
|
|
task.result = error.FileNotFound;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (task.mode != .readonly) {
|
|
|
|
task.result = error.ModeUnsupported;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var path_buffer = std.mem.zeroes([4096]u8);
|
|
|
|
|
|
|
|
if (archive.len >= path_buffer.len) {
|
|
|
|
task.result = error.FileNotFound;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std.mem.copy(u8, path_buffer[0 ..], archive);
|
|
|
|
|
|
|
|
ext.SDL_ClearError();
|
|
|
|
|
|
|
|
const rw_ops = ext.SDL_RWFromFile(&path_buffer, "rb") orelse {
|
|
|
|
task.result = error.FileNotFound;
|
|
|
|
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
var entry = std.mem.zeroes(oar.Entry);
|
|
|
|
const entry_buffer = std.mem.asBytes(&entry);
|
|
|
|
|
|
|
|
ext.SDL_ClearError();
|
|
|
|
|
|
|
|
if (ext.SDL_RWread(rw_ops, entry_buffer, @sizeOf(u8),
|
|
|
|
entry_buffer.len) != entry_buffer.len) {
|
|
|
|
|
|
|
|
task.result = error.FileNotFound;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std.mem.eql(u8, entry.name_buffer[0 .. entry.name_length],
|
|
|
|
task.path.buffer[0 .. task.path.length])) {
|
|
|
|
|
|
|
|
task.result = @ptrCast(*FileAccess, rw_ops);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
var to_read = math.roundUp(u64,
|
|
|
|
entry.file_size, entry_buffer.len);
|
|
|
|
|
|
|
|
while (to_read != 0) {
|
|
|
|
const read = @intCast(i64, std.math.min(
|
|
|
|
to_read, std.math.maxInt(i64)));
|
|
|
|
|
|
|
|
ext.SDL_ClearError();
|
|
|
|
|
|
|
|
if (ext.SDL_RWseek(rw_ops, read, ext.RW_SEEK_CUR) < 0) {
|
|
|
|
task.result = error.FileNotFound;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cannot be less than zero because it is derived from
|
|
|
|
// `read`.
|
|
|
|
to_read -= @intCast(u64, read);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
.native => |native| {
|
|
|
|
if (native.len == 0) {
|
|
|
|
task.result = error.FileNotFound;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var path_buffer = std.mem.zeroes([4096]u8);
|
|
|
|
const seperator = '/';
|
|
|
|
|
|
|
|
const seperator_length =
|
|
|
|
@boolToInt(native[native.len - 1] != seperator);
|
|
|
|
|
|
|
|
if ((native.len + seperator_length +
|
|
|
|
task.path.length) >= path_buffer.len) {
|
|
|
|
|
|
|
|
task.result = error.FileNotFound;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std.mem.copy(u8, path_buffer[0 ..], native);
|
|
|
|
|
|
|
|
if (seperator_length != 0)
|
|
|
|
path_buffer[native.len] = seperator;
|
|
|
|
|
|
|
|
std.mem.copy(u8, path_buffer[native.len .. path_buffer.len],
|
|
|
|
task.path.buffer[0 .. task.path.length]);
|
|
|
|
|
|
|
|
ext.SDL_ClearError();
|
|
|
|
|
|
|
|
task.result = @ptrCast(*FileAccess, ext.SDL_RWFromFile(
|
|
|
|
&path_buffer, switch (task.mode) {
|
|
|
|
.readonly => "rb",
|
|
|
|
.overwrite => "wb",
|
|
|
|
.append => "ab",
|
|
|
|
}) orelse {
|
|
|
|
|
|
|
|
task.result = error.FileNotFound;
|
|
|
|
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var task = Task{
|
|
|
|
.mode = mode,
|
|
|
|
.path = &path,
|
2022-10-10 10:15:45 +01:00
|
|
|
.app_context = app_context,
|
2022-10-09 23:10:13 +01:00
|
|
|
.result = error.FileNotFound,
|
|
|
|
};
|
|
|
|
|
2022-10-10 10:15:45 +01:00
|
|
|
var message = AppContext.Message{
|
2022-10-09 23:10:13 +01:00
|
|
|
.frame = @frame(),
|
|
|
|
|
|
|
|
.kind = .{.task = .{
|
|
|
|
.data = &task,
|
|
|
|
.action = Task.process,
|
|
|
|
}},
|
|
|
|
};
|
|
|
|
|
2022-10-10 10:15:45 +01:00
|
|
|
suspend app_context.schedule(&message);
|
2022-10-09 23:10:13 +01:00
|
|
|
|
|
|
|
return task.result;
|
2022-09-30 16:07:18 +01:00
|
|
|
}
|
2022-09-24 23:09:02 +01:00
|
|
|
};
|
|
|
|
|
2022-09-30 09:50:18 +01:00
|
|
|
///
|
2022-09-30 16:07:18 +01:00
|
|
|
/// [PathError.TooLong] occurs when creating a path that is greater than the maximum size **in
|
|
|
|
/// bytes**.
|
2022-09-30 09:50:18 +01:00
|
|
|
///
|
2022-09-30 16:07:18 +01:00
|
|
|
pub const PathError = error {
|
|
|
|
TooLong,
|
|
|
|
};
|
2022-09-24 23:09:02 +01:00
|
|
|
|
2022-09-30 09:50:18 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// Attempts to create a [Path] with `file_system` as the file-system root and the path
|
|
|
|
/// components in `sequences` as a fully qualified path from the root.
|
2022-09-30 09:50:18 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// A [Path] value is returned containing the fully qualified path from the file-system root or
|
|
|
|
/// a [PathError] if it could not be created.
|
2022-10-06 10:24:39 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
pub fn joinedPath(file_system: *const FileSystem,
|
|
|
|
sequences: []const []const u8) PathError!Path {
|
2022-10-04 23:15:59 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
var path = Path{
|
|
|
|
.file_system = file_system,
|
|
|
|
.buffer = std.mem.zeroes([Path.max]u8),
|
|
|
|
.length = 0,
|
|
|
|
};
|
2022-10-04 23:15:59 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
if (sequences.len != 0) {
|
|
|
|
const last_sequence_index = sequences.len - 1;
|
2022-10-04 23:15:59 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
for (sequences) |sequence, index| if (sequence.len != 0) {
|
|
|
|
var components = mem.Spliterator(u8){
|
|
|
|
.source = sequence,
|
|
|
|
.delimiter = "/",
|
|
|
|
};
|
2022-10-06 10:24:39 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
while (components.next()) |component| if (component.len != 0) {
|
|
|
|
for (component) |byte| {
|
|
|
|
if (path.length == Path.max) return error.TooLong;
|
2022-10-06 10:24:39 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
path.buffer[path.length] = byte;
|
|
|
|
path.length += 1;
|
|
|
|
}
|
2022-10-06 10:24:39 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
if (components.hasNext()) {
|
2022-10-04 23:15:59 +01:00
|
|
|
if (path.length == Path.max) return error.TooLong;
|
2022-09-30 09:50:18 +01:00
|
|
|
|
2022-10-04 23:15:59 +01:00
|
|
|
path.buffer[path.length] = '/';
|
|
|
|
path.length += 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
if (index < last_sequence_index) {
|
|
|
|
if (path.length == Path.max) return error.TooLong;
|
|
|
|
|
|
|
|
path.buffer[path.length] = '/';
|
|
|
|
path.length += 1;
|
|
|
|
}
|
|
|
|
};
|
2022-10-04 23:15:59 +01:00
|
|
|
}
|
2022-10-09 23:10:13 +01:00
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
2022-09-30 16:07:18 +01:00
|
|
|
};
|
2022-09-30 09:50:18 +01:00
|
|
|
|
2022-09-30 16:07:18 +01:00
|
|
|
///
|
|
|
|
///
|
|
|
|
///
|
|
|
|
pub const GraphicsContext = opaque {
|
2022-09-24 23:09:02 +01:00
|
|
|
///
|
|
|
|
///
|
|
|
|
///
|
2022-09-30 16:07:18 +01:00
|
|
|
pub const Event = struct {
|
2022-10-01 17:46:21 +01:00
|
|
|
keys_up: Keys = std.mem.zeroes(Keys),
|
|
|
|
keys_down: Keys = std.mem.zeroes(Keys),
|
|
|
|
keys_held: Keys = std.mem.zeroes(Keys),
|
2022-09-30 16:07:18 +01:00
|
|
|
|
2022-10-01 17:46:21 +01:00
|
|
|
const Keys = [256]bool;
|
2022-09-30 16:07:18 +01:00
|
|
|
};
|
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
///
|
|
|
|
///
|
|
|
|
///
|
2022-09-30 16:07:18 +01:00
|
|
|
const Implementation = struct {
|
|
|
|
event: Event,
|
|
|
|
};
|
2022-09-24 23:09:02 +01:00
|
|
|
|
2022-09-27 21:45:32 +01:00
|
|
|
///
|
2022-09-24 23:09:02 +01:00
|
|
|
///
|
|
|
|
///
|
2022-09-30 16:07:18 +01:00
|
|
|
pub fn poll(graphics_context: *GraphicsContext) ?*const Event {
|
|
|
|
_ = graphics_context;
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
///
|
|
|
|
///
|
|
|
|
pub fn present(graphics_context: *GraphicsContext) void {
|
|
|
|
// TODO: Implement;
|
|
|
|
_ = graphics_context;
|
2022-09-27 21:45:32 +01:00
|
|
|
}
|
2022-09-24 23:09:02 +01:00
|
|
|
};
|
2022-09-30 09:50:18 +01:00
|
|
|
|
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// Returns a graphics runner that uses `Errors` as its error set.
|
2022-09-30 09:50:18 +01:00
|
|
|
///
|
2022-09-30 16:07:18 +01:00
|
|
|
pub fn GraphicsRunner(comptime Errors: type) type {
|
2022-10-10 10:15:45 +01:00
|
|
|
return fn (*AppContext, *GraphicsContext) callconv(.Async) Errors!void;
|
2022-10-06 10:24:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// [Log.info] represents a log message which is purely informative and does not indicate any kind
|
|
|
|
/// of issue.
|
2022-10-06 10:24:39 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// [Log.debug] represents a log message which is purely for debugging purposes and will only occurs
|
|
|
|
/// in debug builds.
|
2022-10-06 10:24:39 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// [Log.warning] represents a log message which is a warning about a issue that does not break
|
|
|
|
/// anything important but is not ideal.
|
2022-10-06 10:24:39 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
pub const Log = enum(u32) {
|
2022-10-06 10:24:39 +01:00
|
|
|
info = ext.SDL_LOG_PRIORITY_INFO,
|
|
|
|
debug = ext.SDL_LOG_PRIORITY_DEBUG,
|
|
|
|
warning = ext.SDL_LOG_PRIORITY_WARN,
|
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
///
|
2022-10-10 10:15:45 +01:00
|
|
|
/// Writes `utf8_message` as the log kind identified by `log` with `app_context` as the execution
|
2022-10-09 23:10:13 +01:00
|
|
|
/// context.
|
|
|
|
///
|
2022-10-10 10:15:45 +01:00
|
|
|
pub fn write(log: Log, app_context: *AppContext, utf8_message: []const u8) void {
|
2022-10-09 23:10:13 +01:00
|
|
|
const Task = struct {
|
|
|
|
log: Log,
|
|
|
|
utf8_message: []const u8,
|
2022-10-06 10:24:39 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
const Task = @This();
|
2022-10-06 10:24:39 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
fn process(data: *anyopaque) void {
|
|
|
|
const task = @ptrCast(*Task, @alignCast(@alignOf(Task), data));
|
2022-10-06 10:24:39 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
ext.SDL_LogMessage(ext.SDL_LOG_CATEGORY_APPLICATION, @enumToInt(task.log),
|
|
|
|
"%.*s", task.utf8_message.len, task.utf8_message.ptr);
|
|
|
|
}
|
|
|
|
};
|
2022-10-06 10:24:39 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
var task = Task{
|
|
|
|
.log = log,
|
|
|
|
.utf8_message = utf8_message,
|
|
|
|
};
|
2022-10-06 10:24:39 +01:00
|
|
|
|
2022-10-10 10:15:45 +01:00
|
|
|
var message = AppContext.Message{
|
2022-10-09 23:10:13 +01:00
|
|
|
.frame = @frame(),
|
2022-09-30 16:07:18 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
.kind = .{.task = .{
|
|
|
|
.data = &task,
|
|
|
|
.action = Task.process,
|
|
|
|
}}
|
|
|
|
};
|
|
|
|
|
2022-10-10 10:15:45 +01:00
|
|
|
suspend app_context.schedule(&message);
|
2022-10-09 23:10:13 +01:00
|
|
|
}
|
|
|
|
};
|
2022-10-06 10:24:39 +01:00
|
|
|
|
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// [RunError.InitFailure] occurs if a necessary resource fails to be acquired or allocated.
|
2022-10-06 10:24:39 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// [RunError.AlreadyRunning] occurs if a runner has already been started.
|
2022-10-06 10:24:39 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
pub const RunError = error {
|
|
|
|
InitFailure,
|
|
|
|
AlreadyRunning,
|
|
|
|
};
|
2022-10-06 10:24:39 +01:00
|
|
|
|
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// Runs a graphical application referenced by `run` with `error` as its error set and `allocator`
|
|
|
|
/// as the underlying memory allocation strategy for its graphical runtime.
|
2022-10-06 10:24:39 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
/// Should an error from `run` occur, an `Error` is returned, otherwise a [RunError] is returned if
|
|
|
|
/// the underlying runtime fails and is logged.
|
2022-10-06 10:24:39 +01:00
|
|
|
///
|
2022-10-09 23:10:13 +01:00
|
|
|
pub fn runGraphics(comptime Error: anytype,
|
|
|
|
comptime run: GraphicsRunner(Error)) (RunError || Error)!void {
|
2022-10-07 21:25:49 +01:00
|
|
|
|
2022-09-30 16:07:18 +01:00
|
|
|
if (ext.SDL_Init(ext.SDL_INIT_EVERYTHING) != 0) {
|
2022-10-01 21:12:12 +01:00
|
|
|
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize runtime");
|
2022-09-30 16:07:18 +01:00
|
|
|
|
|
|
|
return error.InitFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
defer ext.SDL_Quit();
|
|
|
|
|
|
|
|
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 {
|
2022-10-01 21:12:12 +01:00
|
|
|
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to create window");
|
2022-09-30 16:07:18 +01:00
|
|
|
|
|
|
|
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 {
|
2022-10-01 21:12:12 +01:00
|
|
|
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to create renderer");
|
2022-09-30 16:07:18 +01:00
|
|
|
|
|
|
|
return error.InitFailure;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
defer ext.SDL_DestroyRenderer(renderer);
|
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
const user_path_prefix = ext.SDL_GetPrefPath("ona", "ona") orelse {
|
2022-10-10 10:15:45 +01:00
|
|
|
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to load user path");
|
2022-10-06 10:24:39 +01:00
|
|
|
|
2022-10-06 23:22:07 +01:00
|
|
|
return error.InitFailure;
|
|
|
|
};
|
2022-10-06 10:24:39 +01:00
|
|
|
|
2022-10-09 23:10:13 +01:00
|
|
|
defer ext.SDL_free(user_path_prefix);
|
2022-10-07 21:25:49 +01:00
|
|
|
|
2022-10-10 10:15:45 +01:00
|
|
|
var app_context = AppContext.Implementation.init("./data.oar", user_path_prefix
|
2022-10-09 23:10:13 +01:00
|
|
|
[0 .. std.mem.len(user_path_prefix)]) catch |err| {
|
2022-10-07 21:25:49 +01:00
|
|
|
|
|
|
|
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",
|
|
|
|
});
|
2022-10-01 21:12:12 +01:00
|
|
|
|
2022-10-04 13:51:46 +01:00
|
|
|
return error.InitFailure;
|
2022-09-30 16:07:18 +01:00
|
|
|
};
|
|
|
|
|
2022-10-10 10:15:45 +01:00
|
|
|
defer app_context.deinit();
|
2022-10-01 21:12:12 +01:00
|
|
|
|
2022-10-10 10:15:45 +01:00
|
|
|
app_context.start() catch |err| {
|
2022-10-09 23:10:13 +01:00
|
|
|
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, switch (err) {
|
2022-10-04 13:51:46 +01:00
|
|
|
// Not possible for it to have already been started.
|
|
|
|
error.AlreadyStarted => unreachable,
|
2022-10-09 23:10:13 +01:00
|
|
|
error.OutOfThreads => "Failed to start file-system work processor",
|
|
|
|
});
|
2022-10-01 21:12:12 +01:00
|
|
|
|
|
|
|
return error.InitFailure;
|
|
|
|
};
|
2022-10-01 20:12:58 +01:00
|
|
|
|
2022-09-30 16:07:18 +01:00
|
|
|
var graphics_context = GraphicsContext.Implementation{
|
|
|
|
.event = .{
|
|
|
|
|
|
|
|
},
|
|
|
|
};
|
2022-09-30 09:50:18 +01:00
|
|
|
|
2022-10-10 10:15:45 +01:00
|
|
|
return run(@ptrCast(*AppContext, &app_context), @ptrCast(*GraphicsContext, &graphics_context));
|
2022-09-30 09:50:18 +01:00
|
|
|
}
|