Skip to content

Commit

Permalink
Files + ReadMe update.
Browse files Browse the repository at this point in the history
Title.
  • Loading branch information
Mateos81 committed Mar 12, 2017
1 parent 988b8ea commit 1989fb8
Show file tree
Hide file tree
Showing 7 changed files with 502 additions and 0 deletions.
22 changes: 22 additions & 0 deletions KFTempArchiveExtractor.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.24720.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KFTempArchiveExtractor", "KFTempArchiveExtractor\KFTempArchiveExtractor.csproj", "{3B418181-8BE4-4D18-876A-16DA9810F0C3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3B418181-8BE4-4D18-876A-16DA9810F0C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3B418181-8BE4-4D18-876A-16DA9810F0C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3B418181-8BE4-4D18-876A-16DA9810F0C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3B418181-8BE4-4D18-876A-16DA9810F0C3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions KFTempArchiveExtractor/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
61 changes: 61 additions & 0 deletions KFTempArchiveExtractor/KFTempArchiveExtractor.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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>{3B418181-8BE4-4D18-876A-16DA9810F0C3}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>KFTempArchiveExtractor</RootNamespace>
<AssemblyName>KFTempArchiveExtractor</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" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TempArchiveReader.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.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>
241 changes: 241 additions & 0 deletions KFTempArchiveExtractor/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
using System;
using System.IO;
using System.Linq;

namespace TempArchiveExtractor
{
/// <summary>
/// Main class.
/// </summary>
public class Program
{
#region Fields

/// <summary>
/// Base path where to create a folder per Archive file.
/// </summary>
private static string _baseExtractionPath = string.Empty;

/// <summary>
/// Indicates if either the original behaviour is used or not.
/// </summary>
private static bool _originalBehaviour = true;

#endregion Fields

#region Main

/// <summary>
/// Main function.
/// </summary>
/// <param name="args">Application arguments.</param>
public static void Main(string[] args)
{
if (args.Length != 0)
{
OriginalBehaviour(args);

ProcessOverPausing();

return;
}

// New behaviour
_originalBehaviour = false;

// List the KF Archive files to process
DirectoryInfo kf = new DirectoryInfo(GetKFInstallDir());
FileInfo[] archiveFiles = kf.GetFiles().Where(x => x.Name.StartsWith("TempArchive")).ToArray();

if (archiveFiles.Length == 0)
{
Console.WriteLine("No file to process...");
return;
}

// If needed, create the sub-folder in application's path
_baseExtractionPath =
Path.Combine(
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location),
"KF Archive Files");

if (Directory.Exists(_baseExtractionPath))
{
Directory.CreateDirectory(_baseExtractionPath);
}

// Process the files
foreach (FileInfo archiveFile in archiveFiles)
{
Process(archiveFile);
}

ProcessOverPausing();
}

#endregion Main

#region Methods

/// <summary>
/// Retrieves KF installation directory from the system registry.
/// </summary>
/// <returns>The path to the local KF installation directory.</returns>
private static string GetKFInstallDir()
{
return Microsoft.Win32.Registry.GetValue(
@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 1250",
"InstallLocation",
null).ToString();
}

/// <summary>
/// Original behaviour: drag and drop one file onto the executable.
/// One difference though: the <see cref="Process(FileInfo)"/> function will create a sub-folder per Archive file.
/// </summary>
/// <param name="args">Application arguments.</param>
private static void OriginalBehaviour(string[] args)
{
if (0 == args.Length || string.IsNullOrWhiteSpace(args[0]))
{
Console.Out.WriteLine("Usage: TempArchiveExtractor [TempArchiveFile]");
return;
}

FileInfo archiveFile = new FileInfo(args[0]);

if (!Process(archiveFile))
{
Console.WriteLine($"File {archiveFile.Name} incorrectly processed.");
}
}

/// <summary>
/// Processes a KF Archive file.
/// </summary>
/// <param name="archiveFile">KF Archive file.</param>
/// <returns>true if the file has been correctly processed, false otherwise.</returns>
private static bool Process(FileInfo archiveFile)
{
Console.WriteLine();

if (!archiveFile.Exists)
{
Console.Out.WriteLine("No such file.");

ProcessOverPausing();

return false;
}

Console.WriteLine($"Processing file {archiveFile.Name}...");

FileStream archiveStream = null;
try
{
archiveStream = archiveFile.OpenRead();
TempArchiveReader reader = new TempArchiveReader(archiveStream);

int fileCount = reader.ReadInt32();
Console.Out.WriteLine($"Archive contains {fileCount} files.");

// Create own Archive file folder if needed
DirectoryInfo archiveOwnFolder = new DirectoryInfo(
Path.Combine(
_baseExtractionPath,
archiveFile.Name));

if (!archiveOwnFolder.Exists)
{
Console.WriteLine($"Creating {archiveOwnFolder.Name} folder.");
archiveOwnFolder.Create();
}

// Process each inner file from the Archive file
for (int i = 0; i < fileCount; i++)
{
string filePath = reader.ReadFString();
int fileLen = reader.ReadFCompactIndex();

FileInfo fi =
new FileInfo(
Path.Combine(
archiveOwnFolder.FullName,
filePath));

// Either confirm each file or extract everything without prompt
if (_originalBehaviour)
{
string msg =
string.Format(
"{0} {1} Size: 0x{2:X} {2:N0} [Y/N] : ",
fi.Exists ? "Overwrite" : "Extract",
filePath,
fileLen);
Console.Out.Write(msg);
string resp = Console.In.ReadLine();
if ("Y".Equals(resp.ToUpper()))
{
DirectoryInfo di = fi.Directory;
if (!di.Exists)
{
di.Create();
}

using (FileStream fs = fi.OpenWrite())
{
reader.ReadIntoStream(fileLen, fs);
}
}
else
{
reader.Skip(fileLen);
}
}
else
{
Console.Out.WriteLine($"Extracting {filePath}.");

DirectoryInfo di = fi.Directory;
if (!di.Exists)
{
di.Create();
}

using (FileStream fs = fi.OpenWrite())
{
reader.ReadIntoStream(fileLen, fs);
}
}
}
}
catch (Exception ex)
{
Console.Out.WriteLine("Error reading archive.");
Console.Out.WriteLine(ex);

return false;
}
finally
{
archiveStream?.Close();
}

Console.WriteLine($"{archiveFile.Name} processed successfully.");

return true;
}

/// <summary>
/// Pausing the console to leave it open, so the user can read the output.
/// </summary>
private static void ProcessOverPausing()
{
Console.WriteLine();
Console.Write("Process finished. Pausing...");
Console.Read();
}

#endregion Methods
}
}
36 changes: 36 additions & 0 deletions KFTempArchiveExtractor/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;

// Les informations générales relatives à un assembly dépendent de
// l'ensemble d'attributs suivant. Changez les valeurs de ces attributs pour modifier les informations
// associées à un assembly.
[assembly: AssemblyTitle("KFTempArchiveExtractor")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("KFTempArchiveExtractor")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// L'affectation de la valeur false à ComVisible rend les types invisibles dans cet assembly
// aux composants COM. Si vous devez accéder à un type dans cet assembly à partir de
// COM, affectez la valeur true à l'attribut ComVisible sur ce type.
[assembly: ComVisible(false)]

// Le GUID suivant est pour l'ID de la typelib si ce projet est exposé à COM
[assembly: Guid("3b418181-8be4-4d18-876a-16da9810f0c3")]

// Les informations de version pour un assembly se composent des quatre valeurs suivantes :
//
// Version principale
// Version secondaire
// Numéro de build
// Révision
//
// Vous pouvez spécifier toutes les valeurs ou indiquer les numéros de build et de révision par défaut
// en utilisant '*', comme indiqué ci-dessous :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading

0 comments on commit 1989fb8

Please sign in to comment.