Something went wrong. Try again.
A puzzle game of bent arrows sent off a board of dots, written in Zig.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! The level the player has reached, kept as the number alone in a file//! named `level`: on Linux in `$XDG_STATE_HOME/pfeilstorch`//! (`~/.local/state/pfeilstorch` when that is not set), and on Android in//! the app's own internal storage.
const std = @import("std");const Io = std.Io;const rules = @import("rules");
pub const Store = struct { /// The directory, as `buf[0..len]`; empty when neither variable says /// where it is. A length rather than a slice, so that the store can be /// copied: a slice would go on pointing into the copy it came from. buf: [std.fs.max_path_bytes]u8 = undefined, len: usize = 0,
pub fn init(env: *const std.process.Environ.Map) Store { var s: Store = .{}; if (env.get("XDG_STATE_HOME")) |state| if (state.len > 0 and state[0] == '/') { s.set("{s}/pfeilstorch", state); return s; }; if (env.get("HOME")) |home| if (home.len > 0) s.set("{s}/.local/state/pfeilstorch", home); return s; }
/// The store in `path`, which is the directory itself. pub fn at(path: []const u8) Store { var s: Store = .{}; s.set("{s}", path); return s; }
fn set(s: *Store, comptime fmt: []const u8, base: []const u8) void { const written = std.fmt.bufPrint(&s.buf, fmt, .{base}) catch { s.len = 0; return; }; s.len = written.len; }
pub fn dir(s: *const Store) ?[]const u8 { return if (s.len == 0) null else s.buf[0..s.len]; }
/// The level reached, or null if none has been saved or it cannot be /// read. pub fn load(s: *const Store, io: Io) ?rules.level.Number { const d = s.dir() orelse return null; var path_buf: [std.fs.max_path_bytes]u8 = undefined; const path = std.fmt.bufPrint(&path_buf, "{s}/level", .{d}) catch return null; var buf: [32]u8 = undefined; const text = Io.Dir.cwd().readFile(io, path, &buf) catch return null; return parse(text); }
pub fn save(s: *const Store, io: Io, level: rules.level.Number) !void { const d = s.dir() orelse return error.NoStateDirectory; try Io.Dir.cwd().createDirPath(io, d); var path_buf: [std.fs.max_path_bytes]u8 = undefined; const path = try std.fmt.bufPrint(&path_buf, "{s}/level", .{d}); var text_buf: [16]u8 = undefined; const text = try std.fmt.bufPrint(&text_buf, "{d}\n", .{level}); try Io.Dir.cwd().writeFile(io, .{ .sub_path = path, .data = text }); }};
fn parse(text: []const u8) ?rules.level.Number { const n = std.fmt.parseInt(rules.level.Number, std.mem.trim(u8, text, " \t\r\n"), 10) catch return null; return if (n == 0) null else n;}
test parse { try std.testing.expectEqual(37, parse("37\n").?); try std.testing.expectEqual(null, parse("0\n")); try std.testing.expectEqual(null, parse("thirty-seven")); try std.testing.expectEqual(null, parse(""));}
test "the directory comes from the environment, and survives a copy" { var env: std.process.Environ.Map = .init(std.testing.allocator); defer env.deinit(); try env.put("HOME", "/home/someone"); const a = Store.init(&env); // Copied again, onto a stack frame of its own. const b = struct { fn copy(s: Store) Store { return s; } }.copy(a); try std.testing.expectEqualStrings("/home/someone/.local/state/pfeilstorch", b.dir().?); try env.put("XDG_STATE_HOME", "/state"); try std.testing.expectEqualStrings("/state/pfeilstorch", Store.init(&env).dir().?); // Relative is not allowed, and falls back to HOME. try env.put("XDG_STATE_HOME", "state"); try std.testing.expectEqualStrings("/home/someone/.local/state/pfeilstorch", Store.init(&env).dir().?);}
test "a level saved is the level loaded" { const io = std.testing.io; var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var s: Store = .{}; s.set(".zig-cache/tmp/{s}/pfeilstorch", &tmp.sub_path); try std.testing.expectEqual(null, s.load(io)); try s.save(io, 37); try std.testing.expectEqual(37, s.load(io).?);}
test "a store can be put in a directory of the caller's choosing" { try std.testing.expectEqualStrings("/data/user/0/app/files", Store.at("/data/user/0/app/files").dir().?);}