using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Linq;
using System.Diagnostics;
using System.Text.RegularExpressions;
using MoonSharp.Interpreter;
using MoonSharp.Interpreter.Interop;
using MoonSharp.Interpreter.Loaders;
using MoonSharp.RemoteDebugger;
namespace Fungus
{
public class FungusScript : MonoBehaviour
{
///
/// Custom file loader for MoonSharp that loads in all Lua scripts in the project.
/// Scripts must be placed in a Resources/Lua directory.
///
protected class FungusScriptLoader : ScriptLoaderBase
{
// Give the script loader access to the list of accessible Lua Modules.
private IEnumerable luaScripts;
public FungusScriptLoader(IEnumerable luaScripts)
{
this.luaScripts = luaScripts;
}
///
// Bypasses the standard path resolution logic for require.
///
protected override string ResolveModuleName(string modname, string[] paths)
{
return modname;
}
public override object LoadFile(string file, Table globalContext)
{
foreach (TextAsset luaScript in luaScripts)
{
// Case insensitive string compare to allow standard Lua naming conventions in code
if (String.Compare(luaScript.name, file, true) == 0)
{
return luaScript.text;
}
}
return "";
}
public override bool ScriptFileExists(string name)
{
foreach (TextAsset luaScript in luaScripts)
{
// Case insensitive string compare to allow standard Lua naming conventions in code
if (String.Compare(luaScript.name, name, true) == 0)
{
return true;
}
}
return false;
}
}
protected Script interpreter;
///
/// Returns the MoonSharp interpreter instance used to run Lua code.
///
public Script Interpreter { get { return interpreter; } }
///
/// Launches the remote Lua debugger in your browser and breaks execution at the first executed Lua command.
///
[Tooltip("Launches the remote Lua debugger in your browser and breaks execution at the first executed Lua command.")]
public bool remoteDebugger = false;
///
/// Lua script file which defines the global string table used for localisation.
///
[Tooltip("Lua script file which defines the global string table used for localisation.")]
public TextAsset stringTable;
///
/// The currently selected language in the string table. Affects variable substitution.
///
[Tooltip("The currently selected language in the string table. Affects variable substitution.")]
public string activeLanguage = "en";
///
/// Time scale factor to apply when running Lua scripts.
/// This allows pausing of a FungusScript by setting timescale to 0.
/// Use the GetTime() and GetDeltaTime() functions to get scaled time values.
/// If negative, then GetTime() and GetDeltaTime() return the same values as the standard Time class.
///
[Tooltip("Time scale factor to apply when running Lua scripts. If negative then uses the same values as the standard Time class.")]
public float timeScale = -1f;
///
/// Instance of remote debugging service when debugging option is enabled.
///
protected RemoteDebuggerService remoteDebuggerService;
///
/// Flag used to avoid startup dependency issues.
///
protected bool initialised = false;
///
/// Cached referenence to the string table (if loaded).
///
protected Table stringTableCached;
protected virtual void Start()
{
InitInterpreter();
}
///
/// Initialise the Lua interpreter so we can start running Lua code.
///
public virtual void InitInterpreter()
{
if (initialised)
{
return;
}
Script.DefaultOptions.DebugPrint = (s) => { UnityEngine.Debug.Log(s); };
// In some use cases (e.g. downloadable Lua files) some Lua modules can pose a potential security risk.
// You can restrict which core lua modules are available here if needed.
// See the MoonSharp documentation for details.
interpreter = new Script(CoreModules.Preset_Complete);
RegisterLuaScriptFiles();
RegisterCLRTypes();
RegisterFungus();
RegisterCLRClassObjects();
RegisterBindings();
LoadStringTable();
if (remoteDebugger)
{
ActivateRemoteDebugger(interpreter);
}
initialised = true;
}
///
/// Register all Lua files in the project so they can be accessed at runtime.
///
protected virtual void RegisterLuaScriptFiles()
{
object[] result = Resources.LoadAll("Lua", typeof(TextAsset));
interpreter.Options.ScriptLoader = new FungusScriptLoader(result.OfType());
}
///
/// Registers a commonly used subset of Unity types for Lua interop.
/// To register more types externally to this class, register them in the Awake method of any
/// monobehavior in your scene.
///
protected virtual void RegisterCLRTypes()
{
// Core types needed by FungusScript
UserData.RegisterType();
UserData.RegisterType();
UserData.RegisterType();
UserData.RegisterType();
UserData.RegisterType();
UserData.RegisterType();
UserData.RegisterType();
// Monobehaviors and other Unity.Objects
UserData.RegisterType();
UserData.RegisterType();
UserData.RegisterType();
UserData.RegisterType();
UserData.RegisterType();
UserData.RegisterType();
UserData.RegisterType();
UserData.RegisterType();
// Value types
UserData.RegisterType();
UserData.RegisterType();
UserData.RegisterType();
UserData.RegisterType();
UserData.RegisterType();
UserData.RegisterType