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]); 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 override LuaValue Evaluate(LuaValue baseValue, LuaTable enviroment) { LuaValue value = LuaValue.GetKeyValue(baseValue, new LuaString(this.Method)); 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)); List<LuaValue> args = new List<LuaValue>(); foreach (var arg in this.Args.ArgList) args.Add(arg.Evaluate(enviroment)); args.Insert(0, baseValue); return function.Function.Invoke(args.ToArray()); } } else { throw new Exception("Invoke method call on non function value."); } }
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 override LuaValue Execute(LuaTable enviroment, out bool isBreak) { // LuaValue[] values = this.ExprList.ConvertAll(expr => expr.Evaluate(enviroment)).ToArray(); List<LuaValue> values = new List<LuaValue>(); foreach (var expr in this.ExprList) values.Add(expr.Evaluate(enviroment)); LuaValue[] neatValues = LuaMultiValue.UnWrapLuaValues(values.ToArray()); for (int i = 0; i < Math.Min(this.NameList.Count, neatValues.Length); i++) { enviroment.RawSetValue(this.NameList[i], neatValues[i]); } if (neatValues.Length < this.NameList.Count) { for (int i = neatValues.Length; i < this.NameList.Count - neatValues.Length; i++) { enviroment.RawSetValue(this.NameList[i], LuaNil.Nil); } } isBreak = false; return null; }
public override LuaValue Evaluate(LuaValue baseValue, LuaTable enviroment) { LuaValue value = LuaValue.GetKeyValue(baseValue, new LuaString(this.Method)); 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 { //[PixelCrushers]List<LuaValue> args = this.Args.ArgList.ConvertAll(arg => arg.Evaluate(enviroment)); List<LuaValue> args = LuaInterpreterExtensions.EvaluateAll(this.Args.ArgList, enviroment); args.Insert(0, baseValue); return function.Function.Invoke(args.ToArray()); } } else { throw new Exception("Invoke method call on non function value."); } }
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 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 LuaValue RunFile(string luaFile, LuaTable enviroment) { //[PixelCrushers] //return Interpreter(File.ReadAllText(luaFile), enviroment); UnityEngine.Debug.LogWarning("LuaInterpreter.RunFile() is disabled in this version of LuaInterpreter."); return LuaNil.Nil; }
/// <summary> /// This method replaces List.ConvertAll(), which isn't supported in WinRT. /// </summary> /// <returns>The list of evaluated expressions.</returns> /// <param name="exprList">A list of expressions to evaluate.</param> /// <param name="environment">Lua environment.</param> public static List<LuaValue> EvaluateAll(List<Expr> exprList, LuaTable environment) { List<LuaValue> values = new List<LuaValue>(); foreach (var expr in exprList) { values.Add(expr.Evaluate(environment)); } return values; }
// Parses and executes code public static LuaValue Interpreter(string UserCode, LuaTable Environment) { Chunk Chunk = Parse(UserCode); Chunk.Environment = Environment; return Chunk.Execute(); }
public override LuaValue Execute(LuaTable enviroment, out bool isBreak) { //[PixelCrushers]LuaValue[] values = this.ExprList.ConvertAll(expr => expr.Evaluate(enviroment)).ToArray(); LuaValue[] values = LuaInterpreterExtensions.EvaluateAll(this.ExprList, 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) { 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) { if (baseValue == null || (baseValue is LuaNil)) { throw new System.NullReferenceException("Cannot assign to a null value. Are you trying to assign to a nonexistent table element?."); } 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) { LuaValue baseValue = this.Base.Evaluate(enviroment); foreach (Access access in this.Accesses) { baseValue = access.Evaluate(baseValue, enviroment); } return baseValue; }
public override LuaValue Execute(LuaTable environment, out bool isBreak) { LuaValue[] values = ExprList.ConvertAll(expr => expr.Evaluate(environment)).ToArray(); LuaValue[] neatValues = LuaMultiValue.UnWrapLuaValues(values); for (int i = 0; i < Math.Min(VarList.Count, neatValues.Length); i++) { Var var = VarList[i]; if (var.Accesses.Count == 0) { VarName varName = var.Base as VarName; if (varName != null) { SetKeyValue(environment, new LuaString(varName.Name), values[i]); LuaInterpreter.CodeReport.AssignedVariables.Add(new UserCode.Variable { VarName = varName.Name, VarValue = values[i].Value.ToString() }); continue; } } else { LuaValue baseValue = var.Base.Evaluate(environment); for (int j = 0; j < var.Accesses.Count - 1; j++) { Access access = var.Accesses[j]; baseValue = access.Evaluate(baseValue, environment); } 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(environment), values[i]); } } } isBreak = false; return null; }
public override LuaValue Execute(LuaTable enviroment, out bool isBreak) { // LuaValue[] values = this.ExprList.ConvertAll(expr => expr.Evaluate(enviroment)).ToArray(); List<LuaValue> values = new List<LuaValue>(); foreach (var expr in this.ExprList) values.Add(expr.Evaluate(enviroment)); LuaValue[] neatValues = LuaMultiValue.UnWrapLuaValues(values.ToArray()); 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) { 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; }
// Creates global environment for the program and registers libraries public static LuaTable CreateGlobalEnviroment() { LuaTable global = new LuaTable(); BaseLib.RegisterFunctions(global); StringLib.RegisterModule(global); MathLib.RegisterModule(global); global.SetNameValue("_G", global); return global; }
public override LuaValue Execute(LuaTable enviroment, out bool isBreak) { // LuaValue[] values = this.ExprList.ConvertAll(expr => expr.Evaluate(enviroment)).ToArray(); List<LuaValue> values = new List<LuaValue>(); foreach (var expr in this.ExprList) values.Add(expr.Evaluate(enviroment)); LuaValue[] neatValues = LuaMultiValue.UnWrapLuaValues(values.ToArray()); 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 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 LuaValue Evaluate(LuaTable environment) { return new LuaFunction( new LuaFunc(delegate (LuaValue[] args) { var table = new LuaTable(environment); List<string> names = 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 (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)); } } } else if (ParamList.IsVarArg != null) { table.SetNameValue("...", new LuaMultiValue(args)); } Chunk.Environment = table; return Chunk.Execute(); }) ); }
public override LuaValue Evaluate(LuaValue baseValue, LuaTable enviroment) { LuaFunction function = baseValue as LuaFunction; if (function != null) { if (function.Function.Method.DeclaringType.FullName == "Language.Lua.Library.BaseLib" && (function.Function.Method.Name == "loadstring" || function.Function.Method.Name == "dofile")) { if (this.Args.String != null) { return function.Function.Invoke(new LuaValue[] { this.Args.String.Evaluate(enviroment), enviroment }); } else { return function.Function.Invoke(new LuaValue[] { this.Args.ArgList[0].Evaluate(enviroment), enviroment }); } } if (this.Args.Table != null) { return function.Function.Invoke(new LuaValue[] { this.Args.Table.Evaluate(enviroment) }); } else if (this.Args.String != null) { return function.Function.Invoke(new LuaValue[] { this.Args.String.Evaluate(enviroment) }); } else { // List<LuaValue> args = this.Args.ArgList.ConvertAll(arg => arg.Evaluate(enviroment)); List<LuaValue> args = new List<LuaValue>(); foreach (var arg in this.Args.ArgList) args.Add(arg.Evaluate(enviroment)); return function.Function.Invoke(LuaMultiValue.UnWrapLuaValues(args.ToArray())); } } else { throw new Exception("Invoke function call on non function value."); } }
public override LuaValue Execute(LuaTable enviroment, out bool isBreak) { while (true) { var returnValue = this.Body.Execute(enviroment, out isBreak); if (returnValue != null || isBreak == true) { isBreak = false; return returnValue; } LuaValue condition = this.Condition.Evaluate(enviroment); if (condition.GetBooleanValue() == true) { break; } } return null; }
public override LuaValue Evaluate(LuaValue baseValue, LuaTable enviroment) { LuaFunction function = baseValue as LuaFunction; if (function != null) { //[PixelCrushers] Removed (not WinRT compatible, and not needed for Dialogue System) //if (function.Function.Method.DeclaringType.FullName == "Language.Lua.Library.BaseLib" && // (function.Function.Method.Name == "loadstring" || function.Function.Method.Name == "dofile")) //{ // if (this.Args.String != null) // { // return function.Function.Invoke(new LuaValue[] { this.Args.String.Evaluate(enviroment), enviroment }); // } // else // { // return function.Function.Invoke(new LuaValue[] { this.Args.ArgList[0].Evaluate(enviroment), enviroment }); // } //} if (this.Args.Table != null) { return function.Function.Invoke(new LuaValue[] { this.Args.Table.Evaluate(enviroment) }); } else if (this.Args.String != null) { return function.Function.Invoke(new LuaValue[] { this.Args.String.Evaluate(enviroment) }); } else { //[PixelCrushers] Was: List<LuaValue> args = this.Args.ArgList.ConvertAll(arg => arg.Evaluate(enviroment)); List<LuaValue> args = LuaInterpreterExtensions.EvaluateAll(this.Args.ArgList, enviroment); return function.Function.Invoke(LuaMultiValue.UnWrapLuaValues(args.ToArray())); } } else { throw new Exception("Invoke function call on non function value."); } }
public override LuaValue Execute(LuaTable enviroment, out bool isBreak) { //[PixelCrushers]LuaValue[] values = this.ExprList.ConvertAll(expr => expr.Evaluate(enviroment)).ToArray(); LuaValue[] values = LuaInterpreterExtensions.EvaluateAll(this.ExprList, enviroment).ToArray(); LuaValue[] neatValues = LuaMultiValue.UnWrapLuaValues(values); for (int i = 0; i < Math.Min(this.NameList.Count, neatValues.Length); i++) { enviroment.RawSetValue(this.NameList[i], neatValues[i]); } if (neatValues.Length < this.NameList.Count) { for (int i = neatValues.Length; i < this.NameList.Count - neatValues.Length; i++) { enviroment.RawSetValue(this.NameList[i], LuaNil.Nil); } } isBreak = false; return null; }
private LuaValue InfixBinaryOperation(Term LeftOperand, string Operator, Term RightOperand, LuaTable enviroment) { LuaValue leftValue = LeftOperand.Evaluate(enviroment); LuaValue rightValue = RightOperand.Evaluate(enviroment); switch (Operator) { case "+": var left = leftValue as LuaNumber; var right = rightValue as LuaNumber; if (left != null && right != null) { return new LuaNumber(left.Number + right.Number); } else { LuaFunction func = GetMetaFunction("__add", leftValue, rightValue); if (func != null) { return func.Invoke(new LuaValue[] { leftValue, rightValue }); } } break; case "-": left = leftValue as LuaNumber; right = rightValue as LuaNumber; if (left != null && right != null) { return new LuaNumber(left.Number - right.Number); } else { LuaFunction func = GetMetaFunction("__sub", leftValue, rightValue); if (func != null) { return func.Invoke(new LuaValue[] { leftValue, rightValue }); } } break; case "*": left = leftValue as LuaNumber; right = rightValue as LuaNumber; if (left != null && right != null) { return new LuaNumber(left.Number * right.Number); } else { LuaFunction func = GetMetaFunction("__mul", leftValue, rightValue); if (func != null) { return func.Invoke(new LuaValue[] { leftValue, rightValue }); } } break; case "/": left = leftValue as LuaNumber; right = rightValue as LuaNumber; if (left != null && right != null) { return new LuaNumber(left.Number / right.Number); } else { LuaFunction func = GetMetaFunction("__div", leftValue, rightValue); if (func != null) { return func.Invoke(new LuaValue[] { leftValue, rightValue }); } } break; case "%": left = leftValue as LuaNumber; right = rightValue as LuaNumber; if (left != null && right != null) { return new LuaNumber(left.Number % right.Number); } else { LuaFunction func = GetMetaFunction("__mod", leftValue, rightValue); if (func != null) { return func.Invoke(new LuaValue[] { leftValue, rightValue }); } } break; case "^": left = leftValue as LuaNumber; right = rightValue as LuaNumber; if (left != null && right != null) { return new LuaNumber(Math.Pow(left.Number, right.Number)); } else { LuaFunction func = GetMetaFunction("__pow", leftValue, rightValue); if (func != null) { return func.Invoke(new LuaValue[] { leftValue, rightValue }); } } break; case "==": return LuaBoolean.From(leftValue.Equals(rightValue)); case "~=": return LuaBoolean.From(leftValue.Equals(rightValue) == false); case "<": int? compare = Compare(leftValue, rightValue); if (compare != null) { return LuaBoolean.From(compare < 0); } else { LuaFunction func = GetMetaFunction("__lt", leftValue, rightValue); if (func != null) { return func.Invoke(new LuaValue[] { leftValue, rightValue }); } } break; case ">": compare = Compare(leftValue, rightValue); if (compare != null) { return LuaBoolean.From(compare > 0); } else { LuaFunction func = GetMetaFunction("__gt", leftValue, rightValue); if (func != null) { return func.Invoke(new LuaValue[] { leftValue, rightValue }); } } break; case "<=": compare = Compare(leftValue, rightValue); if (compare != null) { return LuaBoolean.From(compare <= 0); } else { LuaFunction func = GetMetaFunction("__le", leftValue, rightValue); if (func != null) { return func.Invoke(new LuaValue[] { leftValue, rightValue }); } } break; case ">=": compare = Compare(leftValue, rightValue); if (compare != null) { return LuaBoolean.From(compare >= 0); } else { LuaFunction func = GetMetaFunction("__ge", leftValue, rightValue); if (func != null) { return func.Invoke(new LuaValue[] { leftValue, rightValue }); } } break; case "..": if ((leftValue is LuaString || leftValue is LuaNumber) && (rightValue is LuaString || rightValue is LuaNumber)) { return new LuaString(string.Concat(leftValue, rightValue)); } else { LuaFunction func = GetMetaFunction("__concat", leftValue, rightValue); if (func != null) { return func.Invoke(new LuaValue[] { leftValue, rightValue }); } } break; case "and": bool leftBool = leftValue.GetBooleanValue(); //[PixelCrushers]bool rightBool = rightValue.GetBooleanValue(); if (leftBool == false) { return leftValue; } else { return rightValue; } case "or": leftBool = leftValue.GetBooleanValue(); //[PixelCrushers]rightBool = rightValue.GetBooleanValue(); if (leftBool == true) { return leftValue; } else { return rightValue; } } return null; }
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); } break; case "not": var rightBool = rightValue as LuaBoolean; if (rightBool != null) { return LuaBoolean.From(!rightBool.BoolValue); } break; } return LuaNil.Nil; }
public LuaTable CreateModuleFromTypes(IEnumerable<Type> types, string moduleName = "") { LuaTable module = new LuaTable(); StringBuilder moduleDocs = new StringBuilder(); foreach(Type t in types) { if (t.GetCustomAttributes(true).OfType<LuaExcludeAttribute>().Any()) continue; string typeName = t.Name; moduleDocs.AppendLine(typeName); StringBuilder typeDoc = new StringBuilder(); LuaTable subModule = new LuaTable(); LuaTable metatable = new LuaTable(); metatable.SetNameValue("__index", metatable); subModule.MetaTable = metatable; // Create a constructor where appropriate ConstructorInfo constructorInfo = null; if (t.IsValueType) { subModule.Register("new", (args) => { return ObjectToLua(Activator.CreateInstance(t)); }); typeDoc.AppendFormat("new() - creates a new instance of the {0} type.\n", typeName); } else { if ((constructorInfo = t.GetConstructor(new Type[] { })) != null) { subModule.Register("new", (args) => { try { return ObjectToLua(constructorInfo.Invoke(new object[] { })); } catch (Exception e) { Console.Error.WriteLine("Lua constructor for {0} threw an exception of type {1}: {2}", t.Name, e.GetType().Name, e.Message); if (e.InnerException != null) Console.Error.WriteLine("Inner exception of type {0}: {1}", e.InnerException.GetType().Name, e.InnerException.Message); } return LuaNil.Nil; }); } else if ((constructorInfo = t.GetConstructor(new Type[] { typeof(LuaValue[]) })) != null) { subModule.Register("new", (args) => { try { return ObjectToLua(constructorInfo.Invoke(new object[] { args })); } catch (Exception e) { Console.Error.WriteLine("Lua constructor for {0} threw an exception of type {1}: {2}", t.Name, e.GetType().Name, e.Message); if (e.InnerException != null) Console.Error.WriteLine("Inner exception of type {0}: {1}", e.InnerException.GetType().Name, e.InnerException.Message); } return LuaNil.Nil; }); } if (constructorInfo != null) { if (constructorInfo.GetCustomAttributes(true).OfType<LuaCommandUsageAttribute>().Any()) { var attribute = constructorInfo.GetCustomAttributes(true).OfType<LuaCommandUsageAttribute>().Single(); typeDoc.Append(attribute.Usage); } else { typeDoc.AppendFormat("new() - creates a new instance of the {0} type.\n", typeName); } } } // Create member accessors var members = from m in t.GetMembers(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public) where (m.MemberType == MemberTypes.Field || m.MemberType == MemberTypes.Property || m.MemberType == MemberTypes.Method) select m; foreach(MemberInfo member in members) { bool excludeGetter = false, excludeSetter = false; if (member.GetCustomAttributes(true).OfType<LuaExcludeAttribute>().Any()) { LuaExcludeAttribute exclude = member.GetCustomAttributes(true).OfType<LuaExcludeAttribute>().Single(); excludeSetter = true; if (!exclude.ReadOnly) excludeGetter = true; } var field = member as FieldInfo; var prop = member as PropertyInfo; var method = member as MethodInfo; string memberType = ""; if (field != null) { memberType = field.FieldType.Name; } else if (prop != null) { memberType = prop.PropertyType.Name; } else if (method != null) { memberType = method.DeclaringType.Name; } LuaFunction func = excludeSetter ? null : GetLuaFunc(member as MethodInfo); if(func != null) { // It's a function, and we'll register it. No way to create // documentation for it, so ideally it also makes use of the // LuaCommandUsage attribute for documentation. metatable.SetNameValue(member.Name, func); if (member.GetCustomAttributes(true).OfType<LuaCommandUsageAttribute>().Any()) { LuaCommandUsageAttribute usage = member.GetCustomAttributes(true).OfType<LuaCommandUsageAttribute>().Single(); typeDoc.AppendLine(String.Format("{0}:{1} - {2}", t.Name, member.Name, usage.Usage)); } else { typeDoc.AppendLine(member.Name); } } LuaFunction getter = excludeGetter ? null : GetAccessorLuaFunction(member); if(getter != null) { typeDoc.AppendFormat("{0} - Gets a {1} value from the {2} struct\n", "Get" + member.Name, memberType, typeName); metatable.SetNameValue("Get" + member.Name, getter); } LuaFunction setter = excludeSetter ? null : GetSetterLuaFunction(member); if(setter != null) { typeDoc.AppendFormat("{0} - Sets a {1} value to the {2} struct\n", "Set" + member.Name, memberType, typeName); metatable.SetNameValue("Set" + member.Name, setter); } } // Register the type metatable and help docs _environment.SetMetatableForType(t, metatable); module.SetNameValue(typeName, subModule); LuaEnvironment.RegisterNewUsage(moduleName + "." + typeName, typeDoc.ToString()); } // Register the module's documentation LuaEnvironment.RegisterNewUsage(moduleName, moduleDocs.ToString()); return module; }
public abstract LuaValue Execute(LuaTable enviroment, out bool isBreak);
public override LuaValue Execute(LuaTable environment, out bool isBreak) { throw new NotImplementedException(); }
public override LuaValue Execute(LuaTable environment, out bool isBreak) { LuaValue[] values = ExprList.ConvertAll(expr => expr.Evaluate(environment)).ToArray(); LuaValue[] neatValues = LuaMultiValue.UnWrapLuaValues(values); for (int i = 0; i < Math.Min(NameList.Count, neatValues.Length); i++) { environment.RawSetValue(NameList[i], neatValues[i]); } if (neatValues.Length < NameList.Count) { for (int i = neatValues.Length; i < NameList.Count - neatValues.Length; i++) { environment.RawSetValue(NameList[i], LuaNil.Nil); } } isBreak = false; return null; }
public override LuaValue Execute(LuaTable environment, out bool isBreak) { environment.SetNameValue(Name, Body.Evaluate(environment)); isBreak = false; return null; }
public override LuaValue Execute(LuaTable enviroment, out bool isBreak) { this.Expr.Evaluate(enviroment); isBreak = false; return(null); }
/// <summary> /// Initializes a new instance of the <see cref="PixelCrushers.DialogueSystem.LuaTableWrapper"/> class. /// </summary> /// <param name="luaTable">Lua table.</param> public LuaTableWrapper(LuaTable luaTable) { this.luaTable = luaTable; }
public override LuaValue Execute(LuaTable enviroment, out bool isBreak) { throw new NotImplementedException(); }