From 5dc2c61f1df30d2dd84fbba6d0e8895a62c3b2b5 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 16 Nov 2026 23:46:16 -0300 Subject: Export a session to a file of its own, and import one export-session [S] FILE writes session S (this run's by default, committed first), with the marks and clips that point into it, as session 0 of a new database: written beside FILE, synced, renamed over it. import FILE appends each session of such a file to this database, from "import ", its marks and clips renamed NAME@ (cut to 32 bytes). The copy is value by value, the same few lines for both, over whatever a session holds; sessions lists where one came from. test-import records test/past.script's session, exports it, imports it into another database and replays it there: the mark it carries is the State half a second in exactly, and two seconds in is frame 120's. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/host/Console.zig | 20 ++++++- src/host/Host.zig | 2 +- src/host/Sessions.zig | 149 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 166 insertions(+), 5 deletions(-) (limited to 'src/host') diff --git a/src/host/Console.zig b/src/host/Console.zig index 9f14830..75da732 100644 --- a/src/host/Console.zig +++ b/src/host/Console.zig @@ -17,6 +17,7 @@ const host_commands = [_][]const u8{ "help", "overlay memory", "overlay sound", "get", "set", "reset", "save", "load", "snapshot", "recording", "marks", "clips", "play session", "record", "stop", "play", "pause", "step", "seek start", "seek end", "diff", "quit", "exit", "overlay timing", "sessions", "timing on", "timing off", "timing frame", "timing work", "timing reset", "timing stats", + "export-session", "import", }; visible: bool = false, @@ -158,7 +159,8 @@ pub fn execute(self: *Console, host: *Host, line: []const u8, now: u64) ![]const if (eq(command, "help")) { self.log("save|load|diff [MARK] snapshot MARK marks | record|play [CLIP] recording CLIP clips | stop | pause"); - self.log("step N (frames) | seek start|end| | sessions | reset | get|set [] | overlay memory|sound | quit"); + self.log("step N (frames) | seek start|end| | sessions | play session S [at SECS] | export-session [S] FILE | import FILE"); + self.log("reset | get|set [] | overlay memory|sound | quit"); self.log("timing [on|off|frame|work|reset|stats] | overlay timing"); if (host.forward(self, line, now) == .unknown) self.log("Backtick hides the console. Tab completes."); return &.{}; @@ -290,6 +292,20 @@ pub fn execute(self: *Console, host: *Host, line: []const u8, now: u64) ![]const } else if (eq(command, "sessions")) { if (arg != null) return error.UnexpectedArgument; try self.listSessions(host); + } else if (eq(command, "export-session")) { + const s = &host.sessions; + const first = arg orelse return error.ExpectedFile; + const index = if (rest.len == 0) s.session else std.fmt.parseInt(u64, first, 10) catch return error.InvalidSession; + const path = if (rest.len == 0) first else std.mem.trim(u8, rest, " \t"); + try s.exportSession(index, path); + self.print("Exported session {d} to {s}.", .{ index, path }); + } else if (eq(command, "import")) { + const path = arg orelse return error.ExpectedFile; + if (rest.len != 0) return error.UnexpectedArgument; + const s = &host.sessions; + const first = try s.import(path); + const n = try s.count(.sessions); + self.print("Imported {s} as session {d}..{d}; marks and clips are NAME@{s}.", .{ path, first, n - 1, std.fs.path.basename(path) }); } else if (eq(command, "marks") or eq(command, "clips")) { if (arg != null) return error.UnexpectedArgument; try self.listNamed(host, eq(command, "marks")); @@ -349,7 +365,7 @@ fn listSessions(self: *Console, host: *Host) !void { const d = (m.ended -| m.started) / std.time.ns_per_s; break :blk std.fmt.bufPrint(&end_buf, "{d}:{d:0>2}", .{ d / 60, d % 60 }) catch ""; }; - self.print("{d}: {d:0>4}-{d:0>2}-{d:0>2} {d:0>2}:{d:0>2} builds {d} frames {d} {s}", .{ i, day.year, md.month.numeric(), md.day_index + 1, ds.getHoursIntoDay(), ds.getMinutesIntoHour(), m.stretches, m.frames, end }); + self.print("{d}: {d:0>4}-{d:0>2}-{d:0>2} {d:0>2}:{d:0>2} builds {d} frames {d} {s} {s}", .{ i, day.year, md.month.numeric(), md.day_index + 1, ds.getHoursIntoDay(), ds.getMinutesIntoHour(), m.stretches, m.frames, end, m.from() }); } } diff --git a/src/host/Host.zig b/src/host/Host.zig index d31f4b7..1a75b2a 100644 --- a/src/host/Host.zig +++ b/src/host/Host.zig @@ -715,7 +715,7 @@ pub fn runLine(self: *Host, line: []const u8, now: u64) !void { fn movesState(line: []const u8) bool { var words = std.mem.tokenizeAny(u8, line, " \t"); const verb = words.next() orelse return false; - const still = [_][]const u8{ "help", "overlay", "timing", "get", "snapshot", "recording", "diff", "stop", "pause", "save", "quit", "exit", "sessions", "marks", "clips" }; + const still = [_][]const u8{ "help", "overlay", "timing", "get", "snapshot", "recording", "diff", "stop", "pause", "save", "quit", "exit", "sessions", "marks", "clips", "export-session", "import" }; for (still) |w| if (std.mem.eql(u8, verb, w)) return false; return true; } diff --git a/src/host/Sessions.zig b/src/host/Sessions.zig index 4399c63..c5fc185 100644 --- a/src/host/Sessions.zig +++ b/src/host/Sessions.zig @@ -550,7 +550,20 @@ pub fn events(s: *Sessions, session: u64, from: u64, start: u64, end: u64, out: } /// Session `i`'s summary. -pub const Summary = struct { started: u64, ended: u64, frames: u64, stretches: u64, keys: u64 }; +pub const Summary = struct { + started: u64, + ended: u64, + frames: u64, + stretches: u64, + keys: u64, + /// Where it came from ("import "), cut short. + from_bytes: [48]u8 = undefined, + from_len: usize = 0, + + pub fn from(m: *const Summary) []const u8 { + return m.from_bytes[0..m.from_len]; + } +}; pub fn summary(s: *Sessions, i: u64) !Summary { switch (s.db) { @@ -558,14 +571,146 @@ pub fn summary(s: *Sessions, i: u64) !Summary { inline .memory, .file => |*db| { const R = Read(@TypeOf(db.*)); const ses = try R.session(db, i); - return .{ + var m: Summary = .{ .started = try R.uint(ses, .started), .ended = try R.uint(ses, .ended), .frames = try R.uint(ses, .frames), .stretches = if (try R.list(ses, .stretches)) |l| try l.count() else 0, .keys = if (try R.list(ses, .keys)) |l| try l.count() else 0, }; + const from = try R.bytes(ses, .from, s.gpa); + defer s.gpa.free(from); + m.from_len = @min(from.len, m.from_bytes.len); + @memcpy(m.from_bytes[0..m.from_len], from[0..m.from_len]); + return m; + }, + } +} + +// --------------------------------------------------------------------------- +// Export and import + +/// Writes session `i`, with the marks and clips in it (now of session 0), to +/// a new database at `path` (written beside it, synced, then renamed): a +/// playtest's sessions to send, which `import` reads. +pub fn exportSession(s: *Sessions, i: u64, path: []const u8) !void { + if (s.db == .off) return error.NotRecording; + if (i == s.session) try s.commit(.none); + if (i >= try s.count(.sessions)) return error.NoSuchSession; + const cwd = std.Io.Dir.cwd(); + var buf: [std.fs.max_path_bytes + 8]u8 = undefined; + const tmp = try std.fmt.bufPrint(&buf, "{s}.tmp", .{path}); + const file = try cwd.createFile(s.io, tmp, .{ .read = true, .truncate = true }); + errdefer cwd.deleteFile(s.io, tmp) catch {}; + defer file.close(s.io); + var buffer: std.Io.Writer.Allocating = .init(s.gpa); + defer buffer.deinit(); + var out = try Disk.init(s.io, .{ .file = file, .buffer = &buffer, .fsync = true }); + switch (s.db) { + .off => unreachable, + inline .memory, .file => |*db| try out.runContext(Transfer(@TypeOf(db.*), Disk){ .gpa = s.gpa, .from = db, .only = i, .label = null }), + } + try cwd.rename(tmp, cwd, path, s.io); +} + +/// Appends the sessions of the database at `path` (an `exportSession`'s) to +/// this one, their `from` "import ", with their marks and clips, +/// named `@` (cut to `max_name`). Returns the first one's +/// index. +pub fn import(s: *Sessions, path: []const u8) !u64 { + if (s.db == .off) return error.NotRecording; + const file = try std.Io.Dir.cwd().openFile(s.io, path, .{}); + defer file.close(s.io); + if (try file.length(s.io) == 0) return error.NotASessionsDatabase; + var buffer: std.Io.Writer.Allocating = .init(s.gpa); + defer buffer.deinit(); + var in = try Disk.init(s.io, .{ .file = file, .buffer = &buffer, .fsync = false }); + const first = try s.count(.sessions); + switch (s.db) { + .off => unreachable, + inline .memory, .file => |*db| try db.runContext(Transfer(Disk, @TypeOf(db.*)){ .gpa = s.gpa, .from = &in, .only = null, .label = std.fs.path.basename(path) }), + } + return first; +} + +/// A transaction appending database `from`'s sessions (`only` one, else +/// all) to the one it runs in, with the marks and clips pointing into them; +/// `label`, an import's file name, goes in their `from` and names. +fn Transfer(comptime From: type, comptime To: type) type { + return struct { + gpa: std.mem.Allocator, + from: *From, + only: ?u64, + label: ?[]const u8, + + const R = Read(From); + const Map = To.HashMap(.read_write); + const List = To.ArrayList(.read_write); + + pub fn run(ctx: @This(), cursor: *To.Cursor(.read_write)) !void { + const src = try R.root(ctx.from); + if (try R.uint(src, .version) != 1) return error.NotASessionsDatabase; + const all = try R.list(src, .sessions) orelse return error.NotASessionsDatabase; + const root = try Map.init(cursor.*); + try root.put(key(.version), .{ .uint = 1 }); + const sessions = try List.init(try root.putCursor(key(.sessions))); + const base = try sessions.count(); + var buf: [std.fs.max_path_bytes + 8]u8 = undefined; + for (0..try all.count()) |i| { + if (ctx.only) |o| if (o != i) continue; + var to = try sessions.appendCursor(); + try copy(From, To, ctx.gpa, try all.getCursor(@intCast(i)) orelse return error.CorruptSession, &to); + if (ctx.label) |l| try (try Map.init(to)).put(key(.from), .{ .bytes = try std.fmt.bufPrint(&buf, "import {s}", .{l}) }); + } + for ([_]K{ .marks, .clips }) |which| { + const entries = try R.list(src, which) orelse continue; + const list = try List.init(try root.putCursor(key(which))); + var it = try entries.iterator(); + while (try it.next()) |e| { + const session = try R.uint(try R.Map.init(e), .session); + if (ctx.only) |o| if (o != session) continue; + var to = try list.appendCursor(); + try copy(From, To, ctx.gpa, e, &to); + const item = try Map.init(to); + try item.put(key(.session), .{ .uint = base + if (ctx.only == null) session else 0 }); + const l = ctx.label orelse continue; + var name: [max_name]u8 = undefined; + const old = (try R.field(try R.Map.init(e), .name)).readBytes(&name) catch return error.CorruptSession; + const new = std.fmt.bufPrint(&buf, "{s}@{s}", .{ old, l }) catch unreachable; + try item.put(key(.name), .{ .bytes = new[0..@min(new.len, max_name)] }); + } + } + } + }; +} + +/// Copies the value at `from` (a uint, bytes, or a list or map of them) to `to`. +fn copy(comptime From: type, comptime To: type, gpa: std.mem.Allocator, from: From.Cursor(.read_only), to: *To.Cursor(.read_write)) anyerror!void { + switch ((try from.slot()).tag) { + .uint => try to.write(.{ .uint = try from.readUint() }), + .bytes, .short_bytes => { + const bytes = try from.readBytesAlloc(gpa, null); + defer gpa.free(bytes); + try to.write(.{ .bytes = bytes }); + }, + .array_list => { + const list = try To.ArrayList(.read_write).init(to.*); + var it = try from.iterator(); + while (try it.next()) |c| { + var next = try list.appendCursor(); + try copy(From, To, gpa, c, &next); + } + }, + .hash_map => { + const map = try To.HashMap(.read_write).init(to.*); + var it = try from.iterator(); + while (try it.next()) |c| { + const kv = try c.readKeyValuePair(); + var next = try map.putCursor(kv.hash); + try copy(From, To, gpa, kv.value_cursor, &next); + } }, + else => return error.CorruptSession, } } -- cgit v1.3