/*
文件名:装备拓展.nut
路径:MyProject/装备拓展.nut
创建日期:2026-01-15	10:02
文件用途:
*/




Cb_ItemGloballyUniqueIdentifier_t_SetServerId_Enter_Func <- {};
Cb_ItemGloballyUniqueIdentifier_t_SetServerId_Leave_Func <- {};
_Hook_Register_Currency_Func_("0x08894782", ["pointer", "int", "pointer"], Cb_ItemGloballyUniqueIdentifier_t_SetServerId_Enter_Func, Cb_ItemGloballyUniqueIdentifier_t_SetServerId_Leave_Func);

// Cb_CInventory_MakeItemPacket_Enter_Func <- {};
// Cb_CInventory_MakeItemPacket_Leave_Func <- {};
// _Hook_Register_Currency_Func_("0x084FC6BC", ["pointer", "int", "int", "pointer", "int"], Cb_CInventory_MakeItemPacket_Enter_Func, Cb_CInventory_MakeItemPacket_Leave_Func);



class EquipmentExpandC {

    MysqlObject = null;

    constructor() {
        MysqlObject = Mysql(Str_Ptr("127.0.0.1"), 3306, Str_Ptr("taiwan_cain"), Str_Ptr("game"), Str_Ptr("uu5!^%jg"));
        MysqlObject.Exec_Sql(format("SET NAMES %s", "latin1"));

        local check_table_sql = "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'equip_only_id';"
        local Ret = MysqlObject.Select(check_table_sql, ["int"]);
        if (Ret.len() == 0 || Ret[0][0] == null) {
            local sql = "CREATE TABLE equip_only_id (id INT(11) NOT NULL AUTO_INCREMENT COMMENT '32位自增唯一ID，初始值10000', PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT '纯自增ID表' AUTO_INCREMENT=10000;";
            MysqlObject.Exec_Sql(sql);
        }

        //HOOKServerId生成uuid
        Cb_ItemGloballyUniqueIdentifier_t_SetServerId_Enter_Func["_装备拓展_"] <- function(args) {
            args[1] = GenerateUUID();
            print("赋予唯一id： " + args[1]);
            return args;
        }.bindenv(this);

        // //补充数据包
        // Cb_CInventory_MakeItemPacket_Leave_Func["_装备拓展_"] <- function(args) {

        //     // local ItemObj = Item(Sq_CallFunc(S_Ptr("0x084FC1DE"), "int", ["pointer", "int", "int", "pointer"], args[0], args[1], args[2]));
        //     // print("装备编号: " + ItemObj.GetIndex());
        //     // local Pack = Packet(args[3]);
        //     // local Buffer = Memory.alloc(30);
        //     // //无需重置为0 需要写数据的地方自己会覆写

        //     // Buffer.add(18).writeInt(NativePointer())

        //     // Pack.Put_BinaryEx(DB_JewelSocketData.C_Object, 30);
        // }.bindenv(this);

        // //额外数据包,发送装备数据给本地处理
        // Cb_InterfacePacketBuf_put_packet_Leave_Func["_装备拓展_"] <- function(args) {
        //     local Inven_Item = NativePointer(args[1]);
        //     if (Inven_Item.add(1).readU8() == 1) {
        //         local Uuid = Inven_Item.add(21).readInt();
        //         print("uuid: " + Uuid);
        //         local Pack = Packet(args[0]);
        //         print("包位置: " + Pack.GetPos());
        //         // Pack.Put_BinaryEx(JewelSocketData.C_Object, 30);
        //     }
        // }.bindenv(this);
    }

    function GenerateUUID() {
        MysqlObject.Exec_Sql("INSERT INTO equip_only_id VALUES ();");
        local Ret = MysqlObject.Select("SELECT LAST_INSERT_ID() AS new_auto_id;", ["int"]);
        return Ret[0][0];
    }

    // Cb_CInventory_insertItemIntoInventory_Leave_Func["_装备拓展_"] <- function(args) {
    //     local SUser = User(NativePointer(args[0]).readPointer());
    //     local ItemObject = Item(NativePointer(Haker.CpuContext.ebp).add(12).C_Object);
    //     local ServerId = NativePointer(ItemObject.C_Object).add(21).readInt();
    //     //数据库主键从10000开始自增
    //     if (ServerId< 10000) {
    //         local NewUuid = GenerateUUID();
    //         NativePointer(ItemObject.C_Object).add(21).writeInt(NewUuid);
    //     }
    // }.bindenv(this);
}



Timer.SetTimeOut(function() {

    getroottable()._EquipmentExpand_ <- EquipmentExpandC();

    print("特殊插件·装备拓展 - 已加载");
}, 1);