Browse Source

Pre compile Lua script on startup for fast execution

master
chrisgregan 9 years ago
parent
commit
d17900d7f3
  1. 56
      Assets/Fungus/FungusLua/Scripts/LuaEnvironment.cs
  2. 54
      Assets/Fungus/FungusLua/Scripts/LuaScript.cs

56
Assets/Fungus/FungusLua/Scripts/LuaEnvironment.cs

@ -202,22 +202,11 @@ namespace Fungus
/// <param name="runAsCoroutine">Run the Lua code as a coroutine to support asynchronous operations.</param>
/// <param name="onComplete">Method to callback when the Lua code finishes exection. Supports return parameters.</param>
/// </summary>
public void DoLuaString(string luaString, string friendlyName, bool runAsCoroutine, Action<DynValue> onComplete = null)
public virtual void DoLuaString(string luaString, string friendlyName, bool runAsCoroutine, Action<DynValue> onComplete = null)
{
InitEnvironment();
Closure fn = LoadLuaString(luaString, friendlyName);
// Load the Lua script
DynValue res = null;
try
{
res = interpreter.LoadString(luaString, null, friendlyName);
}
catch (InterpreterException ex)
{
LogException(ex.DecoratedMessage, luaString);
}
if (res == null)
if (fn == null)
{
if (onComplete != null)
{
@ -230,14 +219,14 @@ namespace Fungus
// Execute the Lua script
if (runAsCoroutine)
{
StartCoroutine(RunLuaCoroutineInternal(res.Function, luaString, onComplete));
StartCoroutine(RunLuaCoroutineInternal(fn, luaString, onComplete));
}
else
{
DynValue returnValue = null;
try
{
returnValue = res.Function.Call();
returnValue = fn.Call();
}
catch (InterpreterException ex)
{
@ -251,13 +240,42 @@ namespace Fungus
}
}
/// <summary>
/// Loads and compiles a string containing Lua script, returning a closure (Lua function) which can be executed later.
/// <param name="luaString">The Lua code to be run.</param>
/// <param name="friendlyName">A descriptive name to be used in error reports.</param>
/// </summary>
public virtual Closure LoadLuaString(string luaString, string friendlyName)
{
InitEnvironment();
// Load the Lua script
DynValue res = null;
try
{
res = interpreter.LoadString(luaString, null, friendlyName);
}
catch (InterpreterException ex)
{
LogException(ex.DecoratedMessage, luaString);
}
if (res.Type != DataType.Function)
{
UnityEngine.Debug.LogError("Failed to create Lua function from Lua string");
return null;
}
return res.Function;
}
/// <summary>
/// Starts a Unity coroutine which updates a Lua coroutine each frame.
/// <param name="closure">A MoonSharp closure object representing a function.</param>
/// <param name="debugInfo">Debug text to display if an exception occurs (usually the Lua code that is being executed).</param>
/// <param name="onComplete">A delegate method that is called when the coroutine completes. Includes return parameter.</param>
/// </summary>
public void RunLuaCoroutine(Closure closure, string debugInfo, Action<DynValue> onComplete = null)
public virtual void RunLuaCoroutine(Closure closure, string debugInfo, Action<DynValue> onComplete = null)
{
StartCoroutine(RunLuaCoroutineInternal(closure, debugInfo, onComplete));
}
@ -268,7 +286,7 @@ namespace Fungus
/// <param name="debugInfo">Debug text to display if an exception occurs (usually the Lua code that is being executed).</param>
/// <param name="onComplete">A delegate method that is called when the coroutine completes. Includes return parameter.</param>
/// </summary>
protected IEnumerator RunLuaCoroutineInternal(Closure closure, string debugInfo, Action<DynValue> onComplete = null)
protected virtual IEnumerator RunLuaCoroutineInternal(Closure closure, string debugInfo, Action<DynValue> onComplete = null)
{
DynValue co = interpreter.CreateCoroutine(closure);
@ -296,7 +314,7 @@ namespace Fungus
/// <summary>
/// Start a Unity coroutine from a Lua call.
/// </summary>
public Task RunUnityCoroutine(IEnumerator coroutine)
public virtual Task RunUnityCoroutine(IEnumerator coroutine)
{
if (coroutine == null)
{

54
Assets/Fungus/FungusLua/Scripts/LuaScript.cs

@ -19,7 +19,7 @@ namespace Fungus
/// <summary>
/// The Lua Environment to use when executing Lua script.
/// </summary>
[Tooltip("The Lua Environment to use when executing Lua script.")]
[Tooltip("The Lua Environment to use when executing Lua script.")]
public LuaEnvironment luaEnvironment;
/// <summary>
@ -45,6 +45,9 @@ namespace Fungus
protected bool initialised;
// Stores the compiled Lua code for fast execution later.
protected Closure luaFunction;
// Recursively build the full hierarchy path to this game object
private static string GetPath(Transform current)
{
@ -60,6 +63,9 @@ namespace Fungus
InitLuaScript();
}
/// <summary>
/// Initialises the Lua environment and compiles the Lua string for execution later on.
/// </summary>
protected virtual void InitLuaScript()
{
if (initialised)
@ -85,7 +91,34 @@ namespace Fungus
// Cache a descriptive name to use in Lua error messages
friendlyName = GetPath(transform) + ".LuaScript";
initialised = true;
string s = GetLuaString();
luaFunction = luaEnvironment.LoadLuaString(s, friendlyName);
// Always initialise when playing in the editor.
// Allows the user to edit the Lua script while the game is playing.
if ( !(Application.isPlaying && Application.isEditor) )
{
initialised = true;
}
}
/// <summary>
/// Returns the Lua string to be executed.
/// </summary>
/// <returns>The lua string.</returns>
protected virtual string GetLuaString()
{
string s = "";
if (luaFile != null)
{
s = luaFile.text;
}
else if (luaScript.Length > 0)
{
s = luaScript;
}
return s;
}
/// <summary>
@ -94,7 +127,7 @@ namespace Fungus
/// </summary>
public virtual void OnExecute()
{
// Make sure the environment is initialised before executing
// Make sure the script and Lua environment are initialised before executing
InitLuaScript();
if (luaEnvironment == null)
@ -103,20 +136,7 @@ namespace Fungus
}
else
{
// Ensure the Lua Environment is initialised first.
luaEnvironment.InitEnvironment();
string s = "";
if (luaFile != null)
{
s = luaFile.text;
}
else if (luaScript.Length > 0)
{
s = luaScript;
}
luaEnvironment.DoLuaString(s, friendlyName, runAsCoroutine);
luaEnvironment.RunLuaCoroutine(luaFunction, friendlyName);
}
}
}

Loading…
Cancel
Save