From 03d207423c429e0311e857d36f8a817e5bc4b83a Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Thu, 31 Mar 2016 17:19:06 +0100 Subject: [PATCH 1/2] Register bound types automatically --- .../FungusScript/Scripts/FungusScript.cs | 39 ++++++++----------- .../FungusScript/Scripts/LuaBindings.cs | 8 ++++ 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/Assets/Fungus/FungusScript/Scripts/FungusScript.cs b/Assets/Fungus/FungusScript/Scripts/FungusScript.cs index 13747780..16d42331 100644 --- a/Assets/Fungus/FungusScript/Scripts/FungusScript.cs +++ b/Assets/Fungus/FungusScript/Scripts/FungusScript.cs @@ -103,12 +103,6 @@ namespace Fungus [HideInInspector] public List registerTypes = new List(); - /// - /// A text file listing the c# extension types that can be accessed from Lua. - /// - [HideInInspector] - public List registerExtensionTypes = new List(); - /// /// Instance of remote debugging service when debugging option is enabled. /// @@ -187,22 +181,13 @@ namespace Fungus char[] separators = { '\r', '\n' }; foreach (string typeName in textFile.text.Split(separators, StringSplitOptions.RemoveEmptyEntries)) { - RegisterType(typeName.Trim(), false); - } - } - - // Register extension types - foreach (TextAsset textFile in registerExtensionTypes) - { - if (textFile == null) - { - continue; - } + // Skip comments and empty lines + if (typeName.StartsWith("#") || typeName.Trim() == "") + { + continue; + } - char[] separators = { '\r', '\n' }; - foreach (string typeName in textFile.text.Split(separators, StringSplitOptions.RemoveEmptyEntries)) - { - RegisterType(typeName.Trim(), true); + RegisterType(typeName); } } } @@ -210,9 +195,17 @@ namespace Fungus /// /// Register a type given it's assembly qualified name. /// - public virtual void RegisterType(string typeName, bool extensionType) + public static void RegisterType(string typeName) { - System.Type t = System.Type.GetType(typeName); + bool extensionType = false; + string registerName = typeName; + if (typeName.StartsWith("E:")) + { + extensionType = true; + registerName = registerName.Substring(2); + } + + System.Type t = System.Type.GetType(registerName); if (t == null) { UnityEngine.Debug.LogWarning("Type not found: " + typeName); diff --git a/Assets/Fungus/FungusScript/Scripts/LuaBindings.cs b/Assets/Fungus/FungusScript/Scripts/LuaBindings.cs index 5508542d..56315f52 100644 --- a/Assets/Fungus/FungusScript/Scripts/LuaBindings.cs +++ b/Assets/Fungus/FungusScript/Scripts/LuaBindings.cs @@ -51,6 +51,8 @@ namespace Fungus [Tooltip("The list of Unity objects to be bound to make them accessible in Lua script.")] public List boundObjects = new List(); + public List boundTypes = new List(); + /// /// Always ensure there is at least one row in the bound objects list. /// @@ -95,6 +97,12 @@ namespace Fungus Debug.LogError("Bindings table must not be null"); } + // Register types of bound object with MoonSharp + foreach (string typeName in boundTypes) + { + FungusScript.RegisterType(typeName); + } + for (int i = 0; i < boundObjects.Count; ++i) { // Ignore empty keys From e60488bc5b65a2ab4bc9b8b576fc80782e0cda6b Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Thu, 31 Mar 2016 17:19:27 +0100 Subject: [PATCH 2/2] Single file for registering types in text file --- .../Resources/Types/ExtensionTypes.txt | 2 +- .../Resources/Types/RegisterTypes.txt | 70 +++++++++++-------- .../Scripts/Editor/FungusScriptEditor.cs | 18 ----- .../Scripts/Editor/LuaBindingsEditor.cs | 21 ++++-- 4 files changed, 58 insertions(+), 53 deletions(-) diff --git a/Assets/Fungus/FungusScript/Resources/Types/ExtensionTypes.txt b/Assets/Fungus/FungusScript/Resources/Types/ExtensionTypes.txt index 0897d1de..c65238fb 100644 --- a/Assets/Fungus/FungusScript/Resources/Types/ExtensionTypes.txt +++ b/Assets/Fungus/FungusScript/Resources/Types/ExtensionTypes.txt @@ -1,2 +1,2 @@ -Fungus.FungusScriptExtensions, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null + diff --git a/Assets/Fungus/FungusScript/Resources/Types/RegisterTypes.txt b/Assets/Fungus/FungusScript/Resources/Types/RegisterTypes.txt index 0a0e9324..03133998 100644 --- a/Assets/Fungus/FungusScript/Resources/Types/RegisterTypes.txt +++ b/Assets/Fungus/FungusScript/Resources/Types/RegisterTypes.txt @@ -1,32 +1,40 @@ -Fungus.AnimatorVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.AudioSourceVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.Block, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.BooleanVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.Character, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.ColorVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.Command, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.CommandInfoAttribute, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.FacingDirection, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.FloatVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.Flowchart, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.FungusPrefs, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.FungusScript, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.GameObjectVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.IntegerVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.Label, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.MaterialVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.MenuDialog, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.ObjectVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.PortraitState, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.SayDialog, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.SpriteVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.StringVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.Task, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.TextureVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.TransformVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.Variable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.Vector2Variable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null -Fungus.Vector3Variable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null +# List of c# types to register with MoonSharp +# LuaBindings usually takes care of registering the types of bound objects, but in some +# cases you'll need to register a type manually by adding it to this list. +# You can also register types using MoonSharp's UserData class in the Awake method of a component. +# To register an extension type, add the E: prefix +# If the type is defined in the main assembly then you can just use namespace.typename +# If the type is defined in any other assembly you need to use the full assembly qualified type name. + +Fungus.AnimatorVariable +Fungus.AudioSourceVariable +Fungus.Block +Fungus.BooleanVariable +Fungus.Character +Fungus.ColorVariable +Fungus.Command +Fungus.CommandInfoAttribute +Fungus.FacingDirection +Fungus.FloatVariable +Fungus.Flowchart +Fungus.FungusPrefs +Fungus.FungusScript +Fungus.GameObjectVariable +Fungus.IntegerVariable +Fungus.Label +Fungus.MaterialVariable +Fungus.MenuDialog +Fungus.ObjectVariable +Fungus.PortraitState +Fungus.SayDialog +Fungus.SpriteVariable +Fungus.StringVariable +Fungus.Task +Fungus.TextureVariable +Fungus.TransformVariable +Fungus.Variable +Fungus.Vector2Variable +Fungus.Vector3Variable System.Action, System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Collections.IEnumerator, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 @@ -84,3 +92,7 @@ UnityEngine.Vector2, UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyTok UnityEngine.Vector3, UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null UnityEngine.Vector4, UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null UnityEngine.VerticalWrapMode, UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null + +# Extension types + +E:Fungus.FungusScriptExtensions diff --git a/Assets/Fungus/FungusScript/Scripts/Editor/FungusScriptEditor.cs b/Assets/Fungus/FungusScript/Scripts/Editor/FungusScriptEditor.cs index 444a561e..f486c056 100644 --- a/Assets/Fungus/FungusScript/Scripts/Editor/FungusScriptEditor.cs +++ b/Assets/Fungus/FungusScript/Scripts/Editor/FungusScriptEditor.cs @@ -16,9 +16,6 @@ namespace Fungus protected SerializedProperty registerTypesProp; protected ReorderableList registerTypeList; - protected SerializedProperty registerExtensionTypesProp; - protected ReorderableList registerExtensionTypesList; - protected virtual void OnEnable() { registerTypesProp = serializedObject.FindProperty("registerTypes"); @@ -33,20 +30,6 @@ namespace Fungus SerializedProperty element = registerTypesProp.GetArrayElementAtIndex(index); EditorGUI.PropertyField(r, element, new GUIContent("")); }; - - registerExtensionTypesProp = serializedObject.FindProperty("registerExtensionTypes"); - - registerExtensionTypesList = new ReorderableList(serializedObject, registerExtensionTypesProp, true, true, true, true); - - registerExtensionTypesList.drawHeaderCallback = (Rect rect) => { - EditorGUI.LabelField(rect, "Register Extension Types"); - }; - - registerExtensionTypesList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => { - Rect r = new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight); - SerializedProperty element = registerExtensionTypesProp.GetArrayElementAtIndex(index); - EditorGUI.PropertyField(r, element, new GUIContent("")); - }; } public override void OnInspectorGUI() @@ -56,7 +39,6 @@ namespace Fungus serializedObject.Update(); registerTypeList.DoLayoutList(); - registerExtensionTypesList.DoLayoutList(); serializedObject.ApplyModifiedProperties(); } diff --git a/Assets/Fungus/FungusScript/Scripts/Editor/LuaBindingsEditor.cs b/Assets/Fungus/FungusScript/Scripts/Editor/LuaBindingsEditor.cs index c36dbb31..3bb77b28 100644 --- a/Assets/Fungus/FungusScript/Scripts/Editor/LuaBindingsEditor.cs +++ b/Assets/Fungus/FungusScript/Scripts/Editor/LuaBindingsEditor.cs @@ -148,6 +148,12 @@ namespace Fungus ShowBindingMemberInfo(); + // Update the bound types on every tick to make sure they're up to date. + // This could be a bit heavy on performance if the bound object list is long, but + // I couldn't get it to work reliably by only updating when the list has changed. + // This only happens when inspecting a Fungus Bindings component so I think it'll be ok. + PopulateBoundTypes(); + serializedObject.ApplyModifiedProperties(); } @@ -339,7 +345,7 @@ namespace Fungus /// /// Update the list of bound types on the LuaBindings object. /// - protected virtual void PopulateUserDataTypes() + protected virtual void PopulateBoundTypes() { LuaBindings luaBindings = target as LuaBindings; @@ -361,12 +367,17 @@ namespace Fungus } } - string s = ""; + // Store the final list of types in the luaBindings object + SerializedProperty boundTypesProp = serializedObject.FindProperty("boundTypes"); + boundTypesProp.ClearArray(); + int index = 0; foreach (System.Type t in typeSet) { - s += t.AssemblyQualifiedName + "\n"; + boundTypesProp.InsertArrayElementAtIndex(index); + SerializedProperty element = boundTypesProp.GetArrayElementAtIndex(index); + element.stringValue = t.AssemblyQualifiedName; + index++; } - Debug.Log(s); } /// @@ -432,7 +443,7 @@ namespace Fungus AddSubType(typeSet, containedType); } } - else + else if (t != typeof(System.Object)) { // Non-IEnumerable/IEnumerator types will be registered. typeSet.Add(t);