Exemple #1
0
        public static LuaValue Lines(LuaValue[] values)
        {
            LuaUserdata data   = values[0] as LuaUserdata;
            TextReader  reader = data.Value as TextReader;

            LuaFunction func = new LuaFunction((LuaValue[] args) =>
            {
                LuaUserdata _data  = values[0] as LuaUserdata;
                TextReader _reader = data.Value as TextReader;

                string line = _reader.ReadLine();

                if (line != null)
                {
                    return(new LuaString(line));
                }
                else
                {
                    return(LuaNil.Nil);
                }
            }
                                               );

            return(new LuaMultiValue(new LuaValue[] { func, data, LuaNil.Nil }));
        }
Exemple #2
0
        public static LuaValue seek(LuaValue[] values)
        {
            LuaUserdata data   = values[0] as LuaUserdata;
            Stream      stream = null;

            StreamWriter writer = data.Value as StreamWriter;

            if (writer != null)
            {
                stream = writer.BaseStream;
            }
            else
            {
                StreamReader reader = data.Value as StreamReader;
                if (reader != null)
                {
                    stream = reader.BaseStream;
                }
            }

            if (stream != null)
            {
                LuaString whenceStr = values.Length > 1 ? values[1] as LuaString : null;
                string    whence    = whenceStr == null ? "cur" : whenceStr.Text;

                LuaNumber offsetNum = values.Length > 1 && whenceStr == null ? values[1] as LuaNumber : null;
                offsetNum = values.Length > 2 && offsetNum == null ? values[2] as LuaNumber : null;
                long offset = offsetNum == null ? 0 : (long)offsetNum.Number;

                stream.Seek(offset, GetSeekOrigin(whence));
            }

            return(null);
        }
Exemple #3
0
        public static LuaValue StaticClassToLuaTable(Type type)
        {
            LuaUserdata data = new LuaUserdata(null);

            data.MetaTable = GetControlMetaTable(true, type);
            data.MetaTable.SetNameValue("staticClassType", ToLuaValue(type));
            return(data);
        }
Exemple #4
0
        public static LuaValue Flush(LuaValue[] values)
        {
            LuaUserdata data   = values[0] as LuaUserdata;
            TextWriter  writer = data.Value as TextWriter;

            writer.Flush();
            return(null);
        }
Exemple #5
0
        public static LuaValue RunApp(LuaValue[] values)
        {
            LuaUserdata data = values[0] as LuaUserdata;
            Form        form = (Form)data.Value;

            form.PerformLayout();
            Application.Run(form);
            return(null);
        }
Exemple #6
0
        /*
         * public static LuaValue Seek(LuaValue[] values)
         * {
         *  LuaUserdata data = values[0] as LuaUserdata;
         *  Stream stream = null;
         *
         *  TextWriter writer = data.Value as TextWriter;
         *  if (writer != null)
         *  {
         *      stream = writer.BaseStream;
         *  }
         *  else
         *  {
         *      TextReader reader = data.Value as TextReader;
         *      if (reader != null)
         *      {
         *          stream = reader.BaseStream;
         *      }
         *  }
         *
         *  if (stream != null)
         *  {
         *      LuaString whenceStr = values.Length > 1 ? values[1] as LuaString : null;
         *      string whence = whenceStr == null ? "cur" : whenceStr.Text;
         *
         *      LuaNumber offsetNum = values.Length > 1 && whenceStr == null ? values[1] as LuaNumber : null;
         *      offsetNum = values.Length > 2 && offsetNum == null ? values[2] as LuaNumber : null;
         *      long offset = offsetNum == null ? 0 : (long)offsetNum.Number;
         *
         *      stream.Seek(offset, GetSeekOrigin(whence));
         *  }
         *
         *  return null;
         * }
         *
         * static SeekOrigin GetSeekOrigin(string whence)
         * {
         *  switch (whence)
         *  {
         *      case "set":
         *          return SeekOrigin.Begin;
         *      case "end":
         *          return SeekOrigin.End;
         *      case "cur":
         *      default:
         *          return SeekOrigin.Current;
         *  }
         * }
         */
        public static LuaValue Write(LuaValue[] values)
        {
            LuaUserdata data   = values[0] as LuaUserdata;
            TextWriter  writer = data.Value as TextWriter;

            for (int i = 1; i < values.Length; i++)
            {
                writer.Write(values[i].Value);
            }

            return(null);
        }
Exemple #7
0
        public static LuaValue Read(LuaValue[] values)
        {
            LuaUserdata data   = values[0] as LuaUserdata;
            TextReader  reader = data.Value as TextReader;

            LuaNumber number = values.Length > 1 ? values[1] as LuaNumber : null;

            if (number != null)
            {
                if (number.Number == 0)
                {
                    return(LuaString.Empty);
                }

                if (reader.Peek() == -1)
                {
                    return(LuaNil.Nil);
                }

                char[] block = new char[(int)number.Number];
                int    chars = reader.Read(block, 0, block.Length);
                return(new LuaString(new string(block, 0, chars)));
            }

            LuaString param = values.Length > 1 ? values[1] as LuaString : null;
            string    mode  = param == null ? "*l" : param.Text;

            switch (mode)
            {
            case "*l":
                if (reader.Peek() == -1)
                {
                    return(LuaNil.Nil);
                }
                return(new LuaString(reader.ReadLine()));

            case "*a":
                return(new LuaString(reader.ReadToEnd()));

            case "*n":
                List <char> buffer = new List <char>();
                int         ch     = reader.Peek();
                while (ch >= '0' && ch <= '9')
                {
                    buffer.Add((char)reader.Read());
                    ch = reader.Peek();
                }
                return(new LuaNumber(int.Parse(new string(buffer.ToArray()))));
            }

            return(null);
        }
 public static LuaValue ToLuaValue(object value)
 {
     if ((value as LuaValue) != null)
     {
         return(value as LuaValue);
     }
     if (value is int || value is double || value is float)
     {
         return(new LuaNumber(Convert.ToDouble(value)));
     }
     else if (value is MuMech.MovingAverage)
     {
         return(new LuaNumber(((MuMech.MovingAverage)value).value));
     }
     else if (value is string)
     {
         return(new LuaString((string)value));
     }
     else if (value is bool)
     {
         return(LuaBoolean.From((bool)value));
     }
     else if (value == null)
     {
         return(LuaNil.Nil);
     }
     else if (value.GetType().IsEnum)
     {
         return(new LuaString(value.ToString()));
     }
     else if (value.GetType().IsArray)
     {
         LuaTable table = new LuaTable();
         foreach (var item in (value as Array))
         {
             table.AddValue(ToLuaValue(item));
         }
         return(table);
     }
     else if (value is LuaValue)
     {
         return((LuaValue)value);
     }
     else
     {
         LuaUserdata data = new LuaUserdata(value);
         data.MetaTable = GetControlMetaTable();
         return(data);
     }
 }
Exemple #9
0
        public static LuaValue GetControlCreator(LuaValue[] values)
        {
            LuaString typeString = values[0] as LuaString;
            string    typeName   = "System.Windows.Forms." + typeString.Text;
            Type      type       = Assembly.GetAssembly(typeof(Application)).GetType(typeName, true, true);

            LuaFunction func = new LuaFunction((LuaValue[] args) =>
            {
                object control = Activator.CreateInstance(type);
                LuaTable table = args[0] as LuaTable;
                string name    = null;

                if (table.Count > 0)
                {
                    AddChildControls(control, table);
                }

                if (table.Count > 0)
                {
                    foreach (var pair in table.KeyValuePairs)
                    {
                        string member = (pair.Key as LuaString).Text;

                        if (member == "Name")
                        {
                            name = (string)pair.Value.Value;
                            continue;
                        }

                        SetMemberValue(control, type, member, pair.Value.Value);
                    }
                }

                LuaUserdata data = new LuaUserdata(control);
                data.MetaTable   = GetControlMetaTable();

                if (name != null)
                {
                    LuaTable enviroment = currentModule.GetValue("_G") as LuaTable;
                    enviroment.SetNameValue(name, data);
                }

                return(data);
            }
                                               );

            currentModule.SetNameValue(typeString.Text, func);
            return(func);
        }
Exemple #10
0
        public static LuaValue Close(LuaValue[] values)
        {
            LuaUserdata data   = values[0] as LuaUserdata;
            TextReader  reader = data.Value as TextReader;

            if (reader != null)
            {
                reader.Close();
                return(null);
            }

            TextWriter writer = data.Value as TextWriter;

            if (writer != null)
            {
                writer.Close();
            }

            return(null);
        }
        public static LuaTable GetControlMetaTable()
        {
            if (controlMetaTable == null)
            {
                controlMetaTable = new LuaTable();

                controlMetaTable.SetNameValue("__index", new LuaFunction((values) =>
                {
                    LuaUserdata control = values[0] as LuaUserdata;
                    Type type           = control.Value.GetType();

                    LuaString member = values[1] as LuaString;
                    if (member != null)
                    {
                        return(GetMemberValue(control.Value, type, member.Text));
                    }

                    LuaNumber index = values[1] as LuaNumber;
                    if (index != null)
                    {
                        return(GetIndexerValue(control.Value, type, index.Number));
                    }

                    return(LuaNil.Nil);
                }));

                controlMetaTable.SetNameValue("__newindex", new LuaFunction((values) =>
                {
                    LuaUserdata control = values[0] as LuaUserdata;
                    LuaString member    = values[1] as LuaString;
                    LuaValue value      = values[2];

                    Type type = control.Value.GetType();
                    SetMemberValue(control.Value, type, member.Text, value.Value);
                    return(null);
                }));
            }

            return(controlMetaTable);
        }
Exemple #12
0
        public static LuaValue output(LuaValue[] values)
        {
            if (values == null || values.Length == 0)
            {
                return(new LuaUserdata(DefaultOutput));
            }
            else
            {
                LuaString file = values[0] as LuaString;
                if (file != null)
                {
                    DefaultOutput = File.CreateText(file.Text);
                    return(null);
                }

                LuaUserdata data = values[0] as LuaUserdata;
                if (data != null && data.Value is TextWriter)
                {
                    DefaultOutput = data.Value as TextWriter;
                }
                return(null);
            }
        }
Exemple #13
0
        public static LuaValue Input(LuaValue[] values)
        {
            if (values == null || values.Length == 0)
            {
                return(new LuaUserdata(DefaultInput));
            }
            else
            {
                LuaString file = values[0] as LuaString;
                if (file != null)
                {
                    DefaultInput = File.OpenText <LuaRuntime>(file.Text);
                    return(null);
                }

                LuaUserdata data = values[0] as LuaUserdata;
                if (data != null && data.Value is TextReader)
                {
                    DefaultInput = data.Value as TextReader;
                }
                return(null);
            }
        }
Exemple #14
0
        /// <summary>
        /// Sets the assignment
        /// </summary>
        /// <param name="baseValue"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        private static void SetKeyValue(LuaValue baseValue, LuaValue key, LuaValue value)
        {
            LuaValue newIndex = LuaNil.Nil;
            LuaTable table    = baseValue as LuaTable;

            if (table != null)
            {
                if (table.MetaTable != null)
                {
                    newIndex = table.MetaTable.GetValue("__newindex");
                    // to be finished at the end of this method
                }

                if (newIndex == LuaNil.Nil)
                {
                    table.SetKeyValue(key, value);
                    return;
                }
            }
            else if ((baseValue as LuaClass) != null)
            {
                LuaClass c = baseValue as LuaClass;
                // null checks (mainly for debugging)
                if (c.Self.MetaTable == null)
                {
                    c.GenerateMetaTable();
                }
                //throw new Exception("Class metatable is nil!");
                newIndex = c.Self.MetaTable.GetValue("__newindex");
                if (newIndex == LuaNil.Nil)
                {
                    c.Self.SetKeyValue(key, value);
                }
                else
                {
                    (newIndex as LuaFunction).Invoke(new LuaValue[] { baseValue, key, value });
                }
                return;
            }
            else
            {
                LuaUserdata userdata = baseValue as LuaUserdata;
                if (userdata != null)
                {
                    if (userdata.MetaTable != null)
                    {
                        newIndex = userdata.MetaTable.GetValue("__newindex");
                    }

                    if (newIndex == LuaNil.Nil)
                    {
                        throw new Exception("Assign field of userdata without __newindex defined.");
                    }
                }
            }

            LuaFunction func = newIndex as LuaFunction;

            if (func != null)
            {
                func.Invoke(new LuaValue[] { baseValue, key, value });
            }
            else
            {
                SetKeyValue(newIndex, key, value);
            }
        }
Exemple #15
0
        public static LuaTable GetControlMetaTable(bool isStatic = false, Type classType = null)
        {
            LuaTable controlMetaTable = null;

            if (isStatic == false && instanceControlMetaTable == null || isStatic == true && controlMetaTables.ContainsKey(classType) == false)
            {
                controlMetaTable = new LuaTable();

                controlMetaTable.SetNameValue("__index", new LuaFunction((values) =>
                {
                    bool isStaticClass  = false;
                    LuaUserdata control = values[0] as LuaUserdata;
                    Type type;
                    if (control.MetaTable.ContainsKey(new LuaString("staticClassType")))
                    {
                        isStaticClass = true;
                        type          = values[0].MetaTable.GetValue("staticClassType").Value as Type;
                    }
                    else
                    {
                        type = control.Value.GetType();
                    }

                    LuaString member = values[1] as LuaString;
                    if (member != null)
                    {
                        return(GetMemberValue((isStaticClass == false ? control.Value : null), type, member.Text));
                    }

                    LuaNumber index = values[1] as LuaNumber;
                    if (index != null)
                    {
                        return(GetIndexerValue((isStaticClass == false ? control.Value : null), type, index.Number));
                    }

                    return(LuaNil.Nil);
                }));

                controlMetaTable.SetNameValue("__newindex", new LuaFunction((values) =>
                {
                    bool isStaticClass  = false;
                    LuaUserdata control = values[0] as LuaUserdata;
                    Type type;
                    if (control.MetaTable.ContainsKey(new LuaString("staticClassType")))
                    {
                        isStaticClass = true;
                        type          = values[0].MetaTable.GetValue("staticClassType").Value as Type;
                    }
                    else
                    {
                        type = control.Value.GetType();
                    }

                    LuaString member = values[1] as LuaString;
                    LuaValue value   = values[2];

                    SetMemberValue((isStaticClass == false ? control.Value : null), type, member.Text, value.Value);
                    return(null);
                }));

                if (isStatic == false)
                {
                    instanceControlMetaTable = controlMetaTable;
                }
                else
                {
                    controlMetaTables[classType] = controlMetaTable;
                }
            }

            if (isStatic == false)
            {
                return(instanceControlMetaTable);
            }
            else
            {
                return(controlMetaTables[classType]);
            }
        }
Exemple #16
0
        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.");
            }
        }
Exemple #17
0
        public override LuaValue Evaluate(LuaValue baseValue, LuaTable enviroment)
        {
            LuaFunction function = baseValue as LuaFunction;

            if (function != null)
            {
                if (function.Function.Method.DeclaringType.FullName == "SharpLua.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));
                    return(function.Function.Invoke(LuaMultiValue.UnWrapLuaValues(args.ToArray())));
                }
            }
            else if ((baseValue as LuaTable) != null)
            {
                List <LuaValue> args = this.Args.ArgList.ConvertAll(arg => arg.Evaluate(enviroment));
                args.Insert(0, baseValue);
                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     u    = baseValue as LuaUserdata;
                if (u.MetaTable != null)
                {
                    if (u.MetaTable.GetValue("__call") != null)
                    {
                        return(ObjectToLua.ToLuaValue((u.MetaTable.GetValue("__call") as LuaFunction).Invoke(args.ToArray())));
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
            else
            {
                throw new Exception("Invoke function call on non function value.");
            }
        }