public static void RegisterFunctions(LuaTable mod) { mod.Register("attributes", Attributes); mod.Register("chdir", ChDir); mod.Register("lock", Lock); mod.Register("currentdir", CurrentDir); mod.Register("mkdir", Mkdir); mod.Register("delete", Delete); mod.Register("unlock", Unlock); mod.Register("copy", (LuaValue[] args) => { File.Copy(args[0].ToString(), args[1].ToString()); return LuaBoolean.True; }); mod.Register("write", (LuaValue[] args) => { using (StreamWriter sw = new StreamWriter(args[0].ToString())) { for (int i = 1; i < args.Length; i++) sw.WriteLine(args[i].ToString()); sw.Close(); } return LuaNil.Nil; }); }
public override LuaValue Evaluate(LuaValue baseValue, LuaTable enviroment) { LuaValue value = null; try {LuaValue.GetKeyValue(baseValue, new LuaString(this.Method)); } catch (Exception) { } LuaFunction function = value as LuaFunction; if (function != null) { if (this.Args.Table != null) { return function.Function.Invoke(new LuaValue[] { baseValue, this.Args.Table.Evaluate(enviroment) }); } else if (this.Args.String != null) { return function.Function.Invoke(new LuaValue[] { baseValue, this.Args.String.Evaluate(enviroment) }); } else { List<LuaValue> args = this.Args.ArgList.ConvertAll(arg => arg.Evaluate(enviroment)); args.Insert(0, baseValue); return function.Function.Invoke(args.ToArray()); } } // method call on table would be like _G:script() else if ((baseValue as LuaTable) != null) { List<LuaValue> args = this.Args.ArgList.ConvertAll(arg => arg.Evaluate(enviroment)); return ((baseValue as LuaTable).MetaTable.GetValue("__call") as LuaFunction).Invoke(args.ToArray()); } else if ((baseValue as LuaClass) != null) { LuaClass c = baseValue as LuaClass; List<LuaValue> args = this.Args.ArgList.ConvertAll(arg => arg.Evaluate(enviroment)); args.Insert(0, new LuaString(this.Method)); if (c.Self.MetaTable == null) c.GenerateMetaTable(); return (c.Self.MetaTable.GetValue("__call") as LuaFunction).Invoke(args.ToArray()); } else if ((baseValue as LuaUserdata) != null) { List<LuaValue> args = this.Args.ArgList.ConvertAll(arg => arg.Evaluate(enviroment)); LuaUserdata obj = baseValue as LuaUserdata; object o = obj.Value; if (obj.MetaTable != null) { if (obj.MetaTable.GetValue(this.Method) != null) { LuaValue o2 = obj.MetaTable.GetValue(this.Method); if ((o2 as LuaFunction) != null) return (o2 as LuaFunction).Invoke(args.ToArray()); else if ((o2 as LuaTable) != null) throw new NotImplementedException(); // TODO } } return ObjectToLua.ToLuaValue(o.GetType().GetMethod(this.Method, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Invoke(o, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, args.ToArray(), CultureInfo.CurrentCulture)); } else { throw new Exception("Invoke method call on non function value."); } }
public static LuaValue Date(LuaValue[] values) { LuaString format = null; if (values.Length > 0) format = values[0] as LuaString; if (format != null) { if (format.Text == "*t") { LuaTable table = new LuaTable(); DateTime now = DateTime.Now; table.SetNameValue("year", new LuaNumber (now.Year)); table.SetNameValue("month", new LuaNumber (now.Month )); table.SetNameValue("day", new LuaNumber (now.Day)); table.SetNameValue("hour", new LuaNumber (now.Hour)); table.SetNameValue("min", new LuaNumber (now.Minute)); table.SetNameValue("sec", new LuaNumber (now.Second)); table.SetNameValue("wday", new LuaNumber ((int)now.DayOfWeek)); table.SetNameValue("yday", new LuaNumber (now.DayOfYear)); table.SetNameValue("isdst", LuaBoolean.From(now.IsDaylightSavingTime())); } else { return new LuaString(DateTime.Now.ToString(format.Text)); } } return new LuaString(DateTime.Now.ToShortDateString()); }
/// <summary> /// Executes the chunk /// </summary> /// <param name="enviroment">The environment to run in</param> /// <param name="isBreak">whether to break execution</param> /// <returns></returns> public override LuaValue Execute(LuaTable enviroment, out bool isBreak) { LuaValue condition = this.Condition.Evaluate(enviroment); if (condition.GetBooleanValue() == true) { return this.ThenBlock.Execute(enviroment, out isBreak); } else { foreach (ElseifBlock elseifBlock in this.ElseifBlocks) { condition = elseifBlock.Condition.Evaluate(enviroment); if (condition.GetBooleanValue() == true) { return elseifBlock.ThenBlock.Execute(enviroment, out isBreak); } } if (this.ElseBlock != null) { return this.ElseBlock.Execute(enviroment, out isBreak); } } isBreak = false; return null; }
public static LuaTable CreateMetaTable() { LuaTable metatable = new LuaTable(); RegisterFunctions(metatable); metatable.SetNameValue("__index", metatable); return metatable; }
public static void RegisterFunctions(LuaTable module) { module.SetNameValue("huge", new LuaNumber(double.MaxValue)); module.SetNameValue("pi", new LuaNumber(Math.PI)); module.Register("abs", Abs); module.Register("acos", Acos); module.Register("asin", Asin); module.Register("atan", Atan); module.Register("atan2", Atan2); module.Register("ceil", Ceil); module.Register("cos", Cos); module.Register("cosh", Cosh); module.Register("deg", Deg); module.Register("exp", Exp); module.Register("floor", Floor); module.Register("fmod", Fmod); module.Register("log", Log); module.Register("log10", Log10); module.Register("max", Max); module.Register("min", Min); module.Register("modf", ModF); module.Register("pow", Pow); module.Register("rad", Rad); module.Register("random", Random); module.Register("randomseed", RandomSeed); module.Register("sin", Sin); module.Register("sinh", SinH); module.Register("sqrt", Sqrt); module.Register("tan", Tan); module.Register("tanh", TanH); }
/// <summary> /// Executes the chunk /// </summary> /// <param name="enviroment">Runs in the given environment</param> /// <param name="isBreak">whether to break execution</param> /// <returns></returns> public override LuaValue Execute(LuaTable enviroment, out bool isBreak) { LuaNumber start = this.Start.Evaluate(enviroment) as LuaNumber; LuaNumber end = this.End.Evaluate(enviroment) as LuaNumber; double step = 1; if (this.Step != null) { step = (this.Step.Evaluate(enviroment) as LuaNumber).Number; } var table = new LuaTable(enviroment); table.SetNameValue(this.VarName, start); this.Body.Enviroment = table; while (step > 0 && start.Number <= end.Number || step <= 0 && start.Number >= end.Number) { var returnValue = this.Body.Execute(out isBreak); if (returnValue != null || isBreak == true) { isBreak = false; return returnValue; } start.Number += step; } isBreak = false; return null; }
public static void RegisterFunctions(LuaTable module) { module.Register("print", Print); module.Register("type", Type); module.Register("getmetatable", GetMetaTable); module.Register("setmetatable", SetMetaTable); module.Register("tostring", Tostring); module.Register("tonumber", Tonumber); module.Register("ipairs", IPairs); module.Register("pairs", Pairs); module.Register("next", Next); module.Register("assert", Assert); module.Register("error", Error); module.Register("rawget", RawGet); module.Register("rawset", RawSet); module.Register("select", Select); module.Register("dofile", DoFile); module.Register("loadstring", LoadString); module.Register("unpack", UnPack); module.Register("pcall", Pcall); module.Register("openfile", OpenFile); module.Register("require", Require); module.Register("set", Set); module.Register("loadfile", LoadFile); module.Register("xpcall", XPcall); module.Register("wait", Wait); module.Register("loadbin", LoadBin); module.Register("ssave", SSave); module.Register("sload", SLoad); module.Register("createauserdata", (LuaValue[] args) => { return new LuaUserdata(null); }); module.Register("iarray", IterateDotNetList); }
public void RegisterModule(SharpLua.LuaTypes.LuaTable environment) { LuaTable mod = new LuaTable(); RegisterFunctions(mod); LuaTable mt = new LuaTable(); mt.Register("__newindex", (LuaValue[] args) => { string key = args[1].ToString(); if (key.ToLower() == "key") { CryptoLib.key = IExtendFramework.Encryption.SampleObjects.CreateRijndaelKeyWithSHA512(args[2].ToString()); Console.WriteLine("Encryption Key is now " + ByteToString(CryptoLib.key)); } else if (key.ToLower() == "iv") { iv = IExtendFramework.Encryption.SampleObjects.CreateRijndaelIVWithSHA512(args[2].ToString()); Console.WriteLine("Encryption IV is now " + ByteToString(CryptoLib.iv)); } return LuaNil.Nil; }); mod.MetaTable = mt; environment.SetNameValue(ModuleName, mod); }
public override LuaValue Evaluate(LuaTable enviroment) { LuaTable table = new LuaTable(); foreach (Field field in this.FieldList) { NameValue nameValue = field as NameValue; if (nameValue != null) { table.SetNameValue(nameValue.Name, nameValue.Value.Evaluate(enviroment)); continue; } KeyValue keyValue = field as KeyValue; if (keyValue != null) { table.SetKeyValue( keyValue.Key.Evaluate(enviroment), keyValue.Value.Evaluate(enviroment)); continue; } ItemValue itemValue = field as ItemValue; if (itemValue != null) { table.AddValue(itemValue.Value.Evaluate(enviroment)); continue; } } return table; }
public void RegisterModule(SharpLua.LuaTypes.LuaTable environment) { LuaTable mod = new LuaTable(); RegisterFunctions(mod); LuaTable mt = new LuaTable(); mt.Register("__newindex", (LuaValue[] args) => { string key = args[1].ToString(); if (key.ToLower() == "key") { CryptoLib.key = IExtendFramework.Encryption.SampleObjects.CreateRijndaelKeyWithSHA512(args[2].ToString()); Console.WriteLine("Encryption Key is now " + ByteToString(CryptoLib.key)); } else if (key.ToLower() == "iv") { iv = IExtendFramework.Encryption.SampleObjects.CreateRijndaelIVWithSHA512(args[2].ToString()); Console.WriteLine("Encryption IV is now " + ByteToString(CryptoLib.iv)); } return(LuaNil.Nil); }); mod.MetaTable = mt; environment.SetNameValue(ModuleName, mod); }
public static LuaValue CreateClass(LuaValue[] args) { LuaTable from = new LuaTable(); if (args.Length > 0) if (args[0].GetTypeCode() == "table" && ((IsClass(new LuaValue[] {args[0]}) as LuaBoolean).BoolValue == false)) from = args[0] as LuaTable; LuaClass nClass = new LuaClass("CLASS_" + classCount++, false, false); List<LuaClass> Parents = new List<LuaClass>(); for (int i = 0; i < args.Length; i++) { LuaClass c = args[i] as LuaClass; if (c == null) continue; if (c.Final) throw new Exception("Cannot inherit from a final class"); else { Parents.Add(c); c.ChildClasses.Add(nClass); } } nClass.ParentClasses = Parents; TableLib.Copy(new LuaValue[] {nClass.Self, from}); return nClass; }
public static void RegisterModule(LuaTable enviroment) { LuaTable module = new LuaTable(); RegisterFunctions(module); enviroment.SetNameValue("WinForms", module); module.SetNameValue("_G", enviroment); currentModule = module; }
public static void RegisterFunctions(LuaTable module) { module.Register("close", Close); module.Register("read", Read); module.Register("write", Write); module.Register("lines", Lines); module.Register("flush", Flush); module.Register("seek", Seek); }
public static void RegisterFunctions(LuaTable module) { module.Register("create", Create); module.Register("resume", Resume); module.Register("running", Running); module.Register("status", Status); module.Register("wrap", Wrap); module.Register("yield", Yield); }
public static void RegisterFunctions(LuaTable module) { module.Register("write", Write); module.Register("writeline", WriteLine); module.Register("clear", new LuaFunc(delegate (LuaValue[] args) { A8Console.Clear(); return LuaNil.Nil; })); }
public static void RegisterFunctions(LuaTable module) { module.Register("Run", RunApp); module.Register("ShowMessage", ShowMessage); module.Register("MessageBox", ShowMessage); module.Register("msgbox", ShowMessage); LuaTable metaTable = new LuaTable(); metaTable.Register("__index", GetControlCreator); module.MetaTable = metaTable; }
public override LuaValue Evaluate(LuaTable enviroment) { LuaValue baseValue = this.Base.Evaluate(enviroment); foreach (Access access in this.Accesses) { baseValue = access.Evaluate(baseValue, enviroment); } return baseValue; }
/// <summary> /// Executes the chunk /// </summary> /// <param name="enviroment">Runs in the given environment</param> /// <param name="isBreak">whether to break execution</param> /// <returns></returns> public override LuaValue Execute(LuaTable enviroment, out bool isBreak) { LuaValue[] values = this.ExprList.ConvertAll(expr => expr.Evaluate(enviroment)).ToArray(); LuaValue[] neatValues = LuaMultiValue.UnWrapLuaValues(values); if (neatValues.Length < 3) //probably LuaUserdata. Literal will also fail... { return ExecuteAlternative(enviroment, out isBreak); } LuaFunction func = neatValues[0] as LuaFunction; LuaValue state = neatValues[1]; LuaValue loopVar = neatValues[2]; var table = new LuaTable(enviroment); this.Body.Enviroment = table; while (true) { LuaValue result = func.Invoke(new LuaValue[] { state, loopVar }); LuaMultiValue multiValue = result as LuaMultiValue; if (multiValue != null) { neatValues = LuaMultiValue.UnWrapLuaValues(multiValue.Values); loopVar = neatValues[0]; for (int i = 0; i < Math.Min(this.NameList.Count, neatValues.Length); i++) { table.SetNameValue(this.NameList[i], neatValues[i]); } } else { loopVar = result; table.SetNameValue(this.NameList[0], result); } if (loopVar == LuaNil.Nil) { break; } var returnValue = this.Body.Execute(out isBreak); if (returnValue != null || isBreak == true) { isBreak = false; return returnValue; } } isBreak = false; return null; }
public static void RegisterFunctions(LuaTable module) { module.Register("input", Input); module.Register("output", Output); module.Register("open", Open); module.Register("read", Read); module.Register("write", Write); module.Register("flush", Flush); module.Register("tmpfile", TmpFile); module.Register("close", Close); module.Register("lines", Lines); }
public static void RegisterFunctions(LuaTable module) { module.Register("clock", Clock); module.Register("date", Date); module.Register("time", Time); module.Register("exit", Exit); module.Register("getenv", GetEnv); module.Register("remove", Remove); module.Register("rename", Rename); module.Register("tmpname", TmpName); module.Register("difftime", DiffTime); }
/// <summary> /// Added by Arjen...initialize .NET object as LuaUserData using reflection /// </summary> /// <param name="obj">.NET object</param> /// <param name="init">True if object should reflect in LUA else empty metatable</param> public LuaUserdata(object obj, bool init) { if (init) { MetaTable = SharpLua.ObjectToLua.GetControlMetaTable(); this.Object = obj; } else { MetaTable = new LuaTable(); this.Object = obj; } }
/// <summary> /// Runs the Statement /// </summary> /// <param name="enviroment">The environment to run in</param> /// <param name="isBreak">whether to break execution or not</param> /// <returns></returns> public override LuaValue Execute(LuaTable enviroment, out bool isBreak) { LuaValue[] values = this.ExprList.ConvertAll(expr => expr.Evaluate(enviroment)).ToArray(); LuaValue[] neatValues = LuaMultiValue.UnWrapLuaValues(values); for (int i = 0; i < Math.Min(this.VarList.Count, neatValues.Length); i++) { Var var = this.VarList[i]; if (var.Accesses.Count == 0) { VarName varName = var.Base as VarName; if (varName != null) { // inital creation, no metatable yet SetKeyValue(enviroment, new LuaString(varName.Name), values[i]); continue; } } else { LuaValue baseValue = var.Base.Evaluate(enviroment); for (int j = 0; j < var.Accesses.Count - 1; j++) { Access access = var.Accesses[j]; baseValue = access.Evaluate(baseValue, enviroment); } Access lastAccess = var.Accesses[var.Accesses.Count - 1]; NameAccess nameAccess = lastAccess as NameAccess; if (nameAccess != null) { SetKeyValue(baseValue, new LuaString(nameAccess.Name), values[i]); continue; } KeyAccess keyAccess = lastAccess as KeyAccess; if (lastAccess != null) { SetKeyValue(baseValue, keyAccess.Key.Evaluate(enviroment), values[i]); } } } isBreak = false; return null; }
public override LuaValue Evaluate(LuaTable enviroment) { double number; if (string.IsNullOrEmpty(this.HexicalText)) { number = double.Parse(this.Text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent); } else { number = int.Parse(this.HexicalText, NumberStyles.HexNumber); } return new LuaNumber (number); }
public override LuaValue Evaluate(LuaTable enviroment) { if (this.LeftOperand == null) { return PrefixUnaryOperation(Operator, RightOperand, enviroment); } else if (this.RightOperand == null) { return LeftOperand.Evaluate(enviroment); } else { return InfixBinaryOperation(LeftOperand, Operator, RightOperand, enviroment); } }
public static void RegisterFunctions(LuaTable module) { module.Register("concat", Concat); module.Register("insert", Insert); module.Register("remove", Remove); module.Register("removeitem", RemoveItem); module.Register("maxn", Maxn); module.Register("sort", Sort); module.Register("copy", Copy); // 3 different ways to call one function... module.Register("dump", PrintContents); module.Register("print", PrintContents); module.Register("printcontents", PrintContents); module.Register("find", Find); }
/// <summary> /// Executes the chunk /// </summary> /// <param name="enviroment">The environment to run in</param> /// <param name="isBreak">whether to break execution</param> /// <returns></returns> public override LuaValue Execute(LuaTable enviroment, out bool isBreak) { LuaTable table = enviroment; if (this.Name.MethodName == null) { for (int i = 0; i < this.Name.FullName.Count - 1; i++) { LuaValue obj = enviroment.GetValue(this.Name.FullName[i]); table = obj as LuaTable; if (table == null) { throw new Exception("Not a table: " + this.Name.FullName[i]); } } table.SetNameValue( this.Name.FullName[this.Name.FullName.Count - 1], this.Body.Evaluate(enviroment)); } else { for (int i = 0; i < this.Name.FullName.Count; i++) { LuaValue obj = enviroment.GetValue(this.Name.FullName[i]); if ((obj as LuaClass) != null) table = (obj as LuaClass).Self; else table = obj as LuaTable; if (table == null) { throw new Exception("Not a table: " + this.Name.FullName[i]); } } this.Body.ParamList.NameList.Insert(0, "self"); table.SetNameValue( this.Name.MethodName, this.Body.Evaluate(enviroment)); } isBreak = false; return null; }
public static void RegisterFunctions(LuaTable module) { module.Register("byte", Byte); module.Register("char", Char); module.Register("format", Format); module.Register("len", Len); module.Register("sub", Sub); module.Register("lower", Lower); module.Register("upper", Upper); module.Register("rep", Rep); module.Register("reverse", Reverse); module.Register("find", Find); module.Register("gmatch", GMatch); module.Register("match", Match); module.Register("gsub", GSub); }
private LuaValue PrefixUnaryOperation(string Operator, Term RightOperand, LuaTable enviroment) { LuaValue rightValue = RightOperand.Evaluate(enviroment); switch (Operator) { case "-": var number = rightValue as LuaNumber; if (number != null) { return new LuaNumber(-number.Number); } else { LuaFunction func = GetMetaFunction("__unm", rightValue, null); if (func != null) { return func.Invoke(new LuaValue[] { rightValue }); } } break; case "#": var table = rightValue as LuaTable; if (table != null) { return new LuaNumber(table.Length); } var str = rightValue as LuaString; if (str != null) { return new LuaNumber(str.Text.Length); } if ((rightValue.MetaTable.GetValue("__len") != null) && ((rightValue.MetaTable.GetValue("__len") as LuaFunction) != null)) return (rightValue.MetaTable.GetValue("__len") as LuaFunction).Invoke(new LuaValue[] { }); break; case "not": var rightBool = rightValue as LuaBoolean; if (rightBool != null) { return LuaBoolean.From(!rightBool.BoolValue); } break; } return LuaNil.Nil; }
public static LuaValue Attributes(LuaValue[] args) { string fn = args[0].ToString(); LuaTable ret = new LuaTable(); FileInfo f = new FileInfo(fn); ret.SetNameValue("filename", new LuaString(fn)); ret.SetNameValue("dir", new LuaString(f.DirectoryName)); ret.SetNameValue("drive", new LuaString(Path.GetPathRoot(f.DirectoryName))); ret.SetNameValue("attributes", new LuaString(f.Attributes.ToString())); ret.SetNameValue("access", new LuaString(f.LastAccessTime.ToString())); ret.SetNameValue("modification", new LuaString(f.LastWriteTime.ToString())); ret.SetNameValue("ext", new LuaString(f.Extension)); ret.SetNameValue("size", new LuaString(f.Length.ToString())); return ret; }
public LuaValue Evaluate(LuaTable enviroment) { return new LuaFunction( new LuaFunc(delegate(LuaValue[] args) { var table = new LuaTable(enviroment); List<string> names = this.ParamList.NameList; if (names.Count > 0) { int argCount = Math.Min(names.Count, args.Length); for (int i = 0; i < argCount; i++) { table.SetNameValue(names[i], args[i]); } if (this.ParamList.HasVarArg) { if (argCount < args.Length) { LuaValue[] remainedArgs = new LuaValue[args.Length - argCount]; for (int i = 0; i < remainedArgs.Length; i++) { remainedArgs[i] = args[argCount + i]; } table.SetNameValue("...", new LuaMultiValue(remainedArgs)); table.SetNameValue("arg", new LuaMultiValue(remainedArgs)); } } } else if (this.ParamList.IsVarArg != null) { table.SetNameValue("...", new LuaMultiValue(args)); } this.Chunk.Enviroment = table; return this.Chunk.Execute(); }) ); }
private LuaBoolean() { MetaTable = new LuaTable(); }