/*
文件名:AvatarUseJewel.nut
路径:Dps_A/ProjectClass/AvatarUseJewel/AvatarUseJewel.nut
创建日期:2024-10-09	19:52
文件用途:时装镶嵌
*/
class AvatarUseJewel {

    function WongWork_CAvatarItemMgr_getJewelSocketData(a, b) {
        return Sq_CallFunc(S_Ptr("0x82F98F8"), "pointer", ["pointer", "int"], a, b);
    }

    function CStackableItem_getJewelTargetSocket(C_Object) {
        return Sq_CallFunc(S_Ptr("0x0822CA28"), "int", ["pointer"], C_Object);
    }

    function CInventory_delete_item(C_Object, Type, Slot, Count, Ps, Log) {
        return Sq_CallFunc(S_Ptr("0x850400C"), "int", ["pointer", "int", "int", "int", "int", "int"], C_Object, Type, Slot, Count, Ps, Log);
    }

    function api_set_JewelSocketData(jewelSocketData, slot, emblem_item_id) {
        if (jewelSocketData) {
            NativePointer(jewelSocketData).add(slot * 6 + 2).writeInt(emblem_item_id);
        }
    }

    function DB_UpdateAvatarJewelSlot_makeRequest(a, b, c) {
        return Sq_CallFunc(S_Ptr("0x843081C"), "pointer", ["int", "int", "pointer"], a, b, c);
    }

    //获取时装在数据库中的uid
    function api_get_avartar_ui_id(avartar) {
        return NativePointer(avartar).add(7).readInt();
    }

    function FixFunction() {
        Haker.LoadHook("0x8217BD6", ["int", "pointer", "pointer", "int"],
            function(args) {
                //角色
                local SUser = User(args[1]);
                //包数据
                local Pack = Packet(args[2]);

                //校验角色状态是否允许镶嵌
                if (!SUser || SUser.GetState() != 3) {
                    return;
                }
                //时装所在的背包槽
                local Inven_Slot = Pack.GetShort();
                //时装item_id
                local Item_Id = Pack.GetInt();
                //本次镶嵌徽章数量
                local Emblem_Count = Pack.GetByte();

                //获取时装道具
                local InvemObj = SUser.GetInven();
                local AvatarObj = InvemObj.GetSlot(2, Inven_Slot);

                //校验时装 数据是否合法
                if (!AvatarObj || AvatarObj.IsEmpty || (AvatarObj.GetIndex() != Item_Id) || SUser.CheckItemLock(2, Inven_Slot)) return;


                local Avartar_AddInfo = AvatarObj.GetAdd_Info();
                //获取时装管理器
                local Inven_AvartarMgr = InvemObj.GetAvatarItemMgr();

                //获取时装插槽数据
                local Jewel_Socket_Data = AvatarUseJewel.WongWork_CAvatarItemMgr_getJewelSocketData(Inven_AvartarMgr, Avartar_AddInfo);

                if (!Jewel_Socket_Data) return;

                //最多只支持3个插槽
                if (Emblem_Count <= 3) {
                    local emblems = {};

                    for (local i = 0; i< Emblem_Count; i++) {
                        //徽章所在的背包槽
                        local emblem_inven_slot = Pack.GetShort();
                        //徽章item_id
                        local emblem_item_id = Pack.GetInt();
                        //该徽章镶嵌的时装插槽id
                        local avartar_socket_slot = Pack.GetByte();

                        //获取徽章道具
                        local EmblemObje = InvemObj.GetSlot(1, emblem_inven_slot);

                        //校验徽章及插槽数据是否合法
                        if (!EmblemObje || EmblemObje.IsEmpty || (EmblemObje.GetIndex() != emblem_item_id) || (avartar_socket_slot >= 3)) return;

                        //校验徽章是否满足时装插槽颜色要求
                        //获取徽章pvf数据
                        local citem = PvfItem.GetPvfItemById(emblem_item_id);
                        if (!citem) return;

                        //校验徽章类型
                        if (!citem.IsStackable() || citem.GetItemType() != 20) return;

                        //获取徽章支持的插槽
                        local emblem_socket_type = AvatarUseJewel.CStackableItem_getJewelTargetSocket(citem.C_Object);

                        //获取要镶嵌的时装插槽类型
                        local avartar_socket_type = NativePointer(Jewel_Socket_Data).add(avartar_socket_slot * 6).readShort();

                        if (!(emblem_socket_type & avartar_socket_type)) return;

                        emblems[avartar_socket_slot] <- [emblem_inven_slot, emblem_item_id];
                    }

                    //开始镶嵌
                    foreach(avartar_socket_slot, emblemObject in emblems) {
                        //删除徽章
                        local emblem_inven_slot = emblemObject[0];
                        AvatarUseJewel.CInventory_delete_item(InvemObj.C_Object, 1, emblem_inven_slot, 1, 8, 1);
                        //设置时装插槽数据
                        local emblem_item_id = emblemObject[1];
                        AvatarUseJewel.api_set_JewelSocketData(Jewel_Socket_Data, avartar_socket_slot, emblem_item_id);
                    }

                    //时装插槽数据存档
                    AvatarUseJewel.DB_UpdateAvatarJewelSlot_makeRequest(SUser.GetCID(), AvatarUseJewel.api_get_avartar_ui_id(AvatarObj.C_Object), Jewel_Socket_Data);

                    //通知客户端时装数据已更新
                    SUser.SendUpdateItemList(1, 1, Inven_Slot);

                    //回包给客户端
                    local Pack = Packet();
                    Pack.Put_Header(1, 204);
                    Pack.Put_Int(1);
                    Pack.Finalize(true);
                    SUser.Send(Pack);
                    Pack.Delete();
                }

                return null;
            },

            function(args) {
                return 0;
            }
        );
    }

    constructor() {
        FixFunction();
    }
}