Esempio n. 1
0
        /// <summary>
        ///     Checks the table Type for a <see cref="PrebuiltConfigurationsEmbeddedResourceAttribute"/> or a
        ///     <see cref="PrebuiltConfigurationsFilePathAttribute"/>, and deserializes the table configurations
        ///     if found.
        /// </summary>
        /// <param name="tableType">
        ///     Table type.
        /// </param>
        /// <param name="tableId">
        ///     Table id.
        /// </param>
        /// <param name="serializer">
        ///     Used to deserialize table data.
        /// </param>
        /// <param name="logger">
        ///     Used to log relevant messages.
        /// </param>
        /// <returns>
        ///     TableConfigurations or null.
        /// </returns>
        public static TableConfigurations GetPrebuiltTableConfigurations(
            Type tableType,
            Guid tableId,
            ISerializer serializer,
            ILogger logger)
        {
            Guard.NotNull(serializer, nameof(serializer));

            TableConfigurations tableConfigurations =
                GetPrebuiltConfigurationFromExternalFile(tableType, tableId, serializer, logger);

            if (tableConfigurations != null)
            {
                // todo:should we attempt to combine any embedded prebuilt configuration if both external and embedded attributes are set?
                return(tableConfigurations);
            }

            tableConfigurations = GetPrebuiltConfigurationFromEmbeddedResource(tableType, tableId, serializer, logger);

            if (tableConfigurations != null)
            {
                return(tableConfigurations);
            }

            tableConfigurations = new TableConfigurations(tableId);
            tableConfigurations.Configurations = Enumerable.Empty <TableConfiguration>();

            return(tableConfigurations);
        }
        private static TableDescriptor GetTableDescriptor(
            Type type,
            string propertyName,
            ISerializer tableConfigSerializer,
            ILogger logger)
        {
            Guard.NotNull(type, nameof(type));

            TableDescriptor tableDescriptor = null;
            IEnumerable <RequiresCookerAttribute> cookerAttributes = null;

            var tableDescriptorPropertyInfo = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Static);

            if (tableDescriptorPropertyInfo != null)
            {
                var getMethodInfo = tableDescriptorPropertyInfo.GetMethod;
                if (!getMethodInfo.IsPublic)
                {
                    logger?.Warn(
                        $"Type {type} appears to be a Table, but the " +
                        $"{nameof(TableAttribute.TableDescriptorPropertyName)}.get method is not found as public.");
                    return(null);
                }

                if (getMethodInfo.ReturnType != typeof(TableDescriptor))
                {
                    logger?.Warn(
                        $"Type {type} appears to be a Table, but the " +
                        $"{nameof(TableAttribute.TableDescriptorPropertyName)}.get method must return type " +
                        $"{nameof(TableDescriptor)}.");
                    return(null);
                }

                // Allow [RequiresCooker] attribute on the property that returns the TableDescriptor
                cookerAttributes = tableDescriptorPropertyInfo.GetCustomAttributes <RequiresCookerAttribute>();
                tableDescriptor  = (TableDescriptor)getMethodInfo.Invoke(null, null);
            }
            else
            {
                // The table descriptor property didn't exist, check for a field instead.

                var tableDescriptorFieldInfo = type.GetField(propertyName, BindingFlags.Public | BindingFlags.Static);
                if (tableDescriptorFieldInfo == null)
                {
                    logger?.Warn(
                        $"Type {type} appears to be a class, but the " +
                        $"{nameof(TableAttribute.TableDescriptorPropertyName)} property is not found as public and " +
                        "static.");
                    return(null);
                }

                // Allow [RequiresCooker] attribute on the property that returns the TableDescriptor
                cookerAttributes = tableDescriptorFieldInfo.GetCustomAttributes <RequiresCookerAttribute>();
                tableDescriptor  = tableDescriptorFieldInfo.GetValue(null) as TableDescriptor;
            }

            if (tableDescriptor == null)
            {
                return(null);
            }

            tableDescriptor.Type = type;
            tableDescriptor.PrebuiltTableConfigurations = TableConfigurations.GetPrebuiltTableConfigurations(
                type,
                tableDescriptor.Guid,
                tableConfigSerializer,
                logger);

            foreach (var cooker in cookerAttributes)
            {
                tableDescriptor.AddRequiredDataCooker(cooker.RequiredDataCookerPath);
            }

            return(tableDescriptor);
        }