From 2ea30cfd28999087f2c5ac50c82d1ba5f2eda164 Mon Sep 17 00:00:00 2001 From: tea Date: Sun, 5 Feb 2023 22:14:21 +0800 Subject: [PATCH] Add glance feature (draft) --- BT-lang/Runtime/BTL.cs | 26 ++++++++++++-- BT-lang/Runtime/Summarizer.cs | 50 ++++++++++++++++++++++++++ BT-lang/Runtime/Summarizer.cs.meta | 11 ++++++ Elk/Runtime/Basic/Builder.cs | 2 +- Elk/Runtime/Basic/Runtime/CallGraph.cs | 11 ++++++ 5 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 BT-lang/Runtime/Summarizer.cs create mode 100644 BT-lang/Runtime/Summarizer.cs.meta diff --git a/BT-lang/Runtime/BTL.cs b/BT-lang/Runtime/BTL.cs index 9fac39a..25586f8 100644 --- a/BT-lang/Runtime/BTL.cs +++ b/BT-lang/Runtime/BTL.cs @@ -1,6 +1,5 @@ -using System; -using System.Linq; -using UnityEngine; +using System; using System.Collections.Generic; using System.Linq; +using UnityEngine; using UnityEditor; using Elk; using Elk.Basic; using Elk.Basic.Runtime; using Cx = Elk.Basic.Runtime.Context; using Record = Elk.Memory.Record; @@ -26,6 +25,12 @@ public partial class BTL : MonoBehaviour, LogSource{ [Header("Debugging")] public bool pauseOnErrors = false; public bool logErrors = true; + #if UNITY_EDITOR + const float editorLabelHeight = 3.2f; + Color editorLabelColor = Color.black; + string editorLabelText = "Hello"; + static Summarizer summarizer = new Summarizer(); + #endif // Vars _vars; @@ -93,6 +98,9 @@ void Update(){ vars.output = interpreter.Run(vars.context)?.ToString(); vars.exceptionMessage = null; vars.log = vars.context.graph.Format(); + var strings = new List(); + vars.context.graph.Dump(strings); + editorLabelText = summarizer.AddAll(strings); if(useHistory) history.Log(vars.log, transform); vars.context = null; }catch(Exception ex){ @@ -104,6 +112,18 @@ void Update(){ } } + #if UNITY_EDITOR + void OnDrawGizmos(){ + GUIStyle style = new GUIStyle(); + style.normal.textColor = editorLabelColor; + Handles.Label( + transform.position + Vector3.up * editorLabelHeight, + editorLabelText, + style + ); + } + #endif + void OnValidate(){ if(path == null) return; if(path.EndsWith(".txt")){ diff --git a/BT-lang/Runtime/Summarizer.cs b/BT-lang/Runtime/Summarizer.cs new file mode 100644 index 0000000..a04dbcb --- /dev/null +++ b/BT-lang/Runtime/Summarizer.cs @@ -0,0 +1,50 @@ +using System.Collections.Generic; + +public class Summarizer{ + + List entries = new List(); + + public string AddAll(List arg){ + Entry sel = null; + string selValue = null; + foreach(var str in arg){ + if(str.Contains("✗")) continue; + var e = Add(str); + if(sel == null || sel.count > e.count){ + sel = e; + selValue = str; + } + } + return selValue; + } + + + Entry Add(string arg){ + arg = Strip(arg); + var e = entries.Find( x => x.value == arg); + if(e == null){ + e = new Entry(1, arg); + entries.Add(e); + return e; + }else{ + e.count ++; + return e; + } + } + + string Strip(string arg){ + int i = arg.IndexOf("("); + if(i >= 0) return arg.Substring(0, i); + else return arg; + } + + class Entry{ + public int count; + public string value; + public Entry(int count, string value){ + this.count = count; + this.value = value; + } + } + +} diff --git a/BT-lang/Runtime/Summarizer.cs.meta b/BT-lang/Runtime/Summarizer.cs.meta new file mode 100644 index 0000000..8dd810c --- /dev/null +++ b/BT-lang/Runtime/Summarizer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c0c1eecc793d5cc408e4c58cb514399e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Elk/Runtime/Basic/Builder.cs b/Elk/Runtime/Basic/Builder.cs index 6f8e695..dc54923 100644 --- a/Elk/Runtime/Basic/Builder.cs +++ b/Elk/Runtime/Basic/Builder.cs @@ -39,7 +39,7 @@ Module Parse(string path, IEnumerable submodules){ src = src.Substring(shebang.Length); // TODO submodule injection will confuse line numbers if(submodules != null) foreach(var e in submodules){ - //nityEngine.Debug.Log($"Add submodule {e} (ref: {path})"); + if(string.IsNullOrEmpty(e)) continue; src = "with " + e + ";\n" + src; } try{ diff --git a/Elk/Runtime/Basic/Runtime/CallGraph.cs b/Elk/Runtime/Basic/Runtime/CallGraph.cs index 1fd0c63..31ea7fb 100644 --- a/Elk/Runtime/Basic/Runtime/CallGraph.cs +++ b/Elk/Runtime/Basic/Runtime/CallGraph.cs @@ -43,6 +43,17 @@ public string Format(){ return @out.ToString(); } + public void Dump(List @out) + => Dump(@out, root); + + void Dump(List @out, Node arg){ + @out.Add(arg.info); + if(arg.children == null) return; + foreach(var child in arg.children){ + Dump(@out, child); + } + } + void Format(Node node, StringBuilder @out, string spaces){ @out.Append(spaces); @out.Append(node.info);