Abstract binary file format detection into FileFormatReader.Load

This commit is contained in:
Katy Coe
2019-10-21 10:15:06 +02:00
parent 661d2c6ca7
commit 2ae0904226
2 changed files with 20 additions and 7 deletions

View File

@@ -7,6 +7,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using NoisyCowStudios.Bin2Object;
namespace Il2CppInspector
@@ -35,6 +37,22 @@ namespace Il2CppInspector
List<U> ReadMappedObjectPointerArray<U>(uint uiAddr, int count) where U : new();
}
internal class FileFormatReader
{
// Helper method to try all defined file formats when the contents of the binary is unknown
public static IFileFormatReader Load(Stream stream) {
var types = Assembly.GetExecutingAssembly().DefinedTypes
.Where(x => x.ImplementedInterfaces.Contains(typeof(IFileFormatReader)) && !x.IsGenericTypeDefinition);
foreach (var type in types) {
if (type.BaseType.GetMethod("Load", new [] {typeof(Stream)})
.Invoke(null, new object[] { stream }) is IFileFormatReader loaded)
return loaded;
}
return null;
}
}
internal class FileFormatReader<T> : BinaryObjectReader, IFileFormatReader where T : FileFormatReader<T>
{
public FileFormatReader(Stream stream) : base(stream) { }