/*
文件名:ScriptManager.nut
路径:Core/BaseClass/ScriptManager/ScriptManager.nut
创建日期:2024-10-11	12:24
文件用途:pvf 管理器
*/
class Script {

    C_Object = null;

    constructor(Path = "/home/neople/game/Script.pvf") {
        print("正在初始化PVF...");
        local StartTime = time();

        C_Object = Asset_LoadScript(Path);

        print("PVF初始化完毕!!!");
        print("用时: " + (time() - StartTime) + "秒");
        getroottable()._Script_Data_ <- this;
    }


    function GetFileInfo(Path) {
        local size = Asset_GetPvfFileSize(C_Object, Path);
        if (size) {
            local blobobj = blobex(size);
            Asset_GetPvfFile(C_Object, Path, blobobj);
            return blobobj;
        } else return null;
    }

    function GetBinString(Key) {
        return Asset_GetPvfBinString(C_Object, Key);
    }

    function GetLoadString(Type, Key) {
        return Asset_GetPvfLoadString(C_Object, Type, Key);
    }
}


class _PVF_Data_ {
    //数据
    Data = null;
    //位置
    Pos = 0;
    //最大值
    Max = 0;

    function _typeof() {
        return "pvf_data";
    }

    constructor(gData) {
        Data = gData;
        Max = gData.len();
    }

    function Last() {
        if (Pos > 0) {
            Pos--;
            return Get();
        }
        return null;
    }

    function Seek(i) {
        if (Pos > 0 && Pos<(Max - 1)) {
            Pos = i;
        }
    }

    function Seekg(i) {
        Pos += i;
    }

    function Get() {
        local Ret = Data[Pos];
        if (Pos<(Max - 1)) {
            Pos++;
        }
        return Ret;
    }

    function Eof() {
        if (Pos == Max - 1)
            return true;
    }

    function Next() {
        if (Pos<(Max - 1)) {
            Pos++;
            return Get();
        }
        return null;
    }
}




class GlobaData {
    //动画文件Map
    Ani = null;

    constructor() {
        Ani = {};
    }

    //获取文件的IO
    function GetFile(Path) {
        return getroottable()._Script_Data_.GetFileInfo(Path);
    }

    //获取文件并处理
    function GetFileData(Path, Func) {
        local IO = GetFile(Path);
        if (IO) {
            return ResolvingData(IO, Func, Path);
        } else {
            print(Path + "找不到文件！");
            return null;
        }
    }

    //获取动画文件
    function GetAni(Path) {
        if (Path in Ani)
            return Ani[Path];
        else {
            local IO = GetFile(Path);
            if (IO) {
                Ani[Path] <- InitPvfAni(IO);
                Ani[Path].filepath <- Path;
                return Ani[Path];
            } else {
                print(Path + "找不到文件！");
                return null;
            }
        }
    }

    function ResolvingData(IO, Func, Path) {
        local DataTable = {};
        DataTable.filepath <- Path;
        local Type = Path.slice(0, Path.find("/")).tolower();
        local DataArr = [];
        local Length = IO.len();
        if (Length >= 7) {
            local i = 2;
            while (true) {
                if (i< Length && Length - i >= 5) {
                    local str = UnpackData(IO, i, Type);
                    i += 5;
                    DataArr.push(str);
                } else break;
            }
            Func(DataTable, _PVF_Data_(DataArr));
            return DataTable;
        }
        return null;
    }

    function UnpackData(IO, i, Type) {
        IO.seek(i); //内容指示位
        local currentByte = IO.readn('c'); //内容指示位
        local after = IO.GetInt();
        switch (currentByte) {
            case 9: {
                local NewcurrentByte = IO.readn('c'); //内容指示位
                local Newafter = IO.GetInt();
                local Buf = getroottable()._Script_Data_.GetBinString(Newafter);
                if (!Buf) {
                    Buf = "";
                } else {
                    Buf = getroottable()._Script_Data_.GetLoadString(Type, Buf);
                }
                return Buf;
            }
            case 10: {
                local Buf = getroottable()._Script_Data_.GetBinString(after);
                if (!Buf) {
                    Buf = "";
                } else {
                    Buf = getroottable()._Script_Data_.GetLoadString(Type, Buf);
                }
                return Buf;
            }
            case 2: {
                IO.seek(-4, 'c');
                local ret = IO.readn('i');
                return ret;
            }
            case 4: {
                local Bbuf = blob(4);
                Bbuf.writen(after, 'i');
                Bbuf.seek(0);
                local Buf = Bbuf.readn('f');
                return Buf.tofloat();
            }
            case 6:
            case 8:
            case 7:
            case 5: {
                local Buf = getroottable()._Script_Data_.GetBinString(after);
                if (!Buf) Buf = "";
                return Buf;
            }
            default:
                return "";
        }

    }

}
getroottable().ScriptData <- GlobaData();