From 9a8ba59d68728d223376c8793b288d8a6d079c7b Mon Sep 17 00:00:00 2001 From: Riyyi Date: Mon, 17 Aug 2026 16:56:48 +0200 Subject: [PATCH] Store clean_path, also add to reader API --- src/chunks/read_assets.odin | 24 +++++++++++++++++------- src/chunks/reader.odin | 2 ++ src/file/file.odin | 4 ++++ src/file/list_dir.odin | 13 +++++++++---- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/chunks/read_assets.odin b/src/chunks/read_assets.odin index 6ed1d12..186729f 100644 --- a/src/chunks/read_assets.odin +++ b/src/chunks/read_assets.odin @@ -7,14 +7,23 @@ import "core:strings" // ----------------------------------------- -file_exists :: proc(r: ^Reader, path: string) -> (bool, u32) { +file_exists :: proc( + r: ^Reader, + path: string, + allocator := context.allocator, +) -> ( + found: bool, + index: u32, + error: Error, +) { + clean_path := os.clean_path(path, allocator) or_return for i in 0 ..< r.header.number_of_assets { str := string(r.indices[i].relpath[:]) relpath := strings.trim_right_null(str) - if path == relpath do return true, i + if clean_path == relpath do return true, i, nil } - return false, 0 + return false, 0, nil } // Returning `[]u8` is owned by the caller. @@ -26,11 +35,12 @@ read_file :: proc( bytes: []u8, err: Error, ) { + clean_path := os.clean_path(path, allocator) or_return // Check filesystem first, so chunks can be overriden - if os.exists(path) { + if os.exists(clean_path) { // Open file - file := os.open(path, {.Read}) or_return + file := os.open(clean_path, {.Read}) or_return defer os.close(file) // Read @@ -42,7 +52,7 @@ read_file :: proc( } // Check chunk - if found, i := file_exists(r, path); found { + if found, i := file_exists(r, clean_path) or_return; found { output_path := os.name(r.output) entry := r.indices[i] @@ -91,6 +101,6 @@ read_file :: proc( return bytes, nil } - fmt.eprintln("asset: ", path) + fmt.eprintln("asset: ", clean_path) return nil, .Asset_Not_Exist } diff --git a/src/chunks/reader.odin b/src/chunks/reader.odin index 99e7811..7d40851 100644 --- a/src/chunks/reader.odin +++ b/src/chunks/reader.odin @@ -37,6 +37,8 @@ reader_init :: proc( r: Reader, err: Error, ) { + r.output = output + output_path := os.name(output) // Chunk path diff --git a/src/file/file.odin b/src/file/file.odin index 687ed71..58224e4 100644 --- a/src/file/file.odin +++ b/src/file/file.odin @@ -1,11 +1,13 @@ package file +import "base:runtime" import "core:fmt" import "core:os" // ----------------------------------------- Error :: union #shared_nil { + runtime.Allocator_Error, os.Error, File_Error, } @@ -42,6 +44,8 @@ format_error :: proc(ferr: Error) -> string { switch e in ferr { case nil: return "" + case runtime.Allocator_Error: + return fmt.tprintf("allocator: {}", e) case os.Error: return fmt.tprintf("os: {}", e) case File_Error: diff --git a/src/file/list_dir.odin b/src/file/list_dir.odin index d01685c..c8d91e8 100644 --- a/src/file/list_dir.odin +++ b/src/file/list_dir.odin @@ -111,20 +111,25 @@ list_dir_queue :: proc( append(queue, strings.clone(entry.fullpath, queue^.allocator)) case os.File_Type.Regular: { - relpath, was_allocation := strings.replace( + relpath_dirty, was_allocation := strings.replace( entry.fullpath, working_dir, ".", 1, result^.allocator, ) - if !was_allocation { - relpath = strings.clone(relpath, result^.allocator) - } + // Remove unneeded references to the current or parent directory (./). + relpath := os.clean_path( + relpath_dirty, + result^.allocator, + ) or_return append( result, File_Entry{relpath = relpath, size = entry.size}, ) + if was_allocation { + delete(relpath_dirty, result^.allocator) + } } case: // skip other types }