Esempio n. 1
0
        public static LuaValue GetKeyValue(LuaValue baseValue, LuaValue key)
        {
            LuaTable table = baseValue as LuaTable;

            if (((baseValue as LuaClass) != null) && table == null)
            {
                table = (baseValue as LuaClass).Self;
            }

            if (table != null)
            {
                return(table.GetValue(key));
            }
            else
            {
                LuaUserdata userdata = baseValue as LuaUserdata;
                if (userdata != null)
                {
                    if (userdata.MetaTable != null)
                    {
                        LuaValue index = userdata.MetaTable.GetValue("__index");
                        if (index != null)
                        {
                            LuaFunction func = index as LuaFunction;
                            if (func != null)
                            {
                                // its a getter function
                                return(func.Invoke(new LuaValue[] { baseValue, key }));
                            }
                            else
                            {
                                // its a probably a table
                                return(GetKeyValue(index, key));
                            }
                        }
                    }
                }

                throw new Exception(string.Format("Access field '{0}' not from a table.", key.Value));
            }
        }
Esempio n. 2
0
 public static LuaValue ToLuaValue(object value)
 {
     if ((value as LuaValue) != null)
         return value as LuaValue;
     if (value is int || value is double)
     {
         return new LuaNumber(Convert.ToDouble(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;
     }
 }
Esempio n. 3
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;
        }