Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

Commit

Permalink
Added per-file update
Browse files Browse the repository at this point in the history
Added new project that is building UpdateInfo.yml
Improved kinda the UI control in downloaders, but still needs some improving
Removed NAppUpdate
Added Polish
  • Loading branch information
Aragas committed Dec 14, 2016
1 parent 40dc6dc commit ae57749
Show file tree
Hide file tree
Showing 40 changed files with 1,976 additions and 246 deletions.
119 changes: 119 additions & 0 deletions P3D-Legacy Launcher UpdateInfoBuilder/Crc32.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright (c) Damien Guard. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Originally published at http://damieng.com/blog/2006/08/08/calculating_crc32_in_c_and_net

using System;
using System.Collections.Generic;
using System.Security.Cryptography;

namespace DamienG.Security.Cryptography
{
/// <summary>
/// Implements a 32-bit CRC hash algorithm compatible with Zip etc.
/// </summary>
/// <remarks>
/// Crc32 should only be used for backward compatibility with older file formats
/// and algorithms. It is not secure enough for new applications.
/// If you need to call multiple times for the same data either use the HashAlgorithm
/// interface or remember that the result of one Compute call needs to be ~ (XOR) before
/// being passed in as the seed for the next Compute call.
/// </remarks>
public sealed class Crc32 : HashAlgorithm
{
public const UInt32 DefaultPolynomial = 0xedb88320u;
public const UInt32 DefaultSeed = 0xffffffffu;

static UInt32[] defaultTable;

readonly UInt32 seed;
readonly UInt32[] table;
UInt32 hash;

public Crc32()
: this(DefaultPolynomial, DefaultSeed)
{
}

public Crc32(UInt32 polynomial, UInt32 seed)
{
table = InitializeTable(polynomial);
this.seed = hash = seed;
}

public override void Initialize()
{
hash = seed;
}

protected override void HashCore(byte[] array, int ibStart, int cbSize)
{
hash = CalculateHash(table, hash, array, ibStart, cbSize);
}

protected override byte[] HashFinal()
{
var hashBuffer = UInt32ToBigEndianBytes(~hash);
HashValue = hashBuffer;
return hashBuffer;
}

public override int HashSize { get { return 32; } }

public static UInt32 Compute(byte[] buffer)
{
return Compute(DefaultSeed, buffer);
}

public static UInt32 Compute(UInt32 seed, byte[] buffer)
{
return Compute(DefaultPolynomial, seed, buffer);
}

public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer)
{
return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
}

static UInt32[] InitializeTable(UInt32 polynomial)
{
if (polynomial == DefaultPolynomial && defaultTable != null)
return defaultTable;

var createTable = new UInt32[256];
for (var i = 0; i < 256; i++)
{
var entry = (UInt32) i;
for (var j = 0; j < 8; j++)
if ((entry & 1) == 1)
entry = (entry >> 1) ^ polynomial;
else
entry = entry >> 1;
createTable[i] = entry;
}

if (polynomial == DefaultPolynomial)
defaultTable = createTable;

return createTable;
}

static UInt32 CalculateHash(UInt32[] table, UInt32 seed, IList<byte> buffer, int start, int size)
{
var hash = seed;
for (var i = start; i < start + size; i++)
hash = (hash >> 8) ^ table[buffer[i] ^ hash & 0xff];
return hash;
}

static byte[] UInt32ToBigEndianBytes(UInt32 uint32)
{
var result = BitConverter.GetBytes(uint32);

if (BitConverter.IsLittleEndian)
Array.Reverse(result);

return result;
}
}
}
22 changes: 22 additions & 0 deletions P3D-Legacy Launcher UpdateInfoBuilder/Data/UpdateInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections.Generic;

using YamlDotNet.Serialization;

namespace P3D.Legacy.Launcher.UpdateInfoBuilder.Data
{
public class UpdateInfo
{
public static SerializerBuilder SerializerBuilder { get; } = new SerializerBuilder();
public static DeserializerBuilder DeserializerBuilder { get; } = new DeserializerBuilder();

public List<UpdateFileEntry> Files { get; set; } = new List<UpdateFileEntry>();
}

public class UpdateFileEntry
{
public string AbsoluteFilePath { get; set; }
public string CRC32 { get; set; }
public string SHA1 { get; set; }
public long Size { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{317AA66C-58E7-4005-9568-93CFAF5353CD}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>P3D.Legacy.Launcher.UpdateInfoBuilder</RootNamespace>
<AssemblyName>P3D-Legacy Launcher UpdateInfoBuilder</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="YamlDotNet, Version=4.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\YamlDotNet.4.0.0\lib\net35\YamlDotNet.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Crc32.cs" />
<Compile Include="Data\UpdateInfo.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
51 changes: 51 additions & 0 deletions P3D-Legacy Launcher UpdateInfoBuilder/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;

using DamienG.Security.Cryptography;

using P3D.Legacy.Launcher.UpdateInfoBuilder.Data;

namespace P3D.Legacy.Launcher.UpdateInfoBuilder
{
public static class Program
{
private static string MainFolderPath { get; } = AppDomain.CurrentDomain.BaseDirectory;

private const string OutputFilename = "UpdateInfo.yml";
private static string OutputFilePath { get; } = Path.Combine(MainFolderPath, OutputFilename);

public static void Main(string[] args)
{
var updateInfoPath = args[0];


//var allAbsoluteFilePaths = Directory.GetFiles(updateInfoPath, "*.*", SearchOption.AllDirectories).Select(filePath => filePath.Replace(updateInfoPath, ""));
var allAbsoluteFilePaths = Directory.GetFiles(updateInfoPath, "*.*", SearchOption.AllDirectories).Select(filePath => filePath.Substring(updateInfoPath.Length + 1));

var crc32 = new Crc32();
var sha1 = new SHA1Managed();
var updateFileEntries = new List<UpdateFileEntry>();
foreach (var absoluteFilePath in allAbsoluteFilePaths)
{
var filePath = Path.Combine(updateInfoPath, absoluteFilePath);
var length = new FileInfo(filePath).Length;
using (var fs = File.OpenRead(filePath))
{
var crc32Hash = string.Empty;
var sha1Hash = string.Empty;
crc32Hash = crc32.ComputeHash(fs).Aggregate(crc32Hash, (current, b) => current + b.ToString("x2").ToLower());
sha1Hash = sha1.ComputeHash(fs).Aggregate(sha1Hash, (current, b) => current + b.ToString("x2").ToLower());
updateFileEntries.Add(new UpdateFileEntry { AbsoluteFilePath = absoluteFilePath, CRC32 = crc32Hash, SHA1 = sha1Hash, Size = length });
}
}

var serializer = UpdateInfo.SerializerBuilder.Build();
var content = serializer.Serialize(new UpdateInfo { Files = updateFileEntries });

File.WriteAllText(OutputFilePath, content);
}
}
}
36 changes: 36 additions & 0 deletions P3D-Legacy Launcher UpdateInfoBuilder/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("P3D-Legacy Launcher UpdateInfoBuilder")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("P3D-Legacy Launcher UpdateInfoBuilder")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("317aa66c-58e7-4005-9568-93cfaf5353cd")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
4 changes: 4 additions & 0 deletions P3D-Legacy Launcher UpdateInfoBuilder/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="YamlDotNet" version="4.0.0" targetFramework="net452" />
</packages>
6 changes: 6 additions & 0 deletions P3D-Legacy Launcher.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "P3D-Legacy Launcher", "P3D-
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "P3D-Legacy Launcher Updater", "P3D-Legacy Launcher Updater\P3D-Legacy Launcher Updater.csproj", "{6EE47050-ED83-4944-ACBB-D7F74E8C14F3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "P3D-Legacy Launcher UpdateInfoBuilder", "P3D-Legacy Launcher UpdateInfoBuilder\P3D-Legacy Launcher UpdateInfoBuilder.csproj", "{317AA66C-58E7-4005-9568-93CFAF5353CD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{6EE47050-ED83-4944-ACBB-D7F74E8C14F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6EE47050-ED83-4944-ACBB-D7F74E8C14F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6EE47050-ED83-4944-ACBB-D7F74E8C14F3}.Release|Any CPU.Build.0 = Release|Any CPU
{317AA66C-58E7-4005-9568-93CFAF5353CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{317AA66C-58E7-4005-9568-93CFAF5353CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{317AA66C-58E7-4005-9568-93CFAF5353CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{317AA66C-58E7-4005-9568-93CFAF5353CD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Loading

0 comments on commit ae57749

Please sign in to comment.