Exemple #1
0
        protected static object getNativeValueForLuaValue(Type t, LuaValue luaValue)
        {
            object value = null;

            if (t == typeof(Int32) ||
                t == typeof(Int64) ||
                t == typeof(Int16) ||
                t == typeof(UInt16) ||
                t == typeof(UInt32) ||
                t == typeof(UInt64))
            {
                value = luaValue.toInteger();
            }
            else if (t == typeof(double) ||
                     t == typeof(float))
            {
                value = luaValue.toNumber();
            }
            else if (t == typeof(bool))
            {
                value = luaValue.toBoolean();
            }
            else if (t == typeof(string))
            {
                value = luaValue.toString();
            }
            else if (t.IsArray)
            {
                //数组
                if (t == typeof(byte[]))
                {
                    //二进制数组
                    value = luaValue.toData();
                }
                else if (t == typeof(Int32[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <Int32> intArr = new List <Int32> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(item.toInteger());
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(Int64[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <Int64> intArr = new List <Int64> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(item.toInteger());
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(Int16[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <Int16> intArr = new List <Int16> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(Convert.ToInt16(item.toInteger()));
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(UInt32[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <UInt32> intArr = new List <UInt32> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(Convert.ToUInt32(item.toInteger()));
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(UInt64[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <UInt64> intArr = new List <UInt64> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(Convert.ToUInt64(item.toInteger()));
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(UInt16[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <UInt16> intArr = new List <UInt16> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(Convert.ToUInt16(item.toInteger()));
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(bool[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <bool> boolArr = new List <bool> ();
                        foreach (LuaValue item in arr)
                        {
                            boolArr.Add(Convert.ToBoolean(item.toInteger()));
                        }
                        value = boolArr.ToArray();
                    }
                }
                else if (t == typeof(double[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <double> doubleArr = new List <double> ();
                        foreach (LuaValue item in arr)
                        {
                            doubleArr.Add(item.toNumber());
                        }
                        value = doubleArr.ToArray();
                    }
                }
                else if (t == typeof(float[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <float> floatArr = new List <float> ();
                        foreach (LuaValue item in arr)
                        {
                            floatArr.Add((float)item.toNumber());
                        }
                        value = floatArr.ToArray();
                    }
                }
                else if (t == typeof(string[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <string> floatArr = new List <string> ();
                        foreach (LuaValue item in arr)
                        {
                            floatArr.Add(item.toString());
                        }
                        value = floatArr.ToArray();
                    }
                }
                else
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        ArrayList objArr = new ArrayList();
                        foreach (LuaValue item in arr)
                        {
                            objArr.Add(item.toObject());
                        }
                        value = objArr.ToArray();
                    }
                }
            }
            else if (t == typeof(Hashtable))
            {
                //字典
                Dictionary <string, LuaValue> map = luaValue.toMap();
                if (map != null)
                {
                    if (t == typeof(Dictionary <string, int>))
                    {
                        Dictionary <string, int> dict = new Dictionary <string, int> ();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, kv.Value.toInteger());
                        }
                        value = dict;
                    }
                    else if (t == typeof(Dictionary <string, int>))
                    {
                        Dictionary <string, int> dict = new Dictionary <string, int> ();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, kv.Value.toInteger());
                        }
                        value = dict;
                    }
                    else if (t == typeof(Dictionary <string, uint>))
                    {
                        Dictionary <string, uint> dict = new Dictionary <string, uint> ();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, Convert.ToUInt32(kv.Value.toInteger()));
                        }
                        value = dict;
                    }
                    else if (t == typeof(Dictionary <string, Int16>))
                    {
                        Dictionary <string, Int16> dict = new Dictionary <string, Int16> ();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, Convert.ToInt16(kv.Value.toInteger()));
                        }
                        value = dict;
                    }
                    else if (t == typeof(Dictionary <string, UInt16>))
                    {
                        Dictionary <string, UInt16> dict = new Dictionary <string, UInt16> ();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, Convert.ToUInt16(kv.Value.toInteger()));
                        }
                        value = dict;
                    }
                    else if (t == typeof(Dictionary <string, Int64>))
                    {
                        Dictionary <string, Int64> dict = new Dictionary <string, Int64> ();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, Convert.ToInt64(kv.Value.toInteger()));
                        }
                        value = dict;
                    }
                    else if (t == typeof(Dictionary <string, UInt64>))
                    {
                        Dictionary <string, UInt64> dict = new Dictionary <string, UInt64> ();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, Convert.ToUInt64(kv.Value.toInteger()));
                        }
                        value = dict;
                    }
                    else if (t == typeof(Dictionary <string, bool>))
                    {
                        Dictionary <string, bool> dict = new Dictionary <string, bool> ();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, kv.Value.toBoolean());
                        }
                        value = dict;
                    }
                    else if (t == typeof(Dictionary <string, double>))
                    {
                        Dictionary <string, double> dict = new Dictionary <string, double> ();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, kv.Value.toNumber());
                        }
                        value = dict;
                    }
                    else if (t == typeof(Dictionary <string, float>))
                    {
                        Dictionary <string, float> dict = new Dictionary <string, float> ();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, (float)kv.Value.toNumber());
                        }
                        value = dict;
                    }
                    else if (t == typeof(Dictionary <string, string>))
                    {
                        Dictionary <string, string> dict = new Dictionary <string, string> ();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, kv.Value.toString());
                        }
                        value = dict;
                    }
                    else
                    {
                        Hashtable dict = new Hashtable();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, kv.Value.toObject());
                        }
                        value = dict;
                    }
                }
            }
            else
            {
                value = luaValue.toObject();
            }

            return(value);
        }
        /// <summary>
        /// 获取类型匹配的构造方法
        /// </summary>
        /// <returns>构造方法.</returns>
        /// <param name="t">类型.</param>
        /// <param name="arguments">参数列表.</param>
        protected static ConstructorInfo getConstructor(Type targetType, Type t, List <LuaValue> arguments)
        {
            int             targetMatchDegree  = 0;
            ConstructorInfo targetConstructor  = null;
            ConstructorInfo defaultConstructor = null;

            if (t.GetInterface("cn.vimfung.luascriptcore.LuaExportType") != null)
            {
                ConstructorInfo[] constructors = t.GetConstructors();
                foreach (ConstructorInfo constructor in constructors)
                {
                    //检测是否为排除方法
                    LuaExclude isExclude = Attribute.GetCustomAttribute(constructor, typeof(LuaExclude), true) as LuaExclude;
                    if (isExclude != null)
                    {
                        continue;
                    }

                    int matchDegree = 0;
                    int index       = 0;

                    ParameterInfo[] paramInfos = constructor.GetParameters();
                    if (paramInfos.Length == 0)
                    {
                        //默认构造函数
                        defaultConstructor = constructor;
                    }

                    if (paramInfos.Length != arguments.Count)
                    {
                        continue;
                    }

                    bool hasMatch = true;
                    foreach (ParameterInfo paramInfo in paramInfos)
                    {
                        Type     paramType = paramInfo.ParameterType;
                        LuaValue arg       = arguments [index];

                        if (typeof(int) == paramType ||
                            typeof(long) == paramType ||
                            typeof(short) == paramType ||
                            typeof(uint) == paramType ||
                            typeof(ushort) == paramType ||
                            typeof(ulong) == paramType)
                        {
                            if (arg.type == LuaValueType.Integer || arg.type == LuaValueType.Number)
                            {
                                matchDegree++;
                            }
                        }
                        else if (typeof(float) == paramType ||
                                 typeof(double) == paramType)
                        {
                            if (arg.type == LuaValueType.Number)
                            {
                                matchDegree++;
                            }
                        }
                        else if (typeof(bool) == paramType)
                        {
                            if (arg.type == LuaValueType.Boolean)
                            {
                                matchDegree++;
                            }
                        }
                        else if (typeof(string) == paramType)
                        {
                            if (arg.type == LuaValueType.String)
                            {
                                matchDegree++;
                            }
                        }
                        else if (typeof(byte[]) == paramType)
                        {
                            if (arg.type == LuaValueType.Data ||
                                arg.type == LuaValueType.String)
                            {
                                matchDegree++;
                            }
                            else
                            {
                                hasMatch = false;
                            }
                        }
                        else if (paramType.IsArray || typeof(IList).IsAssignableFrom(paramType))
                        {
                            if (arg.type == LuaValueType.Array)
                            {
                                matchDegree++;
                            }
                            else
                            {
                                hasMatch = false;
                            }
                        }
                        else if (typeof(IDictionary).IsAssignableFrom(t))
                        {
                            if (arg.type == LuaValueType.Map)
                            {
                                matchDegree++;
                            }
                            else
                            {
                                hasMatch = false;
                            }
                        }
                        else
                        {
                            object obj = arg.toObject();
                            if (obj.GetType().IsAssignableFrom(paramType))
                            {
                                matchDegree++;
                            }
                            else
                            {
                                hasMatch = false;
                            }
                        }

                        if (!hasMatch)
                        {
                            break;
                        }

                        index++;
                    }

                    if (hasMatch && matchDegree > targetMatchDegree)
                    {
                        targetConstructor = constructor;
                        targetMatchDegree = matchDegree;
                    }
                }

                if (targetConstructor == null)
                {
                    //检测父类构造方法
                    targetConstructor = getConstructor(targetType, t.BaseType, arguments);
                }

                if (targetConstructor == null && targetType == t)
                {
                    targetConstructor = defaultConstructor;
                }
            }

            return(targetConstructor);
        }
        protected static object getNativeValueForLuaValue(Type t, LuaValue luaValue)
        {
            object value = null;

            if (t == typeof(Int32) ||
                t == typeof(Int64) ||
                t == typeof(Int16) ||
                t == typeof(UInt16) ||
                t == typeof(UInt32) ||
                t == typeof(UInt64))
            {
                value = luaValue.toInteger();
            }
            else if (t == typeof(double) ||
                     t == typeof(float))
            {
                value = luaValue.toNumber();
            }
            else if (t == typeof(bool))
            {
                value = luaValue.toBoolean();
            }
            else if (t == typeof(string))
            {
                value = luaValue.toString();
            }
            else if (t.IsArray || typeof(IList).IsAssignableFrom(t))
            {
                //数组
                if (t == typeof(byte[]))
                {
                    //二进制数组
                    value = luaValue.toData();
                }
                else if (t == typeof(Int32[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <Int32> intArr = new List <Int32> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(item.toInteger());
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(Int64[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <Int64> intArr = new List <Int64> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(item.toInteger());
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(Int16[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <Int16> intArr = new List <Int16> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(Convert.ToInt16(item.toInteger()));
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(UInt32[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <UInt32> intArr = new List <UInt32> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(Convert.ToUInt32(item.toInteger()));
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(UInt64[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <UInt64> intArr = new List <UInt64> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(Convert.ToUInt64(item.toInteger()));
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(UInt16[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <UInt16> intArr = new List <UInt16> ();
                        foreach (LuaValue item in arr)
                        {
                            intArr.Add(Convert.ToUInt16(item.toInteger()));
                        }
                        value = intArr.ToArray();
                    }
                }
                else if (t == typeof(bool[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <bool> boolArr = new List <bool> ();
                        foreach (LuaValue item in arr)
                        {
                            boolArr.Add(Convert.ToBoolean(item.toInteger()));
                        }
                        value = boolArr.ToArray();
                    }
                }
                else if (t == typeof(double[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <double> doubleArr = new List <double> ();
                        foreach (LuaValue item in arr)
                        {
                            doubleArr.Add(item.toNumber());
                        }
                        value = doubleArr.ToArray();
                    }
                }
                else if (t == typeof(float[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <float> floatArr = new List <float> ();
                        foreach (LuaValue item in arr)
                        {
                            floatArr.Add((float)item.toNumber());
                        }
                        value = floatArr.ToArray();
                    }
                }
                else if (t == typeof(string[]))
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        List <string> floatArr = new List <string> ();
                        foreach (LuaValue item in arr)
                        {
                            floatArr.Add(item.toString());
                        }
                        value = floatArr.ToArray();
                    }
                }
                else
                {
                    List <LuaValue> arr = luaValue.toArray();
                    if (arr != null)
                    {
                        ArrayList objArr = new ArrayList();
                        foreach (LuaValue item in arr)
                        {
                            objArr.Add(item.toObject());
                        }
                        value = objArr.ToArray();
                    }
                }
            }
            else if (typeof(IDictionary).IsAssignableFrom(t))
            {
                //字典
                Dictionary <string, LuaValue> map = luaValue.toMap();
                if (map != null)
                {
                    if (t.IsGenericType)
                    {
                        //为泛型
                        Type[] types     = t.GetGenericArguments();
                        Type   valueType = types [1];

                        IDictionary dict = Activator.CreateInstance(t) as IDictionary;
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, getNativeValueForLuaValue(valueType, kv.Value));
                        }

                        value = dict;
                    }
                    else if (typeof(Hashtable).IsAssignableFrom(t))
                    {
                        Hashtable dict = new Hashtable();
                        foreach (KeyValuePair <string, LuaValue> kv in map)
                        {
                            dict.Add(kv.Key, kv.Value.toObject());
                        }
                        value = dict;
                    }
                }
            }
            else
            {
                value = luaValue.toObject();
            }

            return(value);
        }
Exemple #4
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;
            }
        }