public static Quaternion valueToQuaternion(LuaValue val) { Quaternion ret = Quaternion.identity; LuaTable t = val as LuaTable; if (t != null) { try { ret.x = (float)(t.GetValue(1) as LuaNumber).Number; } catch (Exception) { throw new ArgumentException("Invalid quatertion x value"); } try { ret.y = (float)(t.GetValue(2) as LuaNumber).Number; } catch (Exception) { throw new ArgumentException("Invalid quatertion y value"); } try { ret.z = (float)(t.GetValue(3) as LuaNumber).Number; } catch (Exception) { throw new ArgumentException("Invalid quatertion z value"); } try { ret.w = (float)(t.GetValue(4) as LuaNumber).Number; } catch (Exception) { throw new ArgumentException("Invalid quatertion w value"); } } else { throw new ArgumentException("Invalid quaternion"); } return(ret); }
private static void SetPropertyValue(object obj, object value, PropertyInfo propertyInfo) { if (propertyInfo.PropertyType.FullName == "System.Int32") { propertyInfo.SetValue(obj, (int)(double)value, null); } else if (propertyInfo.PropertyType.IsEnum) { object enumValue = Enum.Parse(propertyInfo.PropertyType, (string)value); propertyInfo.SetValue(obj, enumValue, null); } else if (propertyInfo.PropertyType.FullName == "System.Drawing.Image") { LuaTable enviroment = LuaRuntime.GlobalEnvironment.GetValue("_G") as LuaTable; LuaString workDir = enviroment.GetValue("_WORKDIR") as LuaString; var image = System.Drawing.Image.FromFile(Path.Combine(workDir.Text, (string)value)); propertyInfo.SetValue(obj, image, null); } else { propertyInfo.SetValue(obj, value, null); } }
/// <summary> /// A REPL (Read, Eval, Print, Loop function) /// </summary> /// <param name="args">Application startup args</param> public static void REPL(string[] args) { // Create global variables //Application.EnableVisualStyles(); //Application.SetCompatibleTextRenderingDefault(false); GlobalEnvironment = LuaRuntime.CreateGlobalEnviroment(); PrintBanner(); // how to handle errors #if DEBUG GlobalEnvironment.SetNameValue("DEBUG", LuaBoolean.True); #else GlobalEnvironment.SetNameValue("DEBUG", LuaBoolean.False); #endif Prompt = "> "; // load startup scripts LoadFiles(); // check command line args if (args.Length > 0) { string file = args[0]; if (File.Exists(file)) { try { GlobalEnvironment.SetNameValue("_WORKDIR", new LuaString(Path.GetDirectoryName(file))); if (file.EndsWith(".out") || file.EndsWith(".luac") || file.EndsWith(".sluac")) { Chunk c = Serializer.Deserialize(file) as Chunk; c.Enviroment = GlobalEnvironment; bool isBreak; c.Execute(GlobalEnvironment, out isBreak).ToString(); } else { LuaRuntime.RunFile(file, GlobalEnvironment); } // it loaded successfully if (args.Length > 1) // possibly interactive mode after { Console.WriteLine("Loaded file '" + Path.GetFileName(file) + "'"); } } catch (Exception error) { Console.WriteLine(error.Message); } //Console.ReadKey(true); //return; // instead, go to interactive } else { Console.WriteLine(file + " not found."); } } // check for interactive mode foreach (string arg in args) { if (arg.ToUpper() == "-I") { GoInteractive = true; } } if (args.Length == 0) { GoInteractive = true; } if (GoInteractive) { while (true) { Console.Write(Prompt); string line = Console.ReadLine(); if (line == "quit" || line == "exit" || line == "bye") { break; } else { try { LuaValue v = Run(line, GlobalEnvironment); if (v == null) { Console.WriteLine("=> [no returned value]"); } else { Console.WriteLine("=> " + v.ToString()); } } catch (Parser.ParserException error) { // create spacing StringBuilder sb = new StringBuilder(); for (int i = 0; i < Prompt.Length; i++) { sb.Append(" "); } for (int i = 0; i < error.FirstErrorIndex; i++) { sb.Append(" "); } Console.WriteLine(sb.ToString() + "^"); Console.WriteLine("Error: " + error.Message); GlobalEnvironment.SetNameValue("LASTERROR", ObjectToLua.ToLuaValue(error)); } catch (Exception error) { if (((LuaBoolean)GlobalEnvironment.GetValue(GlobalEnvironment.GetKey("DEBUG"))) == LuaBoolean.True) { Console.WriteLine(error.ToString()); } else { Console.WriteLine("Error: " + error.Message); } GlobalEnvironment.SetNameValue("LASTERROR", ObjectToLua.ToLuaValue(error)); } } } } }
/// <summary> /// Creates a global environment with all the base modules registered and /// some default values set. /// </summary> /// <returns></returns> public static LuaTable CreateGlobalEnviroment(bool createBaseLib = true, bool createStringLib = true, bool createTableLib = true, bool createOSLib = true, bool createIOLib = true, bool createFileLib = true, bool createMathLib = true, bool createScriptLib = true, bool createWinFormsLib = true, bool createConsoleLib = true, bool createCoroutineLib = true, bool createPackageLib = true, bool createClassLib = true, bool createFileSystemLib = true) { LuaTable global = new LuaTable(); // Register Lua Modules if (createBaseLib) { BaseLib.RegisterFunctions(global); } if (createStringLib) { StringLib.RegisterModule(global); } if (createTableLib) { TableLib.RegisterModule(global); } if (createIOLib) { IOLib.RegisterModule(global); } if (createFileLib) { FileLib.RegisterModule(global); } if (createMathLib) { MathLib.RegisterModule(global); } if (createOSLib) { OSLib.RegisterModule(global); } if (createScriptLib) { ScriptLib.RegisterModule(global); } //if (createWinFormsLib) // WinFormLib.RegisterModule(global); if (createConsoleLib) { ConsoleLib.RegisterModule(global); } if (createCoroutineLib) { CoroutineLib.RegisterModule(global); } if (createPackageLib) { PackageLib.RegisterModule(global); } if (createClassLib) { ClassLib.RegisterModule(global); } if (createFileSystemLib) { FileSystemLib.RegisterModule(global); } //global.SetNameValue("_WORKDIR", new LuaString(Application.StartupPath + "\\")); global.SetNameValue("_VERSION", new LuaString("Sharp Lua 1.1")); global.SetNameValue("_G", global); if (createPackageLib) { // set package.preload table LuaTable preload = (LuaTable)(global.GetValue("package") as LuaTable).GetValue("preload"); if (createStringLib) { preload.SetNameValue("string", (LuaTable)global.GetValue("string")); } if (createTableLib) { preload.SetNameValue("table", (LuaTable)global.GetValue("table")); } if (createIOLib) { preload.SetNameValue("io", (LuaTable)global.GetValue("io")); } if (createFileLib) { preload.SetNameValue("file", (LuaTable)global.GetValue("file")); } if (createMathLib) { preload.SetNameValue("math", (LuaTable)global.GetValue("math")); } if (createOSLib) { preload.SetNameValue("os", (LuaTable)global.GetValue("os")); } if (createScriptLib) { preload.SetNameValue("script", (LuaTable)global.GetValue("script")); } //if (createWinFormsLib) // preload.SetNameValue("WinForms", (LuaTable) global.GetValue("WinForms")); if (createConsoleLib) { preload.SetNameValue("console", (LuaTable)global.GetValue("console")); } if (createCoroutineLib) { preload.SetNameValue("coroutine", (LuaTable)global.GetValue("coroutine")); } if (createPackageLib) // wait a second... { preload.SetNameValue("package", (LuaTable)global.GetValue("package")); } if (createClassLib) { preload.SetNameValue("class", (LuaTable)global.GetValue("class")); } if (createFileSystemLib) { preload.SetNameValue("filesystem", (LuaTable)global.GetValue("filesystem")); } } if (createFileSystemLib) { FileSystemLib.currentDir = global.GetValue("_WORKDIR").ToString(); } GlobalEnvironment = global; return(global); }
public static Vector3d valueToVector3d(LuaValue val) { Vector3d ret = Vector3d.zero; LuaTable t = val as LuaTable; LuaString s = val as LuaString; if (t != null) { try { ret.x = (t.GetValue(1) as LuaNumber).Number; } catch (Exception) { throw new ArgumentException("Invalid vector x value"); } try { ret.y = (t.GetValue(2) as LuaNumber).Number; } catch (Exception) { throw new ArgumentException("Invalid vector y value"); } try { ret.z = (t.GetValue(3) as LuaNumber).Number; } catch (Exception) { throw new ArgumentException("Invalid vector z value"); } } else if (s != null) { switch (s.Text) { case "forward": case "f": ret = Vector3d.forward; break; case "back": case "b": ret = Vector3d.back; break; case "left": case "l": ret = Vector3d.left; break; case "right": case "r": ret = Vector3d.right; break; case "up": case "u": ret = Vector3d.up; break; case "down": case "d": ret = Vector3d.down; break; default: throw new ArgumentException("Invalid vector string"); } } else { throw new ArgumentException("Invalid vector"); } return(ret); }