Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Mar 2, 2023
1 parent e729ff1 commit ad1743a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 23 deletions.
30 changes: 18 additions & 12 deletions src/FetchBannerlordVersion/Fetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand All @@ -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 =>
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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.");
Expand Down
40 changes: 29 additions & 11 deletions src/FetchBannerlordVersion/MetadataHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit ad1743a

Please sign in to comment.