chore: special case saving for ROMS without titles

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-04-27 18:08:44 -05:00
parent 417810581b
commit a87b46898b
3 changed files with 22 additions and 12 deletions

View File

@ -2,8 +2,8 @@ const std = @import("std");
const Allocator = std.mem.Allocator;
const log = std.log.scoped(.Backup);
const correctTitle = @import("../util.zig").correctTitle;
const safeTitle = @import("../util.zig").safeTitle;
const escape = @import("../util.zig").escape;
const asString = @import("../util.zig").asString;
const intToBytes = @import("../util.zig").intToBytes;
const backup_kinds = [5]Needle{
@ -76,6 +76,11 @@ pub const Backup = struct {
const file_path = try self.getSaveFilePath(path);
defer self.alloc.free(file_path);
// FIXME: Don't rely on this lol
if (std.mem.eql(u8, file_path[file_path.len - 12 .. file_path.len], "untitled.sav")) {
return log.err("ROM header lacks title, no save loaded", .{});
}
const file: std.fs.File = try std.fs.openFileAbsolute(file_path, .{});
const file_buf = try file.readToEndAlloc(self.alloc, try file.getEndPos());
defer self.alloc.free(file_buf);
@ -98,7 +103,10 @@ pub const Backup = struct {
return log.info("Loaded Save from {s}", .{file_path});
}
log.err("EEPROM can either be 0x200 bytes or 0x2000 byes, but {s} was {X:} bytes", .{ file_path, file_buf.len, });
log.err("EEPROM can either be 0x200 bytes or 0x2000 byes, but {s} was {X:} bytes", .{
file_path,
file_buf.len,
});
},
.None => return SaveError.UnsupportedBackupKind,
}
@ -112,8 +120,10 @@ pub const Backup = struct {
}
fn getSaveFilename(self: *const Self) ![]const u8 {
const title = correctTitle(safeTitle(self.title));
return try std.mem.concat(self.alloc, u8, &[_][]const u8{ title, ".sav" });
const title = asString(escape(self.title));
const name = if (title.len != 0) title else "untitled";
return try std.mem.concat(self.alloc, u8, &[_][]const u8{ name, ".sav" });
}
fn writeSaveToDisk(self: Self, path: []const u8) !void {

View File

@ -29,7 +29,7 @@ const is_binary: bool = false;
const log = std.log.scoped(.GUI);
pub const log_level = if (builtin.mode != .Debug) .info else std.log.default_level;
const correctTitle = @import("util.zig").correctTitle;
const asString = @import("util.zig").asString;
pub fn main() anyerror!void {
// Allocator for Emulator + CLI
@ -106,7 +106,7 @@ pub fn main() anyerror!void {
defer emu_thread.join();
var title_buf: [0x20]u8 = std.mem.zeroes([0x20]u8);
const window_title = try std.fmt.bufPrint(&title_buf, "ZBA | {s}", .{correctTitle(cpu.bus.pak.title)});
const window_title = try std.fmt.bufPrint(&title_buf, "ZBA | {s}", .{asString(cpu.bus.pak.title)});
const window = createWindow(window_title, gba_width, gba_height);
defer SDL.SDL_DestroyWindow(window);

View File

@ -60,7 +60,7 @@ pub fn intToBytes(comptime T: type, value: anytype) [@sizeOf(T)]u8 {
///
/// This function returns a slice of everything just before the first
/// `\0`
pub fn correctTitle(title: [12]u8) []const u8 {
pub fn asString(title: [12]u8) []const u8 {
var len = title.len;
for (title) |char, i| {
if (char == 0) {
@ -76,7 +76,7 @@ pub fn correctTitle(title: [12]u8) []const u8 {
/// array consisting of ASCII that won't make any file system angry
///
/// e.g. POKEPIN R/S to POKEPIN R_S
pub fn safeTitle(title: [12]u8) [12]u8 {
pub fn escape(title: [12]u8) [12]u8 {
var result: [12]u8 = title;
for (result) |*char| {