// 定义修复老技能槽信息的函数
function FixOldSkillSlotInfo() {
    Cb_reform_ui_group_no_Leave_Func["月光修复老技能"] <- function(args) {
        local skillClassValue = NativePointer(args[1]).sub(0x60).readInt();
        if (skillClassValue == 197) {
            NativePointer(args[1]).writeInt(4);
        }
        return 0;
    }

    Cb_get_skillslot_group_Leave_Func["月光修复老技能"] <- function(args) {
        local slot = args[1];
        if (slot< 8 || slot >= 198) return -1;
        else if (slot >= 160) return 4;
        else if (slot >= 122) return 3;
        else if (slot >= 84) return 2;
        else if (slot >= 46) return 1;
        else return 0;
    }

    Cb_get_skillslot_no_Leave_Func["月光修复老技能"] <- function(args) {
        local thisP = args[0];
        local skillId = args[1];
        local group = args[2];
        local slot = args[3];
        local is_active_skill = args[4];

        if (!thisP || !NativePointer(thisP).readPointer()) {
            return -1;
        }

        local buf = Sq_CallFunc(S_Ptr("0x86067DE"), "pointer", ["pointer", "int"], thisP, slot);
        if (!buf) {

            return -1;
        }

        if (is_active_skill && Sq_CallFunc(S_Ptr("0x8608D58"), "byte", ["pointer", "int"], thisP, skillId)) {
            local end = skillId ? 7 : 5;
            for (local i = 0; i <= end; ++i) {
                if (NativePointer(buf).add(2 * i).readU8() == skillId) {
                    return i;
                }
            }
            for (local i = 198; i <= 203; ++i) {
                if (NativePointer(buf).add(2 * i).readU8() == skillId) {
                    return i;
                }
            }
        }
        local pos = 8 + group * 38;
        for (local i = 0; i< 38; ++i) {
            if (NativePointer(buf).add(2 * (pos + i)).readU8() == skillId) {
                return pos + i;
            }
        }
        return -1;
    }

    Cb_get_skillslot_no2_Leave_Func["月光修复老技能"] <- function(args) {
        local thisP = args[0];
        local buf = args[1];
        local skillId = args[2];
        local group = args[3];
        local is_active_skill = args[4];

        if (!thisP || !NativePointer(thisP).readPointer()) {
            return -1;
        }

        if (is_active_skill) {
            local end = skillId ? 7 : 5;
            for (local i = 0; i <= end; ++i) {
                if (NativePointer(buf).add(2 * i).readU8() == skillId) {
                    return i;
                }
            }
            for (local i = 198; i <= 203; ++i) {
                if (NativePointer(buf).add(2 * i).readU8() == skillId) {
                    return i;
                }
            }
        }

        local pos = 8 + group * 38;
        for (local i = 0; i< 38; ++i) {
            if (NativePointer(buf).add(2 * (pos + i)).readU8() == skillId) {
                return pos + i;
            }
        }
        return -1;
    }


    NativePointer(0x08604596).writeS8(5);
    NativePointer(0x0860459C).writeS8(5);
    NativePointer(0x08606A27).writeS8(5);
    NativePointer(0x0860795E).writeS8(5);

    NativePointer(0x08608CD5).writeS8(8);
    NativePointer(0x08608CCF).writeS8(8);
    NativePointer(0x08608969).writeS8(8);
    NativePointer(0x08609001).writeS8(8);

    NativePointer(0x08605090).writeUInt(8 + 38 * 4);
    NativePointer(0x08605097).writeUInt(8 + 38 * 5);
    NativePointer(0x0860514F).writeUInt(8 + 38 * 3);
    NativePointer(0x08605156).writeUInt(8 + 38 * 4);
    NativePointer(0x0860513F).writeUInt(8 + 38 * 2);
    NativePointer(0x08605146).writeUInt(8 + 38 * 3);
    NativePointer(0x0860512F).writeUInt(8 + 38 * 1);
    NativePointer(0x08605136).writeUInt(8 + 38 * 2);
    NativePointer(0x0860511F).writeUInt(8 + 38 * 0);
    NativePointer(0x08605126).writeUInt(8 + 38 * 1);
    NativePointer(0x0866C6C4).writeUInt(196);
}

Timer.SetTimeOut(function() {
    // 调用函数启用修复
    FixOldSkillSlotInfo();
}, 1);