From 9ccfeaa2b7fb23e111e0a4c245c6961fd8804556 Mon Sep 17 00:00:00 2001 From: Yuxiao Mao Date: Tue, 14 Jan 2025 12:19:34 +0100 Subject: [PATCH] [hlmem] Simplify FileReader js with BytesInput --- other/haxelib/hlmem/FileReader.hx | 47 ++++++++----------------------- other/haxelib/hlmem/Memory.hx | 2 +- 2 files changed, 12 insertions(+), 37 deletions(-) diff --git a/other/haxelib/hlmem/FileReader.hx b/other/haxelib/hlmem/FileReader.hx index 4d15886f6..0a940d63f 100644 --- a/other/haxelib/hlmem/FileReader.hx +++ b/other/haxelib/hlmem/FileReader.hx @@ -14,71 +14,46 @@ class FileReader { #if js // js read file in once for better performance - var memBytes : haxe.io.Bytes; - var memPos : Int; + var memInput : haxe.io.BytesInput; #else var memInput : sys.io.FileInput; #end public inline function new(file : String) { #if js - memBytes = sys.io.File.getBytes(file); - memPos = 0; + memInput = new haxe.io.BytesInput(sys.io.File.getBytes(file)); #else memInput = sys.io.File.read(file); #end } public inline function close() { - #if js - memBytes = null; - memPos = 0; - #else if( memInput != null ) memInput.close(); memInput = null; - #end } public inline function readString(length : Int) : String { - #if js - var str = memBytes.getString(memPos, 3); - memPos += 3; - #else - var str = memInput.read(3).toString(); - #end - return str; + return memInput.readString(length); } public inline function readByte() : Int { - #if js - var b = memBytes.get(memPos); - memPos += 1; - #else - var b = memInput.readByte(); - #end - return b; + return memInput.readByte(); } public inline function readInt32() : Int { - #if js - var i = memBytes.getInt32(memPos); - memPos += 4; - #else - var i = memInput.readInt32(); - #end - return i; + return memInput.readInt32(); } - public inline function readPointer( is64 : Bool ) : Block.Pointer { + public inline function readPointer( is64 : Bool ) : haxe.Int64 { var low = readInt32(); var high = is64 ? readInt32() : 0; - return cast haxe.Int64.make(high,low); + return haxe.Int64.make(high,low); } public inline function tell() : Float { #if js - return memPos; + return memInput.position; #else return tell2(@:privateAccess memInput.__f); #end @@ -96,11 +71,11 @@ class FileReader { var dpos = Std.int(pos); switch( mode ) { case SeekBegin: - memPos = dpos; + memInput.position = dpos; case SeekCur: - memPos += dpos; + memInput.position += dpos; case SeekEnd: - memPos = memBytes.length + dpos; + memInput.position = memInput.length + dpos; } #else if( !seek2(@:privateAccess memInput.__f, pos, mode.getIndex()) ) diff --git a/other/haxelib/hlmem/Memory.hx b/other/haxelib/hlmem/Memory.hx index 49647d744..729977962 100644 --- a/other/haxelib/hlmem/Memory.hx +++ b/other/haxelib/hlmem/Memory.hx @@ -118,7 +118,7 @@ class Memory { } inline function readPointer() : Pointer { - return memFileReader.readPointer(is64); + return cast memFileReader.readPointer(is64); } function goto( b : Block ) {