111 lines
5.3 KiB
C#
111 lines
5.3 KiB
C#
using Kaitai;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Text;
|
|
|
|
namespace TSUnpack {
|
|
public static class IO {
|
|
/// <summary>
|
|
/// Read a Microsoft Train Simulator Packaged Activity file.
|
|
/// </summary>
|
|
/// <param name="path">Path to the APK file to read.</param>
|
|
/// <returns>A parsed Packaged Activity.</returns>
|
|
public static MstsApk Read(string path) {
|
|
return Read(File.OpenRead(path));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Read a Microsoft Train Simulator Packaged Activity file.
|
|
/// </summary>
|
|
/// <param name="stream">Readable stream for a valid Packaged Activity file.</param>
|
|
/// <returns>A parsed Packaged Activity.</returns>
|
|
public static MstsApk Read(Stream stream) {
|
|
return new MstsApk(new KaitaiStream(new GZipStream(stream, CompressionMode.Decompress)));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Unpack a Microsoft Train Simulator Packaged Activity to a directory.
|
|
/// </summary>
|
|
/// <param name="path">Path to the APK file to unpack.</param>
|
|
/// <param name="destination">Root directory under which files will be unpacked.</param>
|
|
public static void Unpack(string path, string destination) {
|
|
Unpack(path, destination, false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Unpack a Microsoft Train Simulator Packaged Activity to a directory.
|
|
/// </summary>
|
|
/// <param name="path">Path to the APK file to unpack.</param>
|
|
/// <param name="destination">Root directory under which files will be unpacked.</param>
|
|
/// <param name="overwrite">Whether or not to overwrite existing files.</param>
|
|
public static void Unpack(string path, string destination, bool overwrite) {
|
|
Unpack(File.OpenRead(path), destination, overwrite);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Unpack a Microsoft Train Simulator Packaged Activity to a directory.
|
|
/// </summary>
|
|
/// <param name="stream">Readable stream for a valid Packaged Activity file.</param>
|
|
/// <param name="destination">Root directory under which files will be unpacked.</param>
|
|
public static void Unpack(Stream stream, string destination) {
|
|
Unpack(stream, destination, false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Unpack a Microsoft Train Simulator Packaged Activity to a directory.
|
|
/// </summary>
|
|
/// <param name="stream">Readable stream for a valid Packaged Activity file.</param>
|
|
/// <param name="destination">Root directory under which files will be unpacked.</param>
|
|
/// <param name="overwrite">Whether or not to overwrite existing files.</param>
|
|
public static void Unpack(Stream stream, string destination, bool overwrite) {
|
|
Unpack(Read(stream), destination, overwrite);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Unpack a Microsoft Train Simulator Packaged Activity to a directory.
|
|
/// </summary>
|
|
/// <param name="apk">A parsed Package Activity to unpack.</param>
|
|
/// <param name="destination">Root directory under which files will be unpacked.</param>
|
|
public static void Unpack(MstsApk apk, string destination) {
|
|
Unpack(apk, destination, false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Unpack a Microsoft Train Simulator Packaged Activity to a directory.
|
|
/// </summary>
|
|
/// <param name="apk">A parsed Package Activity to unpack.</param>
|
|
/// <param name="destination">Root directory under which files will be unpacked.</param>
|
|
/// <param name="overwrite">Whether or not to overwrite existing files.</param>
|
|
public static void Unpack(MstsApk apk, string destination, bool overwrite) {
|
|
foreach (MstsApk.File apkFile in apk.Files) {
|
|
Unpack(apkFile, destination, overwrite);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Unpack a file from a Microsoft Train Simulator Packaged Activity to a directory.
|
|
/// </summary>
|
|
/// <param name="apk">A file from a parsed Package Activity to unpack.</param>
|
|
/// <param name="destination">Root directory under which the file will be unpacked.</param>
|
|
public static void Unpack(MstsApk.File apkFile, string destination) {
|
|
Unpack(apkFile, destination, false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Unpack a file from a Microsoft Train Simulator Packaged Activity to a directory.
|
|
/// </summary>
|
|
/// <param name="apk">A file from a parsed Package Activity to unpack.</param>
|
|
/// <param name="destination">Root directory under which the file will be unpacked.</param>
|
|
/// <param name="overwrite">Whether or not to overwrite an existing file.</param>
|
|
public static void Unpack(MstsApk.File apkFile, string destination, bool overwrite) {
|
|
string path = Path.Combine(destination, apkFile.Name.Value.Replace('\\', Path.DirectorySeparatorChar));
|
|
if (!overwrite && File.Exists(path))
|
|
throw new IOException(string.Format("File {0} already exists.", path), unchecked((int)0x80070050));
|
|
string dir = Path.GetDirectoryName(path);
|
|
if (!Directory.Exists(dir))
|
|
Directory.CreateDirectory(dir);
|
|
File.WriteAllBytes(path, Encoding.Unicode.GetBytes(apkFile.Content));
|
|
}
|
|
}
|
|
}
|