Ejemplo n.º 1
0
        } // proc SetPropertyValue

        public override bool Execute()
        {
            try
            {
                global.DoChunk(chunk, this.BuildEngine, this.Log);
                return(true);
            }
            catch (LuaRuntimeException e)
            {
                Log.LogError("{0} (at line {1},{2})", e.Message, this.chunk.ChunkName, e.Line);
                return(false);
            }
        } // func Execute
Ejemplo n.º 2
0
        }         // func ParseArgument

        private static void RunScript(Func <string> code, string sName)
        {
            try
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();

                // compile chunk
                LuaChunk c = lua.CompileChunk(code(), sName, new LuaCompileOptions()
                {
                    DebugEngine = debugEngine
                });

                string sCompileTime = String.Format("{0:N0} ms", sw.ElapsedMilliseconds);
                sw.Reset();
                sw.Start();

                // run chunk
                LuaResult r        = global.DoChunk(c);
                string    sRunTime = String.Format("{0:N0} ms", sw.ElapsedMilliseconds);

                string sSize;
                if (c.Size < 0)
                {
                    sSize = "unknown";
                }
                else if (c.Size == 0)
                {
                    sSize = String.Empty;
                }
                else
                {
                    sSize = c.Size.ToString("N0") + " byte";
                }

                // start with a new line
                if (Console.CursorLeft > 0)
                {
                    Console.WriteLine();
                }

                // print result
                if (r.Count > 0)
                {
                    for (int i = 0; i < r.Count; i++)
                    {
                        WriteVariable(i, r[i]);
                    }
                }

                // print summary
                const string csCompile = "==> compile: ";
                const string csRuntime = " run: ";

                Console.CursorLeft = Console.WindowWidth - csCompile.Length - (sSize.Length > 0 ? sSize.Length + 3 : 0) - sCompileTime.Length - csRuntime.Length - sRunTime.Length - 1;
                WriteText(ConsoleColor.DarkGreen, csCompile);
                WriteText(ConsoleColor.Green, sCompileTime);
                if (sSize.Length > 0)
                {
                    WriteText(ConsoleColor.DarkGreen, " [");
                    WriteText(ConsoleColor.Green, sSize);
                    WriteText(ConsoleColor.DarkGreen, "]");
                }
                WriteText(ConsoleColor.DarkGreen, csRuntime);
                WriteText(ConsoleColor.Green, sRunTime);
                Console.WriteLine();
            }
            catch (LuaParseException e)
            {
                WriteText(ConsoleColor.DarkRed, String.Format("Parse error at line {0:N0} (column: {1:N0}):", e.Line, e.Column));
                Console.WriteLine();
                WriteText(ConsoleColor.DarkRed, "  " + e.Message);
                Console.WriteLine();
            }
            catch (Exception e)
            {
                Exception ex = e is TargetInvocationException ? e.InnerException : e;
                WriteException(ex);
            }
        } // proc RunScript
Ejemplo n.º 3
0
        public bool Create()
        {
            ScriptEnvironment lastEnvironment = null, oldLastEnvironment = null;

            try
            {
                if (ms_luaState == null)
                {
                    ms_luaState = new Lua();

                    //ms_luaDebug = new LuaStackTraceDebugger();
                    ms_luaDebug = null;

                    if (Resource.Manager.Configuration.ScriptDebug)
                    {
                        ms_luaDebug = new LuaStackTraceDebugger();
                    }

                    ms_luaCompileOptions = new LuaCompileOptions();
                    ms_luaCompileOptions.DebugEngine = ms_luaDebug;

                    ms_initChunks = new []
                    {
                        ms_luaState.CompileChunk("system/MessagePack.lua", ms_luaCompileOptions),
                        ms_luaState.CompileChunk("system/dkjson.lua", ms_luaCompileOptions),
                        ms_luaState.CompileChunk("system/resource_init.lua", ms_luaCompileOptions)
                    };
                }

                m_luaEnvironment = ms_luaState.CreateEnvironment();

                foreach (var func in ms_luaFunctions)
                {
                    //m_luaEnvironment[func.Key] = Delegate.CreateDelegate
                    var parameters = func.Value.GetParameters()
                                    .Select(p => p.ParameterType)
                                    .Concat(new Type[] { func.Value.ReturnType })
                                    .ToArray();

                    var delegType = Expression.GetDelegateType
                    (
                        parameters
                    );

                    var deleg = Delegate.CreateDelegate
                    (
                        delegType,
                        null,
                        func.Value
                    );

                    var expParameters = parameters.Take(parameters.Count() - 1).Select(a => Expression.Parameter(a)).ToArray();

                    var pushedEnvironment = Expression.Variable(typeof(PushedEnvironment));

                    Expression<Func<PushedEnvironment>> preFunc = () => PushEnvironment(this);
                    Expression<Action<PushedEnvironment>> postFunc = env => env.PopEnvironment();

                    Expression body;

                    if (func.Value.ReturnType.Name != "Void")
                    {
                        var retval = Expression.Variable(func.Value.ReturnType);

                        body = Expression.Block
                        (
                            func.Value.ReturnType,
                            new[] { retval, pushedEnvironment },

                            Expression.Assign
                            (
                                pushedEnvironment,
                                Expression.Invoke(preFunc)
                            ),

                            Expression.Assign
                            (
                                retval,
                                Expression.Call(func.Value, expParameters)
                            ),

                            Expression.Invoke(postFunc, pushedEnvironment),

                            retval
                        );
                    }
                    else
                    {
                        body = Expression.Block
                        (
                            func.Value.ReturnType,
                            new[] { pushedEnvironment },

                            Expression.Assign
                            (
                                pushedEnvironment,
                                Expression.Invoke(preFunc)
                            ),

                            Expression.Call(func.Value, expParameters),

                            Expression.Invoke(postFunc, pushedEnvironment)
                        );
                    }

                    var lambda = Expression.Lambda(delegType, body, expParameters);

                    m_luaEnvironment[func.Key] = lambda.Compile();
                }

                InitHandler = null;

                /*m_luaNative = LuaL.LuaLNewState();
                LuaL.LuaLOpenLibs(m_luaNative);

                LuaLib.LuaNewTable(m_luaNative);
                LuaLib.LuaSetGlobal(m_luaNative, "luanet");

                InitHandler = null;

                m_luaState = new NLua.Lua(m_luaNative);*/

                lock (m_luaEnvironment)
                {
                    lastEnvironment = ms_currentEnvironment;
                    ms_currentEnvironment = this;

                    oldLastEnvironment = LastEnvironment;
                    LastEnvironment = lastEnvironment;

                    // load global data files
                    foreach (var chunk in ms_initChunks)
                    {
                        m_luaEnvironment.DoChunk(chunk);
                    }
                }

                return true;
            }
            catch (Exception e)
            {
                this.Log().Error(() => "Error creating script environment for resource " + m_resource.Name + ": " + e.Message, e);

                if (e.InnerException != null)
                {
                    this.Log().Error(() => "Inner exception: " + e.InnerException.Message, e.InnerException);
                }

                PrintLuaStackTrace(e);
            }
            finally
            {
                ms_currentEnvironment = lastEnvironment;
                LastEnvironment = oldLastEnvironment;
            }

            return false;
        }
Ejemplo n.º 4
-1
		private void DoScript(Lua l, LuaGlobal g, string sScript)
		{
			Type type = typeof(RunLuaTests);
			using (Stream src = type.Assembly.GetManifestResourceStream(type, "CompTests." + sScript))
			using (StreamReader sr = new StreamReader(src))
			{
				Console.WriteLine();
				Console.WriteLine(new string('=', 60));
				Console.WriteLine("= " + sScript);
				Console.WriteLine();
				try
				{
					g.DoChunk(l.CompileChunk(sr, sScript, LuaDeskop.StackTraceCompileOptions));
				}
				catch (Exception e)
				{
					if (e is LuaException)
						Console.WriteLine("Line: {0}, Column: {1}", ((LuaException)e).Line, ((LuaException)e).Column);
					Console.WriteLine("StackTrace:");
					Console.WriteLine(LuaExceptionData.GetData(e).StackTrace);
					throw;
				}
			}
		}