From 03d207423c429e0311e857d36f8a817e5bc4b83a Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Thu, 31 Mar 2016 17:19:06 +0100 Subject: [PATCH] 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