//初始化定时器
_Timer_Object <- Timer();

//初始化连接池
Cb_MySQL_Open_Enter_Func.info <- function(args) {
    local PoolObj = MysqlPool.GetInstance();
    local IP = NativePointer(args[1]).readUtf8String();
    local Port = args[2];
    local Username = NativePointer(args[4]).readUtf8String();
    local Password = NativePointer(args[5]).readUtf8String();
    PoolObj.SetBaseConfiguration(IP, Port, Username, Password);
    //连接池大小
    PoolObj.PoolSize = 10;
    //初始化
    PoolObj.Init();
}

function sqr_main() {
    if (getroottable().rawin("DP_S_VERSION") && DP_S_VERSION >= 25.332) {
        // 定义 ANSI 颜色代码
        local COLOR_RESET = "\x1b[0m";
        local COLOR_MAGENTA = "\x1b[95m";
        local COLOR_CYAN = "\x1b[96m";
        local COLOR_YELLOW = "\x1b[93m";

        // 自定义字符串重复函数
        function repeatString(str, times) {
            local result = "";
            for (local i = 0; i< times; i++) {
                result += str;
            }
            return result;
        }

        // 获取版本号
        local Version = getroottable().rawin("DP_S_VERSION") ? DP_S_VERSION : 1.0;
        local VersionStr = format("v%.3f", Version);

        // 动态计算边框宽度（不包含颜色代码的纯文本长度）
        local baseText = "🚀 DP-S 插件 " + VersionStr + " 🚀";
        local textLength = baseText.len(); // 实际显示文本长度
        local borderWidth = textLength + 4; // 左右各留2个空格

        // 构建动态边框
        local borderTop = COLOR_CYAN + "╔" + repeatString("═", borderWidth) + "╗" + COLOR_RESET;
        local middleLine = format(
            "%s║     %s%s%s     ║%s",
            COLOR_CYAN,
            COLOR_MAGENTA,
            baseText,
            COLOR_CYAN,
            COLOR_RESET
        );
        local borderBottom = COLOR_CYAN + "╚" + repeatString("═", borderWidth) + "╝" + COLOR_RESET;

        // 组合横幅
        local banner = format(
            "\n\t\t\t    %s\n\t\t\t    %s\n\t\t\t    %s\n%s\n%s",
            borderTop,
            middleLine,
            borderBottom,
            format("\t\t\t    %s🌟 欢迎使用 DP-S 插件! 永久免费,一键安装.请勿上当受骗. %s\n", COLOR_YELLOW, COLOR_RESET),
            format("\t\t\t    %s🌟 QQ群号: 850022626 ! 联系作者QQ: 947330670 %s\n", COLOR_YELLOW, COLOR_RESET)
        );

        // 输出到控制台
        print(banner);

        dofile("/dp_s/Main.nut");

        //尝试调用用户自定义脚本
        _Hook_Register_Currency_DelayHook_();
        main();
        sqr_main();
        _GlobalConfig();
        _Official_Project_();
    } else {
        dofile("/dp_s/Main.nut");
        sqr_main();
    }
}



if (getroottable().rawin("DP_S_VERSION") && DP_S_VERSION >= 25.329 && DP_S_VERSION< 25.332) {
    function _Rindro_OldLogic_Enter(CUser, PacketId, PacketSrc) {

        //区域移动
        if (PacketId == 38) {
            local Ret = Cb_move_area(CUser, NativePointer(PacketSrc).add(0x0D).readU8(), NativePointer(PacketSrc).add(0x0E).readU8());
            if (!Ret) {
                return -1;
            }
        }
        //普通输入
        else if (PacketId == 17) {
            Timer.SetTimeOut(function() {
                local Size = NativePointer(PacketSrc).add(20).readInt();
                local Str = NativePointer(PacketSrc).add(24).readUtf8String(Size);
                Cb_base_input(CUser, Str);
            }, 1);
        }
        //GM输入
        else if (PacketId == 179) {
            Timer.SetTimeOut(function() {
                local Size = NativePointer(PacketSrc).add(13).readInt();
                local Str = NativePointer(PacketSrc).add(17).readUtf8String(Size);
                Cb_gm_input(CUser, Str.slice(2));
            }, 1);
        }
        //特殊道具使用
        else if (PacketId == 255) {
            Timer.SetTimeOut(function() {
                Cb_use_item_sp(CUser, NativePointer(PacketSrc).add(0x0D).readInt());
            }, 1);
        }
        //返回选择角色
        else if (PacketId == 7) {
            Timer.SetTimeOut(function() {
                Cb_return_select_character(CUser);
            }, 1);
        }
        //组队 创建和加入
        else if (PacketId == 10) {
            local Ret = Cb_userpartycreate(CUser);
            if (!Ret) {
                return -1;
            }
        }
        //下线
        else if (PacketId == 3) {
            Timer.SetTimeOut(function() {
                Cb_player_exit(CUser);
            }, 1);
        }
        // //查看信息
        // else if (PacketId == 8) {
        //     Cb_see_information(CUser, PacketSrc);
        // }
        //自定义包
        else if (PacketId == 130) {
            local Size = NativePointer(PacketSrc).add(13).readInt();
            local Str = NativePointer(PacketSrc).add(17).readUtf8String(Size);
            Timer.SetTimeOut(function(CUser, Str) {
                OnClientSocketMsg(CUser, Str);
            }, 1, CUser, Str);
        }
        return 1;
    }

    function _Rindro_OldLogic_Leave(CUser, PacketId, PacketSrc) {
        //更换装备
        if (PacketId == 19) {
            Cb_player_chanage_equ(CUser);
        }
        //组队 同意组队和同意加入
        else if (PacketId == 11) {
            Cb_userpartyagree(CUser);
        }
        //组队 退出队伍
        else if (PacketId == 13) {
            Cb_userpartyexit(CUser);
        }
        //组队 踢出队伍
        else if (PacketId == 14) {
            Cb_userpartykick(CUser);
        }
        //组队 委任队长
        else if (PacketId == 124) {
            Cb_userpartygivemaster(CUser);
        }
        return null;
    };

    Haker.LoadHook("0x08594E52", ["pointer", "int", "pointer", "int", "int"],
        function(args) {
            local CallArr = [];
            local v4 = Sq_CallFunc(S_Ptr("0x080CC18E"), "pointer", []);
            local cuser = Sq_CallFunc(S_Ptr("0x082947A4"), "pointer", ["pointer", "int"], v4, args[1]);
            if (!cuser) return;
            local PacketId = NativePointer(args[2]).add(1).readShort();
            local Ret = _Rindro_OldLogic_Enter(cuser, PacketId, args[2]);
            if (Ret == -1) {
                NativePointer(args[2]).add(1).writeShort(-1);
            }
            return null;
        },
        function(args) {
            local CallArr = [];
            local v4 = Sq_CallFunc(S_Ptr("0x080CC18E"), "pointer", []);
            local cuser = Sq_CallFunc(S_Ptr("0x082947A4"), "pointer", ["pointer", "int"], v4, args[1]);
            if (!cuser) return;
            local PacketId = NativePointer(args[2]).add(1).readShort();
            Timer.SetTimeOut(function() {
                _Rindro_OldLogic_Leave(cuser, PacketId, args[2]);
            }, 1);
            return null;
        }
    );
}

Haker.LoadHook("0x081E8C78", ["pointer", "pointer", "pointer", "int"],
    function(args) {
        local SUser = User(args[1]);
        local SNU = Memory.allocUtf8String(Dps_Self_Ip);
        SUser.SendBlob(SNU);
        return null;
    },
    function(args) {
        return null;
    });
