Skip to content

Commit

Permalink
pushing project to site...
Browse files Browse the repository at this point in the history
  • Loading branch information
frostbone25 committed Sep 28, 2022
1 parent 7d4e486 commit 157c7c6
Show file tree
Hide file tree
Showing 18 changed files with 398 additions and 0 deletions.
25 changes: 25 additions & 0 deletions dat-extractor.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.32328.378
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dat-extractor", "dat-extractor\dat-extractor.csproj", "{26EC68A5-551C-4024-83A1-A8B6D5F97371}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{26EC68A5-551C-4024-83A1-A8B6D5F97371}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{26EC68A5-551C-4024-83A1-A8B6D5F97371}.Debug|Any CPU.Build.0 = Debug|Any CPU
{26EC68A5-551C-4024-83A1-A8B6D5F97371}.Release|Any CPU.ActiveCfg = Release|Any CPU
{26EC68A5-551C-4024-83A1-A8B6D5F97371}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DD771827-A88F-416C-AA2E-15DC4EC607A8}
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions dat-extractor/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.7.2" />
</startup>
</configuration>
35 changes: 35 additions & 0 deletions dat-extractor/DatFileEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace dat_extractor
{
public struct DatFileEntry
{
/// <summary>
/// Pointer offset in the .dat file where the data for the file starts.
/// </summary>
public uint FileOffset;

/// <summary>
/// Not sure, this value is always zero.
/// </summary>
public uint Unknown;

/// <summary>
/// How large the file data chunk is in the dat file.
/// </summary>
public uint FileDataSize;

/// <summary>
/// Pointer offset in the .dat file where the string for the file name starts.
/// </summary>
public uint FileNameOffset;

/// <summary>
/// The parsed string in the .dat file which is the file name
/// </summary>
public string FileName;

/// <summary>
/// The parsed byte array for the actual file data itself.
/// </summary>
public byte[] Data;
}
}
166 changes: 166 additions & 0 deletions dat-extractor/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace dat_extractor
{
internal class Program
{
static void Main(string[] args)
{
//if there are not exactly 2 arguments, don't continue.
if(args.Length != 2)
{
Console.WriteLine("Incorrect amount of arguments!");
Utilities.PrintProgramUsage();
return;
}

//get both arguments
string inputPath = args[0];
string outputPath = args[1];

//validate the input path
if(!Directory.Exists(inputPath))
{
Console.WriteLine("Input directory does not exist!");
Utilities.PrintProgramUsage();
return;
}

//validate the output path
if (!Directory.Exists(outputPath))
{
Console.WriteLine("Output directory does not exist!");
Utilities.PrintProgramUsage();
return;
}

//declare and initalize a dynamic array of dat files in the input directory.
List<string> datFiles = new List<string>();

//get all the files in the input directory
string[] inputDirectoryFiles = Directory.GetFiles(inputPath);

//iterate through each file
foreach (string inputDirectoryFile in inputDirectoryFiles)
{
//if we find a file with a .dat extension
if (Path.GetExtension(inputDirectoryFile) == ".dat")
{
//add it to the list
Console.WriteLine("Found {0}", inputDirectoryFile);
datFiles.Add(inputDirectoryFile);
}
}

//let the user know what we found.
Console.WriteLine("Found {0} files, commencing extraction...", datFiles.Count);

//iterate through each dat file in the directory.
foreach(string datFile in datFiles)
{
Console.WriteLine("------------------------------------------------------");
Console.WriteLine("Extracting {0}", Path.GetFileName(datFile));
ExtractDatFile(datFile);
Console.WriteLine("Finished extracting {0}", Path.GetFileName(datFile));
Console.WriteLine("------------------------------------------------------");
}

Console.WriteLine("Finished, press a key to end.", datFiles.Count);
Console.ReadKey();
}

/// <summary>
/// Extracts a single .dat file, and creates a directory with the dat file name, and extracts the files into the directory.
/// </summary>
/// <param name="inputPath"></param>
private static void ExtractDatFile(string inputPath)
{
//construct the directory to which the extracted files in the .dat archive will be written to.
string inputFileName = Path.GetFileNameWithoutExtension(inputPath);
string inputDirectory = Path.GetDirectoryName(inputPath);
string extractedDirectory = inputDirectory + "/" + inputFileName;

//make sure it exists, otherwise create it.
if(Directory.Exists(extractedDirectory) == false)
Directory.CreateDirectory(extractedDirectory);

//open the .dat file, and we will read the binary file.
using(BinaryReader reader = new BinaryReader(File.OpenRead(inputPath)))
{
//parse the magic
string magic = Utilities.CombineChars(reader.ReadChars(4));
Console.WriteLine("Magic: {0}", magic);

//parse the total file size
uint fileSize = reader.ReadUInt32(); //total file size
Console.WriteLine("File Size: {0}", fileSize);

//parse the unknown value
uint unknown1 = reader.ReadUInt32(); //unknown1
Console.WriteLine("unknown1: {0}", unknown1);

//parse the amount of files listed in the dat archive
uint filecount = reader.ReadUInt32(); //filecount
Console.WriteLine("filecount: {0}", filecount);

//create an array with the amount of files in the dat archive.
DatFileEntry[] datFileEntries = new DatFileEntry[filecount];

//iterate through the dat file entry table.
for (int i = 0; i < filecount; i++)
{
//create our struct
datFileEntries[i] = new DatFileEntry();

//parse the file data offset in the dat file
Console.WriteLine("-------- Item {0} --------");
datFileEntries[i].FileOffset = reader.ReadUInt32();
Console.WriteLine("FileOffset: {0}", datFileEntries[i].FileOffset);

//parse the unknown value
datFileEntries[i].Unknown = reader.ReadUInt32();
Console.WriteLine("Unknown: {0}", datFileEntries[i].Unknown); //always 0

//parse the size of the file data
datFileEntries[i].FileDataSize = reader.ReadUInt32();
Console.WriteLine("FileDataSize: {0}", datFileEntries[i].FileDataSize);

//parse the file name offset in the dat file
datFileEntries[i].FileNameOffset = reader.ReadUInt32();
Console.WriteLine("FileNameOffset: {0}", datFileEntries[i].FileNameOffset);

Console.WriteLine("-------- Item End --------");
}

//table ends
//now we will iterate through the entires again, but with the offsets that we parsed.
//we will go through the file and set the pointer at the offsets to parse the needed data for the file.

//iterate once again through each of the file entires
for(int i = 0; i < datFileEntries.Length; i++)
{
//use the file name offset and seek to the position in the dat file where the file name string is located and parse it.
reader.BaseStream.Seek(datFileEntries[i].FileNameOffset, SeekOrigin.Begin);
datFileEntries[i].FileName = Utilities.ReadLengthPrefixedString(reader);
Console.WriteLine("[ENTRY {0}] File Name: {1}", i, datFileEntries[i].FileName);

//use the file data offset and seek to the position in the dat file where the file data is located and parse all of the bytes.
reader.BaseStream.Seek(datFileEntries[i].FileOffset, SeekOrigin.Begin);
datFileEntries[i].Data = reader.ReadBytes((int)datFileEntries[i].FileDataSize);
Console.WriteLine("[ENTRY {0}] Data Size: {1}", i, datFileEntries[i].Data.Length);

//construct the new file path and write the binary data for the file into the disk.
string newFilePath = string.Format("{0}/{1}", extractedDirectory, datFileEntries[i].FileName);
File.WriteAllBytes(newFilePath, datFileEntries[i].Data);
}

Console.WriteLine("left off at: {0}", reader.BaseStream.Position);
}
}
}
}
36 changes: 36 additions & 0 deletions dat-extractor/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("dat-extractor")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("HP")]
[assembly: AssemblyProduct("dat-extractor")]
[assembly: AssemblyCopyright("Copyright © HP 2022")]
[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("26ec68a5-551c-4024-83a1-a8b6d5f97371")]

// 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")]
49 changes: 49 additions & 0 deletions dat-extractor/Utilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace dat_extractor
{
public static class Utilities
{
/// <summary>
/// Combines a char array into a single string.
/// </summary>
/// <param name="chars"></param>
/// <returns></returns>
public static string CombineChars(char[] chars)
{
string result = "";

for (int i = 0; i < chars.Length; i++)
{
result += chars[i];
}

return result;
}

/// <summary>
/// Reads a string, which has in binary a uint32 serialized before the actual string itself to indicate length.
/// </summary>
/// <param name="reader"></param>
/// <returns></returns>
public static string ReadLengthPrefixedString(BinaryReader reader)
{
//read the uint32 that is serialized right before the actual string, this is the length of the string.
uint length = reader.ReadUInt32();

//read each char and combine them into a single string.
return CombineChars(reader.ReadChars((int)length));
}

public static void PrintProgramUsage()
{
Console.WriteLine("Program Usage:");
Console.WriteLine("app.exe [input directory] [output directory]");
}
}
}
Binary file added dat-extractor/bin/Debug/dat-extractor.exe
Binary file not shown.
6 changes: 6 additions & 0 deletions dat-extractor/bin/Debug/dat-extractor.exe.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.7.2" />
</startup>
</configuration>
Binary file added dat-extractor/bin/Debug/dat-extractor.pdb
Binary file not shown.
55 changes: 55 additions & 0 deletions dat-extractor/dat-extractor.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" 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>{26EC68A5-551C-4024-83A1-A8B6D5F97371}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>dat_extractor</RootNamespace>
<AssemblyName>dat-extractor</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</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="DatFileEntry.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utilities.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// <autogenerated />
using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0c8e2c9e7669cfd45cf74f44a0fbb64fec83f5a6
Loading

0 comments on commit 157c7c6

Please sign in to comment.