diff --git a/game/assets/assets.odin b/game/assets/assets.odin index 5840084..0ca2e8d 100644 --- a/game/assets/assets.odin +++ b/game/assets/assets.odin @@ -7,12 +7,12 @@ import "core:io" import "core:log" import "core:math" import lg "core:math/linalg" -import "core:os/os2" import "core:strconv" import "game:debug" import "game:halfedge" import "game:physics/bvh" import "game:physics/collision" +import "libs:physfs" import "libs:tracy" import rl "vendor:raylib" import "vendor:raylib/rlgl" @@ -188,9 +188,9 @@ get_bvh :: proc(assetman: ^Asset_Manager, path: cstring) -> Loaded_BVH { // TODO: cache result // Reads a two column comma separated csv file as a curve get_curve_2d :: proc(assetman: ^Asset_Manager, path: string) -> (curve: Loaded_Curve_2D) { - data, err := os2.read_entire_file_from_path(path, context.temp_allocator) + data, err := physfs.read_entire_file(path, context.temp_allocator) if err != nil { - log.errorf("Failed to read curve: %s", path) + log.errorf("Failed to read curve: %s, %v", path, err) return } @@ -250,7 +250,7 @@ get_curve_2d :: proc(assetman: ^Asset_Manager, path: string) -> (curve: Loaded_C } get_convex :: proc(assetman: ^Asset_Manager, path: cstring) -> (result: Loaded_Convex) { - bytes, err := os2.read_entire_file(string(path), context.temp_allocator) + bytes, err := physfs.read_entire_file(string(path), context.temp_allocator) if err != nil { log.errorf("error reading file %v %s", err) return diff --git a/libs/physfs/helpers.odin b/libs/physfs/helpers.odin new file mode 100644 index 0000000..5746ceb --- /dev/null +++ b/libs/physfs/helpers.odin @@ -0,0 +1,36 @@ +package physfs + +import "core:strings" + +read_entire_file :: proc( + path: string, + allocator := context.allocator, +) -> ( + data: []byte, + err: ErrorCode, +) { + pathc := strings.clone_to_cstring(path, context.temp_allocator) + file := openRead(pathc) + if file == nil { + return nil, getLastErrorCode() + } + defer close(file) + + size := fileLength(file) + if size == -1 { + return nil, getLastErrorCode() + } + + if size == 0 { + return nil, .OK + } + + data = make([]byte, int(size), allocator) + bytes_read := readBytes(file, raw_data(data), u64(size)) + if bytes_read != size { + delete_slice(data, allocator) + return nil, getLastErrorCode() + } + + return data, .OK +}