From 0f318e509e4051506100ae5825773d96b1457b80 Mon Sep 17 00:00:00 2001 From: KikooDX Date: Wed, 24 Feb 2021 17:58:38 +0100 Subject: [PATCH] Add versioning byte. Remove bloat goal from README.md I don't want to ever use this, but if someone want to add weird stuff to this format I think it's for the best to do this. --- README.md | 5 ++--- kbleformat.md | 9 +++++---- sample.kble | Bin 517 -> 518 bytes src/level.zig | 19 +++++++++++++------ 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 78dfd8d..3c68ed2 100644 --- a/README.md +++ b/README.md @@ -7,10 +7,9 @@ This is a work in progress program in unusable state. Subject to change. * Efficiency, simplicity and clarity are key goals. * Fully keyboard driven modal editing. Mouse support is secondary. -* GUI application made in [Zig](https://ziglang.org/) with +* GUI software made in [Zig](https://ziglang.org/) with [raylib](https://www.raylib.com/). * Configurable (format unknown). -* [LDtk](https://ldtk.io/) inspired tiling rulesets. # Build instructions Build requirements: [Zig](https://ziglang.org/download/) master branch, @@ -44,7 +43,7 @@ Verbs: File (read/write): * `e`: load level contained in `sample.kble` -* `w`: write level to `sample.kble` +* `w`: write level to `sample.kble` *See `kbleformat.md` for technical informations.* Modes: diff --git a/kbleformat.md b/kbleformat.md index f97442f..88caa21 100644 --- a/kbleformat.md +++ b/kbleformat.md @@ -4,9 +4,10 @@ Recommanded extension: `.kble` Data stored: level size, level content. Encoding: -* first byte indicates how big is a chunk of data (in bytes) -* second and third bytes indicates level width -* fourth and fifth bytes indicates level height -* 6th => (6 + data_size * width * height) contains level data +* 1st byte indicates format version (`0` currently) +* 2nd byte indicates how big is a chunk of data (in bytes) +* 3rd and 4th bytes indicates level width +* 5th and 6th bytes indicates level height +* 7th → (7 + data_size × width × height) contains level data If the encoding is incorrect, return an error or crash the program. diff --git a/sample.kble b/sample.kble index 3e6f813bafcbf268b323039abcb1c73c61cd54b5..9096459aa2422242b21b5719466406e1f02bbd9e 100644 GIT binary patch delta 9 QcmZo=X=7nz*ucsJ01CMQ%m4rY delta 21 ccmZo;X=Pz$Vh~^um@LSsJJFwOqwPsX04p2>uK)l5 diff --git a/src/level.zig b/src/level.zig index db4a92d..013d0fd 100644 --- a/src/level.zig +++ b/src/level.zig @@ -9,7 +9,6 @@ const ray = @cImport({ }); const std = @import("std"); const expect = std.testing.expect; -const assert = std.debug.assert; const Vec2 = @import("vec2.zig"); const Rect = @import("rect.zig"); @@ -72,10 +71,16 @@ pub fn init_read(allocator: *std.mem.Allocator, kble_file_path: []const u8) !Sel // Get reader. var reader: std.fs.File.Reader = file.reader(); - // Read first byte and check than the value matches the size of cell_type. + // Read first byte and check than the value matches the current format version. + { + const version = try reader.readByte(); + if (version != 0) unreachable; + } + + // Read second byte and check than the value matches the size of cell_type. { const cell_spec_byte = try reader.readByte(); - assert(cell_spec_byte == expected_bytes_per_cell); + if (cell_spec_byte != expected_bytes_per_cell) unreachable; } // Read four bytes and use them for width and height. Two bytes each. @@ -99,8 +104,8 @@ pub fn init_read(allocator: *std.mem.Allocator, kble_file_path: []const u8) !Sel }); // Non-null width and height. - assert(width > 0); - assert(height > 0); + if (width == 0) unreachable; + if (height == 0) unreachable; self = try Self.init(allocator, width, height); } @@ -130,7 +135,9 @@ pub fn write(self: Self, kble_file_path: []const u8) !void { // Get writer. var writer: std.fs.File.Writer = file.writer(); - // Write first byte, indicates cell size in bytes. + // Write first byte, kbleformat version number (hopefully will never have to increase). + try writer.writeByte(0); + // Write second byte, indicates cell size in bytes. try writer.writeByte(expected_bytes_per_cell); // Write level height and level width.