diff options
Diffstat (limited to 'src/host/Host.zig')
| -rw-r--r-- | src/host/Host.zig | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/src/host/Host.zig b/src/host/Host.zig index 1c9d65a..d31f4b7 100644 --- a/src/host/Host.zig +++ b/src/host/Host.zig @@ -28,8 +28,9 @@ pub const Options = struct { /// Local 9P control socket, opt-in. ninep: ?[]const u8 = null, /// State file; the sessions database goes next to it, `<name>.xitdb` - /// for `<name>.state`. - state: []const u8 = "sketch.state", + /// for `<name>.state`. Empty: `$XDG_DATA_HOME/<name>/<name>.state` + /// (`~/.local/share` without it), for a shipped game. + state: []const u8 = "", /// Playing a script: from fresh state, sessions go to memory only. script: bool = false, /// Offscreen video, silent audio, no vsync: for scripts and CI. @@ -91,6 +92,8 @@ reloads: usize = 0, /// file's inode, size and modification time): sessions' stretches carry it, /// and `play` warns on a mismatch. build: u64 = 0, +/// The state file's path when the host chose it (`Options.state`), `gpa`'s. +state_path: []const u8 = "", /// Copies of the sketch's info strings, replaced on every load. info_arena: std.heap.ArenaAllocator, name: []const u8 = "", @@ -121,7 +124,8 @@ exported: u64 = 0, dialog: Dialog = .{}, control: ?*Control = null, -pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std.process.Environ.Map, options: Options, linked: ?abi.Sketch) !void { +pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std.process.Environ.Map, given: Options, linked: ?abi.Sketch) !void { + var options = given; if (options.headless) { // Overriding: a plain hint loses to the environment, and a user's // SDL_VIDEO_DRIVER=wayland would open real windows during tests. @@ -165,6 +169,16 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std // Colors carry alpha: fills blend, as they do on the web. _ = c.SDL_SetRenderDrawBlendMode(renderer, c.SDL_BLENDMODE_BLEND); + var data_buf: [std.fs.max_path_bytes]u8 = undefined; + const state_path = if (given.state.len > 0) "" else blk: { + const data = environ.get("XDG_DATA_HOME") orelse try std.fmt.bufPrint(&data_buf, "{s}/.local/share", .{environ.get("HOME") orelse "."}); + const path = try std.fmt.allocPrint(gpa, "{s}/{s}/{s}.state", .{ data, info.name, info.name }); + errdefer gpa.free(path); + try std.Io.Dir.cwd().createDirPath(io, std.fs.path.dirname(path).?); + break :blk path; + }; + errdefer gpa.free(state_path); + if (state_path.len > 0) options.state = state_path; var state = try memory.StateFile.open(gpa, options.state); errdefer state.close(); var store_buf: [std.fs.max_path_bytes]u8 = undefined; @@ -192,6 +206,7 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std .sessions = .{ .gpa = gpa, .io = io }, .sketch = sketch, .library = library, + .state_path = state_path, .info_arena = .init(gpa), .frame_arena = .init(gpa), .input = .{ .exporting = options.export_to != null }, @@ -253,6 +268,7 @@ pub fn deinit(self: *Host) void { self.pending.deinit(self.gpa); self.frame_arena.deinit(); self.info_arena.deinit(); + self.gpa.free(self.state_path); if (self.library) |*l| l.close(); c.SDL_Quit(); } |
