diff --git a/src/asset.ml b/src/asset.ml new file mode 100644 index 0000000..e9a3980 --- /dev/null +++ b/src/asset.ml @@ -0,0 +1,25 @@ +open! Import +include (val Ohlog.sublogs logger "asset") + +exception Error of string * string + +let load_file path = + trace (fun m -> m "open text file %S" path); + let abspath = + Unix.realpath + (Printf.sprintf "assets/%s" path) + in + let fd = + try Unix.openfile abspath [O_RDONLY] 0 + with Unix.Unix_error (ENOENT, _, _) -> + raise (Error (path, "not found")) + in + let len = (Unix.fstat fd).st_size in + let buf = Bytes.create len in + trace (fun m -> m "length=%d" len); + let rec read i = + match Unix.read fd buf i (len - i) with + | 0 -> Unix.close fd; i + | n -> read (i + n) + in + Bytes.sub_string buf 0 (read 0)