/// <summary>
        /// Gets the reflection information for a given record type and version from the cache, creating it if it is not
        /// already present.
        /// </summary>
        /// <param name="recordType">The record type.</param>
        /// <param name="version">The version to get.</param>
        /// <returns>A <see cref="RecordFieldInformation"/> relevant for the given version.</returns>
        public RecordFieldInformation GetRecordInformation(Type recordType, WarcraftVersion version)
        {
            var infoKey = new RecordInformationIdentifier(recordType, version);

            if (!_informationCache.ContainsKey(infoKey))
            {
                var recordInfo = new RecordFieldInformation(recordType, version);
                _informationCache.Add(infoKey, recordInfo);
            }

            return(_informationCache[infoKey]);
        }
Exemple #2
0
        /// <summary>
        /// Reads a property value from the given <see cref="BinaryReader"/>.
        /// </summary>
        /// <param name="reader">The reader, containing the data.</param>
        /// <param name="recordInfo">The reflected field information.</param>
        /// <param name="property">The property which will contain the data.</param>
        /// <param name="elementType">The element type of the field. This is primarily used for reading arrays.</param>
        /// <param name="version">The version of the record.</param>
        /// <returns>The value that should be assigned to the property.</returns>
        public static object ReadPropertyValue
        (
            BinaryReader reader,
            RecordFieldInformation recordInfo,
            PropertyInfo property,
            Type elementType,
            WarcraftVersion version
        )
        {
            object fieldValue;

            if (DBCInspector.IsPropertyForeignKey(property))
            {
                // Get the foreign key information
                var foreignKeyAttribute = recordInfo.PropertyForeignKeyAttributes[property];

                // Get the inner type
                var keyType  = GetUnderlyingStoredPrimitiveType(elementType);
                var keyValue = reader.Read(keyType);

                // Create the specific ForeignKey type
                var genericKeyFieldType  = typeof(ForeignKey <>);
                var specificKeyFieldType = genericKeyFieldType.MakeGenericType(keyType);

                fieldValue = Activator.CreateInstance
                             (
                    specificKeyFieldType,
                    foreignKeyAttribute.Database,
                    foreignKeyAttribute.Field,
                    keyValue
                             );
            }
            else if (elementType.IsEnum)
            {
                // Get the underlying type of the enum
                var enumType = elementType.GetEnumUnderlyingType();
                fieldValue = reader.Read(enumType);
            }
            else
            {
                if (elementType == typeof(LocalizedStringReference))
                {
                    fieldValue = reader.Read(elementType, version);
                }
                else
                {
                    fieldValue = reader.Read(elementType);
                }
            }

            return(fieldValue);
        }