Spaces To Tabs
This commit is contained in:
@@ -3,7 +3,7 @@ package funkin.backend.utils;
|
||||
/**
|
||||
* All types of utils only usable in a sorted array.
|
||||
*/
|
||||
class SortedArrayUtil {
|
||||
class SortedArrayUtil {
|
||||
/**
|
||||
* Gets the index of a possible new element of an Array of T using an efficient algorithm.
|
||||
* @param array Array of T to check in
|
||||
@@ -15,7 +15,7 @@ package funkin.backend.utils;
|
||||
if (getVal(array[0]) > val) return 0; // in case its the minimum
|
||||
if (getVal(array[array.length-1]) < val) return array.length; // in case its the maximum
|
||||
|
||||
// binary search
|
||||
// binary search
|
||||
var iMin:Int = 0;
|
||||
var iMax:Int = array.length-1;
|
||||
|
||||
@@ -25,25 +25,25 @@ package funkin.backend.utils;
|
||||
i = Math.floor((iMin + iMax) / 2);
|
||||
mid = getVal(array[i]);
|
||||
if (mid < val)
|
||||
iMin = i+1
|
||||
iMin = i+1
|
||||
else if (mid > val)
|
||||
iMax = i-1;
|
||||
else {
|
||||
iMin = i;
|
||||
break;
|
||||
}
|
||||
iMin = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return iMin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds to a sorted array, using binary search.
|
||||
* @param array Array to add to
|
||||
* @param val Value to add
|
||||
* @param getVal Function that returns the value that needs to be sorted
|
||||
*/
|
||||
public static inline function addSorted<T>(array:Array<T>, val:T, getVal:T->Float) {
|
||||
/**
|
||||
* Adds to a sorted array, using binary search.
|
||||
* @param array Array to add to
|
||||
* @param val Value to add
|
||||
* @param getVal Function that returns the value that needs to be sorted
|
||||
*/
|
||||
public static inline function addSorted<T>(array:Array<T>, val:T, getVal:T->Float) {
|
||||
if (val != null)
|
||||
array.insert(binarySearch(array, getVal(val), getVal), val);
|
||||
}
|
||||
array.insert(binarySearch(array, getVal(val), getVal), val);
|
||||
}
|
||||
}
|
||||
@@ -13,146 +13,146 @@ import haxe.zip.InflateImpl;
|
||||
* Half of the code is taken from haxe libraries btw
|
||||
*/
|
||||
class SysZip extends Reader {
|
||||
var input:Input;
|
||||
var fileInput:FileInput;
|
||||
var input:Input;
|
||||
var fileInput:FileInput;
|
||||
|
||||
public var entries:List<SysZipEntry>;
|
||||
public var entries:List<SysZipEntry>;
|
||||
|
||||
/**
|
||||
* Opens a zip from a specified path.
|
||||
* @param path Path to the zip file.
|
||||
*/
|
||||
public static function openFromFile(path:String) {
|
||||
/**
|
||||
* Opens a zip from a specified path.
|
||||
* @param path Path to the zip file.
|
||||
*/
|
||||
public static function openFromFile(path:String) {
|
||||
|
||||
return new SysZip(File.read(path, true));
|
||||
}
|
||||
return new SysZip(File.read(path, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new SysZip from a specified file input.
|
||||
* @param input File input.
|
||||
*/
|
||||
public function new(input:FileInput) {
|
||||
super(input);
|
||||
fileInput = input;
|
||||
}
|
||||
/**
|
||||
* Creates a new SysZip from a specified file input.
|
||||
* @param input File input.
|
||||
*/
|
||||
public function new(input:FileInput) {
|
||||
super(input);
|
||||
fileInput = input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads all the data present in a specified entry.
|
||||
* NOTE: If the entry is compressed, the data won't be decompressed. For decompression, use `unzipEntry`.
|
||||
* @param e Entry
|
||||
*/
|
||||
public function readEntryData(e:SysZipEntry) {
|
||||
var bytes:haxe.io.Bytes = null;
|
||||
/**
|
||||
* Reads all the data present in a specified entry.
|
||||
* NOTE: If the entry is compressed, the data won't be decompressed. For decompression, use `unzipEntry`.
|
||||
* @param e Entry
|
||||
*/
|
||||
public function readEntryData(e:SysZipEntry) {
|
||||
var bytes:haxe.io.Bytes = null;
|
||||
var buf = null;
|
||||
var tmp = null;
|
||||
|
||||
fileInput.seek(e.seekPos, SeekBegin);
|
||||
if (e.crc32 == null) {
|
||||
if (e.compressed) {
|
||||
#if neko
|
||||
// enter progressive mode : we use a different input which has
|
||||
// a temporary buffer, this is necessary since we have to uncompress
|
||||
// progressively, and after that we might have pending read data
|
||||
// that needs to be processed
|
||||
var bufSize = 65536;
|
||||
if (buf == null) {
|
||||
buf = new haxe.io.BufferInput(i, haxe.io.Bytes.alloc(bufSize));
|
||||
tmp = haxe.io.Bytes.alloc(bufSize);
|
||||
i = buf;
|
||||
}
|
||||
var out = new haxe.io.BytesBuffer();
|
||||
var z = new neko.zip.Uncompress(-15);
|
||||
z.setFlushMode(neko.zip.Flush.SYNC);
|
||||
while (true) {
|
||||
if (buf.available == 0)
|
||||
buf.refill();
|
||||
var p = bufSize - buf.available;
|
||||
if (p != buf.pos) {
|
||||
// because of lack of "srcLen" in zip api, we need to always be stuck to the buffer end
|
||||
buf.buf.blit(p, buf.buf, buf.pos, buf.available);
|
||||
buf.pos = p;
|
||||
}
|
||||
var r = z.execute(buf.buf, buf.pos, tmp, 0);
|
||||
out.addBytes(tmp, 0, r.write);
|
||||
buf.pos += r.read;
|
||||
buf.available -= r.read;
|
||||
if (r.done)
|
||||
break;
|
||||
}
|
||||
bytes = out.getBytes();
|
||||
#else
|
||||
var bufSize = 65536;
|
||||
if (tmp == null)
|
||||
tmp = haxe.io.Bytes.alloc(bufSize);
|
||||
var out = new haxe.io.BytesBuffer();
|
||||
var z = new InflateImpl(i, false, false);
|
||||
while (true) {
|
||||
var n = z.readBytes(tmp, 0, bufSize);
|
||||
out.addBytes(tmp, 0, n);
|
||||
if (n < bufSize)
|
||||
break;
|
||||
}
|
||||
bytes = out.getBytes();
|
||||
#end
|
||||
} else
|
||||
bytes = i.read(e.dataSize);
|
||||
e.crc32 = i.readInt32();
|
||||
if (e.crc32 == 0x08074b50)
|
||||
e.crc32 = i.readInt32();
|
||||
e.dataSize = i.readInt32();
|
||||
e.fileSize = i.readInt32();
|
||||
// set data to uncompressed
|
||||
e.dataSize = e.fileSize;
|
||||
e.compressed = false;
|
||||
} else
|
||||
bytes = i.read(e.dataSize);
|
||||
return bytes;
|
||||
}
|
||||
fileInput.seek(e.seekPos, SeekBegin);
|
||||
if (e.crc32 == null) {
|
||||
if (e.compressed) {
|
||||
#if neko
|
||||
// enter progressive mode : we use a different input which has
|
||||
// a temporary buffer, this is necessary since we have to uncompress
|
||||
// progressively, and after that we might have pending read data
|
||||
// that needs to be processed
|
||||
var bufSize = 65536;
|
||||
if (buf == null) {
|
||||
buf = new haxe.io.BufferInput(i, haxe.io.Bytes.alloc(bufSize));
|
||||
tmp = haxe.io.Bytes.alloc(bufSize);
|
||||
i = buf;
|
||||
}
|
||||
var out = new haxe.io.BytesBuffer();
|
||||
var z = new neko.zip.Uncompress(-15);
|
||||
z.setFlushMode(neko.zip.Flush.SYNC);
|
||||
while (true) {
|
||||
if (buf.available == 0)
|
||||
buf.refill();
|
||||
var p = bufSize - buf.available;
|
||||
if (p != buf.pos) {
|
||||
// because of lack of "srcLen" in zip api, we need to always be stuck to the buffer end
|
||||
buf.buf.blit(p, buf.buf, buf.pos, buf.available);
|
||||
buf.pos = p;
|
||||
}
|
||||
var r = z.execute(buf.buf, buf.pos, tmp, 0);
|
||||
out.addBytes(tmp, 0, r.write);
|
||||
buf.pos += r.read;
|
||||
buf.available -= r.read;
|
||||
if (r.done)
|
||||
break;
|
||||
}
|
||||
bytes = out.getBytes();
|
||||
#else
|
||||
var bufSize = 65536;
|
||||
if (tmp == null)
|
||||
tmp = haxe.io.Bytes.alloc(bufSize);
|
||||
var out = new haxe.io.BytesBuffer();
|
||||
var z = new InflateImpl(i, false, false);
|
||||
while (true) {
|
||||
var n = z.readBytes(tmp, 0, bufSize);
|
||||
out.addBytes(tmp, 0, n);
|
||||
if (n < bufSize)
|
||||
break;
|
||||
}
|
||||
bytes = out.getBytes();
|
||||
#end
|
||||
} else
|
||||
bytes = i.read(e.dataSize);
|
||||
e.crc32 = i.readInt32();
|
||||
if (e.crc32 == 0x08074b50)
|
||||
e.crc32 = i.readInt32();
|
||||
e.dataSize = i.readInt32();
|
||||
e.fileSize = i.readInt32();
|
||||
// set data to uncompressed
|
||||
e.dataSize = e.fileSize;
|
||||
e.compressed = false;
|
||||
} else
|
||||
bytes = i.read(e.dataSize);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unzips and returns all of the data present in an entry.
|
||||
* @param f Entry to read from.
|
||||
*/
|
||||
public function unzipEntry(f:SysZipEntry) {
|
||||
var data = readEntryData(f);
|
||||
/**
|
||||
* Unzips and returns all of the data present in an entry.
|
||||
* @param f Entry to read from.
|
||||
*/
|
||||
public function unzipEntry(f:SysZipEntry) {
|
||||
var data = readEntryData(f);
|
||||
|
||||
if (!f.compressed)
|
||||
return data;
|
||||
var c = new haxe.zip.Uncompress(-15);
|
||||
var s = haxe.io.Bytes.alloc(f.fileSize);
|
||||
var r = c.execute(data, 0, s, 0);
|
||||
c.close();
|
||||
if (!r.done || r.read != data.length || r.write != f.fileSize)
|
||||
throw "Invalid compressed data for " + f.fileName;
|
||||
data = s;
|
||||
return data;
|
||||
}
|
||||
if (!f.compressed)
|
||||
return data;
|
||||
var c = new haxe.zip.Uncompress(-15);
|
||||
var s = haxe.io.Bytes.alloc(f.fileSize);
|
||||
var r = c.execute(data, 0, s, 0);
|
||||
c.close();
|
||||
if (!r.done || r.read != data.length || r.write != f.fileSize)
|
||||
throw "Invalid compressed data for " + f.fileName;
|
||||
data = s;
|
||||
return data;
|
||||
}
|
||||
|
||||
public override function read():List<Entry> {
|
||||
if (entries != null)
|
||||
return entries;
|
||||
entries = new List();
|
||||
public override function read():List<Entry> {
|
||||
if (entries != null)
|
||||
return entries;
|
||||
entries = new List();
|
||||
while (true) {
|
||||
var e = readEntryHeader();
|
||||
if (e == null)
|
||||
break;
|
||||
|
||||
var zipEntry:SysZipEntry = cast e;
|
||||
zipEntry.seekPos = fileInput.tell();
|
||||
entries.add(zipEntry);
|
||||
fileInput.seek(e.dataSize, SeekCur);
|
||||
var zipEntry:SysZipEntry = cast e;
|
||||
zipEntry.seekPos = fileInput.tell();
|
||||
entries.add(zipEntry);
|
||||
fileInput.seek(e.dataSize, SeekCur);
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
}
|
||||
|
||||
public function dispose() {
|
||||
if (input != null)
|
||||
input.close();
|
||||
}
|
||||
public function dispose() {
|
||||
if (input != null)
|
||||
input.close();
|
||||
}
|
||||
}
|
||||
|
||||
typedef SysZipEntry = {
|
||||
> Entry,
|
||||
var seekPos:Int;
|
||||
> Entry,
|
||||
var seekPos:Int;
|
||||
}
|
||||
#end
|
||||
Reference in New Issue
Block a user