示例#1
0
        public LuaScriptEngine(Game game) : base(game)
        {
            MoonSharp.Interpreter.Script.DefaultOptions.DebugPrint = text =>
            {
                Diagnostics.LuaScriptConsole._scriptConsoleTextAll =
                    string.Concat(Diagnostics.LuaScriptConsole._scriptConsoleTextAll, text, "\n");
            };

            MainScript = new MoonSharp.Interpreter.Script();

            FunctionInit();

            try
            {
                LuaCompatibility.Apply(MainScript);

                // Load scripts.lua file from file system or big file
                var filePath  = Path.Combine("Data", "Scripts", "scripts.lua");
                var fileEntry = Game.ContentManager.FileSystem.GetFile(filePath);
                if (fileEntry != null)
                {
                    Logger.Info($"Executing file {filePath}");
                    using var fileStream = fileEntry.Open();
                    MainScript.DoStream(fileStream);
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Error while loading script file");
            }

            LuaEventHandlerInit();
        }
示例#2
0
 /// <summary>
 /// Asynchronously loads and executes a stream containing a Lua/MoonSharp script.
 ///
 /// This method is supported only on .NET 4.x and .NET 4.x PCL targets.
 /// </summary>
 /// <param name="script">The script.</param>
 /// <param name="stream">The stream.</param>
 /// <param name="globalContext">The global context.</param>
 /// <returns>
 /// A DynValue containing the result of the processing of the loaded chunk.
 /// </returns>
 public static Task <DynValue> DoStreamAsync(this Script script, Stream stream, Table globalContext = null)
 {
     return(ExecAsync(() => script.DoStream(stream, globalContext)));
 }
 /// <summary>
 /// Asynchronously loads and executes a stream containing a Lua/MoonSharp script.
 ///
 /// This method is supported only on .NET 4.x and .NET 4.x PCL targets.
 /// </summary>
 /// <param name="script">The script.</param>
 /// <param name="stream">The stream.</param>
 /// <param name="globalContext">The global context.</param>
 /// <param name="codeFriendlyName">Name of the code - used to report errors, etc. Also used by debuggers to locate the original source file.</param>
 /// <returns>
 /// A DynValue containing the result of the processing of the loaded chunk.
 /// </returns>
 public static Task <DynValue> DoStreamAsync(this Script script, Stream stream, Table globalContext = null, string codeFriendlyName = null)
 {
     return(ExecAsync(() => script.DoStream(stream, globalContext, codeFriendlyName)));
 }