using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Colosseum.AI;
using Colosseum.AI.BehaviorActions.Conditions;
using Colosseum.Enemy;
using UnityEditor;
using UnityEngine;
namespace Colosseum.Editor
{
///
/// 드로그 Behavior Graph authoring 자산을 현재 BT 우선순위 구조로 재생성합니다.
/// Check 노드는 ConditionalGuardAction + Condition 조합으로 구현됩니다.
///
public static class RebuildDrogBehaviorAuthoringGraph
{
private const string GraphAssetPath = "Assets/_Game/AI/BT_Drog.asset";
[MenuItem("Tools/Colosseum/Rebuild Drog Behavior Authoring Graph")]
private static void Rebuild()
{
UnityEngine.Object graphAsset = AssetDatabase.LoadMainAssetAtPath(GraphAssetPath);
if (graphAsset == null)
{
// 에셋이 없으면 기존 에셋 경로의 타입을 리플렉션으로 찾아 생성합니다.
// BehaviorAuthoringGraph는 Unity.Behavior.Editor 어셈블리에 있습니다.
Type authoringGraphType = null;
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
authoringGraphType = assembly.GetType("Unity.Behavior.Authoring.BehaviorAuthoringGraph");
if (authoringGraphType != null)
break;
}
if (authoringGraphType == null)
{
Debug.LogError("[DrogBTRebuild] BehaviorAuthoringGraph 타입을 모든 어셈블리에서 찾지 못했습니다.");
return;
}
graphAsset = ScriptableObject.CreateInstance(authoringGraphType);
if (graphAsset == null)
{
Debug.LogError("[DrogBTRebuild] BehaviorAuthoringGraph 인스턴스를 생성할 수 없습니다.");
return;
}
AssetDatabase.CreateAsset(graphAsset, GraphAssetPath);
AssetDatabase.SaveAssets();
Debug.Log("[DrogBTRebuild] 새 그래프 자산을 생성했습니다.");
}
try
{
Type authoringGraphType = graphAsset.GetType();
Assembly authoringAssembly = authoringGraphType.Assembly;
Assembly runtimeAssembly = typeof(Unity.Behavior.BehaviorGraph).Assembly;
// 기본 리플렉션 메서드
MethodInfo createNodeMethod = authoringGraphType.BaseType?.GetMethod("CreateNode", BindingFlags.Instance | BindingFlags.Public);
MethodInfo connectEdgeMethod = authoringGraphType.BaseType?.GetMethod("ConnectEdge", BindingFlags.Instance | BindingFlags.Public);
MethodInfo createNodePortsMethod = authoringGraphType.BaseType?.GetMethod("CreateNodePortsForNode", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
MethodInfo buildRuntimeGraphMethod = authoringGraphType.GetMethod("BuildRuntimeGraph", BindingFlags.Instance | BindingFlags.Public);
MethodInfo saveAssetMethod = authoringGraphType.BaseType?.GetMethod("SaveAsset", BindingFlags.Instance | BindingFlags.Public);
MethodInfo setAssetDirtyMethod = authoringGraphType.BaseType?.GetMethod("SetAssetDirty", BindingFlags.Instance | BindingFlags.Public);
MethodInfo getNodeInfoMethod = authoringAssembly.GetType("Unity.Behavior.NodeRegistry", true)
?.GetMethod("GetInfo", BindingFlags.Static | BindingFlags.NonPublic);
if (createNodeMethod == null || connectEdgeMethod == null || buildRuntimeGraphMethod == null || saveAssetMethod == null || setAssetDirtyMethod == null || getNodeInfoMethod == null)
{
Debug.LogError("[DrogBTRebuild] Behavior Authoring 리플렉션 메서드를 찾지 못했습니다.");
return;
}
// ConditionalGuard 리플렉션 타입 (internal)
Type conditionalGuardType = runtimeAssembly.GetType("Unity.Behavior.ConditionalGuardAction");
Type conditionUtilityType = authoringAssembly.GetType("Unity.Behavior.ConditionUtility");
Type conditionModelType = authoringAssembly.GetType("Unity.Behavior.ConditionModel");
Type graphNodeModelType = authoringAssembly.GetType("Unity.Behavior.BehaviorGraphNodeModel");
Type conditionInfoType = authoringAssembly.GetType("Unity.Behavior.ConditionInfo");
if (conditionalGuardType == null) { Debug.LogError("[DrogBTRebuild] ConditionalGuardAction 타입을 찾지 못했습니다."); return; }
if (conditionUtilityType == null) { Debug.LogError("[DrogBTRebuild] ConditionUtility 타입을 찾지 못했습니다."); return; }
if (conditionModelType == null) { Debug.LogError("[DrogBTRebuild] ConditionModel 타입을 찾지 못했습니다."); return; }
if (graphNodeModelType == null) { Debug.LogError("[DrogBTRebuild] BehaviorGraphNodeModel 타입을 찾지 못했습니다."); return; }
if (conditionInfoType == null)
{
Debug.LogError("[DrogBTRebuild] ConditionInfo 타입을 찾지 못했습니다.");
return;
}
Type branchCompositeType = runtimeAssembly.GetType("Unity.Behavior.BranchingConditionComposite");
if (branchCompositeType == null)
{
Debug.LogError("[DrogBTRebuild] BranchingConditionComposite 타입을 찾지 못했습니다.");
return;
}
// SetField(string, VariableModel, Type) — 제네릭 버전과 구분하기 위해 파라미터 수로 필터링
MethodInfo setFieldMethod = conditionModelType.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic)
.FirstOrDefault(m => m.Name == "SetField" && !m.IsGenericMethod && m.GetParameters().Length == 3);
// SetField(string, T) — BehaviorGraphNodeModel 기반 클래스에서 조회 (ConditionModel과 Action 노드 모두 사용)
MethodInfo setFieldValueMethod = graphNodeModelType.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic)
.FirstOrDefault(m => m.Name == "SetField" && m.IsGenericMethod && m.GetParameters().Length == 2);
if (setFieldMethod == null)
{
Debug.LogError("[DrogBTRebuild] ConditionModel.SetField 메서드를 찾지 못했습니다.");
return;
}
if (setFieldValueMethod == null)
{
Debug.LogError("[DrogBTRebuild] SetField 제네릭 메서드를 찾지 못했습니다.");
return;
}
// 기존 에셋의 서브에셋(BehaviorGraph 등)에서 깨진 managed references 클리어
Type behaviorGraphType = typeof(Unity.Behavior.BehaviorGraph);
UnityEngine.Object[] subAssets = AssetDatabase.LoadAllAssetsAtPath(GraphAssetPath);
foreach (var subAsset in subAssets)
{
if (subAsset != null && subAsset.GetType() == behaviorGraphType)
{
UnityEditor.SerializationUtility.ClearAllManagedReferencesWithMissingTypes(subAsset);
EditorUtility.SetDirty(subAsset);
}
}
// AuthoringGraph 자체에서도 깨진 references 클리어
UnityEditor.SerializationUtility.ClearAllManagedReferencesWithMissingTypes(graphAsset);
// 노드 클리어 — 전체 타입 계층에서 필드 찾기
FieldInfo nodesField = FindFieldInHierarchy(authoringGraphType, "m_RootNodes");
if (nodesField == null)
{
Debug.LogError("[DrogBTRebuild] m_RootNodes 필드를 타입 계층 전체에서 찾지 못했습니다.");
return;
}
nodesField.SetValue(graphAsset, Activator.CreateInstance(nodesField.FieldType));
FieldInfo nodesListField = FindFieldInHierarchy(authoringGraphType, "m_Nodes");
if (nodesListField != null)
nodesListField.SetValue(graphAsset, Activator.CreateInstance(nodesListField.FieldType));
FieldInfo nodeModelsInfoField = FindFieldInHierarchy(authoringGraphType, "m_NodeModelsInfo");
if (nodeModelsInfoField != null)
nodeModelsInfoField.SetValue(graphAsset, Activator.CreateInstance(nodeModelsInfoField.FieldType));
FieldInfo runtimeGraphField = FindFieldInHierarchy(authoringGraphType, "m_RuntimeGraph");
if (runtimeGraphField != null)
runtimeGraphField.SetValue(graphAsset, null);
// 클리어 후 에셋을 저장하고 다시 로드하여 잔류 참조가 메모리에 남지 않게 합니다.
EditorUtility.SetDirty(graphAsset);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
// 에셋을 다시 로드 (직렬화된 상태에서 로드하여 클리어 상태 확보)
graphAsset = AssetDatabase.LoadMainAssetAtPath(GraphAssetPath);
if (graphAsset == null)
{
Debug.LogError("[DrogBTRebuild] 에셋 재로드 실패.");
return;
}
authoringGraphType = graphAsset.GetType();
object targetVariable = FindBlackboardVariableModel("Target");
if (targetVariable == null)
{
Debug.LogError("[DrogBTRebuild] Target 블랙보드 변수를 찾지 못했습니다.");
return;
}
// 구조 노드
object startNode = CreateNode(graphAsset, createNodeMethod, getNodeInfoMethod, runtimeAssembly.GetType("Unity.Behavior.Start", true), new Vector2(420f, -800f));
object repeatNode = CreateNode(graphAsset, createNodeMethod, getNodeInfoMethod, runtimeAssembly.GetType("Unity.Behavior.RepeaterModifier", true), new Vector2(420f, -620f));
// ── 프리팹에서 패턴 에셋 로드 ──
const string prefabPath = "Assets/_Game/Prefabs/Bosses/Prefab_Boss_Drog.prefab";
GameObject prefab = AssetDatabase.LoadMainAssetAtPath(prefabPath) as GameObject;
BossCombatBehaviorContext context = prefab?.GetComponent();
if (context == null)
{
Debug.LogError("[DrogBTRebuild] 드로그 프리팹에서 BossCombatBehaviorContext를 찾지 못했습니다.");
return;
}
// protected 필드에서 BossPatternData 에셋 읽기 (리플렉션)
BossPatternData punishPattern = ReadProtectedField(context, "punishPattern");
BossPatternData signaturePattern = ReadProtectedField(context, "signaturePattern");
BossPatternData mobilityPattern = ReadProtectedField(context, "mobilityPattern");
BossPatternData comboPattern = ReadProtectedField(context, "comboPattern");
BossPatternData primaryPattern = ReadProtectedField(context, "primaryPattern");
BossPatternData utilityPattern = ReadProtectedField(context, "utilityPattern");
float punishSearchRadius = ReadProtectedFieldValue(context, "punishSearchRadius", 6f);
// 필수 패턴 검증 (combo는 선택 — 할당되지 않은 경우 해당 Branch만 생략)
if (punishPattern == null || signaturePattern == null || mobilityPattern == null ||
primaryPattern == null || utilityPattern == null)
{
Debug.LogError("[DrogBTRebuild] 프리팹에서 필수 패턴 에셋을 읽지 못했습니다.");
return;
}
if (comboPattern == null)
Debug.LogWarning("[DrogBTRebuild] comboPattern이 할당되지 않았습니다. 해당 Branch를 생략합니다.");
// ── 계단식 우선순위 체인 ──
// 설계안 우선순위: 다운 추가타 > 도약 > 집행 개시 > 기본 루프 > 조합 > 유틸리티
// 각 Branch: CheckPatternReady → true → UsePatternByRole
// false → 다음 우선순위 Branch 시도
// 마지막까지 모든 조건이 false이면 Chase (fallback)
//
// 연결 흐름: Branch.True → FloatingPort(True).InputPort → FloatingPort(True).OutputPort → Action.InputPort
// CreateNodePortsForNode를 호출하여 FloatingPortNodeModel을 자동 생성해야 합니다.
//
// 레이아웃 패턴 (사용자 조정 기준):
// Branch: (-800, y)
// True Floating: (-597, y + 110)
// False Floating: (-1011, y + 114)
// Action: (-598, y + 199)
const float branchX = -800f;
const float truePortOffsetX = 203f;
const float truePortOffsetY = 120f;
const float falsePortOffsetX = -211f;
const float falsePortOffsetY = 124f;
const float actionOffsetX = 202f;
const float actionOffsetY = 219f;
const float startY = -800f;
const float stepY = 320f;
// #1 Punish — 다운 추가타 (전제 조건: 다운된 대상이 반경 이내에 있어야 함)
object downBranch = CreateNode(graphAsset, createNodeMethod, getNodeInfoMethod, branchCompositeType, new Vector2(branchX, startY));
AttachPatternReadyCondition(downBranch, punishPattern, authoringAssembly);
AttachConditionWithValue(downBranch, typeof(IsDownedTargetInRangeCondition), "searchRadius", punishSearchRadius, authoringAssembly);
AttachPhaseConditionIfNeeded(downBranch, punishPattern, authoringAssembly);
SetBranchRequiresAll(downBranch, true);
object downUseNode = CreateNode(graphAsset, createNodeMethod, getNodeInfoMethod, typeof(UsePatternByRoleAction), new Vector2(branchX + actionOffsetX, startY + actionOffsetY));
SetNodeFieldValue(downUseNode, "Pattern", punishPattern, setFieldValueMethod);
LinkTarget(downUseNode, targetVariable);
// #2 Mobility — 도약 (전제 조건: 지나치게 먼 대상이 존재해야 함)
float mobilityTriggerDistance = ReadProtectedFieldValue(context, "mobilityTriggerDistance", 8f);
object leapBranch = CreateNode(graphAsset, createNodeMethod, getNodeInfoMethod, branchCompositeType, new Vector2(branchX, startY + stepY));
AttachPatternReadyCondition(leapBranch, mobilityPattern, authoringAssembly);
AttachConditionWithValue(leapBranch, typeof(IsTargetBeyondDistanceCondition), "minDistance", mobilityTriggerDistance, authoringAssembly);
AttachPhaseConditionIfNeeded(leapBranch, mobilityPattern, authoringAssembly);
SetBranchRequiresAll(leapBranch, true);
object leapUseNode = CreateNode(graphAsset, createNodeMethod, getNodeInfoMethod, typeof(UsePatternByRoleAction), new Vector2(branchX + actionOffsetX, startY + stepY + actionOffsetY));
SetNodeFieldValue(leapUseNode, "Pattern", mobilityPattern, setFieldValueMethod);
LinkTarget(leapUseNode, targetVariable);
// #3 Signature — 집행 개시
object signatureBranch = CreateNode(graphAsset, createNodeMethod, getNodeInfoMethod, branchCompositeType, new Vector2(branchX, startY + stepY * 2));
AttachPatternReadyCondition(signatureBranch, signaturePattern, authoringAssembly);
AttachPhaseConditionIfNeeded(signatureBranch, signaturePattern, authoringAssembly);
object signatureUseNode = CreateNode(graphAsset, createNodeMethod, getNodeInfoMethod, typeof(UsePatternByRoleAction), new Vector2(branchX + actionOffsetX, startY + stepY * 2 + actionOffsetY));
SetNodeFieldValue(signatureUseNode, "Pattern", signaturePattern, setFieldValueMethod);
LinkTarget(signatureUseNode, targetVariable);
// #4 Combo — 콤보 패턴 + 조건부 도약 (Sequence)
// comboBranch.True → Sequence:
// Child 1: 연타2-강타 실행
// Child 2: Branch(거리 초과 대상 존재) → 도약 실행
// 거리 초과 대상이 없으면 Branch Failure → Sequence Failure → comboBranch Failure → primaryBranch로 연결
object comboBranch = null;
object comboUseNode = null;
if (comboPattern != null)
{
// 메인 체인용 Branch (콤보 준비 + 페이즈 조건)
comboBranch = CreateNode(graphAsset, createNodeMethod, getNodeInfoMethod, branchCompositeType, new Vector2(branchX, startY + stepY * 3));
AttachPatternReadyCondition(comboBranch, comboPattern, authoringAssembly);
AttachPhaseConditionIfNeeded(comboBranch, comboPattern, authoringAssembly);
// Sequence: 콤보 실행 → 조건부 도약
object comboSequence = CreateNode(graphAsset, createNodeMethod, getNodeInfoMethod,
runtimeAssembly.GetType("Unity.Behavior.SequenceComposite", true),
new Vector2(branchX + 220f, startY + stepY * 3));
// Child 1: 콤보 패턴 실행 (연타2-강타 + 대기)
comboUseNode = CreateNode(graphAsset, createNodeMethod, getNodeInfoMethod, typeof(UsePatternByRoleAction), new Vector2(branchX + 400f, startY + stepY * 3));
SetNodeFieldValue(comboUseNode, "Pattern", comboPattern, setFieldValueMethod);
LinkTarget(comboUseNode, targetVariable);
// Child 2: 조건부 도약 (거리 초과 대상 있을 때만)
object comboLeapBranch = CreateNode(graphAsset, createNodeMethod, getNodeInfoMethod, branchCompositeType, new Vector2(branchX + 220f, startY + stepY * 3 + 180f));
AttachConditionWithValue(comboLeapBranch, typeof(IsTargetBeyondDistanceCondition), "minDistance", mobilityTriggerDistance, authoringAssembly);
object comboLeapUseNode = CreateNode(graphAsset, createNodeMethod, getNodeInfoMethod, typeof(UsePatternByRoleAction), new Vector2(branchX + 400f, startY + stepY * 3 + 180f));
SetNodeFieldValue(comboLeapUseNode, "Pattern", mobilityPattern, setFieldValueMethod);
LinkTarget(comboLeapUseNode, targetVariable);
ConnectBranch(graphAsset, connectEdgeMethod, comboLeapBranch, "True", comboLeapUseNode);
// Sequence에 자식 연결
ConnectChildren(graphAsset, connectEdgeMethod, comboSequence, comboUseNode, comboLeapBranch);
// 메인 체인: comboBranch.True → Sequence
ConnectBranch(graphAsset, connectEdgeMethod, comboBranch, "True", comboSequence);
}
// #5 Primary — 사거리 + 기본 패턴 준비 (모두 충족)
object primaryBranch = CreateNode(graphAsset, createNodeMethod, getNodeInfoMethod, branchCompositeType, new Vector2(branchX, startY + stepY * 4));
object primaryRangeCondModel = AttachCondition(primaryBranch, typeof(IsTargetInAttackRangeCondition), authoringAssembly);
if (primaryRangeCondModel != null) setFieldMethod.Invoke(primaryRangeCondModel, new object[] { "Target", targetVariable, typeof(GameObject) });
AttachPatternReadyCondition(primaryBranch, primaryPattern, authoringAssembly);
AttachPhaseConditionIfNeeded(primaryBranch, primaryPattern, authoringAssembly);
SetBranchRequiresAll(primaryBranch, true);
object primaryUseNode = CreateNode(graphAsset, createNodeMethod, getNodeInfoMethod, typeof(UsePatternByRoleAction), new Vector2(branchX + actionOffsetX, startY + stepY * 4 + actionOffsetY));
SetNodeFieldValue(primaryUseNode, "Pattern", primaryPattern, setFieldValueMethod);
LinkTarget(primaryUseNode, targetVariable);
// #6 Utility — 유틸리티 (전제 조건: 원거리 대상이 존재해야 함)
float utilityTriggerDistance = ReadProtectedFieldValue(context, "utilityTriggerDistance", 5f);
object utilityBranch = CreateNode(graphAsset, createNodeMethod, getNodeInfoMethod, branchCompositeType, new Vector2(branchX, startY + stepY * 5));
AttachPatternReadyCondition(utilityBranch, utilityPattern, authoringAssembly);
AttachConditionWithValue(utilityBranch, typeof(IsTargetBeyondDistanceCondition), "minDistance", utilityTriggerDistance, authoringAssembly);
AttachPhaseConditionIfNeeded(utilityBranch, utilityPattern, authoringAssembly);
SetBranchRequiresAll(utilityBranch, true);
object utilityUseNode = CreateNode(graphAsset, createNodeMethod, getNodeInfoMethod, typeof(UsePatternByRoleAction), new Vector2(branchX + actionOffsetX, startY + stepY * 5 + actionOffsetY));
SetNodeFieldValue(utilityUseNode, "Pattern", utilityPattern, setFieldValueMethod);
LinkTarget(utilityUseNode, targetVariable);
// #7 Chase — fallback (Branch 아님, Sequence 사용)
object chaseSequence = CreateNode(graphAsset, createNodeMethod, getNodeInfoMethod, runtimeAssembly.GetType("Unity.Behavior.SequenceComposite", true), new Vector2(branchX, startY + stepY * 6));
object chaseRefreshNode = CreateNode(graphAsset, createNodeMethod, getNodeInfoMethod, typeof(RefreshPrimaryTargetAction), new Vector2(branchX + 160f, startY + stepY * 6 + 80f));
object chaseHasTargetNode = CreateNode(graphAsset, createNodeMethod, getNodeInfoMethod, typeof(ValidateTargetAction), new Vector2(branchX + 320f, startY + stepY * 6 + 80f));
object chaseUseNode = CreateNode(graphAsset, createNodeMethod, getNodeInfoMethod, typeof(ChaseTargetAction), new Vector2(branchX + 480f, startY + stepY * 6 + 80f));
// ── FloatingPortNodeModel 생성 + 위치 보정 ──
// Branch 노드의 NamedPort(True/False)에 대해 FloatingPortNodeModel을 생성합니다.
// CreateNodePortsForNode는 기본 위치(Branch + 200px Y)를 사용하므로, 생성 후 사용자 조정 기준 위치로 이동합니다.
var allBranches = new List