Ejemplo n.º 1
0
        private object[] PacketShell(dynamic[] objects)
        {
            for (var i = 0; i < objects.Length; i++)
            {
                if (objects[i] is OldInstance)
                {
                    objects[i] = objects[i].ISerializablePacket.Value;
                }

                if (objects[i] is Array array)
                {
                    var type = objects[i].GetType().GetElementType();
                    if (!typeof(IConvertible).IsAssignableFrom(type))
                    {
                        var packets = typeof(ISerializablePacket).MakeArray(array.Length);
                        for (var j = 0; j < array.Length; j++)
                        {
                            packets.SetValue((array.GetValue(j) as dynamic).ISerializablePacket.Value, j);
                        }
                        objects[i] = packets;
                    }
                    continue;
                }

                if (objects[i] is IDictionary dict)
                {
                    var arguments = dict.GetType().GetGenericArguments();
                    if (typeof(OldInstance).IsAssignableFrom(arguments[0]))
                    {
                        arguments[0] = typeof(IPythonPacket);
                    }

                    if (typeof(OldInstance).IsAssignableFrom(arguments[1]))
                    {
                        arguments[1] = typeof(IPythonPacket);
                    }

                    var newDict = DictionaryExtension.MakeDictionary(arguments[0], arguments[1]);
                    foreach (var item in dict.Keys)
                    {
                        var    value = dict[item];
                        object key   = item;
                        if (arguments[0] == typeof(IPythonPacket))
                        {
                            dict.Remove(item);
                            key = (item as dynamic).ISerializablePacket.Value;
                        }

                        if (arguments[1] == typeof(IPythonPacket))
                        {
                            value = (value as dynamic).ISerializablePacket.Value;
                        }
                        newDict.Add(key, value);
                    }
                    objects[i] = newDict;
                    continue;
                }
            }
            return(objects);
        }
Ejemplo n.º 2
0
        public static void RegisterTableToDictionary <K, V>()
        {
            if (typeof(K) != typeof(IConvertible) && typeof(K) != typeof(ISerializablePacket))
            {
                throw new NotSupportedException($"{typeof(K).Name}类型必须是({nameof(IConvertible)}或{nameof(ISerializablePacket)})");
            }

            if (typeof(V) != typeof(IConvertible) && typeof(V) != typeof(ISerializablePacket))
            {
                throw new NotSupportedException($"{typeof(V).Name}类型必须是({nameof(IConvertible)}或{nameof(ISerializablePacket)})");
            }

            Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
                DataType.Table, typeof(Dictionary <K, V>),
                v => {
                var table     = v.Table.Get("Value").Table;
                var keyType   = v.Table.Get("KeyType").ToObject <Type>();
                var valueType = v.Table.Get("ValueType").ToObject <Type>();

                var values = DictionaryExtension.MakeDictionary(keyType, valueType);

                foreach (var item in table.Pairs)
                {
                    object key = null;
                    if (typeof(IConvertible).IsAssignableFrom(keyType))
                    {
                        key = item.Key.ToObject(keyType);
                    }

                    if (typeof(ISerializablePacket).IsAssignableFrom(keyType))
                    {
                        key = item.Key.Table.Get(nameof(ISerializablePacket)).ToObject <ClassWrapper <ILuaPacket> >().Value as ISerializablePacket;
                    }

                    object value = null;
                    if (typeof(IConvertible).IsAssignableFrom(valueType))
                    {
                        value = item.Value.ToObject(valueType);
                    }

                    if (typeof(ISerializablePacket).IsAssignableFrom(valueType))
                    {
                        value = item.Value.Table.Get(nameof(ISerializablePacket)).ToObject <ClassWrapper <ILuaPacket> >().Value as ISerializablePacket;
                    }

                    if (key == null || value == null)
                    {
                        return(null);
                    }

                    values.Add(key, value);
                }

                return(values);
            }
                );
        }
Ejemplo n.º 3
0
        public IDictionary ReadDictionaryPP()
        {
            if (_readStream.ShiftRight <bool>())
            {
                var length             = _readStream.ShiftRight <ushort>();
                var keyInterfaceName   = _readStream.ShiftRight <string>();
                var valueInterfaceName = _readStream.ShiftRight <string>();

                var dicts = DictionaryExtension.MakeDictionary(
                    PacketManager.GetPacketType(keyInterfaceName),
                    PacketManager.GetPacketType(valueInterfaceName));

                for (var i = 0; i < length; i++)
                {
                    dicts.Add(ReadPacket(), ReadPacket());
                }
                return(dicts);
            }

            return(null);
        }
Ejemplo n.º 4
0
        private static object ParseDictionary(IDictionary dicts)
        {
            if (dicts == null)
            {
                return(null);
            }

            var  arguments = dicts.GetType().GetGenericArguments();
            Type keyType   = arguments[0];

            if (typeof(IPythonPacket).IsAssignableFrom(arguments[0]))
            {
                keyType = typeof(OldInstance);
            }
            Type valueType = arguments[1];

            if (typeof(IPythonPacket).IsAssignableFrom(arguments[1]))
            {
                valueType = typeof(OldInstance);
            }
            var dict = DictionaryExtension.MakeDictionary(keyType, valueType);

            foreach (DictionaryEntry item in dicts)
            {
                var key = item.Key;
                if (item.Key is IPythonPacket packetKey)
                {
                    key = packetKey.Instance;
                }

                var value = item.Value;
                if (item.Value is IPythonPacket packetValue)
                {
                    value = packetValue.Instance;
                }
                dict.Add(key, value);
            }

            return(dict);
        }
Ejemplo n.º 5
0
        public IDictionary ReadDictionaryBB()
        {
            if (_readStream.ShiftRight <bool>())
            {
                var length = _readStream.ShiftRight <ushort>();

                var keyTypeCode   = (TypeCode)_readStream.ShiftRight <byte>();
                var valueTypeCode = (TypeCode)_readStream.ShiftRight <byte>();

                var dicts = DictionaryExtension.MakeDictionary(
                    keyTypeCode.GetBaseType(),
                    valueTypeCode.GetBaseType());

                for (var i = 0; i < length; i++)
                {
                    dicts.Add(Read(), Read());
                }

                return(dicts);
            }

            return(null);
        }