소스 검색

added FileEntry readBytes and fetchBytes

Nicolas Cannasse 3 년 전
부모
커밋
6f61bfb53c
6개의 변경된 파일71개의 추가작업 그리고 0개의 파일을 삭제
  1. 17 0
      hxd/fmt/pak/FileSystem.hx
  2. 8 0
      hxd/fs/BytesFileSystem.hx
  3. 10 0
      hxd/fs/EmbedFileSystem.hx
  4. 20 0
      hxd/fs/FileEntry.hx
  5. 15 0
      hxd/fs/LocalFileSystem.hx
  6. 1 0
      hxd/fs/MultiFileSystem.hx

+ 17 - 0
hxd/fmt/pak/FileSystem.hx

@@ -98,6 +98,23 @@ private class PakEntry extends FileEntry {
 		return pak.read(file.dataSize);
 	}
 
+	override function readBytes( out : haxe.io.Bytes, outPos : Int, pos : Int, len : Int ) : Int {
+		FileSeek.seek(pak,file.dataPosition + pos, SeekBegin);
+		if( pos + len > file.dataSize )
+			len = file.dataSize - pos;
+		var tot = 0;
+		while( len > 0 ) {
+			var k = pak.readBytes(out, outPos, len);
+			if( k <= 0 ) break;
+			len -= k;
+			outPos += k;
+			tot += k;
+			fs.totalReadBytes += k;
+			fs.totalReadCount++;
+		}
+		return tot;
+	}
+
 	override function open() {
 		if( openedBytes == null )
 			openedBytes = fs.getCached(this);

+ 8 - 0
hxd/fs/BytesFileSystem.hx

@@ -22,6 +22,14 @@ class BytesFileEntry extends FileEntry {
 		return bytes;
 	}
 
+	override function readBytes( out : haxe.io.Bytes, outPos : Int, pos : Int, len : Int ) : Int {
+		if( pos + len > bytes.length )
+			len = bytes.length - pos;
+		if( len < 0 ) len = 0;
+		out.blit(outPos, bytes, pos, len);
+		return len;
+	}
+
 	override function open() {
 		pos = 0;
 	}

+ 10 - 0
hxd/fs/EmbedFileSystem.hx

@@ -38,6 +38,16 @@ private class EmbedEntry extends FileEntry {
 		#end
 	}
 
+	override function readBytes( out : haxe.io.Bytes, outPos : Int, pos : Int, len : Int ) : Int {
+		if( bytes == null )
+			open();
+		if( pos + len > bytes.length )
+			len = bytes.length - pos;
+		if( len < 0 ) len = 0;
+		out.blit(outPos, bytes, pos, len);
+		return len;
+	}
+
 	override function open() {
 		#if flash
 		if( bytes == null )

+ 20 - 0
hxd/fs/FileEntry.hx

@@ -11,6 +11,26 @@ class FileEntry {
 	public var isAvailable(get, never) : Bool;
 
 	public function getBytes() : haxe.io.Bytes return null;
+	public function readBytes( out : haxe.io.Bytes, outPos : Int, pos : Int, len : Int ) : Int { throw "readBytes() not implemented"; }
+
+
+	static var TMP_BYTES : haxe.io.Bytes = null;
+	/**
+		Similar to readBytes except :
+		a) a temporary buffer is reused, meaning a single fetchBytes must occur at a single time
+		b) it will throw an Eof exception if the data is not available
+	**/
+	public function fetchBytes( pos, len ) : haxe.io.Bytes {
+		var bytes = TMP_BYTES;
+		if( bytes == null || bytes.length < len ) {
+			var allocSize = (len + 65535) & 0xFFFF0000;
+			bytes = haxe.io.Bytes.alloc(allocSize);
+			TMP_BYTES = bytes;
+		}
+		if( readBytes(bytes,0,pos,len) < len )
+			throw new haxe.io.Eof();
+		return bytes;
+	}
 
 	public function getText() return getBytes().toString();
 

+ 15 - 0
hxd/fs/LocalFileSystem.hx

@@ -24,6 +24,21 @@ class LocalEntry extends FileEntry {
 		return sys.io.File.getBytes(file);
 	}
 
+	override function readBytes( out : haxe.io.Bytes, outPos : Int, pos : Int, len : Int ) : Int {
+		var f = sys.io.File.read(file);
+		f.seek(pos, SeekBegin);
+		var tot = 0;
+		while( len > 0 ) {
+			var k = f.readBytes(out, outPos, len);
+			if( k <= 0 ) break;
+			outPos += k;
+			tot += k;
+			len -= k;
+		}
+		f.close();
+		return tot;
+	}
+
 	override function open() {
 		if( fread != null )
 			fread.seek(0, SeekBegin);

+ 1 - 0
hxd/fs/MultiFileSystem.hx

@@ -12,6 +12,7 @@ private class MultiFileEntry extends FileEntry {
 	}
 
 	override function getBytes() : haxe.io.Bytes return el[0].getBytes();
+	override function readBytes( bytes, outPos, pos, len ) return el[0].readBytes(bytes,outPos,pos,len);
 
 	override function open() el[0].open();
 	override function skip( nbytes : Int ) el[0].skip(nbytes);