public static LuaValue GetKeyValue(LuaValue baseValue, LuaValue key)
        {
            LuaTable table = baseValue as LuaTable;

            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)
                            {
                                return(func.Invoke(new LuaValue[] { baseValue, key }));
                            }
                            else
                            {
                                return(GetKeyValue(index, key));
                            }
                        }
                    }
                }

                //[PixelCrushers] Changed wording:
                throw new Exception(string.Format("Lookup of field '{0}' in the table element failed because the table element itself isn't in the table.", key.Value));
            }
        }
Exemple #2
0
        public static LuaValue GetKeyValue(LuaValue baseValue, LuaValue key)
        {
            LuaTable table = baseValue as LuaTable;

            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)
                            {
                                return(func.Invoke(new LuaValue[] { baseValue, key }));
                            }
                            else
                            {
                                return(GetKeyValue(index, key));
                            }
                        }
                    }
                }

                throw new Exception(string.Format("Access field '{0}' from not a table.", key.Value));
            }
        }
Exemple #3
0
        private static void SetKeyValue(LuaValue baseValue, LuaValue key, LuaValue value)
        {
            LuaValue newIndex = LuaNil.Nil;
            LuaTable table    = baseValue as LuaTable;

            if (table != null)
            {
                if (table.ContainsKey(key))
                {
                    table.SetKeyValue(key, value);
                    return;
                }
                else
                {
                    if (table.MetaTable != null)
                    {
                        newIndex = table.MetaTable.GetValue("__newindex");
                    }

                    if (newIndex == LuaNil.Nil)
                    {
                        table.SetKeyValue(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);
            }
        }