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. 39
      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
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

18
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();
}

21
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
/// <summary>
/// Update the list of bound types on the LuaBindings object.
/// </summary>
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);
}
/// <summary>
@ -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);

39
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)
{
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
/// <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