From a019183609dc17bead38b0a0bef214d6a0c19b9e Mon Sep 17 00:00:00 2001 From: lrc <571244399@qq.com> Date: Thu, 5 Nov 2026 14:10:11 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=8F=8D=E5=B0=84=E7=94=9F=E6=88=90=20UI)?= =?UTF-8?q?:=20=E5=AD=97=E6=AE=B5=E9=85=8D=E6=96=B9=E8=90=BD=E5=9C=A8?= =?UTF-8?q?=E5=AE=BF=E4=B8=BB=E4=BE=A7=E2=80=94=E2=80=94=E9=A2=84=E5=88=B6?= =?UTF-8?q?=E4=BD=93=E6=94=BE=E9=9B=B6=E4=BB=B6=EF=BC=8C=E9=85=8D=E6=96=B9?= =?UTF-8?q?=E5=86=B3=E5=AE=9A=E6=8B=BC=E5=93=AA=E5=87=A0=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 Recipes/(纯逻辑 + 一个预制体组件): - FieldRecipeSet / FieldPlan:类型→配方的匹配与合并,逐字段 override 只覆盖写了的项。 两条规矩:最具体的赢(写命名空间只认该类型,裸名按尾段匹配);没匹配到就说"没有配方", 绝不回退默认配方。 - FieldLayout:给定计划 + 预制体真实有的槽位/输入元素,算出开哪些关哪些; 返回值只答"能不能装",缺装饰件照样装但必须进 problems(注释写死这条契约)。 - FieldPartsAssembler:预制体上的槽位(label/required-mark/note/icon/legend/input/error, input 之下放各输入元素),Collect() 一次按名字收齐,Apply() 只改激活态。 - FieldRecipeTable:配方表资产。菜单路径集中成 HostMenuRoots(宿主不能反向引用蓝图包的路径表)。 XuiavrgInspectorManager 接入:预制体挑好挂上父级后 ApplyFieldRecipe(entry, instance)。 没指定配方表 → 说一句并按预制体原样;预制体没有槽位组件 → 原样; 匹配失败或装配有问题 → 逐条 LogWarning,点名 Entry、来源配方、缺的是什么。 新增 Tests~/RecipeProbe(dotnet 编两份纯逻辑文件):VERDICT RECIPE-PROBE PASS (19/19)。 判据(主工程根):bash check_flowgraph_extensions.sh -> host Lrss3.Xericuiactionvessel sources=209 errors=0 / VERDICT FGEXT-BUILD PASS --- .../Scripts/Recipes/FieldLayout.cs | 104 +++++++++++ .../Scripts/Recipes/FieldPartsAssembler.cs | 116 ++++++++++++ .../Scripts/Recipes/FieldRecipeSet.cs | 169 ++++++++++++++++++ .../Scripts/XuiavrgInspectorManager.cs | 45 +++++ Tests~/RecipeProbe/.gitignore | 3 + Tests~/RecipeProbe/Program.cs | 162 +++++++++++++++++ Tests~/RecipeProbe/RecipeProbe.csproj | 16 ++ Tests~/RecipeProbe/run.sh | 15 ++ 8 files changed, 630 insertions(+) create mode 100644 Runtime/InterfaceAttributeReflectionGenerator/Scripts/Recipes/FieldLayout.cs create mode 100644 Runtime/InterfaceAttributeReflectionGenerator/Scripts/Recipes/FieldPartsAssembler.cs create mode 100644 Runtime/InterfaceAttributeReflectionGenerator/Scripts/Recipes/FieldRecipeSet.cs create mode 100644 Tests~/RecipeProbe/.gitignore create mode 100644 Tests~/RecipeProbe/Program.cs create mode 100644 Tests~/RecipeProbe/RecipeProbe.csproj create mode 100644 Tests~/RecipeProbe/run.sh diff --git a/Runtime/InterfaceAttributeReflectionGenerator/Scripts/Recipes/FieldLayout.cs b/Runtime/InterfaceAttributeReflectionGenerator/Scripts/Recipes/FieldLayout.cs new file mode 100644 index 0000000..b20a8b3 --- /dev/null +++ b/Runtime/InterfaceAttributeReflectionGenerator/Scripts/Recipes/FieldLayout.cs @@ -0,0 +1,104 @@ +using System; +using System.Collections.Generic; + +namespace XericUI.ReflectionUIGenerator +{ + /// + /// 一次装配的决策:给定配方结果 + 预制体上真实存在的槽位/输入元素, + /// 算出"该开哪些、该关哪些、哪里对不上"。 + /// + /// 单独拎出来是因为这一步最容易静默错:槽位改名、元素少一个、配方多要一块—— + /// 都可能"什么都没报错,界面少一块"。所以任何对不上都必须进 problems, + /// 由调用方决定是响亮地报错还是退回空白,绝不悄悄少装。 + /// + public static class FieldLayout + { + /// 槽位在预制体上的名字:就是零件名(小写连字符),输入元素挂在 "input" 之下。 + public static string SlotOf(RecipePart part) + { + switch (part) + { + case RecipePart.Label: return "label"; + case RecipePart.RequiredMark: return "required-mark"; + case RecipePart.Note: return "note"; + case RecipePart.Icon: return "icon"; + case RecipePart.Legend: return "legend"; + case RecipePart.Input: return "input"; + case RecipePart.Error: return "error"; + default: return part.ToString().ToLowerInvariant(); + } + } + + /// + /// 返回值只回答"能不能装":缺零件槽、缺输入元素、压根没槽 —— 不能装。 + /// 缺一个装饰件(必填标记)仍然能装,但一定进 problems: + /// 所以调用方两个都要看,不能只信 bool。 + /// + public static bool Resolve(FieldPlan plan, IEnumerable slotNames, IEnumerable elementNames, + out List activate, out List problems) + { + activate = new List(); + problems = new List(); + if (plan == null) { problems.Add("no plan given"); return false; } + + var slots = new HashSet(StringComparer.Ordinal); + if (slotNames != null) foreach (var s in slotNames) if (!string.IsNullOrEmpty(s)) slots.Add(s); + var elements = new HashSet(StringComparer.Ordinal); + if (elementNames != null) foreach (var e in elementNames) if (!string.IsNullOrEmpty(e)) elements.Add(e); + + if (slots.Count == 0) { problems.Add("the field prefab exposes no slots at all (nothing to assemble into)"); return false; } + + var ok = true; + for (var i = 0; i < plan.parts.Count; i++) + { + var slot = SlotOf(plan.parts[i]); + if (!slots.Contains(slot)) + { + problems.Add("recipe wants part \"" + slot + "\" (" + plan.source + ") but the prefab has no such slot"); + ok = false; + continue; + } + activate.Add(slot); + } + + // 必填没标出来是"看起来能空着提交",属于界面说谎:要么有槽,要么报问题 + if (plan.required && slots.Contains(SlotOf(RecipePart.RequiredMark)) && !activate.Contains(SlotOf(RecipePart.RequiredMark))) + activate.Add(SlotOf(RecipePart.RequiredMark)); + else if (plan.required && !slots.Contains(SlotOf(RecipePart.RequiredMark))) + problems.Add("field is required but the prefab has no \"required-mark\" slot to say so"); + + if (!activate.Contains(SlotOf(RecipePart.Error)) && slots.Contains(SlotOf(RecipePart.Error))) + activate.Add(SlotOf(RecipePart.Error)); // 错误位常驻(初始为空),否则校验失败时没地方显示 + + if (!string.IsNullOrEmpty(plan.inputElement)) + { + if (!activate.Contains(SlotOf(RecipePart.Input))) + { + problems.Add("the recipe names input element \"" + plan.inputElement + "\" but no Input part is planned"); + ok = false; + } + else if (elements.Count == 0) + { + problems.Add("the \"input\" slot has no element variants to choose from"); + ok = false; + } + else if (!elements.Contains(plan.inputElement)) + { + problems.Add("input element \"" + plan.inputElement + "\" is not on the prefab (there: " + + string.Join(", ", Sorted(elements).ToArray()) + ")"); + ok = false; + } + else activate.Add("input/" + plan.inputElement); + } + + return ok; + } + + static List Sorted(HashSet set) + { + var list = new List(set); + list.Sort(StringComparer.Ordinal); + return list; + } + } +} diff --git a/Runtime/InterfaceAttributeReflectionGenerator/Scripts/Recipes/FieldPartsAssembler.cs b/Runtime/InterfaceAttributeReflectionGenerator/Scripts/Recipes/FieldPartsAssembler.cs new file mode 100644 index 0000000..91e5302 --- /dev/null +++ b/Runtime/InterfaceAttributeReflectionGenerator/Scripts/Recipes/FieldPartsAssembler.cs @@ -0,0 +1,116 @@ +using System; +using System.Collections.Generic; +using UnityEngine; + +namespace XericUI.ReflectionUIGenerator +{ + /// 宿主库自己的 Unity 菜单位置(只此一处)。蓝图包的路径表不能被宿主反向引用, + /// 所以这里不共享常量;check_menu_paths.sh 的字面量普查只扫 fork 与蓝图包两棵树。 + internal static class HostMenuRoots + { + public const string ComponentTable = "Xeric Library/Component/Table"; + } + /// 配方表资产:类型 → 配方(含逐字段例外)。匹配与合并的规矩在 。 + [CreateAssetMenu(menuName = HostMenuRoots.ComponentTable + "/Field Recipe Table", fileName = "FieldRecipeTable")] + public sealed class FieldRecipeTable : ScriptableObject + { + [SerializeField] FieldRecipeSet recipes = new FieldRecipeSet(); + + public FieldRecipeSet Recipes { get { return recipes; } } + + public bool TryPlan(string typeName, string fieldName, out FieldPlan plan, out string reason) + { + return recipes.TryPlan(typeName, fieldName, out plan, out reason); + } + + /// 表自身的毛病(空类型/重复/没零件/没 fieldName 的 override)。 + public List Validate() { return recipes.Validate(); } + } + + /// + /// 一个字段预制体上的零件槽位。预制体只放零件(label/note/icon/legend/input/error/required-mark), + /// input 之下再放各类型的输入元素(input-field/slider/enum-dropdown…),运行时按配方激活。 + /// + /// 所以"某字段要多一个小字注释"是改配方,不是改预制体;预制体少了那块零件才是预制体的事, + /// 而它会作为 problem 报出来,不是安静地少装一块。 + /// + [DisallowMultipleComponent] + [AddComponentMenu(HostMenuRoots.ComponentTable + "/Field Parts Assembler")] + public sealed class FieldPartsAssembler : MonoBehaviour + { + + [Serializable] + struct SlotRef + { + public string slot; + public GameObject go; + } + + [SerializeField] List slots = new List(); + [SerializeField] List inputElements = new List(); + + public int SlotCount { get { return slots.Count; } } + public int ElementCount { get { return inputElements.Count; } } + + /// 按约定的槽位名收集一层子物体(省掉一个个手拖)。 + public int Collect() + { + slots.Clear(); + inputElements.Clear(); + for (var i = 0; i < transform.childCount; i++) + { + var child = transform.GetChild(i); + var known = SlotName(child.name); + if (known == null) continue; + slots.Add(new SlotRef { slot = known, go = child.gameObject }); + if (known != "input") continue; + for (var j = 0; j < child.childCount; j++) + { + var element = child.GetChild(j); + if (string.IsNullOrEmpty(SlotName(element.name)) && element.name != "input") + inputElements.Add(element.gameObject); + } + } + return slots.Count; + } + + /// Activate what the plan asks for, hide the rest. Problems are returned, never swallowed. + public bool Apply(FieldPlan plan, out List problems) + { + var slotNames = new List(slots.Count); + for (var i = 0; i < slots.Count; i++) slotNames.Add(slots[i].slot); + var elementNames = new List(inputElements.Count); + for (var i = 0; i < inputElements.Count; i++) + if (inputElements[i] != null) elementNames.Add(inputElements[i].name); + + List activate; + var ok = FieldLayout.Resolve(plan, slotNames, elementNames, out activate, out problems); + + for (var i = 0; i < slots.Count; i++) + { + var s = slots[i]; + if (s.go == null) { problems.Add("slot \"" + s.slot + "\" points at nothing"); ok = false; continue; } + var want = activate.Contains(s.slot); + if (s.slot == "input") want = want && activate.Contains("input/" + (plan == null ? "" : plan.inputElement)); + s.go.SetActive(want); + } + for (var i = 0; i < inputElements.Count; i++) + { + var element = inputElements[i]; + if (element == null) continue; + element.SetActive(activate.Contains("input/" + element.name)); + } + return ok; + } + + static string SlotName(string name) + { + switch (name) + { + case "label": case "required-mark": case "note": case "icon": + case "legend": case "input": case "error": return name; + default: return null; + } + } + } +} diff --git a/Runtime/InterfaceAttributeReflectionGenerator/Scripts/Recipes/FieldRecipeSet.cs b/Runtime/InterfaceAttributeReflectionGenerator/Scripts/Recipes/FieldRecipeSet.cs new file mode 100644 index 0000000..33b8c99 --- /dev/null +++ b/Runtime/InterfaceAttributeReflectionGenerator/Scripts/Recipes/FieldRecipeSet.cs @@ -0,0 +1,169 @@ +using System; +using System.Collections.Generic; + +namespace XericUI.ReflectionUIGenerator +{ + /// 界面上一块可拼的零件。配方决定拼哪几块、按什么顺序摆。 + public enum RecipePart { Label = 0, RequiredMark = 1, Note = 2, Icon = 3, Legend = 4, Input = 5, Error = 6 } + + /// 某个字段的例外:只覆盖写了的那几项。 + [Serializable] + public sealed class FieldOverride + { + public string fieldName = ""; + public bool hasInputOverride; + public string inputOverride = ""; + public bool hasNoteWhen; + public string noteWhen = ""; + public bool hasFriendlyName; + public string friendlyName = ""; + public bool hasRequired; + public bool required; + public string requiredMessage = ""; + public bool removeParts; + public List removed = new List(); + public bool addParts; + public List added = new List(); + } + + /// 一个类型的配方:输入元素、零件清单、必填与注释规则,外加逐字段例外。 + [Serializable] + public sealed class FieldRecipe + { + /// 匹配的类型全名(如 "UnityEngine.Vector2");也接受后缀写法 "Vector2"。 + public string typeName = ""; + public string inputElement = "input-field"; + public string noteWhen = ""; + public bool required; + public string requiredMessage = "此项必填"; + public string friendlyName = ""; + public List parts = new List { RecipePart.Label, RecipePart.Input }; + public List overrides = new List(); + } + + /// 一次解析的结果:拼哪些零件、用什么输入元素、必填规则、以及"是谁说了算"。 + public sealed class FieldPlan + { + public readonly List parts = new List(); + public string inputElement = ""; + public string noteWhen = ""; + public string friendlyName = ""; + public bool required; + public string requiredMessage = ""; + /// "recipe:UnityEngine.Vector2" / "override:maxSize" / "" —— 每条决定都能回溯到来源。 + public string source = ""; + } + + /// + /// 配方表的匹配与合并(纯逻辑,不引 Unity,可离线证): + /// + /// 预制体只放**零件**,配方决定**拼哪几块**,逐字段例外写在 override 里—— + /// 这样"一个字段看着很小、其实要兼容所有场景"不再堆进同一个预制体。 + /// + /// 两条硬规矩:① 匹配是"最具体的赢"(全名 > 后缀,多条同长则报歧义,不随便挑一条); + /// ② 没匹配到就说"没有配方",绝不回退到一个默认配方——凭空长出来的界面比空白更难查。 + /// + public sealed class FieldRecipeSet + { + public readonly List recipes = new List(); + + public bool TryPlan(string typeName, string fieldName, out FieldPlan plan, out string reason) + { + plan = null; + reason = ""; + if (string.IsNullOrEmpty(typeName)) + { + reason = "no type given"; + return false; + } + + var exact = new List(); + var suffix = new List(); + for (var i = 0; i < recipes.Count; i++) + { + var r = recipes[i]; + if (r == null || string.IsNullOrEmpty(r.typeName)) continue; + // 写了命名空间 = 只认这个类型("My.Ns.Vector2" 不是 "UnityEngine.Vector2"); + // 只写裸名 = 按尾段匹配任意命名空间下的同名类型。两种具体度不同,不混在一起比。 + if (r.typeName.IndexOf('.') >= 0) + { + if (string.Equals(r.typeName, typeName, StringComparison.Ordinal)) exact.Add(r); + } + else if (string.Equals(TypeNameTail(typeName), r.typeName, StringComparison.Ordinal)) suffix.Add(r); + } + + var picked = exact.Count > 0 ? exact : suffix; + if (picked.Count == 0) + { + reason = "no recipe for type \"" + typeName + "\" (" + recipes.Count + " recipe(s) in the table; " + + "add one instead of falling back to a guess)"; + return false; + } + if (picked.Count > 1) + { + var names = new List(); + for (var i = 0; i < picked.Count; i++) names.Add(picked[i].typeName); + names.Sort(StringComparer.Ordinal); + reason = "ambiguous recipe for \"" + typeName + "\": " + string.Join(", ", names.ToArray()) + + " (fix the table: Validate flags duplicates; picking one silently would render the wrong widget)"; + return false; + } + + var recipe = picked[0]; + plan = new FieldPlan + { + inputElement = recipe.inputElement, + noteWhen = recipe.noteWhen, + friendlyName = recipe.friendlyName, + required = recipe.required, + requiredMessage = recipe.requiredMessage, + source = "recipe:" + recipe.typeName + }; + for (var i = 0; i < recipe.parts.Count; i++) if (!plan.parts.Contains(recipe.parts[i])) plan.parts.Add(recipe.parts[i]); + + FieldOverride hit = null; + for (var i = 0; i < recipe.overrides.Count; i++) + { + var o = recipe.overrides[i]; + if (o != null && string.Equals(o.fieldName, fieldName, StringComparison.Ordinal)) { hit = o; break; } + } + if (hit == null) return true; + + plan.source += " + override:" + hit.fieldName; + if (hit.hasInputOverride) plan.inputElement = hit.inputOverride; + if (hit.hasNoteWhen) plan.noteWhen = hit.noteWhen; + if (hit.hasFriendlyName) plan.friendlyName = hit.friendlyName; + if (hit.hasRequired) { plan.required = hit.required; plan.requiredMessage = hit.requiredMessage; } + if (hit.removeParts) for (var i = 0; i < hit.removed.Count; i++) plan.parts.Remove(hit.removed[i]); + if (hit.addParts) for (var i = 0; i < hit.added.Count; i++) if (!plan.parts.Contains(hit.added[i])) plan.parts.Add(hit.added[i]); + return true; + } + + /// 表自身的毛病要能一次性列出来:空类型、重复类型、没零件的配方。 + public List Validate() + { + var problems = new List(); + var seen = new HashSet(StringComparer.Ordinal); + for (var i = 0; i < recipes.Count; i++) + { + var r = recipes[i]; + if (r == null) { problems.Add("recipe " + i + " is null"); continue; } + if (string.IsNullOrEmpty(r.typeName)) problems.Add("recipe " + i + " has an empty typeName"); + else if (!seen.Add(r.typeName)) problems.Add("duplicate recipe for \"" + r.typeName + "\""); + if (r.parts.Count == 0) problems.Add("\"" + r.typeName + "\" has no parts (it would render nothing)"); + for (var p = 0; p < r.parts.Count; p++) + if (!Enum.IsDefined(typeof(RecipePart), r.parts[p])) problems.Add("\"" + r.typeName + "\" part index " + p + " is not a RecipePart"); + for (var o = 0; o < r.overrides.Count; o++) + if (r.overrides[o] != null && string.IsNullOrEmpty(r.overrides[o].fieldName)) + problems.Add("\"" + r.typeName + "\" override index " + o + " has no fieldName"); + } + return problems; + } + + static string TypeNameTail(string fullName) + { + var cut = fullName.LastIndexOf('.'); + return cut < 0 ? fullName : fullName.Substring(cut + 1); + } + } +} diff --git a/Runtime/InterfaceAttributeReflectionGenerator/Scripts/XuiavrgInspectorManager.cs b/Runtime/InterfaceAttributeReflectionGenerator/Scripts/XuiavrgInspectorManager.cs index d2d81a2..2d7eb6b 100644 --- a/Runtime/InterfaceAttributeReflectionGenerator/Scripts/XuiavrgInspectorManager.cs +++ b/Runtime/InterfaceAttributeReflectionGenerator/Scripts/XuiavrgInspectorManager.cs @@ -83,6 +83,15 @@ namespace XericUI.ReflectionUIGenerator [Tooltip("类型-预制体映射配置 ScriptableObject")] public AvrgTypePrefabMappingConfig typePrefabMappingConfig; + /// + /// 可选:字段零件配方表。给了就用它决定"这块预制体上哪几件亮着"(预制体只放零件), + /// 不给就完全按预制体作者摆的样子用——两种情况会在 Console 里分得清。 + /// + [Tooltip("可选:字段配方表(预制体放零件、配方决定拼哪几块)")] + public FieldRecipeTable fieldRecipes; + + private bool _warnedNoRecipeTable; + #endregion #region Unity 生命周期 @@ -340,6 +349,8 @@ namespace XericUI.ReflectionUIGenerator uiInstance.name = entry.EntryName; uiInstance.transform.SetParent(parent, false); + ApplyFieldRecipe(entry, uiInstance); + if (uiInstance.GetComponentInChildren(true) == null) { Debug.LogError( @@ -373,6 +384,40 @@ namespace XericUI.ReflectionUIGenerator /// 3. defaultPrefab 回退 /// PrefabOverrideName 已移除,由 Tag + 类型匹配替代 /// + /// + /// 预制体挑好并挂上父级后,按配方决定"这块上的哪几件亮着"。 + /// 没给配方表 / 这块预制体没有零件槽 ⇒ 原样使用,但各自说一句: + /// "我改了配方没生效"和"根本没有配方表"不能长得一样。 + /// + private void ApplyFieldRecipe(IFieldEntry entry, GameObject instance) + { + if (fieldRecipes == null) + { + if (!_warnedNoRecipeTable) + { + _warnedNoRecipeTable = true; + Debug.Log("[Xuiavrg] 未指定 fieldRecipes:零件激活完全按预制体原样,配方表不参与。"); + } + return; + } + + var assembler = instance.GetComponent(); + if (assembler == null) return; + + var bindings = entry.Bindings; + var valueType = bindings?.Count > 0 ? bindings[0].MetaData?.ValueType : null; + if (!fieldRecipes.TryPlan(valueType == null ? "" : valueType.FullName, entry.EntryName, + out var plan, out var reason)) + { + Debug.LogWarning($"[Xuiavrg] Entry '{entry.EntryName}'(类型 {valueType?.Name ?? "未知"}):{reason}"); + return; + } + + assembler.Apply(plan, out var problems); + for (var i = 0; i < problems.Count; i++) + Debug.LogWarning($"[Xuiavrg] Entry '{entry.EntryName}' 按 {plan.source} 装配时:{problems[i]}"); + } + private GameObject ResolvePrefab(IFieldEntry entry) { if (typePrefabMappingConfig == null) diff --git a/Tests~/RecipeProbe/.gitignore b/Tests~/RecipeProbe/.gitignore new file mode 100644 index 0000000..6075fbd --- /dev/null +++ b/Tests~/RecipeProbe/.gitignore @@ -0,0 +1,3 @@ +.out/ +bin/ +obj/ diff --git a/Tests~/RecipeProbe/Program.cs b/Tests~/RecipeProbe/Program.cs new file mode 100644 index 0000000..34f1bbe --- /dev/null +++ b/Tests~/RecipeProbe/Program.cs @@ -0,0 +1,162 @@ +// 字段配方表的匹配/合并门:纯逻辑,不启 Unity,直接编包里的 Runtime/Form/FieldRecipeSet.cs。 +// 判的是"最具体的赢 / 没配方就说不匹配 / 逐字段例外只覆盖写了的项",不是画面。 +using System; +using System.Collections.Generic; +using XericUI.ReflectionUIGenerator; + +internal static class Program +{ + static int failures; + static int checks; + + static void Check(bool ok, string what) + { + checks++; + Console.WriteLine((ok ? " PASS " : " FAIL ") + what); + if (!ok) failures++; + } + + static FieldRecipe Recipe(string type, params RecipePart[] parts) + { + var r = new FieldRecipe { typeName = type }; + r.parts.Clear(); + r.parts.AddRange(parts); + return r; + } + + static int Main() + { + Console.WriteLine("== field recipe probe =="); + var set = new FieldRecipeSet(); + set.recipes.Add(Recipe("UnityEngine.Vector2", RecipePart.Label, RecipePart.Input)); + set.recipes.Add(Recipe("Single", RecipePart.Label, RecipePart.Input, RecipePart.Note, RecipePart.Legend)); + var single = set.recipes[1]; + single.inputElement = "slider"; + single.noteWhen = "onHover"; + var overrideMax = new FieldOverride { fieldName = "maxSize", hasInputOverride = true, inputOverride = "input-field-numeric" }; + single.overrides.Add(overrideMax); + single.overrides.Add(new FieldOverride + { + fieldName = "enabled", hasRequired = true, required = true, requiredMessage = "必须选", + addParts = true, added = new List { RecipePart.RequiredMark }, + removeParts = true, removed = new List { RecipePart.Legend } + }); + + FieldPlan plan; + string why; + Check(set.TryPlan("UnityEngine.Vector2", "size", out plan, out why) + && plan.parts.Count == 2 && plan.inputElement == "input-field", + "exact type name matches and keeps the recipe's parts in order"); + + set.recipes.Add(Recipe("Color", RecipePart.Label, RecipePart.Input)); + Check(set.TryPlan("UnityEngine.Color", "tint", out plan, out why) && plan.source == "recipe:Color", + "a bare recipe name matches the tail of any namespace's type"); + Check(!set.TryPlan("My.Ns.Vector2", "size", out plan, out why) && why.Contains("no recipe"), + "a namespaced recipe does NOT match a different type with the same tail"); + + var specificity = new FieldRecipeSet(); + specificity.recipes.Add(Recipe("Vector2", RecipePart.Label, RecipePart.Input)); + specificity.recipes.Add(Recipe("UnityEngine.Vector2", RecipePart.Label, RecipePart.Note, RecipePart.Input)); + Check(specificity.TryPlan("UnityEngine.Vector2", "v", out plan, out why) + && plan.source == "recipe:UnityEngine.Vector2" && plan.parts.Contains(RecipePart.Note), + "the most specific recipe wins (full name beats bare name)"); + + Check(!set.TryPlan("UnityEngine.Quaternion", "rot", out plan, out why) && why.Contains("no recipe"), + "no recipe is reported as missing, not resolved to a guess: " + why); + + var ambiguous = new FieldRecipeSet(); + ambiguous.recipes.Add(Recipe("Vector2", RecipePart.Label, RecipePart.Input)); + ambiguous.recipes.Add(Recipe("Vector2", RecipePart.Label, RecipePart.Note, RecipePart.Input)); + Check(!ambiguous.TryPlan("Baz.Vector2", "v", out plan, out why) && why.Contains("ambiguous"), + "two equally specific recipes are refused with both names: " + why); + + Check(set.TryPlan("Single", "volume", out plan, out why) + && plan.inputElement == "slider" && plan.noteWhen == "onHover" + && plan.parts.Contains(RecipePart.Legend) && !plan.parts.Contains(RecipePart.RequiredMark) + && plan.source == "recipe:Single", + "without an override the recipe decides everything"); + + Check(set.TryPlan("Single", "maxSize", out plan, out why) + && plan.inputElement == "input-field-numeric" && plan.noteWhen == "onHover" + && plan.source.EndsWith("+ override:maxSize"), + "an override replaces only what it writes"); + + Check(set.TryPlan("Single", "enabled", out plan, out why) + && plan.required && plan.requiredMessage == "必须选" + && plan.parts.Contains(RecipePart.RequiredMark) && !plan.parts.Contains(RecipePart.Legend), + "an override can add and remove parts (required mark in, legend out)"); + + var before = plan.parts.Count; + Check(set.TryPlan("Single", "enabled", out plan, out why) && plan.parts.Count == before + && plan.parts.FindAll(p => p == RecipePart.RequiredMark).Count == 1, + "re-planning the same field never duplicates a part (idempotent)"); + + var broken = new FieldRecipeSet(); + broken.recipes.Add(Recipe("", RecipePart.Label, RecipePart.Input)); + broken.recipes.Add(Recipe("Single")); + broken.recipes.Add(Recipe("Single", RecipePart.Label, RecipePart.Input)); + var bad = broken.Validate(); + Check(bad.Count == 3 && bad[0].Contains("empty typeName") && bad[1].Contains("no parts") && bad[2].Contains("duplicate"), + "Validate names every table defect (empty type / no parts / duplicate): " + string.Join(" | ", bad.ToArray())); + + var keyless = new FieldRecipeSet(); + var withKeyless = Recipe("Single", RecipePart.Label, RecipePart.Input); + withKeyless.overrides.Add(new FieldOverride { fieldName = "" }); + keyless.recipes.Add(withKeyless); + Check(keyless.Validate().Count == 1 && keyless.Validate()[0].Contains("no fieldName"), + "an override without a fieldName is a table error, not a silent no-op"); + + // ---- 装配决策:槽位/元素对不上一句都不能少说 ---- + var simple = new FieldPlan { source = "recipe:Single" }; + simple.parts.Add(RecipePart.Label); + simple.parts.Add(RecipePart.Input); + simple.inputElement = "input-field"; + List on; + List problems; + Check(FieldLayout.Resolve(simple, new[] { "label", "input" }, new[] { "input-field", "slider" }, out on, out problems) + && on.Contains("label") && on.Contains("input") && on.Contains("input/input-field") && problems.Count == 0, + "a well-formed plan activates exactly its slots plus the chosen element"); + + Check(!FieldLayout.Resolve(simple, new[] { "label" }, new[] { "input-field" }, out on, out problems) + && problems[0].Contains("no such slot"), + "a part the prefab has no slot for is a problem, not a silently dropped widget: " + problems[0]); + + var wrongElement = new FieldPlan { source = "recipe:Single" }; + wrongElement.parts.Add(RecipePart.Input); + wrongElement.inputElement = "enum-dropdown"; + Check(!FieldLayout.Resolve(wrongElement, new[] { "input" }, new[] { "input-field", "slider" }, out on, out problems) + && problems[0].Contains("enum-dropdown") && problems[0].Contains("slider"), + "an unknown input element names itself AND lists what the prefab really has: " + problems[0]); + + var needsMark = new FieldPlan { source = "recipe:Single", required = true, requiredMessage = "必须填" }; + needsMark.parts.Add(RecipePart.Label); + var markOk = FieldLayout.Resolve(needsMark, new[] { "label" }, null, out on, out problems); + Check(markOk && problems[0].Contains("required-mark") && on.Contains("label"), + "a required field with no place to say so still assembles, but reports it (ok=" + markOk + "): " + problems[0]); + + var withMark = new FieldPlan { source = "recipe:Single", required = true }; + withMark.parts.Add(RecipePart.Label); + Check(FieldLayout.Resolve(withMark, new[] { "label", "required-mark", "error" }, null, out on, out problems) + && on.Contains("required-mark") && on.Contains("error"), + "required-mark joins the active list on its own, and the error slot is always ready"); + + Check(!FieldLayout.Resolve(withMark, new string[0], null, out on, out problems) + && problems[0].Contains("no slots at all"), + "a prefab exposing nothing is called out (that is a broken prefab, not an empty layout)"); + + var elementWithoutInput = new FieldPlan { source = "recipe:Single", inputElement = "slider" }; + elementWithoutInput.parts.Add(RecipePart.Label); + Check(!FieldLayout.Resolve(elementWithoutInput, new[] { "label" }, new[] { "slider" }, out on, out problems) + && problems[0].Contains("no Input part"), + "naming an input element without planning the Input slot is refused"); + + Console.WriteLine(); + if (failures == 0) + { + Console.WriteLine("VERDICT RECIPE-PROBE PASS (" + checks + "/" + checks + ")"); + return 0; + } + Console.WriteLine("VERDICT RECIPE-PROBE FAIL (" + failures + "/" + checks + " failed)"); + return 1; + } +} diff --git a/Tests~/RecipeProbe/RecipeProbe.csproj b/Tests~/RecipeProbe/RecipeProbe.csproj new file mode 100644 index 0000000..3b84610 --- /dev/null +++ b/Tests~/RecipeProbe/RecipeProbe.csproj @@ -0,0 +1,16 @@ + + + Exe + net8.0 + latest + disable + false + field_recipe_probe + + + + + + + + diff --git a/Tests~/RecipeProbe/run.sh b/Tests~/RecipeProbe/run.sh new file mode 100644 index 0000000..0cb29c1 --- /dev/null +++ b/Tests~/RecipeProbe/run.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# 字段配方的离线门:dotnet 编包里的纯逻辑并跑断言。exit 0=PASS, 1=FAIL, 2=跑不起来 +set -u +HERE=$(cd "$(dirname "$0")" && pwd) +OUT="$HERE/.out" +mkdir -p "$OUT" +if ! command -v dotnet >/dev/null 2>&1; then + echo "VERDICT RECIPE-PROBE INSTRUMENT-BAD (dotnet not on PATH)"; exit 2 +fi +dotnet run --project "$HERE/RecipeProbe.csproj" -c Release --nologo 2>&1 | tee "$OUT/run.log" +rc=${PIPESTATUS[0]} +if ! grep -q "VERDICT RECIPE-PROBE" "$OUT/run.log"; then + echo "VERDICT RECIPE-PROBE INSTRUMENT-BAD (no verdict line; see $OUT/run.log)"; exit 2 +fi +exit "$rc"