diff --git a/src/FetchBannerlordVersion/Fetcher.cs b/src/FetchBannerlordVersion/Fetcher.cs index ec328fc..c303372 100644 --- a/src/FetchBannerlordVersion/Fetcher.cs +++ b/src/FetchBannerlordVersion/Fetcher.cs @@ -9,16 +9,19 @@ namespace FetchBannerlordVersion { public static class Fetcher { - private static CustomAttribute? GetVirtualFileAttributeV1(MetadataReader mdReader) + private static CustomAttribute GetVirtualFileAttributeV1(MetadataReader mdReader) { var vfType = mdReader.TypeDefinitions .GetType(mdReader, "TaleWorlds.Library", "VirtualFolders"); - return vfType + var field = vfType .GetNestedType(mdReader, "Win64_Shipping_Client") .GetNestedType(mdReader, "bin") .GetNestedType(mdReader, "Parameters") - .GetField(mdReader, "Version") + .GetField(mdReader, "Version"); + if (field.Equals(default(FieldDefinition))) + return default; + return field .GetCustomAttributes() .Select(mdReader.GetCustomAttribute) .FirstOrDefault(a => @@ -34,16 +37,19 @@ public static class Fetcher return name == "VirtualFileAttribute"; }); } - private static CustomAttribute? GetVirtualFileAttributeV2(MetadataReader mdReader) + private static CustomAttribute GetVirtualFileAttributeV2(MetadataReader mdReader) { var vfType = mdReader.TypeDefinitions .GetType(mdReader, "TaleWorlds.Library", "VirtualFolders"); - return vfType + var field = vfType .GetNestedType(mdReader, "Shipping_Client") .GetNestedType(mdReader, "bin") .GetNestedType(mdReader, "Parameters") - .GetField(mdReader, "Version") + .GetField(mdReader, "Version"); + if (field.Equals(default)) + return default; + return field .GetCustomAttributes() .Select(mdReader.GetCustomAttribute) .FirstOrDefault(a => @@ -126,7 +132,7 @@ public static string GetVersion(string gameFolderPath, string libAssembly) using var fs = File.OpenRead(Path.Combine(libFolderPath, libAssembly)); using var peReader = new PEReader(fs); var mdReader = peReader.GetMetadataReader(MetadataReaderOptions.None); - var versionVirtualFileAttribute = GetVirtualFileAttributeV1(mdReader)!.Value; + var versionVirtualFileAttribute = GetVirtualFileAttributeV1(mdReader); var attributeReader = mdReader.GetBlobReader(versionVirtualFileAttribute.Value); attributeReader.ReadByte(); attributeReader.ReadByte(); @@ -142,7 +148,7 @@ public static string GetVersion(string gameFolderPath, string libAssembly) using var fs = File.OpenRead(Path.Combine(libFolderPath, libAssembly)); using var peReader = new PEReader(fs); var mdReader = peReader.GetMetadataReader(MetadataReaderOptions.None); - var versionVirtualFileAttribute = GetVirtualFileAttributeV1(mdReader)!.Value; + var versionVirtualFileAttribute = GetVirtualFileAttributeV1(mdReader); var attributeReader = mdReader.GetBlobReader(versionVirtualFileAttribute.Value); attributeReader.ReadByte(); attributeReader.ReadByte(); @@ -169,7 +175,7 @@ public static string GetVersion(string gameFolderPath, string libAssembly) using var fs = File.OpenRead(Path.Combine(libFolderPath, libAssembly)); using var peReader = new PEReader(fs); var mdReader = peReader.GetMetadataReader(MetadataReaderOptions.None); - var versionVirtualFileAttribute = GetVirtualFileAttributeV2(mdReader)!.Value; + var versionVirtualFileAttribute = GetVirtualFileAttributeV2(mdReader); var attributeReader = mdReader.GetBlobReader(versionVirtualFileAttribute.Value); attributeReader.ReadByte(); attributeReader.ReadByte(); @@ -216,15 +222,15 @@ public static VersionType GetVersionType(string gameFolderPath, string libAssemb var versionVirtualFileAttributeV1 = GetVirtualFileAttributeV1(mdReader); - if (versionVirtualFileAttributeV1 is null) + if (versionVirtualFileAttributeV1.Equals(default(CustomAttribute))) { - if (GetVirtualFileAttributeV2(mdReader) is not null) + if (!GetVirtualFileAttributeV2(mdReader).Equals(default(CustomAttribute))) return VersionType.V5; return VersionType.Unknown; } - var attributeReader = mdReader.GetBlobReader(versionVirtualFileAttributeV1.Value.Value); + var attributeReader = mdReader.GetBlobReader(versionVirtualFileAttributeV1.Value); if (attributeReader.ReadByte() != 0x01 || attributeReader.ReadByte() != 0x00) throw new NotSupportedException("Custom Attribute prolog is invalid."); diff --git a/src/FetchBannerlordVersion/MetadataHelpers.cs b/src/FetchBannerlordVersion/MetadataHelpers.cs index 227ea76..caf3ff2 100644 --- a/src/FetchBannerlordVersion/MetadataHelpers.cs +++ b/src/FetchBannerlordVersion/MetadataHelpers.cs @@ -5,18 +5,36 @@ namespace FetchBannerlordVersion { public static class MetadataHelpers { - public static TypeDefinition GetType(this TypeDefinitionHandleCollection tdh, MetadataReader reader, string ns, string typeName) => tdh - .Select(reader.GetTypeDefinition) - .First(x => reader.GetString(x.Name) == typeName && reader.GetString(x.Namespace) == ns); + public static TypeDefinition GetType(this TypeDefinitionHandleCollection tdh, MetadataReader reader, string ns, string typeName) + { + return tdh + .Where(x => !x.IsNil) + .Select(reader.GetTypeDefinition) + .FirstOrDefault(x => reader.GetString(x.Name) == typeName && reader.GetString(x.Namespace) == ns); + } - public static TypeDefinition GetNestedType(this TypeDefinition td, MetadataReader reader, string nestedTypeName) => td - .GetNestedTypes() - .Select(reader.GetTypeDefinition) - .First(x => reader.GetString(x.Name) == nestedTypeName); + public static TypeDefinition GetNestedType(this TypeDefinition td, MetadataReader reader, string nestedTypeName) + { + if (td.Equals(default(TypeDefinition))) + return default; + + return td + .GetNestedTypes() + .Where(x => !x.IsNil) + .Select(reader.GetTypeDefinition) + .FirstOrDefault(x => reader.GetString(x.Name) == nestedTypeName); + } - public static FieldDefinition GetField(this TypeDefinition td, MetadataReader reader, string nestedTypeName) => td - .GetFields() - .Select(reader.GetFieldDefinition) - .First(x => reader.GetString(x.Name) == nestedTypeName); + public static FieldDefinition GetField(this TypeDefinition td, MetadataReader reader, string nestedTypeName) + { + if (td.Equals(default(TypeDefinition))) + return default; + + return td + .GetFields() + .Where(x => !x.IsNil) + .Select(reader.GetFieldDefinition) + .FirstOrDefault(x => reader.GetString(x.Name) == nestedTypeName); + } } } \ No newline at end of file