Example #1
0
        public bool TryGet(USerializer serializer, Type type, out DataSerializer serializationMethods)
        {
            serializationMethods = default;

            if (type.IsConstructedGenericType == false)
            {
                return(false);
            }

            if (type.GetGenericTypeDefinition() != typeof(List <>))
            {
                return(false);
            }

            if (serializer.DataTypesDatabase.TryGet(out ArrayDataTypeLogic arrayDataTypeLogic) == false)
            {
                return(false);
            }

            var elementType = type.GetGenericArguments()[0];

            if (serializer.TryGetDataSerializer(elementType, out var elementDataSerializer, false) == false)
            {
                serializationMethods = default;
                return(false);
            }

            serializationMethods = new ListDataSerializer(type, elementType, elementDataSerializer, arrayDataTypeLogic.Value);
            return(true);
        }
Example #2
0
        public bool TryGet(USerializer serializer, Type type, out DataSerializer serializationMethods)
        {
            serializationMethods = default;

            if (type.IsArray == false)
            {
                serializationMethods = default;
                return(false);
            }

            if (type.GetArrayRank() > 1)
            {
                serializationMethods = default;
                return(false);
            }

            if (serializer.DataTypesDatabase.TryGet(out ArrayDataTypeLogic arrayDataTypeLogic) == false)
            {
                return(false);
            }

            var elementType = type.GetElementType();

            if (serializer.TryGetDataSerializer(elementType, out var elementSerializer, false) == false)
            {
                serializationMethods = default;
                return(false);
            }

            serializationMethods = new ArrayDataSerializer(elementType, elementSerializer, arrayDataTypeLogic.Value);
            return(true);
        }
Example #3
0
        public bool TryGet(USerializer serializer, Type type, out DataSerializer serializationMethods)
        {
            if (type.IsEnum)
            {
                var enumType = type.GetEnumUnderlyingType();
                return(serializer.TryGetDataSerializer(enumType, out serializationMethods, false));
            }

            serializationMethods = default;
            return(false);
        }
Example #4
0
        public static                  FieldData[] GetFields(Type type, USerializer uSerializer, bool initializeDataSerializer = true)
        {
            var bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

            using (var fieldsIterator = new TypeFieldsIterator(8))
            {
                var size   = fieldsIterator.Fill(type, bindingFlags);
                var fields = new FieldData[size];
                var index  = 0;

                for (var i = 0; i < size; i++)
                {
                    var fieldInfo = fieldsIterator[i];

                    if (uSerializer.SerializationPolicy.ShouldSerialize(fieldInfo) == false)
                    {
                        continue;
                    }

                    if (uSerializer.TryGetDataSerializer(fieldInfo.FieldType,
                                                         out var serializationMethods,
                                                         initializeDataSerializer) == false)
                    {
                        continue;
                    }

                    if (serializationMethods == null)
                    {
                        throw new Exception($"Returned null serializer for {fieldInfo.FieldType}");
                    }

                    var fieldOffset = UnsafeUtils.GetFieldOffset(fieldInfo);
                    if (fieldOffset > short.MaxValue)
                    {
                        throw new Exception("Field offset way to big!");
                    }

                    var alternateNames = uSerializer.SerializationPolicy.GetAlternateNames(fieldInfo);

                    fields[index] = new FieldData(fieldInfo.Name, alternateNames, serializationMethods, (ushort)fieldOffset);
                    index++;
                }

                if (index != fields.Length)
                {
                    Array.Resize(ref fields, index);
                }

                OrderFields(fields);

                return(fields);
            }
        }
        public override bool TryInitialize(USerializer serializer)
        {
            _serializer = serializer;
            var type = typeof(TSurrogate);

            if (_serializer.TryGetDataSerializer(type, out _dataSerializer, false))
            {
                return(true);
            }

            _serializer.Logger.Error($"Could not get serialization data for {type}");
            return(false);
        }
Example #6
0
        public bool AddArrayField <TElement>(int hash,
                                             FieldArrayWriter <T, TElement> .GetLengthDelegate getLength, FieldArrayWriter <T, TElement> .GetElementDelegate getElement,
                                             FieldArrayWriter <T, TElement> .SetLengthDelegate setLength, FieldArrayWriter <T, TElement> .SetElementDelegate setElement)
        {
            var memberType = typeof(TElement);

            if (_serializer.TryGetDataSerializer(memberType, out var elementSerializer, false) == false)
            {
                _serializer.Logger.Error($"{typeof(T)} custom serializer, cannot get serialization methods for element of type {memberType}");
                return(false);
            }

            var toAdd = new MemberSerializerStruct(hash, new FieldArrayWriter <T, TElement>(elementSerializer, getLength, setLength, getElement, setElement, _serializer));

            Add(toAdd);
            return(true);
        }