Compare commits

..

No commits in common. "b1338800647c802f9352776fc265ebc7b4948c09" and "fc53a40b3ce3e1f3a4b5954de3ac7f4d331ba653" have entirely different histories.

2 changed files with 22 additions and 33 deletions

View File

@ -3,7 +3,6 @@ const toml = @import("toml");
const Allocator = std.mem.Allocator;
const log = std.log.scoped(.Config);
var state: Config = .{};
const Config = struct {
@ -53,8 +52,6 @@ pub fn load(allocator: Allocator, config_path: []const u8) !void {
var config_file = try std.fs.cwd().openFile(config_path, .{});
defer config_file.close();
log.info("loaded from {s}", .{config_path});
const contents = try config_file.readToEndAlloc(allocator, try config_file.getEndPos());
defer allocator.free(contents);

View File

@ -30,33 +30,29 @@ pub fn main() anyerror!void {
// Main Allocator for ZBA
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer std.debug.assert(!gpa.deinit());
const allocator = gpa.allocator();
// Determine the Data Directory (stores saves, config file, etc.)
const data_path = blk: {
const result = known_folders.getPath(allocator, .data);
const option = result catch |e| exitln("interrupted while attempting to find a data directory: {}", .{e});
break :blk option orelse exitln("no valid data directory could be found", .{});
};
// TODO: Make Error message not Linux Specific
const data_path = try known_folders.getPath(allocator, .data) orelse exit("Unable to Determine XDG Data Path", .{});
defer allocator.free(data_path);
// Parse CLI
const result = clap.parse(clap.Help, &params, clap.parsers.default, .{}) catch |e| exitln("failed to parse cli: {}", .{e});
defer result.deinit();
// TODO: Move config file to XDG Config directory?
const config_path = configFilePath(allocator, data_path) catch |e| exitln("failed to determine the config file path for ZBA: {}", .{e});
const config_path = try configFilePath(allocator, data_path);
defer allocator.free(config_path);
config.load(allocator, config_path) catch |e| exitln("failed to read config file: {}", .{e});
const save_path = try savePath(allocator, data_path);
defer allocator.free(save_path);
const paths = handleArguments(allocator, data_path, &result) catch |e| exitln("failed to handle cli arguments: {}", .{e});
try config.load(allocator, config_path);
// Handle CLI Input
const result = try clap.parse(clap.Help, &params, clap.parsers.default, .{});
defer result.deinit();
const paths = try handleArguments(allocator, data_path, &result);
defer if (paths.save) |path| allocator.free(path);
const log_file = if (config.config().debug.cpu_trace) blk: {
break :blk std.fs.cwd().createFile("zba.log", .{}) catch |e| exitln("failed to create trace log file: {}", .{e});
} else null;
const cpu_trace = config.config().debug.cpu_trace;
const log_file: ?std.fs.File = if (cpu_trace) try std.fs.cwd().createFile("zba.log", .{}) else null;
defer if (log_file) |file| file.close();
// TODO: Take Emulator Init Code out of main.zig
@ -66,7 +62,7 @@ pub fn main() anyerror!void {
var bus: Bus = undefined;
var cpu = Arm7tdmi.init(&scheduler, &bus, log_file);
bus.init(allocator, &scheduler, &cpu, paths) catch |e| exitln("failed to init zba bus: {}", .{e});
try bus.init(allocator, &scheduler, &cpu, paths);
defer bus.deinit();
if (config.config().guest.skip_bios or result.args.skip or paths.bios == null) {
@ -76,7 +72,7 @@ pub fn main() anyerror!void {
var gui = Gui.init(&bus.pak.title, &bus.apu, width, height);
defer gui.deinit();
gui.run(&cpu, &scheduler) catch |e| exitln("failed to run gui thread: {}", .{e});
try gui.run(&cpu, &scheduler);
}
pub fn handleArguments(allocator: Allocator, data_path: []const u8, result: *const clap.Result(clap.Help, &params, clap.parsers.default)) !FilePaths {
@ -84,12 +80,12 @@ pub fn handleArguments(allocator: Allocator, data_path: []const u8, result: *con
log.info("ROM path: {s}", .{rom_path});
const bios_path = result.args.bios;
if (bios_path) |path| log.info("BIOS path: {s}", .{path}) else log.warn("No BIOS provided", .{});
if (bios_path) |path| log.info("BIOS path: {s}", .{path}) else log.info("No BIOS provided", .{});
const save_path = try savePath(allocator, data_path);
log.info("Save path: {s}", .{save_path});
return .{
return FilePaths{
.rom = rom_path,
.bios = bios_path,
.save = save_path,
@ -98,16 +94,13 @@ pub fn handleArguments(allocator: Allocator, data_path: []const u8, result: *con
fn configFilePath(allocator: Allocator, data_path: []const u8) ![]const u8 {
const path = try std.fs.path.join(allocator, &[_][]const u8{ data_path, "zba", "config.toml" });
errdefer allocator.free(path);
// We try to create the file exclusively, meaning that we err out if the file already exists.
// All we care about is a file being there so we can just ignore that error in particular and
// continue down the happy pathj
std.fs.accessAbsolute(path, .{}) catch {
const file_handle = try std.fs.createFileAbsolute(path, .{});
defer file_handle.close();
// TODO: Write Default valeus to config file
file_handle.close();
};
return path;
@ -127,14 +120,13 @@ fn savePath(allocator: Allocator, data_path: []const u8) ![]const u8 {
fn romPath(result: *const clap.Result(clap.Help, &params, clap.parsers.default)) []const u8 {
return switch (result.positionals.len) {
1 => result.positionals[0],
0 => exitln("ZBA requires a path to a GamePak ROM", .{}),
else => exitln("ZBA received too many positional arguments.", .{}),
0 => exit("ZBA requires a path to a GamePak ROM\n", .{}),
else => exit("ZBA received too many positional arguments. \n", .{}),
};
}
fn exitln(comptime format: []const u8, args: anytype) noreturn {
fn exit(comptime format: []const u8, args: anytype) noreturn {
const stderr = std.io.getStdErr().writer();
stderr.print(format, args) catch {}; // Just exit already...
stderr.writeByte('\n') catch {};
std.os.exit(1);
}