Compare commits
	
		
			10 Commits
		
	
	
		
			a17d7e6d41
			...
			102eb0c1e6
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 102eb0c1e6 | |||
| dda533f557 | |||
| 29e9d3c288 | |||
| 06ee31980e | |||
| 4a6428eee2 | |||
| c9559fcb6d | |||
| a8c1fecf8b | |||
| 16e1ede50f | |||
| 4e8691ff58 | |||
| 445b53a609 | 
							
								
								
									
										5
									
								
								.github/workflows/main.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								.github/workflows/main.yml
									
									
									
									
										vendored
									
									
								
							@@ -1,14 +1,9 @@
 | 
			
		||||
name: Nightly
 | 
			
		||||
 | 
			
		||||
on:
 | 
			
		||||
  push:
 | 
			
		||||
    paths:
 | 
			
		||||
      - "**.zig"
 | 
			
		||||
    branches:
 | 
			
		||||
      - main
 | 
			
		||||
  schedule:
 | 
			
		||||
    - cron: '0 0 * * *'
 | 
			
		||||
  workflow_dispatch:
 | 
			
		||||
 | 
			
		||||
jobs:
 | 
			
		||||
  build:
 | 
			
		||||
 
 | 
			
		||||
 Submodule lib/zig-clap updated: 749c43f1f8...e5d09c4b2d
									
								
							 Submodule lib/zig-datetime updated: 932d284521...5ec1c36cf3
									
								
							 Submodule lib/zig-toml updated: be26d50091...5dfa919e03
									
								
							@@ -49,11 +49,11 @@ pub fn config() *const Config {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Reads a config file and then loads it into the global state
 | 
			
		||||
pub fn load(allocator: Allocator, file_path: []const u8) !void {
 | 
			
		||||
    var config_file = try std.fs.cwd().openFile(file_path, .{});
 | 
			
		||||
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}", .{file_path});
 | 
			
		||||
    log.info("loaded from {s}", .{config_path});
 | 
			
		||||
 | 
			
		||||
    const contents = try config_file.readToEndAlloc(allocator, try config_file.getEndPos());
 | 
			
		||||
    defer allocator.free(contents);
 | 
			
		||||
 
 | 
			
		||||
@@ -102,7 +102,7 @@ pub fn deinit(self: *Self) void {
 | 
			
		||||
    // This is so I can deallocate the original `allocator.alloc`. I have to re-make the type
 | 
			
		||||
    // since I'm not keeping it around, This is very jank and bad though
 | 
			
		||||
    // FIXME: please figure out another way
 | 
			
		||||
    self.allocator.free(@ptrCast([*]const ?*anyopaque, self.read_table[0..])[0 .. 3 * table_len]);
 | 
			
		||||
    self.allocator.free(@ptrCast([*]const ?*anyopaque, self.write_tables[0][0..])[0 .. 3 * table_len]);
 | 
			
		||||
    self.* = undefined;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										45
									
								
								src/main.zig
									
									
									
									
									
								
							
							
						
						
									
										45
									
								
								src/main.zig
									
									
									
									
									
								
							@@ -26,44 +26,33 @@ const params = clap.parseParamsComptime(
 | 
			
		||||
    \\
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
pub fn main() void {
 | 
			
		||||
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)
 | 
			
		||||
    // 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 determining the data folder: {}", .{e});
 | 
			
		||||
        const path = option orelse exitln("no valid data folder found", .{});
 | 
			
		||||
        ensureDataDirsExist(path) catch |e| exitln("failed to create folders under \"{s}\": {}", .{ path, e });
 | 
			
		||||
        const option = result catch |e| exitln("interrupted while attempting to find a data directory: {}", .{e});
 | 
			
		||||
        const path = option orelse exitln("no valid data directory could be found", .{});
 | 
			
		||||
        ensureDirectoriesExist(path) catch |e| exitln("failed to create directories under \"{s}\": {}", .{ path, e });
 | 
			
		||||
 | 
			
		||||
        break :blk path;
 | 
			
		||||
    };
 | 
			
		||||
    defer allocator.free(data_path);
 | 
			
		||||
 | 
			
		||||
    // Determine the Config Directory
 | 
			
		||||
    const config_path = blk: {
 | 
			
		||||
        const result = known_folders.getPath(allocator, .roaming_configuration);
 | 
			
		||||
        const option = result catch |e| exitln("interreupted while determining the config folder: {}", .{e});
 | 
			
		||||
        const path = option orelse exitln("no valid config folder found", .{});
 | 
			
		||||
        ensureConfigDirExists(path) catch |e| exitln("failed to create required folder \"{s}\": {}", .{ path, e });
 | 
			
		||||
 | 
			
		||||
        break :blk path;
 | 
			
		||||
    };
 | 
			
		||||
    defer allocator.free(config_path);
 | 
			
		||||
 | 
			
		||||
    // Parse CLI
 | 
			
		||||
    const result = clap.parse(clap.Help, ¶ms, clap.parsers.default, .{}) catch |e| exitln("failed to parse cli: {}", .{e});
 | 
			
		||||
    defer result.deinit();
 | 
			
		||||
 | 
			
		||||
    // TODO: Move config file to XDG Config directory?
 | 
			
		||||
    const cfg_file_path = configFilePath(allocator, config_path) catch |e| exitln("failed to ready config file for access: {}", .{e});
 | 
			
		||||
    defer allocator.free(cfg_file_path);
 | 
			
		||||
    const config_path = configFilePath(allocator, data_path) catch |e| exitln("failed to determine the config file path for ZBA: {}", .{e});
 | 
			
		||||
    defer allocator.free(config_path);
 | 
			
		||||
 | 
			
		||||
    config.load(allocator, cfg_file_path) catch |e| exitln("failed to load config file: {}", .{e});
 | 
			
		||||
    config.load(allocator, config_path) catch |e| exitln("failed to read 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);
 | 
			
		||||
@@ -110,8 +99,8 @@ pub fn handleArguments(allocator: Allocator, data_path: []const u8, result: *con
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn configFilePath(allocator: Allocator, config_path: []const u8) ![]const u8 {
 | 
			
		||||
    const path = try std.fs.path.join(allocator, &[_][]const u8{ config_path, "zba", "config.toml" });
 | 
			
		||||
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.
 | 
			
		||||
@@ -120,7 +109,7 @@ fn configFilePath(allocator: Allocator, config_path: []const u8) ![]const u8 {
 | 
			
		||||
    std.fs.accessAbsolute(path, .{}) catch |e| {
 | 
			
		||||
        if (e != error.FileNotFound) return e;
 | 
			
		||||
 | 
			
		||||
        const config_file = std.fs.createFileAbsolute(path, .{}) catch |err| exitln("failed to create \"{s}\": {}", .{ path, err });
 | 
			
		||||
        const config_file = try std.fs.createFileAbsolute(path, .{});
 | 
			
		||||
        defer config_file.close();
 | 
			
		||||
 | 
			
		||||
        try config_file.writeAll(@embedFile("../example.toml"));
 | 
			
		||||
@@ -129,21 +118,17 @@ fn configFilePath(allocator: Allocator, config_path: []const u8) ![]const u8 {
 | 
			
		||||
    return path;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn ensureDataDirsExist(data_path: []const u8) !void {
 | 
			
		||||
fn ensureDirectoriesExist(data_path: []const u8) !void {
 | 
			
		||||
    var dir = try std.fs.openDirAbsolute(data_path, .{});
 | 
			
		||||
    defer dir.close();
 | 
			
		||||
 | 
			
		||||
    // We want to make sure: %APPDATA%/zba and %APPDATA%/zba/save exist
 | 
			
		||||
    // (~/.local/share/zba/save for linux, ??? for macOS)
 | 
			
		||||
 | 
			
		||||
    // Will recursively create directories
 | 
			
		||||
    try dir.makePath("zba" ++ std.fs.path.sep_str ++ "save");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn ensureConfigDirExists(config_path: []const u8) !void {
 | 
			
		||||
    var dir = try std.fs.openDirAbsolute(config_path, .{});
 | 
			
		||||
    defer dir.close();
 | 
			
		||||
 | 
			
		||||
    try dir.makePath("zba");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn romPath(result: *const clap.Result(clap.Help, ¶ms, clap.parsers.default)) []const u8 {
 | 
			
		||||
    return switch (result.positionals.len) {
 | 
			
		||||
        1 => result.positionals[0],
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user