You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
174 lines
6.3 KiB
174 lines
6.3 KiB
9 years ago
|
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
|
||
|
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
|
||
9 years ago
|
|
||
9 years ago
|
using UnityEngine;
|
||
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using MoonSharp.Interpreter;
|
||
|
|
||
|
namespace Fungus
|
||
|
{
|
||
9 years ago
|
/// <summary>
|
||
|
/// Base class for a component which registers Lua Bindings.
|
||
|
/// When the Lua Environment initialises, it finds all components in the scene that inherit
|
||
|
/// from LuaBindingsBase and calls them to add their bindings.
|
||
|
/// </summary>
|
||
9 years ago
|
public abstract class LuaBindingsBase : MonoBehaviour, ILuaBindings
|
||
9 years ago
|
{
|
||
|
/// <summary>
|
||
|
/// Add all declared bindings to the globals table.
|
||
|
/// </summary>
|
||
9 years ago
|
public abstract void AddBindings(ILuaEnvironment luaEnv);
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
/// <summary>
|
||
|
/// Component which manages a list of bound objects to be accessed in Lua scripts.
|
||
|
/// </summary>
|
||
|
[ExecuteInEditMode]
|
||
9 years ago
|
public class LuaBindings : LuaBindingsBase
|
||
9 years ago
|
{
|
||
|
/// <summary>
|
||
|
/// Represents a single Unity object (+ optional component) bound to a string key.
|
||
|
/// </summary>
|
||
|
[Serializable]
|
||
|
public class BoundObject
|
||
|
{
|
||
|
public string key;
|
||
|
public UnityEngine.Object obj;
|
||
|
public Component component;
|
||
|
}
|
||
|
|
||
9 years ago
|
[Tooltip("Add bindings to every Lua Environment in the scene. If false, only add bindings to a specific Lua Environment.")]
|
||
9 years ago
|
[SerializeField] protected bool allEnvironments = true;
|
||
9 years ago
|
|
||
|
[Tooltip("The specific LuaEnvironment to register the bindings in.")]
|
||
9 years ago
|
[SerializeField] protected LuaEnvironment luaEnvironment;
|
||
9 years ago
|
|
||
9 years ago
|
/// <summary>
|
||
|
/// Name of global table variable to store bindings in. If left blank then each binding will be added as a global variable.
|
||
|
/// </summary>
|
||
|
[Tooltip("Name of global table variable to store bindings in. If left blank then each binding will be added as a global variable.")]
|
||
9 years ago
|
[SerializeField] protected string tableName = "";
|
||
9 years ago
|
|
||
9 years ago
|
[Tooltip("Register all CLR types used by the bound objects so that they can be accessed from Lua. If you don't use this option you will need to register these types yourself.")]
|
||
9 years ago
|
[SerializeField] protected bool registerTypes = true;
|
||
9 years ago
|
|
||
9 years ago
|
[HideInInspector]
|
||
9 years ago
|
[SerializeField] protected List<string> boundTypes = new List<string>();
|
||
9 years ago
|
|
||
9 years ago
|
/// <summary>
|
||
|
/// The list of Unity objects to be bound for access in Lua.
|
||
|
/// </summary>
|
||
|
[Tooltip("The list of Unity objects to be bound to make them accessible in Lua script.")]
|
||
9 years ago
|
[SerializeField] protected List<BoundObject> boundObjects = new List<BoundObject>();
|
||
9 years ago
|
public virtual List<BoundObject> BoundObjects { get { return boundObjects; } }
|
||
9 years ago
|
|
||
9 years ago
|
[Tooltip("Show inherited public members.")]
|
||
9 years ago
|
[SerializeField] protected bool showInherited;
|
||
9 years ago
|
|
||
9 years ago
|
/// <summary>
|
||
|
/// Always ensure there is at least one row in the bound objects list.
|
||
|
/// </summary>
|
||
|
protected virtual void Update()
|
||
|
{
|
||
|
// Add in a single empty line at start
|
||
|
if (boundObjects.Count == 0)
|
||
|
{
|
||
|
boundObjects.Add(null);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Add all declared bindings to the globals table.
|
||
|
/// </summary>
|
||
9 years ago
|
public override void AddBindings(ILuaEnvironment luaEnv)
|
||
9 years ago
|
{
|
||
9 years ago
|
if (!allEnvironments &&
|
||
9 years ago
|
(luaEnvironment != null && !luaEnvironment.Equals(luaEnv)))
|
||
9 years ago
|
{
|
||
|
// Don't add bindings to this environment
|
||
|
return;
|
||
|
}
|
||
|
|
||
9 years ago
|
MoonSharp.Interpreter.Script interpreter = luaEnv.Interpreter;
|
||
9 years ago
|
Table globals = interpreter.Globals;
|
||
|
|
||
9 years ago
|
Table bindingsTable = null;
|
||
|
if (tableName == "")
|
||
|
{
|
||
|
// Add directly to globals table
|
||
|
bindingsTable = globals;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
DynValue res = globals.Get(tableName);
|
||
|
if (res.Type == DataType.Table)
|
||
|
{
|
||
|
// Add to an existing table
|
||
|
bindingsTable = res.Table;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// Create a new table
|
||
|
bindingsTable = new Table(globals.OwnerScript);
|
||
|
globals[tableName] = bindingsTable;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (bindingsTable == null)
|
||
|
{
|
||
|
Debug.LogError("Bindings table must not be null");
|
||
|
}
|
||
|
|
||
9 years ago
|
// Register types of bound objects with MoonSharp
|
||
|
if (registerTypes)
|
||
|
{
|
||
|
foreach (string typeName in boundTypes)
|
||
|
{
|
||
|
LuaEnvironment.RegisterType(typeName);
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
for (int i = 0; i < boundObjects.Count; ++i)
|
||
|
{
|
||
|
// Ignore empty keys
|
||
|
string key = boundObjects[i].key;
|
||
|
if (key == null ||
|
||
|
key == "")
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
// Check for keys used multiple times
|
||
|
if (bindingsTable.Get(key).Type != DataType.Nil)
|
||
|
{
|
||
|
Debug.LogWarning("An item already exists with the same key as binding '" + key + "'. This binding will be ignored.");
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
// Ignore bindings with no object set
|
||
|
GameObject go = boundObjects[i].obj as GameObject;
|
||
|
if (go != null)
|
||
|
{
|
||
|
// Register as gameobject / components
|
||
|
Component component = boundObjects[i].component;
|
||
|
if (component == null)
|
||
|
{
|
||
|
// Bind the key to the gameobject
|
||
|
bindingsTable[key] = go;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// Bind the key to the component
|
||
|
bindingsTable[key] = component;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// Register as other UnityEngine.Object type
|
||
|
bindingsTable[key] = boundObjects[i].obj;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|