~mil/mepo

17c49b40a404883cda6eb5dc852de4307acf5cbb — Miles Alan 6 months ago 7f5bb55 filedump-json
Working stringify
1 files changed, 32 insertions(+), 33 deletions(-)

M src/api/filedump.zig
M src/api/filedump.zig => src/api/filedump.zig +32 -33
@@ 41,46 41,43 @@ fn filedump(mepo: *Mepo, save_types: []const u8, filepath: []const u8) !void {
    // 1. Preferences
    if (std.mem.containsAtLeast(u8, save_types, 1, "r")) {
        for (utilprefs.prefs_mapping) |pref| {
            const format_as: enum { Text, HexText, Number } = .Number;

            const format_as: enum { Text, HexText, Number } = switch (pref.value) {
                .t => .Text,
                .b => .Number,
                .u => .Number,
                .f => .Number,
                .u24 => .HexText
            };
            if (format_as == .Text) {
                var args = std.StringArrayHashMap(std.json.Value).init(arena.allocator());
                try args.put("pref", .{ .string = pref.name });
                try args.put("value", .{ .string = if (pref.value.t) |tv| tv else "" });
                try statements.append(.{
                  .cmd = "prefset_t",
                  .args = try std.json.ArrayHashMap(std.json.Value).jsonParse(arena.allocator(), args, .{})
                  .args = try std.json.ArrayHashMap(std.json.Value).jsonParseFromValue(arena.allocator(), .{.object = args}, .{})
                });
            } else if (format_as == .Number) {
                var args = std.StringArrayHashMap(std.json.Value).init(arena.allocator());
                try args.put("pref", .{ .string = pref.name });
                try args.put("value", .{ .float = switch (pref.value) {
                    .b => @floatFromInt(@intFromBool(pref.value.b)),
                    .u => @floatFromInt(pref.value.u),
                    .f => pref.value.f,
                    else => unreachable
                }});
                try statements.append(.{
                  .cmd = "prefset_n",
                  .args = try std.json.ArrayHashMap(std.json.Value).jsonParseFromValue(arena.allocator(), .{.object = args}, .{})
                });
            } else if (format_as == .HexText) {
                var args = std.StringArrayHashMap(std.json.Value).init(arena.allocator());
                try args.put("pref", .{ .string = pref.name });
                try args.put("value", .{ .string = try std.fmt.allocPrint(arena.allocator(), "#{x}", .{pref.value.u24}) });
                try statements.append(.{
                  .cmd = "prefset_t",
                  .args = try std.json.ArrayHashMap(std.json.Value).jsonParseFromValue(arena.allocator(), .{.object = args}, .{})
                });
            //} else if (format_as == .Number) {
            //    var args = std.StringArrayHashMap(std.json.Value).init(arena.allocator());
            //    try args.put("pref", .{ .string = pref.name });
            //    try args.put("value", .{ .float = switch (pref.value) {
            //        .b => @floatFromInt(@intFromBool(pref.value.b)),
            //        .u => @floatFromInt(pref.value.u),
            //        .u24 => v: {
            //            format_as = .HexText;
            //            break :v -1;
            //        },
            //        .f => pref.value.f,
            //        .t => v: {
            //            format_as = .Text;
            //            break :v -1;
            //        },
            //    }});
            //    try statements.append(.{
            //      .cmd = "prefset_n",
            //      .args = std.json.ArrayHashMap(std.json.Value).jsonParse(arena.allocator(), args, .{})
            //    });
            //} else {
            //    var args = std.StringArrayHashMap(std.json.Value).init(arena.allocator());
            //    try args.put("pref", .{ .string = pref.name });
            //    try args.put("value", .{ .string = try std.fmt.allocPrint(arena.allocator(), "#{x}", .{pref.value.u24}) });
            //    try statements.append(.{
            //      .cmd = "prefset_t",
            //      .args = try std.json.ArrayHashMap(std.json.Value).jsonParse(arena.allocator(), args, .{})
            //    });
            }
            //}
        }
    }



@@ 209,6 206,8 @@ fn filedump(mepo: *Mepo, save_types: []const u8, filepath: []const u8) !void {
    //    ));
    //}

    const str = try std.json.stringifyAlloc(arena.allocator(), statements.items, .{});
    const str = try std.json.stringifyAlloc(arena.allocator(), statements.items, .{
        .whitespace = .indent_2,
    });
    try mepolang_file.writeAll(str);
}