diff --git a/src/core/scheduler.zig b/src/core/scheduler.zig index 9eca1d2..6064b91 100644 --- a/src/core/scheduler.zig +++ b/src/core/scheduler.zig @@ -93,7 +93,7 @@ pub const Scheduler = struct { // invalidates the slice we're iterating over _ = self.queue.removeIndex(i); - log.debug("Removed {?}@{}", .{ event.kind, event.tick }); + // log.debug("Removed {?}@{}", .{ event.kind, event.tick }); break; } } diff --git a/src/main.zig b/src/main.zig index fa44aa3..7b3f249 100644 --- a/src/main.zig +++ b/src/main.zig @@ -70,8 +70,24 @@ pub fn main() void { config.load(allocator, cfg_file_path) catch |e| exitln("failed to load config file: {}", .{e}); - const paths = handleArguments(allocator, data_path, &result) catch |e| exitln("failed to handle cli arguments: {}", .{e}); - defer if (paths.save) |path| allocator.free(path); + var paths = handleArguments(allocator, data_path, &result) catch |e| exitln("failed to handle cli arguments: {}", .{e}); + defer paths.deinit(allocator); + + // if paths.bios is null, then we want to see if it's in the data directory + if (paths.bios == null) blk: { + const bios_path = std.mem.join(allocator, "/", &.{ data_path, "zba", "gba_bios.bin" }) catch |e| exitln("failed to allocate backup bios dir path: {}", .{e}); + defer allocator.free(bios_path); + + _ = std.fs.cwd().statFile(bios_path) catch |e| switch (e) { + error.FileNotFound => { // ZBA will crash on attempt to read BIOS but that's fine + log.err("file located at {s} was not found", .{bios_path}); + break :blk; + }, + else => exitln("error when checking \"{s}\": {}", .{ bios_path, e }), + }; + + paths.bios = allocator.dupe(u8, bios_path) catch |e| exitln("failed to duplicate path to bios: {}", .{e}); + } const log_file = switch (config.config().debug.cpu_trace) { true => std.fs.cwd().createFile("zba.log", .{}) catch |e| exitln("failed to create trace log file: {}", .{e}), @@ -153,13 +169,16 @@ pub fn main() void { } fn handleArguments(allocator: Allocator, data_path: []const u8, result: *const clap.Result(clap.Help, ¶ms, clap.parsers.default)) !FilePaths { - const rom_path = romPath(result); - log.info("ROM path: {?s}", .{rom_path}); + const rom_path = try romPath(allocator, result); + errdefer if (rom_path) |path| allocator.free(path); - const bios_path = result.args.bios; - if (bios_path) |path| log.info("BIOS path: {s}", .{path}) else log.warn("No BIOS provided", .{}); + const bios_path: ?[]const u8 = if (result.args.bios) |path| try allocator.dupe(u8, path) else null; + errdefer if (bios_path) |path| allocator.free(path); const save_path = try std.fs.path.join(allocator, &[_][]const u8{ data_path, "zba", "save" }); + + log.info("ROM path: {?s}", .{rom_path}); + log.info("BIOS path: {?s}", .{bios_path}); log.info("Save path: {s}", .{save_path}); return .{ @@ -203,10 +222,10 @@ fn ensureConfigDirExists(config_path: []const u8) !void { try dir.makePath("zba"); } -fn romPath(result: *const clap.Result(clap.Help, ¶ms, clap.parsers.default)) ?[]const u8 { +fn romPath(allocator: Allocator, result: *const clap.Result(clap.Help, ¶ms, clap.parsers.default)) !?[]const u8 { return switch (result.positionals.len) { 0 => null, - 1 => result.positionals[0], + 1 => try allocator.dupe(u8, result.positionals[0]), else => exitln("ZBA received too many positional arguments.", .{}), }; } diff --git a/src/util.zig b/src/util.zig index cd700c3..42e61ca 100644 --- a/src/util.zig +++ b/src/util.zig @@ -52,7 +52,13 @@ pub fn escape(title: [12]u8) [12]u8 { pub const FilePaths = struct { rom: ?[]const u8, bios: ?[]const u8, - save: ?[]const u8, + save: []const u8, + + pub fn deinit(self: @This(), allocator: Allocator) void { + if (self.rom) |path| allocator.free(path); + if (self.bios) |path| allocator.free(path); + allocator.free(self.save); + } }; pub const io = struct {