Ejemplo n.º 1
0
        private bool CanBeIgnored(GenericUnknownStruct.BaseClassEntry cls, PropertyInfo propInfo, object propValue)
        {
            if (MappingHelper.IgnoredCache.Contains(propInfo))
            {
                return(true);
            }

            if (_defaultValueOverride.Contains(new DefaultValueOverrideEntry(cls, propInfo)))
            {
                return(false);
            }

            return(MappingHelper.GetPropertyHelper(propInfo).IsDefault(propValue));
        }
Ejemplo n.º 2
0
        private void SetProperty(GenericUnknownStruct.BaseClassEntry cls, string propertyName, object value)
        {
            foreach (var prop in cls.GetType().GetProperties())
            {
                var attrName = GetRealName(prop);
                if (attrName != null && attrName == propertyName)
                {
                    MappingHelper.GetPropertyHelper(prop).Set(cls, value);
                    return;
                }
            }

            throw new PropertyNotFoundException(cls.GetType().Name, propertyName);
        }
Ejemplo n.º 3
0
        private void ReadMappedFields(BinaryReader reader, GenericUnknownStruct.BaseClassEntry cls)
        {
            var startPos = reader.BaseStream.Position;

            var fieldInfos = ReadFieldInfos(reader);

            for (int i = 0; i < fieldInfos.Length; i++)
            {
                reader.BaseStream.Position = startPos + fieldInfos[i].Offset;

                var internalType = GetInternalType(fieldInfos[i].Type);
                var ret          = ReadMappedFieldValue(reader, internalType);

                SetProperty(cls, fieldInfos[i].Name, ret);
            }
        }
Ejemplo n.º 4
0
        private string[] GenerateStringList(GenericUnknownStruct.BaseClassEntry cls)
        {
            var result = new HashSet <string>();

            if (_doMapping)
            {
                result.Add(GetRealName(cls.GetType()));
                GenerateStringListFromMappedFields(cls, ref result);
            }
            else
            {
                result.Add(((GenericUnknownStruct.ClassEntry)cls).Name);
                GenerateStringListFromUnmappedFields(((GenericUnknownStruct.ClassEntry)cls).Fields, ref result);
            }

            return(result.ToArray());
        }
Ejemplo n.º 5
0
        private void GenerateStringListFromMappedFields(GenericUnknownStruct.BaseClassEntry cls, ref HashSet <string> strings)
        {
            var props = new List <KeyValuePair <PropertyInfo, object> >();

            foreach (var prop in cls.GetType().GetProperties())
            {
                var propValue = MappingHelper.GetPropertyHelper(prop).Get(cls);
                if (CanBeIgnored(cls, prop, propValue))
                {
                    continue;
                }

                props.Add(new KeyValuePair <PropertyInfo, object>(prop, propValue));
            }

            props = props
                    .GroupBy(p => p.Key.DeclaringType)
                    .Reverse()
                    .SelectMany(g => g)
                    .ToList();

            for (int i = 0; i < props.Count; i++)
            {
                var prop      = props[i].Key;
                var propValue = props[i].Value;
                var(typeString, baseType) = GetTypeStringFromProperty(prop, propValue);

                strings.Add(GetRealName(prop));
                strings.Add(typeString);

                if (prop.PropertyType.IsArray)
                {
                    var eleType = prop.PropertyType.GetElementType();
                    foreach (var val in (IList)propValue)
                    {
                        GetStringValueFromPropValue(eleType, val, baseType, ref strings);
                    }
                }
                else
                {
                    GetStringValueFromPropValue(prop.PropertyType, propValue, baseType, ref strings);
                }
            }
        }
Ejemplo n.º 6
0
        private void SetProperty(GenericUnknownStruct.BaseClassEntry cls, string propertyName, object value)
        {
            foreach (var prop in cls.GetType().GetProperties())
            {
                var attrName = GetRealName(prop);
                if (attrName != null && attrName == propertyName)
                {
                    if (MappingHelper.GetPropertyHelper(prop).IsDefault(value))
                    {
                        var ignore = OnWrongDefaultValue(new WrongDefaultValueEventArgs(cls.GetType().Name, propertyName, value));
                        if (!ignore)
                        {
                            throw new WrongDefaultValueException(cls.GetType().Name, propertyName, value);
                        }
                        _defaultValueOverride.Add(new DefaultValueOverrideEntry(cls, prop));
                    }

                    MappingHelper.GetPropertyHelper(prop).Set(cls, value);
                    return;
                }
            }

            throw new PropertyNotFoundException(cls.GetType().Name, propertyName);
        }
Ejemplo n.º 7
0
 private string GetTypeStringFromProperty(PropertyInfo propInfo, GenericUnknownStruct.BaseClassEntry cls)
 {
     var(typeStr, _) = GetTypeStringFromProperty(propInfo, propInfo.GetValue(cls));
     return(typeStr);
 }
Ejemplo n.º 8
0
        private byte[] GenerateDataFromMappedFields(GenericUnknownStruct.BaseClassEntry cls)
        {
            byte[] result;

            using (var stream = new MemoryStream())
            {
                using (var writer = new BinaryWriter(stream, Encoding.ASCII))
                {
                    var props = new List <KeyValuePair <PropertyInfo, object> >();

                    foreach (var prop in cls.GetType().GetProperties())
                    {
                        var propValue = MappingHelper.GetPropertyHelper(prop).Get(cls);
                        if (CanBeIgnored(prop, propValue))
                        {
                            continue;
                        }

                        props.Add(new KeyValuePair <PropertyInfo, object>(prop, propValue));
                    }

                    props = props
                            .GroupBy(p => p.Key.DeclaringType)
                            .Reverse()
                            .SelectMany(g => g)
                            .ToList();

                    writer.Write((ushort)props.Count);
                    foreach (var prop in props)
                    {
                        writer.Write((ushort)_stringList.IndexOf(GetRealName(prop.Key)));
                        var typeString = GetTypeStringFromProperty(prop.Key, cls);
                        writer.Write((ushort)_stringList.IndexOf(typeString));
                        writer.Write(new byte[4]); // offset
                    }

                    for (int i = 0; i < props.Count; i++)
                    {
                        var pos = writer.BaseStream.Position;
                        writer.BaseStream.Position = 6 + (i * 8);
                        writer.Write((uint)pos);
                        writer.BaseStream.Position = pos;

                        if (props[i].Key.PropertyType.IsArray)
                        {
                            var arr = (IList)props[i].Value;

                            writer.Write(arr.Count);
                            foreach (var val in arr)
                            {
                                WriteValueFromPropValue(writer, val);
                            }
                        }
                        else
                        {
                            WriteValueFromPropValue(writer, props[i].Value);
                        }
                    }
                }

                result = stream.ToArray();
            }

            return(result);
        }
Ejemplo n.º 9
0
 public DefaultValueOverrideEntry(GenericUnknownStruct.BaseClassEntry cls, PropertyInfo propertyInfo)
 {
     Class        = cls;
     PropertyInfo = propertyInfo;
 }