You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
apphost-extract/src/apphost-extract/apphost-extract/AppHostFile.cs

81 lines
2.1 KiB

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace apphost_extract
{
public class AppHostFile
{
private FileStream FileStream;
public AppHostFileHeader Header { get; set; }
private const int HEADER_OFFSET_PTR = 0x27600;
public AppHostFile(FileStream fileStream)
{
FileStream = fileStream;
var headerVA = GetHeaderAddress(HEADER_OFFSET_PTR);
Header = new AppHostFileHeader(FileStream, headerVA);
}
public int GetHeaderAddress(int offset)
{
var buffer = new byte[16];
FileStream.Seek(offset, SeekOrigin.Begin);
FileStream.Read(buffer, 0, buffer.Length);
return BitConverter.ToInt32(buffer, 0);
}
public static AppHostFile Open(string path)
{
try
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
AppHostFile file = new AppHostFile(fs);
Log.Info("File opened successfully!");
return file;
}
catch(Exception ex)
{
Log.Fatal($"Exception when trying to open file: {ex.Message}");
return null;
}
}
public void ExtractAll(string outputDir)
{
Directory.CreateDirectory(outputDir);
foreach (var fileEntry in Header.Manifest.FileEntries)
{
try
{
var bytes = fileEntry.Read();
var name = fileEntry.Name;
var filePath = Path.Combine(outputDir, name);
File.WriteAllBytes(filePath, bytes);
Log.Critical($"Extracted {name}");
}
catch (Exception ex)
{
Log.Error($"Could not extract {fileEntry.Name}: {ex.Message}");
}
}
}
public void Close()
{
FileStream.Close();
}
}
}