Enable nullable context and move WrapperFactory

This commit is contained in:
Matt Nadareski
2023-09-17 00:18:04 -04:00
parent c86b1251a3
commit 93ba88a35f
72 changed files with 616 additions and 96 deletions

View File

@@ -8,7 +8,6 @@ using System.Text;
using System.Threading.Tasks;
using BinaryObjectScanner.Interfaces;
using BinaryObjectScanner.Utilities;
using BinaryObjectScanner.Wrappers;
using SabreTools.Serialization.Wrappers;
namespace BinaryObjectScanner.FileType
@@ -44,7 +43,7 @@ namespace BinaryObjectScanner.FileType
if (contentCheckClasses == null)
contentCheckClasses = InitCheckClasses<IContentCheck>();
return contentCheckClasses;
return contentCheckClasses ?? Enumerable.Empty<IContentCheck>();
}
}
@@ -58,7 +57,7 @@ namespace BinaryObjectScanner.FileType
if (linearExecutableCheckClasses == null)
linearExecutableCheckClasses = InitCheckClasses<ILinearExecutableCheck>();
return linearExecutableCheckClasses;
return linearExecutableCheckClasses ?? Enumerable.Empty<ILinearExecutableCheck>();
}
}
@@ -72,7 +71,7 @@ namespace BinaryObjectScanner.FileType
if (msdosExecutableCheckClasses == null)
msdosExecutableCheckClasses = InitCheckClasses<IMSDOSExecutableCheck>();
return msdosExecutableCheckClasses;
return msdosExecutableCheckClasses ?? Enumerable.Empty<IMSDOSExecutableCheck>();
}
}
@@ -86,7 +85,7 @@ namespace BinaryObjectScanner.FileType
if (newExecutableCheckClasses == null)
newExecutableCheckClasses = InitCheckClasses<INewExecutableCheck>();
return newExecutableCheckClasses;
return newExecutableCheckClasses ?? Enumerable.Empty<INewExecutableCheck>();
}
}
@@ -100,7 +99,7 @@ namespace BinaryObjectScanner.FileType
if (portableExecutableCheckClasses == null)
portableExecutableCheckClasses = InitCheckClasses<IPortableExecutableCheck>();
return portableExecutableCheckClasses;
return portableExecutableCheckClasses ?? Enumerable.Empty<IPortableExecutableCheck>();
}
}
@@ -111,32 +110,56 @@ namespace BinaryObjectScanner.FileType
/// <summary>
/// Cache for all IContentCheck types
/// </summary>
#if NET48
private static IEnumerable<IContentCheck> contentCheckClasses;
#else
private static IEnumerable<IContentCheck>? contentCheckClasses;
#endif
/// <summary>
/// Cache for all ILinearExecutableCheck types
/// </summary>
#if NET48
private static IEnumerable<ILinearExecutableCheck> linearExecutableCheckClasses;
#else
private static IEnumerable<ILinearExecutableCheck>? linearExecutableCheckClasses;
#endif
/// <summary>
/// Cache for all IMSDOSExecutableCheck types
/// </summary>
#if NET48
private static IEnumerable<IMSDOSExecutableCheck> msdosExecutableCheckClasses;
#else
private static IEnumerable<IMSDOSExecutableCheck>? msdosExecutableCheckClasses;
#endif
/// <summary>
/// Cache for all INewExecutableCheck types
/// </summary>
#if NET48
private static IEnumerable<INewExecutableCheck> newExecutableCheckClasses;
#else
private static IEnumerable<INewExecutableCheck>? newExecutableCheckClasses;
#endif
/// <summary>
/// Cache for all IPortableExecutableCheck types
/// </summary>
#if NET48
private static IEnumerable<IPortableExecutableCheck> portableExecutableCheckClasses;
#else
private static IEnumerable<IPortableExecutableCheck>? portableExecutableCheckClasses;
#endif
#endregion
/// <inheritdoc/>
#if NET48
public string Detect(string file, bool includeDebug)
#else
public string? Detect(string file, bool includeDebug)
#endif
{
if (!File.Exists(file))
return null;
@@ -148,7 +171,11 @@ namespace BinaryObjectScanner.FileType
}
/// <inheritdoc/>
#if NET48
public string Detect(Stream stream, string file, bool includeDebug)
#else
public string? Detect(Stream stream, string file, bool includeDebug)
#endif
{
// Try to create a wrapper for the proper executable type
var wrapper = WrapperFactory.CreateExecutableWrapper(stream);
@@ -203,7 +230,11 @@ namespace BinaryObjectScanner.FileType
/// <param name="stream">Stream to scan the contents of</param>
/// <param name="includeDebug">True to include debug data, false otherwise</param>
/// <returns>Set of protections in file, null on error</returns>
#if NET48
public ConcurrentDictionary<IContentCheck, string> RunContentChecks(string file, Stream stream, bool includeDebug)
#else
public ConcurrentDictionary<IContentCheck, string>? RunContentChecks(string? file, Stream stream, bool includeDebug)
#endif
{
// If we have an invalid file
if (string.IsNullOrWhiteSpace(file))
@@ -212,7 +243,11 @@ namespace BinaryObjectScanner.FileType
return null;
// Read the file contents
#if NET48
byte[] fileContent = null;
#else
byte[]? fileContent = null;
#endif
try
{
using (BinaryReader br = new BinaryReader(stream, Encoding.Default, true))
@@ -396,15 +431,23 @@ namespace BinaryObjectScanner.FileType
/// <summary>
/// Initialize all implementations of a type
/// </summary>
#if NET48
private static IEnumerable<T> InitCheckClasses<T>()
=> InitCheckClasses<T>(typeof(GameEngine._DUMMY).Assembly)
.Concat(InitCheckClasses<T>(typeof(Packer._DUMMY).Assembly))
.Concat(InitCheckClasses<T>(typeof(Protection._DUMMY).Assembly));
#else
private static IEnumerable<T>? InitCheckClasses<T>()
#endif
=> InitCheckClasses<T>(typeof(GameEngine._DUMMY).Assembly) ?? Enumerable.Empty<T>()
.Concat(InitCheckClasses<T>(typeof(Packer._DUMMY).Assembly) ?? Enumerable.Empty<T>())
.Concat(InitCheckClasses<T>(typeof(Protection._DUMMY).Assembly) ?? Enumerable.Empty<T>());
/// <summary>
/// Initialize all implementations of a type
/// </summary>
#if NET48
private static IEnumerable<T> InitCheckClasses<T>(Assembly assembly)
#else
private static IEnumerable<T>? InitCheckClasses<T>(Assembly assembly)
#endif
{
return assembly.GetTypes()
.Where(t => t.IsClass && t.GetInterface(typeof(T).Name) != null)