public static void Main(string[] args) { int iCurrentArg = 0; WriteText(ConsoleColor.Gray, "NeoLua Interactive Command"); Console.WriteLine(); WriteText(ConsoleColor.DarkGray, "Version "); WriteText(ConsoleColor.White, String.Format("{0} ({1})", LuaGlobal.VersionString, Lua.Version)); WriteText(ConsoleColor.DarkGray, " by neolithos"); Console.WriteLine(); Console.WriteLine(); WriteText(ConsoleColor.DarkGray, " source code at "); WriteText(ConsoleColor.Gray, "http://neolua.codeplex.com"); Console.WriteLine(); WriteText(ConsoleColor.DarkGray, " supported from "); WriteText(ConsoleColor.Gray, "http://tecware-gmbh.de"); Console.WriteLine(); Console.WriteLine(); WriteText(ConsoleColor.DarkGray, " Write ':h' for help."); Console.WriteLine(); Console.WriteLine(); global = lua.CreateEnvironment <LuaCommandGlobal>(); // change to the samples directory #if DEBUG string sSamples = Path.GetFullPath(@"..\..\Samples"); Debug.Listeners.Add(new ConsoleTraceListener()); #else string sSamples = Path.GetFullPath("Samples"); #endif if (Directory.Exists(sSamples)) { Environment.CurrentDirectory = sSamples; } while (true) { string sLine; Commands cmd; if (iCurrentArg < args.Length) { cmd = ParseArgument(args[iCurrentArg++], out sLine); } else { cmd = InputCommand(out sLine); } switch (cmd) { case Commands.List: // list all variables in global WriteText(ConsoleColor.DarkYellow, "List global:"); Console.WriteLine(); foreach (var c in global) { WriteVariable(c.Key, c.Value); } Console.WriteLine(); break; case Commands.Load: RunScript(() => File.ReadAllText(Path.GetFullPath(sLine)), Path.GetFileName(sLine)); break; case Commands.Debug: if (sLine == "trace") { WriteText(ConsoleColor.DarkYellow, "Compile emits traceline code, now."); Console.WriteLine(); debugEngine = debugConsole; } else if (sLine == Boolean.TrueString) { WriteText(ConsoleColor.DarkYellow, "Compile emits stack trace information and runtime functions, now."); Console.WriteLine(); debugEngine = LuaStackTraceDebugger.Default; } else { WriteText(ConsoleColor.DarkYellow, "Compile creates dynamic functions, now."); Console.WriteLine(); debugEngine = null; } Console.WriteLine(); break; case Commands.Environment: WriteText(ConsoleColor.DarkYellow, "New environment created."); Console.WriteLine(); Console.WriteLine(); global = lua.CreateEnvironment <LuaCommandGlobal>(); break; case Commands.Cache: lua.DumpRuleCaches(Console.Out); Console.WriteLine(); break; case Commands.Help: WriteText(ConsoleColor.DarkYellow, "Commands:"); Console.WriteLine(); WriteCommand(":q", "Exit the application."); WriteCommand(":list", "Lists all global variables."); WriteCommand(":load", "Loads the lua-script from a file."); WriteCommand(":debugoff", "Tell the compiler to emit no debug informations."); WriteCommand(":debugon", "Let the compiler emit debug informations."); WriteCommand(":debugtrace", "Let the compiler emit trace line functionality."); WriteCommand(":c", "Clears the current script buffer."); WriteCommand(":env", "Create a fresh environment."); WriteCommand(":cache", "Shows the content of the binder cache."); Console.WriteLine(); break; case Commands.Run: if (sLine.Length > 0) { RunScript(() => sLine, "line"); } break; case Commands.Exit: return; } } } // Main
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 <LuaGlobal>(); 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); }
public static void Main(string[] args) { int iCurrentArg = 0; WriteText(ConsoleColor.Gray, "NeoLua Interactive Command"); Console.WriteLine(); WriteText(ConsoleColor.DarkGray, "Version "); WriteText(ConsoleColor.White, String.Format("{0} ({1})", LuaGlobal.VersionString, Lua.Version)); WriteText(ConsoleColor.DarkGray, " by neolithos"); Console.WriteLine(); Console.WriteLine(); WriteText(ConsoleColor.DarkGray, " source code at "); WriteText(ConsoleColor.Gray, "http://neolua.codeplex.com"); Console.WriteLine(); WriteText(ConsoleColor.DarkGray, " supported from "); WriteText(ConsoleColor.Gray, "http://tecware-gmbh.de"); Console.WriteLine(); Console.WriteLine(); WriteText(ConsoleColor.DarkGray, " Write ':h' for help."); Console.WriteLine(); Console.WriteLine(); global = lua.CreateEnvironment<LuaCommandGlobal>(); // change to the samples directory #if DEBUG string sSamples = Path.GetFullPath(@"..\..\Samples"); #else string sSamples = Path.GetFullPath("Samples"); #endif if (Directory.Exists(sSamples)) Environment.CurrentDirectory = sSamples; while (true) { string sLine; Commands cmd; if (iCurrentArg < args.Length) cmd = ParseArgument(args[iCurrentArg++], out sLine); else cmd = InputCommand(out sLine); switch (cmd) { case Commands.List: // list all variables in global WriteText(ConsoleColor.DarkYellow, "List global:"); Console.WriteLine(); foreach (var c in global) WriteVariable(c.Key, c.Value); Console.WriteLine(); break; case Commands.Load: RunScript(() => File.ReadAllText(Path.GetFullPath(sLine)), Path.GetFileName(sLine)); break; case Commands.Debug: if(sLine =="trace") { WriteText(ConsoleColor.DarkYellow, "Compile emits traceline code, now."); Console.WriteLine(); debugEngine = debugConsole; } else if (sLine == Boolean.TrueString) { WriteText(ConsoleColor.DarkYellow, "Compile emits stack trace information and runtime functions, now."); Console.WriteLine(); debugEngine = LuaStackTraceDebugger.Default; } else { WriteText(ConsoleColor.DarkYellow, "Compile creates dynamic functions, now."); Console.WriteLine(); debugEngine = null; } Console.WriteLine(); break; case Commands.Environment: WriteText(ConsoleColor.DarkYellow, "New environment created."); Console.WriteLine(); Console.WriteLine(); global = lua.CreateEnvironment<LuaCommandGlobal>(); break; case Commands.Cache: lua.DumpRuleCaches(Console.Out); Console.WriteLine(); break; case Commands.Help: WriteText(ConsoleColor.DarkYellow, "Commands:"); Console.WriteLine(); WriteCommand(":q", "Exit the application."); WriteCommand(":list", "Lists all global variables."); WriteCommand(":load", "Loads the lua-script from a file."); WriteCommand(":debugoff", "Tell the compiler to emit no debug informations."); WriteCommand(":debugon", "Let the compiler emit debug informations."); WriteCommand(":debugtrace", "Let the compiler emit trace line functionality."); WriteCommand(":c", "Clears the current script buffer."); WriteCommand(":env", "Create a fresh environment."); WriteCommand(":cache", "Shows the content of the binder cache."); Console.WriteLine(); break; case Commands.Run: if (sLine.Length > 0) RunScript(() => sLine, "line"); break; case Commands.Exit: return; } } }
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; }