Browse Source

Register bound types automatically

master
chrisgregan 9 years ago
parent
commit
03d207423c
  1. 33
      Assets/Fungus/FungusScript/Scripts/FungusScript.cs
  2. 8
      Assets/Fungus/FungusScript/Scripts/LuaBindings.cs

33
Assets/Fungus/FungusScript/Scripts/FungusScript.cs

@ -103,12 +103,6 @@ namespace Fungus
[HideInInspector]
public List<TextAsset> registerTypes = new List<TextAsset>();
/// <summary>
/// A text file listing the c# extension types that can be accessed from Lua.
/// </summary>
[HideInInspector]
public List<TextAsset> registerExtensionTypes = new List<TextAsset>();
/// <summary>
/// Instance of remote debugging service when debugging option is enabled.
/// </summary>
@ -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)
// 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
/// <summary>
/// Register a type given it's assembly qualified name.
/// </summary>
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);

8
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<BoundObject> boundObjects = new List<BoundObject>();
public List<string> boundTypes = new List<string>();
/// <summary>
/// Always ensure there is at least one row in the bound objects list.
/// </summary>
@ -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

Loading…
Cancel
Save