/// <summary>
        /// Gets a Lua expression that is of the given type.
        /// </summary>
        /// <param name="type">The type of expression.</param>
        /// <returns>A valid Lua expression of the given type.</returns>
        static string GetValueForType(LuaValueType type)
        {
            switch (type)
            {
            case LuaValueType.Nil:
                return("nil");

            case LuaValueType.String:
                return("'foobar'");

            case LuaValueType.Bool:
                return("true");

            case LuaValueType.Table:
                return("{}");

            case LuaValueType.Function:
                return("function() end");

            case LuaValueType.Number:
                return("123");

            case LuaValueType.Thread:
                return("coroutine.create(function() end)");

            case LuaValueType.UserData:
                return("UserData()");

            default:
                throw new NotImplementedException();
            }
        }
Beispiel #2
0
    public Lua()
    {
        LuaState.loaderDelegate += luaLoader;
        luaState = new LuaState();

        //  LuaDLL.lua_pushstdcallcfunction(luaState.L, import);
        //LuaDLL.lua_setglobal(luaState.L, "using");

        LuaObject.init(luaState.L);
        bindAll(luaState.L);

        GameObject go = new GameObject("LuaSvrProxy");

        lgo = go.AddComponent <LuaSvrGameObject>();
        GameObject.DontDestroyOnLoad(go);
        lgo.state    = luaState;
        lgo.onUpdate = this.tick;

        LuaTimer.reg(luaState.L);
        LuaCoroutine.reg(luaState.L, lgo);
        Helper.reg(luaState.L);
        LuaValueType.reg(luaState.L);
        LuaDLL.luaS_openextlibs(luaState.L);

        /*
         * if (LuaDLL.lua_gettop(luaState.L) != errorReported)
         * {
         *  Debug.LogError("Some function not remove temp value from lua stack. You should fix it.");
         *  errorReported = LuaDLL.lua_gettop(luaState.L);
         * }
         * */
    }
Beispiel #3
0
        static int GetClassType(IntPtr L)
        {
            int reference = LuaDLL.tolua_getmetatableref(L, -1);

            if (reference > 0)
            {
                Type t = LuaStatic.GetClassType(L, reference);
                Push(L, t);
            }
            else
            {
                LuaValueType ret = LuaDLL.tolua_getvaluetype(L, -1);

                switch (ret)
                {
                case LuaValueType.Vector3:
                    Push(L, typeof(Vector3));
                    break;

                case LuaValueType.Quaternion:
                    Push(L, typeof(Quaternion));
                    break;

                case LuaValueType.Vector4:
                    Push(L, typeof(Vector4));
                    break;

                case LuaValueType.Color:
                    Push(L, typeof(Color));
                    break;

                case LuaValueType.Ray:
                    Push(L, typeof(Ray));
                    break;

                case LuaValueType.Bounds:
                    Push(L, typeof(Bounds));
                    break;

                case LuaValueType.Vector2:
                    Push(L, typeof(Vector2));
                    break;

                case LuaValueType.LayerMask:
                    Push(L, typeof(LayerMask));
                    break;

                case LuaValueType.RaycastHit:
                    Push(L, typeof(RaycastHit));
                    break;

                default:
                    lhDebug.LogError("type not register to lua");
                    LuaDLL.lua_pushnil(L);
                    break;
                }
            }

            return(1);
        }
Beispiel #4
0
 private void ExpectType(LuaValueType type)
 {
     if (m_type != type)
     {
         throw GenerateTypeError(type.GetTypeName());
     }
 }
Beispiel #5
0
        private void ExpectType(LuaValueType type, int index)
        {
            var foundType = this[index].Type;

            if (foundType != type)
            {
                throw GenerateTypeError(type.GetTypeName(), index);
            }
        }
Beispiel #6
0
        static bool lua_isusertable(IntPtr L, Type t, int pos)
        {
            if (t.IsArray)
            {
                return(true);
            }
            else if (t == typeof(LuaTable))
            {
                return(true);
            }
            else if (t.IsValueType)
            {
                LuaValueType vt = LuaDLL.tolua_getvaluetype(L, pos);

                switch (vt)
                {
                case LuaValueType.Vector3:
                    return(typeof(Vector3) == t);

                case LuaValueType.Quaternion:
                    return(typeof(Quaternion) == t);

                case LuaValueType.Color:
                    return(typeof(Color) == t);

                case LuaValueType.Ray:
                    return(typeof(Ray) == t);

                case LuaValueType.Bounds:
                    return(typeof(Bounds) == t);

                case LuaValueType.Vector2:
                    return(typeof(Vector2) == t);

                case LuaValueType.Vector4:
                    return(typeof(Vector4) == t);

                case LuaValueType.Touch:
                    return(typeof(Touch) == t);

                case LuaValueType.LayerMask:
                    return(typeof(LayerMask) == t);

                case LuaValueType.RaycastHit:
                    return(typeof(RaycastHit) == t);

                default:
                    break;
                }
            }
            else if (LuaDLL.tolua_isvptrtable(L, pos))
            {
                return(IsMatchUserData(L, t, pos));
            }

            return(false);
        }
Beispiel #7
0
        /// <summary>
        /// 初始化LuaValue对象
        /// </summary>
        /// <param name="decoder">对象解码器</param>
        public LuaValue(LuaObjectDecoder decoder)
            : base(decoder)
        {
            int contextId = decoder.readInt32();

            _context = LuaContext.getContext(contextId);
            _tableId = decoder.readString();
            _type    = (LuaValueType)decoder.readInt16();
            _value   = null;

            switch (_type)
            {
            case LuaValueType.Integer:
                _value = decoder.readInt32();
                break;

            case LuaValueType.Boolean:
                _value = decoder.readByte();
                break;

            case LuaValueType.Number:
                _value = decoder.readDouble();
                break;

            case LuaValueType.Data:
                _value = decoder.readBytes();
                break;

            case LuaValueType.String:
                _value = decoder.readString();
                break;

            case LuaValueType.Array:
                _value = readArrayList(decoder);
                break;

            case LuaValueType.Map:
                _value = readHashtable(decoder);
                break;

            case LuaValueType.Function:
                _value = decoder.readObject() as LuaFunction;
                break;

            case LuaValueType.Ptr:
                _value = decoder.readObject() as LuaPointer;
                break;

            case LuaValueType.Tuple:
                _value = decoder.readObject() as LuaTuple;
                break;

            case LuaValueType.Object:
                _value = decoder.readObject();
                break;
            }
        }
Beispiel #8
0
        private void ExpectType(LuaValue key, LuaValueType type)
        {
            var value = this[key];

            if (value.Type != type)
            {
                throw GenerateTypeError(type.GetTypeName(), key);
            }
        }
Beispiel #9
0
        public static string GetTypeName(this LuaValueType type)
        {
            switch (type)
            {
            case LuaValueType.Nil:
            default:
            {
                return("nil");
            }

            case LuaValueType.Boolean:
            {
                return("boolean");
            }

            case LuaValueType.Integer:
            case LuaValueType.Number:
            {
                return("number");
            }

            case LuaValueType.String:
            case LuaValueType.ByteString:
            {
                return("string");
            }

            case LuaValueType.Table:
            {
                return("table");
            }

            case LuaValueType.Object:
            {
                return("object");
            }

            case LuaValueType.Function:
            case LuaValueType.CFunction:
            {
                return("function");
            }

            case LuaValueType.Coroutine:
            {
                return("coroutine");
            }

            case LuaValueType.Userdata:
            {
                return("userdata");
            }
            }
        }
Beispiel #10
0
 public LuaValue(LuaCoroutine value)
 {
     if (value != null)
     {
         m_type  = LuaValueType.Coroutine;
         m_value = new ValueUnion(value);
     }
     else
     {
         m_type  = LuaValueType.Nil;
         m_value = new ValueUnion();
     }
 }
Beispiel #11
0
 public LuaValue(LuaCFunction value)
 {
     if (value != null)
     {
         m_type  = LuaValueType.CFunction;
         m_value = new ValueUnion(value);
     }
     else
     {
         m_type  = LuaValueType.Nil;
         m_value = new ValueUnion();
     }
 }
Beispiel #12
0
 public LuaValue(LuaObject value)
 {
     if (value != null)
     {
         m_type  = LuaValueType.Object;
         m_value = new ValueUnion(value);
     }
     else
     {
         m_type  = LuaValueType.Nil;
         m_value = new ValueUnion();
     }
 }
Beispiel #13
0
 public LuaValue(LuaTable value)
 {
     if (value != null)
     {
         m_type  = LuaValueType.Table;
         m_value = new ValueUnion(value);
     }
     else
     {
         m_type  = LuaValueType.Nil;
         m_value = new ValueUnion();
     }
 }
Beispiel #14
0
 public LuaValue(byte[] value)
 {
     if (value != null)
     {
         m_type  = LuaValueType.ByteString;
         m_value = new ValueUnion(value);
     }
     else
     {
         m_type  = LuaValueType.Nil;
         m_value = new ValueUnion();
     }
 }
        /// <summary>
        /// Runs a test that tests invalid arguments passed to a method.  It will run the test
        /// with all types except the given one.
        /// </summary>
        /// <param name="validType">The valid argument type.</param>
        /// <param name="format">A format string for the code to test.</param>
        /// <param name="allowNil">True to allow nil to be passed.</param>
        protected void RunInvalidTypeTests(LuaValueType validType, string format, bool allowNil = false)
        {
            foreach (var type in Enum.GetValues(typeof(LuaValueType)).Cast<LuaValueType>())
            {
                if (type == validType || (type == LuaValueType.Nil && allowNil))
                    continue;

                try
                {
                    Lua.DoText(string.Format(format, GetValueForType(type)));
                    Assert.Fail("Expected ArgumentException to be thrown for type " + type);
                }
                catch (ArgumentException) { /* noop */ }
            }
        }
Beispiel #16
0
        /// <summary>
        /// Runs a test that tests invalid arguments passed to a method.  It will run the test
        /// with all types except the given one.
        /// </summary>
        /// <param name="validType">The valid argument type.</param>
        /// <param name="format">A format string for the code to test.</param>
        /// <param name="allowNil">True to allow nil to be passed.</param>
        protected void _runInvalidTypeTests(LuaValueType validType, string format,
                                            bool allowNil = false)
        {
            foreach (var type in Enum.GetValues(typeof(LuaValueType)).Cast <LuaValueType>())
            {
                if (type == validType || (type == LuaValueType.Nil && allowNil))
                {
                    continue;
                }

                try {
                    _lua.DoText(string.Format(format, _getValueForType(type)));
                    Assert.Fail("Expected ArgumentException to be thrown for type " + type);
                } catch (ArgumentException) { }
            }
        }
Beispiel #17
0
        public LuaValue ReadValue()
        {
            LuaValueType type = (LuaValueType)ReadInt32();
            Int64        value;

            if (type == LuaValueType.NUMBER)
            {
                value = ReadNumberRaw();
            }
            else
            {
                value = ReadObjectID();
            }

            return(new LuaValue(value, type));
        }
Beispiel #18
0
        void doinit(IntPtr L)
        {
            // LuaTimer.reg(L);
            LuaCoroutine.reg(L, lgo);
            Helper.reg(L);
            LuaValueType.reg(L);
            SLuaDebug.reg(L);
            LuaUtils.reg(luaState.L);

            lgo.state    = luaState;
            lgo.onUpdate = this.tick;
            lgo.init();

            LuaState.errorDelegate = delegate(string msg) {
                errorDelegate(msg);
            };

            inited = true;
        }
Beispiel #19
0
        /// <summary>
        /// 初始化LuaValue对象
        /// </summary>
        /// <param name="decoder">对象解码器</param>
        public LuaValue(LuaObjectDecoder decoder)
        {
            _type  = (LuaValueType)decoder.readInt16();
            _value = null;

            switch (_type)
            {
            case LuaValueType.Integer:
                _value = decoder.readInt32();
                break;

            case LuaValueType.Boolean:
                _value = decoder.readByte();
                break;

            case LuaValueType.Number:
                _value = decoder.readDouble();
                break;

            case LuaValueType.Data:
                _value = decoder.readBytes();
                break;

            case LuaValueType.String:
                _value = decoder.readString();
                break;

            case LuaValueType.Array:
                _value = readArrayList(decoder);
                break;

            case LuaValueType.Map:
                _value = readHashtable(decoder);
                break;

            case LuaValueType.Object:
            {
                _value = decoder.readObject();
                break;
            }
            }
        }
Beispiel #20
0
        void ProcessMessage_AutocompleteOptions()
        {
            AutocompleteResult[] options = null;

            int seqid = m_readBuffer.ReadInt32();
            int count = m_readBuffer.ReadInt32();

            if (count >= 0)
            {
                options = new AutocompleteResult[count];
                for (int index = 0; index < count; ++index)
                {
                    LuaValue     key       = m_readBuffer.ReadValue();
                    LuaValueType valueType = (LuaValueType)m_readBuffer.ReadInt32();
                    options[index] = new AutocompleteResult(key, valueType);
                }
            }
            string message = m_readBuffer.ReadString();

            OnAutocompleteOptions(seqid, options, message);
        }
Beispiel #21
0
        public static object ToVarTable(IntPtr L, int stackPos)
        {
            stackPos = LuaDLL.abs_index(L, stackPos);
            LuaValueType ret = LuaDLL.tolua_getvaluetype(L, stackPos);

            switch (ret)
            {
            case LuaValueType.Vector3:
                return(ToVector3(L, stackPos));

            case LuaValueType.Quaternion:
                return(ToQuaternion(L, stackPos));

            case LuaValueType.Vector4:
                return(ToVector4(L, stackPos));

            case LuaValueType.Color:
                return(ToColor(L, stackPos));

            case LuaValueType.Ray:
                return(ToRay(L, stackPos));

            case LuaValueType.Bounds:
                return(ToBounds(L, stackPos));

            case LuaValueType.Vector2:
                return(ToVector2(L, 2));

            case LuaValueType.LayerMask:
                return(ToLayerMask(L, stackPos));

            default:
                LuaDLL.lua_pushvalue(L, stackPos);
                int reference = LuaDLL.toluaL_ref(L);
                return(LuaStatic.GetTable(L, reference));
            }
        }
Beispiel #22
0
        private void _doinit(IntPtr _luaState, LuaSvrFlag _flag)
        {
            //LuaTimer.reg( _luaState );
#if UNITY_EDITOR
            if (UnityEditor.EditorApplication.isPlaying)
#endif
            LuaCoroutine.reg(_luaState, this);
            SLua.Helper.reg(_luaState);
            if (SLuaSetting.Instance.useLuaValueType)
            {
                LuaValueType.reg(_luaState);
            }

            if ((_flag & LuaSvrFlag.LSF_EXTLIB) != 0)
            {
                LuaDLL.luaS_openextlibs(_luaState);
            }
            if ((_flag & LuaSvrFlag.LSF_3RDDLL) != 0)
            {
                Lua3rdDLL.open(_luaState);
            }

            Inited = true;
        }
 /// <summary>
 /// Gets a Lua expression that is of the given type.
 /// </summary>
 /// <param name="type">The type of expression.</param>
 /// <returns>A valid Lua expression of the given type.</returns>
 static string GetValueForType(LuaValueType type)
 {
     switch (type)
     {
     case LuaValueType.Nil:
         return "nil";
     case LuaValueType.String:
         return "'foobar'";
     case LuaValueType.Bool:
         return "true";
     case LuaValueType.Table:
         return "{}";
     case LuaValueType.Function:
         return "function() end";
     case LuaValueType.Number:
         return "123";
     case LuaValueType.Thread:
         return "coroutine.create(function() end)";
     case LuaValueType.UserData:
         return "UserData()";
     default:
         throw new NotImplementedException();
     }
 }
Beispiel #24
0
 /// <summary>
 /// 初始化一个布尔值
 /// </summary>
 /// <param name="value">布尔值</param>
 public LuaValue(bool value)
 {
     _value = value;
     _type  = LuaValueType.Boolean;
 }
Beispiel #25
0
 /// <summary>
 /// 初始化一个双精度浮点数
 /// </summary>
 /// <param name="value">浮点数</param>
 public LuaValue(double value)
 {
     _value = value;
     _type  = LuaValueType.Number;
 }
Beispiel #26
0
 /// <summary>
 /// 初始化一个整型值
 /// </summary>
 /// <param name="value">整型值</param>
 public LuaValue(int value)
 {
     _value = value;
     _type  = LuaValueType.Integer;
 }
Beispiel #27
0
 /// <summary>
 /// 初始化一个字典值
 /// </summary>
 /// <param name="value">字典.</param>
 public LuaValue(Dictionary <string, LuaValue> value)
 {
     _value = value;
     _type  = LuaValueType.Map;
 }
Beispiel #28
0
 /// <summary>
 /// 初始化一个Nil值
 /// </summary>
 public LuaValue()
 {
     _value = null;
     _type  = LuaValueType.Nil;
 }
Beispiel #29
0
        /// <summary>
        /// 初始化一个对象值
        /// </summary>
        /// <param name="value">对象</param>
        public LuaValue(object value)
        {
            if (value != null)
            {
                if (value is int ||
                    value is uint ||
                    value is Int16 ||
                    value is UInt16 ||
                    value is Int64 ||
                    value is UInt64)
                {
                    _value = value;
                    _type  = LuaValueType.Integer;
                }
                else if (value is double ||
                         value is float)
                {
                    _value = value;
                    _type  = LuaValueType.Number;
                }
                else if (value is bool)
                {
                    _value = value;
                    _type  = LuaValueType.Boolean;
                }
                else if (value is byte[])
                {
                    _value = value;
                    _type  = LuaValueType.Data;
                }
                else if (value is string)
                {
                    _value = value;
                    _type  = LuaValueType.String;
                }
                else if (value is Array)
                {
                    List <LuaValue> arr = new List <LuaValue> ();
                    _value = arr;
                    _type  = LuaValueType.Array;

                    //转换数据
                    foreach (object item in (value as Array))
                    {
                        LuaValue itemValue = new LuaValue(item);
                        arr.Add(itemValue);
                    }
                }
                else if (value is IDictionary)
                {
                    Dictionary <string, LuaValue> dict = new Dictionary <string, LuaValue> ();
                    _value = dict;
                    _type  = LuaValueType.Map;

                    foreach (DictionaryEntry de in (value as IDictionary))
                    {
                        LuaValue itemValue = new LuaValue(de.Value);
                        dict.Add(Convert.ToString(de.Key), itemValue);
                    }
                }
                else if (value is LuaFunction)
                {
                    _value = value;
                    _type  = LuaValueType.Function;
                }
                else if (value is LuaPointer)
                {
                    _value = value;
                    _type  = LuaValueType.Ptr;
                }
                else if (value is LuaTuple)
                {
                    _value = value;
                    _type  = LuaValueType.Tuple;
                }
                else
                {
                    if (value is LuaObjectDescriptor)
                    {
                        _value = value;
                    }
                    else if (value is ILuaObject)
                    {
                        _value = (value as ILuaObject).getDescriptor();
                    }
                    else
                    {
                        _value = new LuaObjectDescriptor(value);
                    }
                    _type = LuaValueType.Object;
                }
            }
            else
            {
                _value = null;
                _type  = LuaValueType.Nil;
            }
        }
Beispiel #30
0
 public virtual LuaExpression CastTo(LuaValueType newType)
 {
     switch (newType)
     {
         case LuaValueType.Boolean:
             switch (GetType())
             {
                 case LuaValueType.Number:
                     return new LuaExpression.BinOp(
                         LuaBinaryOperatorType.IsNotEqualTo,
                         this,
                         new LuaExpression.NumberLiteral(0));
                 default: return this;
             }
         case LuaValueType.Number:
             switch (GetType())
             {
                 case LuaValueType.Boolean:
                     return new LuaExpression.BinOp(
                         LuaBinaryOperatorType.LogicalOr,
                         new LuaExpression.BinOp(
                             LuaBinaryOperatorType.LogicalAnd,
                             this,
                             new LuaExpression.NumberLiteral(1)),
                         new LuaExpression.NumberLiteral(0));
                 default: return this;
             }
         default: return this;
     }
 }
Beispiel #31
0
 /// <summary>
 /// 初始化一个字符串值
 /// </summary>
 /// <param name="value">字符串.</param>
 public LuaValue(string value)
 {
     _value = value;
     _type  = LuaValueType.String;
 }
Beispiel #32
0
 /// <summary>
 /// Creates a string when enumerating on an invalid value.
 /// </summary>
 /// <param name="type">The type of the value.</param>
 /// <returns>An error string.</returns>
 public static string CannotEnumerate(LuaValueType type)
 {
     return string.Format(Resources.CannotEnumerate, type.ToString().ToLower());
 }
Beispiel #33
0
 /// <summary>
 /// Creates a string when performing arithmetic on an invalid value.
 /// </summary>
 /// <param name="type">The type of the value.</param>
 /// <returns>An error string.</returns>
 public static string CannotArithmetic(LuaValueType type)
 {
     return string.Format(Resources.CannotArithmetic, type.ToString().ToLower());
 }
Beispiel #34
0
 /// <summary>
 /// Creates a string when indexing on an invalid value.
 /// </summary>
 /// <param name="type">The type of the value.</param>
 /// <returns>An error string.</returns>
 public static string CannotIndex(LuaValueType type)
 {
     return string.Format(Resources.CannotIndex, type.ToString().ToLower());
 }
Beispiel #35
0
 /// <summary>
 /// 初始化一个数组值
 /// </summary>
 /// <param name="value">数组</param>
 public LuaValue(List <LuaValue> value)
 {
     _value = value;
     _type  = LuaValueType.Array;
 }
Beispiel #36
0
        public object ToObject(int index)
        {
            LuaTypes luaType = LuaLib.lua_type(L, index);

            switch (luaType)
            {
            case LuaTypes.LUA_TNONE:
            case LuaTypes.LUA_TNIL:
            {
                return(null);
            }

            case LuaTypes.LUA_TBOOLEAN:
            {
                return(LuaLib.lua_toboolean(L, index));
            }

            case LuaTypes.LUA_TNUMBER:
            {
                return(LuaLib.lua_tonumber(L, index));
            }

            case LuaTypes.LUA_TSTRING:
            {
                return(LuaLib.lua_tostring(L, index));
            }

            case LuaTypes.LUA_TTABLE:
            {
                LuaValueType valueType = LuaValueType.None;

                LuaLib.lua_pushvalue(L, index);                                                                                 // |t
                LuaLib.lua_pushstring(L, "__valueType");                                                                        // |t|k
                LuaLib.lua_rawget(L, -2);                                                                                       // |t|vt
                if (!LuaLib.lua_isnil(L, -1))
                {
                    valueType = (LuaValueType)LuaLib.lua_tonumber(L, -1);                                       // |t|vt
                }
                LuaLib.lua_pop(L, 2);                                                                           // |

                switch (valueType)
                {
                case LuaValueType.None:
                {
                    return(ToTable(index));
                }

                case LuaValueType.Color:
                {
                    return(ToColor(index));
                }

                case LuaValueType.Quaternion:
                {
                    return(ToQuaternion(index));
                }

                case LuaValueType.Vector2:
                {
                    return(ToVector2(index));
                }

                case LuaValueType.Vector3:
                {
                    return(ToVector3(index));
                }

                case LuaValueType.Vector4:
                {
                    return(ToVector4(index));
                }

                default:
                {
                    return(null);
                }
                }
            }

            case LuaTypes.LUA_TFUNCTION:
            {
                return(ToFunction(index));
            }

            case LuaTypes.LUA_TUSERDATA:
            {
                return(ToCSObject(index));
            }

            default:
            {
                return(null);
            }
            }
        }
Beispiel #37
0
 private LuaExpression(LuaValueType vtype)
 {
     _type = vtype;
 }
Beispiel #38
0
 private LuaExpression()
 {
     _type = LuaValueType.Any;
 }
		public AutocompleteResult(LuaValue key, LuaValueType valueType)
		{
			m_key = key;
			m_valueType = valueType;
		}
Beispiel #40
0
 private LuaExpression(string keyword, LuaValueType vtype)
 {
     _keyword = keyword;
     _type = vtype;
 }