// Attempts to create an ImporterType out of a passed type, will return null if it is invalid public static ImporterType TryCreate(BuildEngine engine, Type type) { // Check if it is derived from IContentImporter if (type.GetInterface(INTERFACE_NAME) == null) { return(null); } // Must have the correct attribute ContentImporterAttribute attrib = (ContentImporterAttribute)type.GetCustomAttributes(ATTRIBUTE_TYPE, false)?.FirstOrDefault(); if (attrib == null) { engine.Logger.EngineError($"The type '{type.FullName}' is a ContentImporter but is missing the required attribute."); return(null); } if (!attrib.Enabled) { engine.Logger.EngineInfo($"Skipping ContentImporter type '{type.FullName}' - it is marked as disabled."); return(null); } // Validate the attribute information if (attrib.DefaultProcessor?.GetInterface(ProcessorType.INTERFACE_NAME) == null) { TypeError(engine, type, $"has an invalid default ContentProcessor type '{attrib.DefaultProcessor?.FullName}'"); return(null); } if (attrib.DisplayName == null) { TypeError(engine, type, "cannot have a null display name."); return(null); } // Ensure some required type info if (type.IsAbstract) { TypeError(engine, type, "is abstract and cannot be instantiated."); return(null); } if (type.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, CallingConventions.HasThis, new Type[0], null) == null) { TypeError(engine, type, "does not have a public no-args constructor, and cannot be instantiated."); return(null); } // Good to go return(new ImporterType(type, attrib)); }
private static void ProcessTypes(IEnumerable <Type> types) { foreach (var t in types) { if (t.IsAbstract) { continue; } if (t.GetInterface(@"IContentImporter") == typeof(IContentImporter)) { var attributes = t.GetCustomAttributes(typeof(ContentImporterAttribute), false); if (attributes.Length != 0) { var importerAttribute = attributes[0] as ContentImporterAttribute; _importers.Add(new ImporterInfo { Attribute = importerAttribute, Type = t }); } else { // If no attribute specify default one var importerAttribute = new ContentImporterAttribute(".*"); importerAttribute.DefaultProcessor = ""; importerAttribute.DisplayName = t.Name; _importers.Add(new ImporterInfo { Attribute = importerAttribute, Type = t }); } } else if (t.GetInterface(@"IContentProcessor") == typeof(IContentProcessor)) { var attributes = t.GetCustomAttributes(typeof(ContentProcessorAttribute), false); if (attributes.Length != 0) { var processorAttribute = attributes[0] as ContentProcessorAttribute; _processors.Add(new ProcessorInfo { Attribute = processorAttribute, Type = t }); } } } }
private void findPipelineEntries(ProjectReference reference) { // Search through the assembly and find content importers and processors. Assembly assembly = Assembly.LoadFrom(reference.StoredReference); foreach (Type type in assembly.GetTypes()) { foreach (object attribute in type.GetCustomAttributes(true)) { if (attribute is ContentImporterAttribute) { ContentImporterAttribute ia = attribute as ContentImporterAttribute; if (ia.DisplayName != null && ia.DisplayName != string.Empty) { importers.Add(new ContentImporterInfo(type.Name, ia.DisplayName, ia.FileExtensions, ia.DefaultProcessor, ia.CacheImportedData)); } else { importers.Add(new ContentImporterInfo(type.Name, type.Name, ia.FileExtensions, ia.DefaultProcessor, ia.CacheImportedData)); } } else if (attribute is ContentProcessorAttribute) { ContentProcessorAttribute pa = attribute as ContentProcessorAttribute; if (pa.DisplayName != null && pa.DisplayName != string.Empty) { processors.Add(new ContentProcessorInfo(type.Name, pa.DisplayName)); } else { processors.Add(new ContentProcessorInfo(type.Name, type.Name)); } } } } }
private void ResolveAssemblies() { _importers = new List <ImporterInfo>(); _processors = new List <ProcessorInfo>(); _writers = new List <Type>(); // Finally load the pipeline assemblies. foreach (var assemblyPath in Assemblies) { Type[] exportedTypes; try { Assembly a; if (string.IsNullOrEmpty(assemblyPath)) { // Get the types from this assembly, which includes all of the // built-in importers, processors and type writers a = Assembly.GetExecutingAssembly(); // The built-in types may not be public, so get all types exportedTypes = a.GetTypes(); } else { a = Assembly.LoadFrom(assemblyPath); // We only look at public types for external importers, processors // and type writers. exportedTypes = a.GetExportedTypes(); } } catch (Exception) { // The assembly failed to load... nothing // we can do but ignore it. continue; } foreach (var t in exportedTypes) { if (!t.IsPublic || t.IsAbstract) { continue; } if (t.GetInterface(@"IContentImporter") != null) { var attributes = t.GetCustomAttributes(typeof(ContentImporterAttribute), false); if (attributes.Length != 0) { var importerAttribute = attributes[0] as ContentImporterAttribute; _importers.Add(new ImporterInfo { attribue = importerAttribute, type = t }); } else { // If no attribute specify default one var importerAttribute = new ContentImporterAttribute(".*"); importerAttribute.DefaultProcessor = ""; importerAttribute.DisplayName = t.Name; _importers.Add(new ImporterInfo { attribue = importerAttribute, type = t }); } } else if (t.GetInterface(@"IContentProcessor") != null) { var attributes = t.GetCustomAttributes(typeof(ContentProcessorAttribute), false); if (attributes.Length != 0) { var processorAttribute = attributes[0] as ContentProcessorAttribute; _processors.Add(new ProcessorInfo { attribue = processorAttribute, type = t }); } } else if (t.GetInterface(@"ContentTypeWriter") != null) { // TODO: This doesn't work... how do i find these? _writers.Add(t); } } } }
private static void ProcessAssemblies() { _importers = new List <ImporterInfo>(); _processors = new List <ProcessorInfo>(); _writers = new List <Type>(); foreach (string assemblyPath in _assemblies) { Type[] exportedTypes; try { Assembly a = Assembly.LoadFrom(assemblyPath); // We only look at public types for external importers, processors // and type writers. exportedTypes = a.GetExportedTypes(); } catch (Exception) { throw; } Type contentTypeWriterType = typeof(ContentTypeWriter <>); foreach (Type t in exportedTypes) { if (!t.IsPublic || t.IsAbstract) { continue; } if (t.GetInterface("IContentImporter") != null) { var attributes = t.GetCustomAttributes(typeof(ContentImporterAttribute), false); if (attributes.Length != 0) { var importerAttribute = attributes[0] as ContentImporterAttribute; _importers.Add(new ImporterInfo { attribue = importerAttribute, type = t }); } else { // If no attribute specify default one var importerAttribute = new ContentImporterAttribute(".*"); importerAttribute.DefaultProcessor = ""; importerAttribute.DisplayName = t.Name; _importers.Add(new ImporterInfo { attribue = importerAttribute, type = t }); } } else if (t.GetInterface("IContentProcessor") != null) { var attributes = t.GetCustomAttributes(typeof(ContentProcessorAttribute), false); if (attributes.Length != 0) { var processorAttribute = attributes[0] as ContentProcessorAttribute; _processors.Add(new ProcessorInfo { attribue = processorAttribute, type = t }); } } else if (t.BaseType != null && t.BaseType.IsGenericType && t.BaseType.GetGenericTypeDefinition() == contentTypeWriterType) { Type baseType = t.BaseType; while ((baseType != null) && baseType.IsGenericType && (baseType.GetGenericTypeDefinition() != contentTypeWriterType)) { baseType = baseType.BaseType; } if (baseType != null) { typeWriterMap.Add(baseType, t); } } } } }
public ImporterInfo(ContentImporterAttribute attribute, Type type) { Attribute = attribute; Type = type; }
private void ResolveAssemblies() { _importers = new List <ImporterInfo>(); _processors = new List <ProcessorInfo>(); _writers = new List <Type>(); // Finally load the pipeline assemblies. foreach (var assemblyPath in Assemblies) { Type[] exportedTypes; DateTime assemblyTimestamp; try { Assembly a; if (string.IsNullOrEmpty(assemblyPath)) { a = Assembly.GetExecutingAssembly(); } else { a = Assembly.LoadFrom(assemblyPath); } exportedTypes = a.GetTypes(); assemblyTimestamp = File.GetLastWriteTime(a.Location); } catch (BadImageFormatException e) { Logger.LogWarning(null, null, "Assembly is either corrupt or built using a different " + "target platform than this process. Reference another target architecture (x86, x64, " + "AnyCPU, etc.) of this assembly. '{0}': {1}", assemblyPath, e.Message); // The assembly failed to load... nothing // we can do but ignore it. continue; } catch (Exception e) { Logger.LogWarning(null, null, "Failed to load assembly '{0}': {1}", assemblyPath, e.Message); continue; } foreach (var t in exportedTypes) { if (t.IsAbstract) { continue; } if (t.GetInterface(@"IContentImporter") != null) { var attributes = t.GetCustomAttributes(typeof(ContentImporterAttribute), false); if (attributes.Length != 0) { var importerAttribute = attributes[0] as ContentImporterAttribute; _importers.Add(new ImporterInfo { attribute = importerAttribute, type = t, assemblyTimestamp = assemblyTimestamp }); } else { // If no attribute specify default one var importerAttribute = new ContentImporterAttribute(".*"); importerAttribute.DefaultProcessor = ""; importerAttribute.DisplayName = t.Name; _importers.Add(new ImporterInfo { attribute = importerAttribute, type = t, assemblyTimestamp = assemblyTimestamp }); } } else if (t.GetInterface(@"IContentProcessor") != null) { var attributes = t.GetCustomAttributes(typeof(ContentProcessorAttribute), false); if (attributes.Length != 0) { var processorAttribute = attributes[0] as ContentProcessorAttribute; _processors.Add(new ProcessorInfo { attribute = processorAttribute, type = t, assemblyTimestamp = assemblyTimestamp }); } } else if (t.GetInterface(@"ContentTypeWriter") != null) { // TODO: This doesn't work... how do i find these? _writers.Add(t); } } } }
public ImporterInfo(ContentImporterAttribute importer, Type type) { this.Importer = importer; this.Type = type; }
private ImporterType(Type type, ContentImporterAttribute attrib) { Type = type; OutputType = type.BaseType.GetGenericArguments()[0]; Attribute = attrib; }