Browse Source

Merge pull request #424 from FungusGames/lua-bindings-base

Lua bindings base
master
Chris Gregan 9 years ago
parent
commit
fd3f057170
  1. 2
      Assets/Fungus/FungusScript/Resources/Types/ExtensionTypes.txt
  2. 70
      Assets/Fungus/FungusScript/Resources/Types/RegisterTypes.txt
  3. 18
      Assets/Fungus/FungusScript/Scripts/Editor/FungusScriptEditor.cs
  4. 21
      Assets/Fungus/FungusScript/Scripts/Editor/LuaBindingsEditor.cs
  5. 33
      Assets/Fungus/FungusScript/Scripts/FungusScript.cs
  6. 8
      Assets/Fungus/FungusScript/Scripts/LuaBindings.cs

2
Assets/Fungus/FungusScript/Resources/Types/ExtensionTypes.txt

@ -1,2 +1,2 @@
Fungus.FungusScriptExtensions, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null 

70
Assets/Fungus/FungusScript/Resources/Types/RegisterTypes.txt

@ -1,32 +1,40 @@
Fungus.AnimatorVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null # List of c# types to register with MoonSharp
Fungus.AudioSourceVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null # LuaBindings usually takes care of registering the types of bound objects, but in some
Fungus.Block, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null # cases you'll need to register a type manually by adding it to this list.
Fungus.BooleanVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null # You can also register types using MoonSharp's UserData class in the Awake method of a component.
Fungus.Character, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null # To register an extension type, add the E: prefix
Fungus.ColorVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null # If the type is defined in the main assembly then you can just use namespace.typename
Fungus.Command, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null # If the type is defined in any other assembly you need to use the full assembly qualified type name.
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.AnimatorVariable
Fungus.FloatVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Fungus.AudioSourceVariable
Fungus.Flowchart, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Fungus.Block
Fungus.FungusPrefs, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Fungus.BooleanVariable
Fungus.FungusScript, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Fungus.Character
Fungus.GameObjectVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Fungus.ColorVariable
Fungus.IntegerVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Fungus.Command
Fungus.Label, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Fungus.CommandInfoAttribute
Fungus.MaterialVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Fungus.FacingDirection
Fungus.MenuDialog, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Fungus.FloatVariable
Fungus.ObjectVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Fungus.Flowchart
Fungus.PortraitState, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Fungus.FungusPrefs
Fungus.SayDialog, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Fungus.FungusScript
Fungus.SpriteVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Fungus.GameObjectVariable
Fungus.StringVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Fungus.IntegerVariable
Fungus.Task, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Fungus.Label
Fungus.TextureVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Fungus.MaterialVariable
Fungus.TransformVariable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Fungus.MenuDialog
Fungus.Variable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Fungus.ObjectVariable
Fungus.Vector2Variable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Fungus.PortraitState
Fungus.Vector3Variable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null 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.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.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Collections.IEnumerator, 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.Vector3, UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
UnityEngine.Vector4, 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 UnityEngine.VerticalWrapMode, UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
# Extension types
E:Fungus.FungusScriptExtensions

18
Assets/Fungus/FungusScript/Scripts/Editor/FungusScriptEditor.cs

@ -16,9 +16,6 @@ namespace Fungus
protected SerializedProperty registerTypesProp; protected SerializedProperty registerTypesProp;
protected ReorderableList registerTypeList; protected ReorderableList registerTypeList;
protected SerializedProperty registerExtensionTypesProp;
protected ReorderableList registerExtensionTypesList;
protected virtual void OnEnable() protected virtual void OnEnable()
{ {
registerTypesProp = serializedObject.FindProperty("registerTypes"); registerTypesProp = serializedObject.FindProperty("registerTypes");
@ -33,20 +30,6 @@ namespace Fungus
SerializedProperty element = registerTypesProp.GetArrayElementAtIndex(index); SerializedProperty element = registerTypesProp.GetArrayElementAtIndex(index);
EditorGUI.PropertyField(r, element, new GUIContent("")); 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() public override void OnInspectorGUI()
@ -56,7 +39,6 @@ namespace Fungus
serializedObject.Update(); serializedObject.Update();
registerTypeList.DoLayoutList(); registerTypeList.DoLayoutList();
registerExtensionTypesList.DoLayoutList();
serializedObject.ApplyModifiedProperties(); serializedObject.ApplyModifiedProperties();
} }

21
Assets/Fungus/FungusScript/Scripts/Editor/LuaBindingsEditor.cs

@ -148,6 +148,12 @@ namespace Fungus
ShowBindingMemberInfo(); 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(); serializedObject.ApplyModifiedProperties();
} }
@ -339,7 +345,7 @@ namespace Fungus
/// <summary> /// <summary>
/// Update the list of bound types on the LuaBindings object. /// Update the list of bound types on the LuaBindings object.
/// </summary> /// </summary>
protected virtual void PopulateUserDataTypes() protected virtual void PopulateBoundTypes()
{ {
LuaBindings luaBindings = target as LuaBindings; 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) 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);
} }
/// <summary> /// <summary>
@ -432,7 +443,7 @@ namespace Fungus
AddSubType(typeSet, containedType); AddSubType(typeSet, containedType);
} }
} }
else else if (t != typeof(System.Object))
{ {
// Non-IEnumerable/IEnumerator types will be registered. // Non-IEnumerable/IEnumerator types will be registered.
typeSet.Add(t); typeSet.Add(t);

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

@ -103,12 +103,6 @@ namespace Fungus
[HideInInspector] [HideInInspector]
public List<TextAsset> registerTypes = new List<TextAsset>(); 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> /// <summary>
/// Instance of remote debugging service when debugging option is enabled. /// Instance of remote debugging service when debugging option is enabled.
/// </summary> /// </summary>
@ -187,22 +181,13 @@ namespace Fungus
char[] separators = { '\r', '\n' }; char[] separators = { '\r', '\n' };
foreach (string typeName in textFile.text.Split(separators, StringSplitOptions.RemoveEmptyEntries)) foreach (string typeName in textFile.text.Split(separators, StringSplitOptions.RemoveEmptyEntries))
{ {
RegisterType(typeName.Trim(), false); // Skip comments and empty lines
} if (typeName.StartsWith("#") || typeName.Trim() == "")
}
// Register extension types
foreach (TextAsset textFile in registerExtensionTypes)
{
if (textFile == null)
{ {
continue; continue;
} }
char[] separators = { '\r', '\n' }; RegisterType(typeName);
foreach (string typeName in textFile.text.Split(separators, StringSplitOptions.RemoveEmptyEntries))
{
RegisterType(typeName.Trim(), true);
} }
} }
} }
@ -210,9 +195,17 @@ namespace Fungus
/// <summary> /// <summary>
/// Register a type given it's assembly qualified name. /// Register a type given it's assembly qualified name.
/// </summary> /// </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) if (t == null)
{ {
UnityEngine.Debug.LogWarning("Type not found: " + typeName); 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.")] [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<BoundObject> boundObjects = new List<BoundObject>();
public List<string> boundTypes = new List<string>();
/// <summary> /// <summary>
/// Always ensure there is at least one row in the bound objects list. /// Always ensure there is at least one row in the bound objects list.
/// </summary> /// </summary>
@ -95,6 +97,12 @@ namespace Fungus
Debug.LogError("Bindings table must not be null"); 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) for (int i = 0; i < boundObjects.Count; ++i)
{ {
// Ignore empty keys // Ignore empty keys

Loading…
Cancel
Save