hch
2025-11-20 ed98029a88cd89702980ac7c40b711afddc5aeb2
Main/Core/NetworkPackage/CustomServerPack/CustomHB426CombinePack.cs
@@ -1,5 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class CustomHB426CombinePack : GameNetPackBasic
@@ -31,8 +31,7 @@
        }
        endTag = _endTag;
        toIndex = _toIndex;
        packList = CombineToSkillPackFromList(guid, packList);
        PrintAllPack();
    }
@@ -45,6 +44,27 @@
        }
        fromIndex = _fromIndex;
        startTag = tag;
    }
    public void PrintAllPack()
    {
        string temp = "CustomHB426CombinePack startTag: " + (startTag != null ? startTag.Tag : "null") +
            " fromIndex: " + fromIndex + " toIndex: " + toIndex + " packList.Count: " + packList.Count + "\n";
        for (int i = 0; i < packList.Count; i++)
        {
            var pack = packList[i];
            if (pack is CustomHB426CombinePack b426Pack)
            {
                temp += "  pack type is " + pack.GetType().Name + " tag is " + (b426Pack.startTag != null ? b426Pack.startTag.Tag : "null") + "\n";
            }
            else
            {
                temp += "  pack type is " + pack.GetType().Name + "\n";
            }
        }
        BattleDebug.LogError(temp);
    }
    public void AddPack(GameNetPackBasic pack)
@@ -74,68 +94,228 @@
        return startTag.Tag == tag.Tag && tag.Sign == 1;
    }
    public static List<GameNetPackBasic> CombineToSkillPackFromList(string _guid, List<GameNetPackBasic> b421SeriesPackList)
    {
        CustomHB426CombinePack combinePack = null;
        Dictionary<string, CustomHB426CombinePack> combineDict = new Dictionary<string, CustomHB426CombinePack>();
        Dictionary<int, GameNetPackBasic> indexDict = new Dictionary<int, GameNetPackBasic>();
        Dictionary<uint, HB427_tagSCUseSkill> skillDict = new Dictionary<uint, HB427_tagSCUseSkill>();
        for (int i = 0; i < b421SeriesPackList.Count; i++)
        {
            var pack = b421SeriesPackList[i];
            if (pack is HB426_tagSCTurnFightTag)
            {
                var tag = pack as HB426_tagSCTurnFightTag;
                if (null == combinePack)
                CustomHB426CombinePack combinePack;
                if (!combineDict.TryGetValue(tag.Tag, out combinePack))
                {
                    combinePack = new CustomHB426CombinePack();
                    combinePack.guid = _guid;
                    combineDict.Add(tag.Tag, combinePack);
                    indexDict.Add(i, combinePack);
                }
                if (tag.Sign == 0)
                {
                    combinePack.SetHB426Start(i, tag);
                    continue;
                }
                //  找到对应的开始标签
                if (combinePack.IsEndPack(tag))
                {
                    combinePack.SetHB426End(i, tag);
                    continue;
                }
                Debug.LogError("No matching start tag or end tag found: " + tag.Tag);
                continue;
            }
            else
            {
                if (pack is HB427_tagSCUseSkill skillPack)
                {
                    //  处理技能之间的链接关系
                    if (skillPack.RelatedSkillID > 0)
                    {
                        // 如果是被 HB426_tagSCTurnFightTag 包裹的第一个技能包 并且前面没有CustomHB426CombinePack
                        // 则需要把CustomHB426CombinePack加入skillPack的subSkillCombinePackList里
                        // 同时 需要把indexDict里删掉对应的subSkillCombinePackList
                        // subSkillList删掉对应skillPack,但是skillPack的parentSkill不变
                        skillDict.TryGetValue(skillPack.RelatedSkillID, out var parentSkill);
                        if (parentSkill != null && skillPack.BattleType == 4)//4=子技能
                        {
                            parentSkill.subSkillList.Add(skillPack);
                            skillPack.parentSkill = parentSkill;
                        }
                        indexDict.Add(i, pack);
                    }
                    else
                    {
                        indexDict.Add(i, pack);
                    }
                    if (skillDict.ContainsKey(skillPack.SkillID))
                    {
                        skillDict[skillPack.SkillID] = skillPack;
                    }
                    else
                    {
                        skillDict.Add(skillPack.SkillID, skillPack);
                    }
                }
                else
                {
                    if (combinePack.IsEndPack(tag))
                    indexDict.Add(i, pack);
                }
            }
        }
        // 如果是被 HB426_tagSCTurnFightTag 包裹的第一个技能包 并且前面没有CustomHB426CombinePack
        // 翻译一下就是 如果CombinePack里的主体Skill包 skillPack.RelatedSkillID > 0
        //  嵌套包内的包合并
        foreach (var combinePack in combineDict.Values)
        {
            for (int i = combinePack.fromIndex + 1; i < combinePack.toIndex; i++)
            {
                if (indexDict.TryGetValue(i, out var pack))
                {
                    indexDict.Remove(i);
                    combinePack.AddPack(pack);
                    if (pack is CustomHB426CombinePack cbPack)
                    {
                        combinePack.SetHB426End(i, tag);
                        break;
                        //  如果是嵌套的包 加入之后 调整i
                        i = cbPack.toIndex;
                    }
                }
            }
        }
            if (null != combinePack)
        for (int i = 0; i < b421SeriesPackList.Count; i++)
        {
            if (indexDict.TryGetValue(i, out var pack))
            {
                combinePack.AddPack(pack);
                if (pack is CustomHB426CombinePack cbPack)
                {
                    HB427_tagSCUseSkill skillPack = cbPack.GetMainHB427SkillPack();
                    if (null == skillPack)
                    {
                        continue;
                    }
                    //  如果是子技能
                    if (skillPack.isSubSkill)
                    {
                        //  让别人来处理
                        continue;
                    }
                    else
                    {
                        //  处理子技能
                        if (skillPack.subSkillList.Count > 0)
                        {
                            var parentSkill = skillPack;
                            List<HB427_tagSCUseSkill> toRemoveSubSkills = new List<HB427_tagSCUseSkill>();
                            foreach (var subSkill in parentSkill.subSkillList)
                            {
                                CustomHB426CombinePack innerCBPack = null;
                                if (cbPack.IsInnerCBPackContainsSkill(subSkill, ref innerCBPack))
                                {
                                    if (cbPack.GetMainHB427SkillPack() == subSkill)
                                    {
                                        parentSkill.subSkillList.Remove(subSkill);
                                        Debug.LogError("子技能不能是主技能: " + subSkill.SkillID);
                                        continue;
                                    }
                                    subSkill.parentCombinePack = innerCBPack;
                                    cbPack.packList.Remove(innerCBPack);
                                    toRemoveSubSkills.Add(subSkill);
                                    parentSkill.subSkillCombinePackList.Add(innerCBPack);
                                    indexDict.Remove(innerCBPack.fromIndex);
                                }
                            }
                            foreach (var subSkill in toRemoveSubSkills)
                            {
                                parentSkill.subSkillList.Remove(subSkill);
                            }
                        }
                        else
                        {
                            // 主技能没有子技能 直接跳过
                            continue;
                        }
                    }
                }
                else if (pack is HB427_tagSCUseSkill skillPack)
                {
                    // Debug.LogError("落单的技能");
                }
            }
        }
        List<GameNetPackBasic> newPackList = new List<GameNetPackBasic>();
        if (null != combinePack)
        // string temp = string.Empty;
        for (int i = 0; i < b421SeriesPackList.Count; i++)
        {
            //  技能包前面的包(不包括b426的开始标签)
            for (int i = 0; i < combinePack.fromIndex; i++)
            if (indexDict.TryGetValue(i, out var pack))
            {
                newPackList.Add(b421SeriesPackList[i]);
                newPackList.Add(pack);
            }
            //  把合并的技能包加进来
            newPackList.Add(combinePack);
            //  技能包后面的包(不包括b426的结束标签)
            for (int i = combinePack.toIndex + 1; i < b421SeriesPackList.Count; i++)
            {
                newPackList.Add(b421SeriesPackList[i]);
            }
            return CombineToSkillPackFromList(_guid, newPackList);
        }
        else
        {
            return b421SeriesPackList;
        }
        // newPackList = BattleManager.ParseBattlePackList(_guid, newPackList);
        // Debug.LogError(temp);
        return newPackList;
    }
    public HB427_tagSCUseSkill GetMainHB427SkillPack()
    {
        for (int i = 0; i < packList.Count; i++)
        {
            var pack = packList[i];
            if (pack is HB427_tagSCUseSkill skillPack)
            {
                return skillPack;
            }
            else if (pack is CustomHB426CombinePack)
            {
                return null;
            }
        }
        return null;
    }
    public bool IsInnerCBPackContainsSkill(HB427_tagSCUseSkill skill, ref CustomHB426CombinePack innerCBPack)
    {
        for (int i = 0; i < packList.Count; i++)
        {
            var pack = packList[i];
            if (pack is CustomHB426CombinePack cbPack)
            {
                if (cbPack.packList.Contains(skill))
                {
                    innerCBPack = cbPack;
                    return true;
                }
            }
        }
        return false;
    }
    public void Distribute()
    {
        BattleField battleField = BattleManager.Instance.GetBattleField(guid);
@@ -151,6 +331,58 @@
        {
            battleField.PlayRecord(skillAction);
        }
        else
        {
            battleField.DistributeNextPackage();
        }
    }
    public static SkillRecordAction CreateSkillAction(string guid, List<GameNetPackBasic> _packList)
    {
        BattleField battleField = BattleManager.Instance.GetBattleField(guid);
        if (null == battleField)
        {
            Debug.LogError("BattleField not found for guid: " + guid);
            return null;
        }
        while (_packList.Count > 0)
        {
            var pack = _packList[0];
            _packList.RemoveAt(0);
            if (pack is HB427_tagSCUseSkill)
            {
                _packList.Insert(0, pack);
                break;
            }
            else if (pack is CustomHB426CombinePack)
            {
                Debug.LogError("无法找到Skill包,先发现了嵌套包");
                return null;
            }
            else
            {
                // Debug.LogError("发现非Skill包,先分发掉: " + pack.GetType().Name);
                PackageRegedit.Distribute(pack);
            }
        }
        HB427_tagSCUseSkill skill = _packList[0] as HB427_tagSCUseSkill;
        _packList.RemoveAt(0);
        if (null == skill)
        {
            Debug.LogError("No HB427_tagSCUseSkill found in packList.");
            return null;
        }
        BattleObject _caster = battleField.battleObjMgr.GetBattleObject((int)skill.ObjID);
        SkillRecordAction skillAction = new SkillRecordAction(battleField, _caster, skill, _packList);
        return skillAction;
    }
    public SkillRecordAction CreateSkillAction()
@@ -163,14 +395,69 @@
            return null;
        }
        //  服务器修改了 现在第一个不一定是B427 也有可能是B428 先放Buff
        while (packList.Count > 0)
        {
            var pack = packList[0];
            packList.RemoveAt(0);
            if (pack is HB427_tagSCUseSkill)
            {
                packList.Insert(0, pack);
                break;
            }
            else if (pack is CustomHB426CombinePack)
            {
                Debug.LogError("无法找到Skill包,先发现了嵌套包");
                return null;
            }
            else
            {
                // Debug.LogError("发现非Skill包,先分发掉: " + pack.GetType().Name);
                PackageRegedit.Distribute(pack);
            }
        }
        if (startTag.Tag.StartsWith("Skill_"))
        {
            if (packList.Count <= 0)
            {
                Debug.LogError("No HB427_tagSCUseSkill found in packList." + startTag.Tag);
                return null;
            }
            HB427_tagSCUseSkill skill = packList[0] as HB427_tagSCUseSkill;
            packList.RemoveAt(0);
            if (null == skill)
            {
                Debug.LogError("No HB427_tagSCUseSkill found in packList.");
                return null;
            }
            BattleObject _caster = battleField.battleObjMgr.GetBattleObject((int)skill.ObjID);
            // BattleDebug.LogError("_caster == null : " + (_caster == null) + " skill.ObjID : " + skill.ObjID);
            SkillRecordAction skillAction = new SkillRecordAction(battleField, _caster, skill, packList);
            return skillAction;
        }
        return null;
    }
#if UNITY_EDITOR
    public static CustomHB426CombinePack CreateCustomPack(string _guid, HB427_tagSCUseSkill skill)
    {
        CustomHB426CombinePack pack = new CustomHB426CombinePack();
        pack.guid = string.Empty;
        pack.startTag = new HB426_tagSCTurnFightTag
        {
            Tag = "Skill_Start",
            Sign = 0
        };
        pack.packList.Add(skill);
        return pack;
    }
#endif
}