/*
文件名:BlobExClass.nut
路径:BaseClass/BaseTool/BlobExClass.nut
创建日期:2024-05-07	17:34
文件用途:拓展的Blob类
*/
class BlobEx extends blob {

    constructor(BaseBlob) {
        base.constructor(BaseBlob.len());
        writeblob(BaseBlob);
    }

    function writeblob(B) {
        base.writeblob(B);
        seek(0);
    }

    function GetUShort() {
        return readn('s');
    }

    function GetShort() {
        return readn('w');
    }

    function charPtrToInt(arr) {
        local value = ((arr[0]) << 0) |
            ((arr[1]) << 8) |
            ((arr[2]) << 16) |
            ((arr[3]) << 24);
        return value;
    }

    // function GetInt() {
    //     local CurTPos = tell();
    //     local Ret = charPtrToInt([this[CurTPos], this[CurTPos + 1], this[CurTPos + 2], this[CurTPos + 3]]);
    //     seek(4, 'c');
    //     return Ret;
    // }

    function GetInt() {
        return readn('i');
    }

    function Get256() {
        local Buf = readn('c');
        return (256.0 + Buf.tofloat()) % 256.0;
    }

    function GetFloat() {
        return readn('f');
    }

    function GetString(count) {
        return stream_myreadstring(count);
    }
    
}

class blobex extends blob {

    //-----------------Metamethods--------------------//
    function _typeof() {
        return "blobex";
    }
    //-----------------Metamethods--------------------//

    constructor(arg) {
        //通过blob构造
        if (typeof arg == "blob") {
            base.constructor(arg.len());
            writeblob(arg);
        }
        //直接构造
        else {
            base.constructor(arg);
        }
    }

    function writeblob(B) {
        base.writeblob(B);
        seek(0);
    }

    function GetUShort() {
        return readn('w');
    }

    function GetShort() {
        return readn('s');
    }

    function charPtrToInt(arr) {
        local value = ((arr[0]) << 0) |
            ((arr[1]) << 8) |
            ((arr[2]) << 16) |
            ((arr[3]) << 24);
        return value;
    }

    function GetInt() {
        local CurTPos = tell();
        local Ret = charPtrToInt([this[CurTPos], this[CurTPos + 1], this[CurTPos + 2], this[CurTPos + 3]]);
        seek(4, 'c');
        return Ret;
    }

    function Get256() {
        local Buf = readn('c');
        return (256.0 + Buf.tofloat()) % 256.0;
    }

    function GetFloat() {
        return readn('f');
    }

    function GetString(count) {
        return stream_myreadstring(count);
    }
}