Move HostModel code into separate assembly

This commit is contained in:
Caelan Sayler
2024-02-02 12:25:16 +00:00
parent d9d4c9b3fa
commit c10b15278f
25 changed files with 53 additions and 72 deletions

View File

@@ -1,4 +1,3 @@
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.0.31815.197 VisualStudioVersion = 17.0.31815.197
@@ -49,6 +48,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VeloWpfSample", "examples\V
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Divergic.Logging.Xunit", "test\Divergic.Logging.Xunit\Divergic.Logging.Xunit.csproj", "{5ED2E9AF-101D-4D2D-B0B5-90A920EF692D}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Divergic.Logging.Xunit", "test\Divergic.Logging.Xunit\Divergic.Logging.Xunit.csproj", "{5ED2E9AF-101D-4D2D-B0B5-90A920EF692D}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Velopack.Packaging.HostModel", "src\Velopack.Packaging.HostModel\Velopack.Packaging.HostModel.csproj", "{E9A2620C-C638-446C-BA30-F62C05709365}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@@ -111,6 +112,10 @@ Global
{5ED2E9AF-101D-4D2D-B0B5-90A920EF692D}.Debug|Any CPU.Build.0 = Debug|Any CPU {5ED2E9AF-101D-4D2D-B0B5-90A920EF692D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5ED2E9AF-101D-4D2D-B0B5-90A920EF692D}.Release|Any CPU.ActiveCfg = Release|Any CPU {5ED2E9AF-101D-4D2D-B0B5-90A920EF692D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5ED2E9AF-101D-4D2D-B0B5-90A920EF692D}.Release|Any CPU.Build.0 = Release|Any CPU {5ED2E9AF-101D-4D2D-B0B5-90A920EF692D}.Release|Any CPU.Build.0 = Release|Any CPU
{E9A2620C-C638-446C-BA30-F62C05709365}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E9A2620C-C638-446C-BA30-F62C05709365}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E9A2620C-C638-446C-BA30-F62C05709365}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E9A2620C-C638-446C-BA30-F62C05709365}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements. // Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license. // The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.IO;
using System.IO.MemoryMappedFiles; using System.IO.MemoryMappedFiles;
namespace Microsoft.NET.HostModel.AppHost namespace Microsoft.NET.HostModel.AppHost
@@ -17,14 +15,12 @@ namespace Microsoft.NET.HostModel.AppHost
{ {
byte* pointer = null; byte* pointer = null;
try try {
{
accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref pointer); accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref pointer);
byte* bytes = pointer + accessor.PointerOffset; byte* bytes = pointer + accessor.PointerOffset;
int position = KMPSearch(searchPattern, bytes, accessor.Capacity); int position = KMPSearch(searchPattern, bytes, accessor.Capacity);
if (position < 0) if (position < 0) {
{
throw new PlaceHolderNotFoundInAppHostException(searchPattern); throw new PlaceHolderNotFoundInAppHostException(searchPattern);
} }
@@ -34,15 +30,11 @@ namespace Microsoft.NET.HostModel.AppHost
offset: 0, offset: 0,
count: patternToReplace.Length); count: patternToReplace.Length);
if (pad0s) if (pad0s) {
{
Pad0(searchPattern, patternToReplace, bytes, position); Pad0(searchPattern, patternToReplace, bytes, position);
} }
} } finally {
finally if (pointer != null) {
{
if (pointer != null)
{
accessor.SafeMemoryMappedViewHandle.ReleasePointer(); accessor.SafeMemoryMappedViewHandle.ReleasePointer();
} }
} }
@@ -50,10 +42,8 @@ namespace Microsoft.NET.HostModel.AppHost
private static unsafe void Pad0(byte[] searchPattern, byte[] patternToReplace, byte* bytes, int offset) private static unsafe void Pad0(byte[] searchPattern, byte[] patternToReplace, byte* bytes, int offset)
{ {
if (patternToReplace.Length < searchPattern.Length) if (patternToReplace.Length < searchPattern.Length) {
{ for (int i = patternToReplace.Length; i < searchPattern.Length; i++) {
for (int i = patternToReplace.Length; i < searchPattern.Length; i++)
{
bytes[i + offset] = 0x0; bytes[i + offset] = 0x0;
} }
} }
@@ -65,27 +55,23 @@ namespace Microsoft.NET.HostModel.AppHost
byte[] patternToReplace, byte[] patternToReplace,
bool pad0s = true) bool pad0s = true)
{ {
using (var mappedFile = MemoryMappedFile.CreateFromFile(filePath)) using (var mappedFile = MemoryMappedFile.CreateFromFile(filePath)) {
{ using (var accessor = mappedFile.CreateViewAccessor()) {
using (var accessor = mappedFile.CreateViewAccessor())
{
SearchAndReplace(accessor, searchPattern, patternToReplace, pad0s); SearchAndReplace(accessor, searchPattern, patternToReplace, pad0s);
} }
} }
} }
internal static unsafe int SearchInFile(MemoryMappedViewAccessor accessor, byte[] searchPattern) public static unsafe int SearchInFile(MemoryMappedViewAccessor accessor, byte[] searchPattern)
{ {
var safeBuffer = accessor.SafeMemoryMappedViewHandle; var safeBuffer = accessor.SafeMemoryMappedViewHandle;
return KMPSearch(searchPattern, (byte*)safeBuffer.DangerousGetHandle(), (int)safeBuffer.ByteLength); return KMPSearch(searchPattern, (byte*) safeBuffer.DangerousGetHandle(), (int) safeBuffer.ByteLength);
} }
public static unsafe int SearchInFile(string filePath, byte[] searchPattern) public static unsafe int SearchInFile(string filePath, byte[] searchPattern)
{ {
using (var mappedFile = MemoryMappedFile.CreateFromFile(filePath)) using (var mappedFile = MemoryMappedFile.CreateFromFile(filePath)) {
{ using (var accessor = mappedFile.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read)) {
using (var accessor = mappedFile.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read))
{
return SearchInFile(accessor, searchPattern); return SearchInFile(accessor, searchPattern);
} }
} }
@@ -95,31 +81,23 @@ namespace Microsoft.NET.HostModel.AppHost
private static int[] ComputeKMPFailureFunction(byte[] pattern) private static int[] ComputeKMPFailureFunction(byte[] pattern)
{ {
int[] table = new int[pattern.Length]; int[] table = new int[pattern.Length];
if (pattern.Length >= 1) if (pattern.Length >= 1) {
{
table[0] = -1; table[0] = -1;
} }
if (pattern.Length >= 2) if (pattern.Length >= 2) {
{
table[1] = 0; table[1] = 0;
} }
int pos = 2; int pos = 2;
int cnd = 0; int cnd = 0;
while (pos < pattern.Length) while (pos < pattern.Length) {
{ if (pattern[pos - 1] == pattern[cnd]) {
if (pattern[pos - 1] == pattern[cnd])
{
table[pos] = cnd + 1; table[pos] = cnd + 1;
cnd++; cnd++;
pos++; pos++;
} } else if (cnd > 0) {
else if (cnd > 0)
{
cnd = table[cnd]; cnd = table[cnd];
} } else {
else
{
table[pos] = 0; table[pos] = 0;
pos++; pos++;
} }
@@ -134,25 +112,17 @@ namespace Microsoft.NET.HostModel.AppHost
int i = 0; int i = 0;
int[] table = ComputeKMPFailureFunction(pattern); int[] table = ComputeKMPFailureFunction(pattern);
while (m + i < bytesLength) while (m + i < bytesLength) {
{ if (pattern[i] == bytes[m + i]) {
if (pattern[i] == bytes[m + i]) if (i == pattern.Length - 1) {
{
if (i == pattern.Length - 1)
{
return m; return m;
} }
i++; i++;
} } else {
else if (table[i] > -1) {
{
if (table[i] > -1)
{
m = m + i - table[i]; m = m + i - table[i];
i = table[i]; i = table[i];
} } else {
else
{
m++; m++;
i = 0; i = 0;
} }
@@ -165,8 +135,7 @@ namespace Microsoft.NET.HostModel.AppHost
public static void CopyFile(string sourcePath, string destinationPath) public static void CopyFile(string sourcePath, string destinationPath)
{ {
var destinationDirectory = new FileInfo(destinationPath).Directory.FullName; var destinationDirectory = new FileInfo(destinationPath).Directory.FullName;
if (!Directory.Exists(destinationDirectory)) if (!Directory.Exists(destinationDirectory)) {
{
Directory.CreateDirectory(destinationDirectory); Directory.CreateDirectory(destinationDirectory);
} }
@@ -181,17 +150,14 @@ namespace Microsoft.NET.HostModel.AppHost
byte[] buf = new byte[bufSize]; byte[] buf = new byte[bufSize];
length = Math.Min(length, sourceViewAccessor.Capacity); length = Math.Min(length, sourceViewAccessor.Capacity);
do do {
{ int bytesRequested = Math.Min((int) length - pos, bufSize);
int bytesRequested = Math.Min((int)length - pos, bufSize); if (bytesRequested <= 0) {
if (bytesRequested <= 0)
{
break; break;
} }
int bytesRead = sourceViewAccessor.ReadArray(pos, buf, 0, bytesRequested); int bytesRead = sourceViewAccessor.ReadArray(pos, buf, 0, bytesRequested);
if (bytesRead > 0) if (bytesRead > 0) {
{
fileStream.Write(buf, 0, bytesRead); fileStream.Write(buf, 0, bytesRead);
pos += bytesRead; pos += bytesRead;
} }

View File

@@ -1,11 +1,7 @@
// If updating HostModel, mark the ResourceUpdater.cs class as partial so these functions can get mixed in // If updating HostModel, mark the ResourceUpdater.cs class as partial so these functions can get mixed in
using System;
using System.Runtime.Versioning;
namespace Microsoft.NET.HostModel namespace Microsoft.NET.HostModel
{ {
[SupportedOSPlatform("windows")]
public partial class ResourceUpdater public partial class ResourceUpdater
{ {
public ResourceUpdater(string peFile, bool bDeleteExistingResources) public ResourceUpdater(string peFile, bool bDeleteExistingResources)

View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<NoWarn>$(NoWarn);CA2007;CS8002;IDE0161</NoWarn>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<RootNamespace>Microsoft.NET.HostModel</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Reflection.Metadata" Version="8.0.0" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
</ItemGroup>
</Project>

View File

@@ -3,18 +3,17 @@
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks> <TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>$(NoWarn);CA2007;CS8002</NoWarn> <NoWarn>$(NoWarn);CA2007;CS8002</NoWarn>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Velopack.Packaging.HostModel\Velopack.Packaging.HostModel.csproj" />
<ProjectReference Include="..\Velopack.Packaging\Velopack.Packaging.csproj" /> <ProjectReference Include="..\Velopack.Packaging\Velopack.Packaging.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AsmResolver.DotNet" Version="5.5.0" /> <PackageReference Include="AsmResolver.DotNet" Version="5.5.0" />
<PackageReference Include="AsmResolver.PE.Win32Resources" Version="5.5.0" /> <PackageReference Include="AsmResolver.PE.Win32Resources" Version="5.5.0" />
<PackageReference Include="System.Reflection.Metadata" Version="8.0.0" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
</ItemGroup> </ItemGroup>
</Project> </Project>