Implement reflection-based importer

The reflection-based importer, for file types that aren't imported via a
legacy bkm file, dynamically searches the current module (i.e.
BizHawk.Client.Common) for all IMovieImport instances with the
ImportExtension attribute.

This is difficult to truly test without more non-legacy import
implementations, but appears to work with a dummy .pjm file.

It may be desirable to instead search the current assembly instead of
the current module, but I think all implementations should probably go
in BizHawk.Client.Common, so I chose to search by module instead.
This commit is contained in:
Iris Ward 2015-09-21 18:13:58 +01:00
parent 189390f001
commit 0e010a2988
1 changed files with 46 additions and 3 deletions

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using BizHawk.Common;
using BizHawk.Common.BufferExtensions;
@ -74,10 +75,52 @@ namespace BizHawk.Client.Common
if (UsesLegacyImporter(ext)) {
return LegacyImportFile(ext, path, out errorMsg, out warningMsg).ToBk2();
} else {
errorMsg = "No importer found for " + ext;
}
return null;
var importers = ImportersForExtension(ext);
var importerType = importers.FirstOrDefault();
if (importerType == default(Type)) {
errorMsg = "No importer found for file type " + ext;
return null;
}
// Create a new instance of the importer class using the no-argument constructor
IMovieImport importer = importerType.GetConstructor(new Type[] { })
.Invoke(new object[] { }) as IMovieImport;
Bk2Movie movie = null;
try {
var result = importer.Import(path);
if (result.Errors.Count() > 0) errorMsg = result.Errors.First();
if (result.Warnings.Count() > 0) warningMsg = result.Warnings.First();
movie = result.Movie;
} catch (Exception ex) {
errorMsg = ex.ToString();
}
return movie;
}
private static IEnumerable<Type> ImportersForExtension(string ext) {
var info = typeof(MovieImport).Module;
var importers = from t in info.GetTypes()
where typeof(IMovieImport).IsAssignableFrom(t)
&& TypeImportsExtension(t, ext)
select t;
return importers;
}
private static bool TypeImportsExtension(Type t, string ext) {
var attrs = (ImportExtension[])t.GetCustomAttributes(typeof(ImportExtension), inherit: false);
if (attrs.Where(a => a.Extension.ToUpper() == ext.ToUpper()).Count() > 0) {
return true;
} else {
return false;
}
}
private static BkmMovie LegacyImportFile(string ext, string path, out string errorMsg, out string warningMsg) {