Skip to content

Commit

Permalink
Merge pull request #10 from Workshell/feature/issue-9
Browse files Browse the repository at this point in the history
ISSUE-9: Fixes issue with file version ordering.
  • Loading branch information
lkinsella authored Jun 24, 2024
2 parents 95f8859 + c7ab1e7 commit 8bdc405
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 34 deletions.
16 changes: 16 additions & 0 deletions src/Workshell.PE.Resources/Version/FileInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Workshell.PE.Resources.Version
{
internal sealed class FileInfo
{
public ushort Length { get; set; }
public ushort ValueLength { get; set; }
public ushort Type { get; set; }
public string Key { get; set; }
}
}
6 changes: 1 addition & 5 deletions src/Workshell.PE.Resources/Version/StringFileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,9 @@ private StringFileInfo(string key, VersionStringTable[] tables)

#region Static Methods

internal static async Task<StringFileInfo> LoadAsync(Stream stream)
internal static async Task<StringFileInfo> LoadAsync(Stream stream, ushort len, ushort valLen, ushort type, string key)
{
var count = 3 * sizeof(ushort);
var len = await stream.ReadUInt16Async().ConfigureAwait(false);
var valLen = await stream.ReadUInt16Async().ConfigureAwait(false);
var type = await stream.ReadUInt16Async().ConfigureAwait(false);
var key = await stream.ReadUnicodeStringAsync().ConfigureAwait(false);

count += (key.Length + 1) * sizeof(ushort);
count += await VersionResource.AlignWordBoundaryAsync(stream).ConfigureAwait(false);
Expand Down
6 changes: 1 addition & 5 deletions src/Workshell.PE.Resources/Version/VarFileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,9 @@ private VarFileInfo(string key, VersionVariable[] vars)

#region Static Methods

internal static async Task<VarFileInfo> LoadAsync(Stream stream)
internal static async Task<VarFileInfo> LoadAsync(Stream stream, ushort len, ushort valLen, ushort type, string key)
{
var count = 3 * sizeof(ushort);
var len = await stream.ReadUInt16Async().ConfigureAwait(false);
var valLen = await stream.ReadUInt16Async().ConfigureAwait(false);
var type = await stream.ReadUInt16Async().ConfigureAwait(false);
var key = await stream.ReadUnicodeStringAsync().ConfigureAwait(false);

count += (key.Length + 1) * sizeof(ushort);
count += await VersionResource.AlignWordBoundaryAsync(stream).ConfigureAwait(false);
Expand Down
14 changes: 9 additions & 5 deletions src/Workshell.PE.Resources/Version/VersionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,32 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Workshell.PE.Resources.Version
{
public sealed class VersionInfo
{
internal VersionInfo(VersionResource resource, uint languageId, FixedFileInfo fixedInfo, StringFileInfo stringInfo, VarFileInfo varInfo)
private readonly IReadOnlyCollection<StringFileInfo> _strings;
private readonly IReadOnlyCollection<VarFileInfo> _variables;

internal VersionInfo(VersionResource resource, uint languageId, FixedFileInfo fixedInfo, IReadOnlyCollection<StringFileInfo> stringInfo, IReadOnlyCollection<VarFileInfo> varInfo)
{
Resource = resource;
Language = languageId;
Fixed = fixedInfo;
Strings = stringInfo;
Variables = varInfo;
_strings = stringInfo;
_variables = varInfo;
}

#region Properties

public VersionResource Resource { get; }
public ResourceLanguage Language { get; }
public FixedFileInfo Fixed { get; }
public StringFileInfo Strings { get; }
public VarFileInfo Variables { get; }
public StringFileInfo Strings => _strings.FirstOrDefault();
public VarFileInfo Variables => _variables.FirstOrDefault();

#endregion
}
Expand Down
90 changes: 74 additions & 16 deletions src/Workshell.PE.Resources/Version/VersionResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,33 @@ internal static async Task<int> AlignWordBoundaryAsync(Stream stream)

while (stream.Position % 4 != 0)
{
await stream.ReadUInt16Async().ConfigureAwait(false);
await stream.ReadUInt16Async()
.ConfigureAwait(false);

count += sizeof(ushort);
}

return count;
}

internal static async Task<FileInfo> LoadBaseInfoAsync(Stream stream)
{
var len = await stream.ReadUInt16Async()
.ConfigureAwait(false);
var valLen = await stream.ReadUInt16Async()
.ConfigureAwait(false);
var type = await stream.ReadUInt16Async()
.ConfigureAwait(false);
var key = await stream.ReadUnicodeStringAsync()
.ConfigureAwait(false);

return new FileInfo()
{
Length = len,
ValueLength = valLen,
Type = type,
Key = key
};
}

#endregion
Expand All @@ -71,42 +92,79 @@ public VersionInfo GetInfo()

public async Task<VersionInfo> GetInfoAsync()
{
return await GetInfoAsync(ResourceLanguage.English.UnitedStates).ConfigureAwait(false);
return await GetInfoAsync(ResourceLanguage.English.UnitedStates)
.ConfigureAwait(false);
}

public VersionInfo GetInfo(ResourceLanguage language)
{
return GetInfoAsync(language).GetAwaiter().GetResult();
return GetInfoAsync(language)
.GetAwaiter()
.GetResult();
}

public async Task<VersionInfo> GetInfoAsync(ResourceLanguage language)
{
var buffer = await GetBytesAsync(language).ConfigureAwait(false);
var buffer = await GetBytesAsync(language)
.ConfigureAwait(false);

using (var mem = new MemoryStream(buffer))
{
var count = 3 * sizeof(ushort);

await mem.ReadBytesAsync(count).ConfigureAwait(false);
await mem.ReadBytesAsync(count)
.ConfigureAwait(false);

var key = await mem.ReadUnicodeStringAsync().ConfigureAwait(false);
var key = await mem.ReadUnicodeStringAsync()
.ConfigureAwait(false);

if (key != "VS_VERSION_INFO")
{
throw new Exception("Invalid file version information.");
}

await AlignWordBoundaryAsync(mem).ConfigureAwait(false);
await AlignWordBoundaryAsync(mem)
.ConfigureAwait(false);

var ffiData = await mem.ReadStructAsync<VS_FIXEDFILEINFO>().ConfigureAwait(false);
var ffiData = await mem.ReadStructAsync<VS_FIXEDFILEINFO>()
.ConfigureAwait(false);
var fixedFileInfo = new FixedFileInfo(ffiData);

await AlignWordBoundaryAsync(mem).ConfigureAwait(false);

var stringFileInfo = await StringFileInfo.LoadAsync(mem).ConfigureAwait(false);

await AlignWordBoundaryAsync(mem).ConfigureAwait(false);

var varFileInfo = await VarFileInfo.LoadAsync(mem).ConfigureAwait(false);
var info = new VersionInfo(this, language, fixedFileInfo, stringFileInfo, varFileInfo);
await AlignWordBoundaryAsync(mem)
.ConfigureAwait(false);

var stringFileInfos = new List<StringFileInfo>();
var varFileInfos = new List<VarFileInfo>();

while ((mem.Position + count) < mem.Length)
{
var baseInfo = await LoadBaseInfoAsync(mem)
.ConfigureAwait(false);

if (baseInfo.Key == "StringFileInfo")
{
var stringFileInfo = await StringFileInfo.LoadAsync(mem, baseInfo.Length, baseInfo.ValueLength, baseInfo.Type, baseInfo.Key)
.ConfigureAwait(false);

stringFileInfos.Add(stringFileInfo);
}
else if (baseInfo.Key == "VarFileInfo")
{
var varFileInfo = await VarFileInfo.LoadAsync(mem, baseInfo.Length, baseInfo.ValueLength, baseInfo.Type, baseInfo.Key)
.ConfigureAwait(false);

varFileInfos.Add(varFileInfo);
}
else
{
throw new Exception("Unknown type - " + baseInfo.Key);
}

await AlignWordBoundaryAsync(mem)
.ConfigureAwait(false);
}

var info = new VersionInfo(this, language, fixedFileInfo, stringFileInfos, varFileInfos);

return info;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Workshell.PE.Resources/Workshell.PE.Resources.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<TargetFrameworks>net8.0;net7.0;net6.0</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<SignAssembly>false</SignAssembly>
<AssemblyOriginatorKeyFile>Workshell.snk</AssemblyOriginatorKeyFile>
Expand Down
2 changes: 1 addition & 1 deletion src/Workshell.PE/Workshell.PE.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<TargetFrameworks>net8.0;net7.0;net6.0</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<SignAssembly>false</SignAssembly>
<AssemblyOriginatorKeyFile>Workshell.snk</AssemblyOriginatorKeyFile>
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.0
4.0.0

0 comments on commit 8bdc405

Please sign in to comment.