diff --git a/README.md b/README.md index 81d5f7f..20d2338 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,12 @@ Modes: * Shift + right click: rectangle unselection. * Left or right click while un·selecting: end selection. +# Backups +*Can be disabled in the configuration.* +Safety backups are made when: +* Reading operation is started (`backup_read.kble`) +* The program exit (`backup_exit.kble`) + # License Copyright (c) 2021 KikooDX diff --git a/src/conf.zig b/src/conf.zig index 14613ce..b70e1e0 100644 --- a/src/conf.zig +++ b/src/conf.zig @@ -26,6 +26,10 @@ pub const mouse_right_btn: c_int = 1; // * true: click to enter, draw, release to exit. pub const mouse_graphic_tablet: bool = false; +// The editor will save the level before reading, exiting and other +// dangerous operations to avoid unexpected data loss. +pub const safety_backup = true; + pub const default_grid_size = .{ .width = 16, .height = 16, diff --git a/src/level.zig b/src/level.zig index 53ce378..733bcd6 100644 --- a/src/level.zig +++ b/src/level.zig @@ -169,6 +169,9 @@ pub fn write(self: Self, kble_file_path: [*:0]const u8) !void { /// Wrapper around `init_read`. pub fn action_read(self: *Self, allocator: *std.mem.Allocator, kble_file_path: [*:0]const u8) void { + // Safety backup before reading. + if (conf.safety_backup) + self.action_write(allocator, "backup_read.kble"); self.deinit(allocator); self.* = Self.init_read(allocator, kble_file_path) catch unreachable; } diff --git a/src/main.zig b/src/main.zig index f41f589..27e170c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -272,6 +272,9 @@ pub fn main() void { mouse.draw(scale, camera); //ray.DrawFPS(0, 0); } + // Safety backup before exiting. + if (conf.safety_backup) + level.write("backup_exit.kble") catch unreachable; } fn add_key_to_buffer(input_buffer: *[input_buffer_len]u32, input_cursor: *u8, key: c_int) void {