2020-09-10 21:10:32 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
2023-03-09 14:04:31 -05:00
|
|
|
|
using BinaryObjectScanner.Interfaces;
|
2022-12-29 21:06:14 -08:00
|
|
|
|
#if NET48
|
2020-09-10 21:10:32 -07:00
|
|
|
|
using StormLibSharp;
|
2022-12-22 21:58:26 -08:00
|
|
|
|
#endif
|
2020-09-10 21:10:32 -07:00
|
|
|
|
|
2023-03-13 21:49:25 -04:00
|
|
|
|
namespace BinaryObjectScanner.FileType
|
2020-09-10 21:10:32 -07:00
|
|
|
|
{
|
2022-12-08 21:32:52 -08:00
|
|
|
|
/// <summary>
|
2022-12-26 12:58:03 -08:00
|
|
|
|
/// MoPaQ game data archive
|
2022-12-08 21:32:52 -08:00
|
|
|
|
/// </summary>
|
2023-03-09 15:07:35 -05:00
|
|
|
|
public class MPQ : IExtractable
|
2020-09-10 21:10:32 -07:00
|
|
|
|
{
|
2023-03-09 14:04:31 -05:00
|
|
|
|
/// <inheritdoc/>
|
2023-03-09 17:16:39 -05:00
|
|
|
|
public string Extract(string file, bool includeDebug)
|
2023-03-09 14:04:31 -05:00
|
|
|
|
{
|
|
|
|
|
|
if (!File.Exists(file))
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read))
|
|
|
|
|
|
{
|
2023-03-09 17:16:39 -05:00
|
|
|
|
return Extract(fs, file, includeDebug);
|
2023-03-09 14:04:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-09 14:39:26 -05:00
|
|
|
|
// TODO: Add stream opening support
|
2023-03-09 14:04:31 -05:00
|
|
|
|
/// <inheritdoc/>
|
2023-03-09 17:16:39 -05:00
|
|
|
|
public string Extract(Stream stream, string file, bool includeDebug)
|
2023-03-09 14:04:31 -05:00
|
|
|
|
{
|
2023-03-09 14:39:26 -05:00
|
|
|
|
#if NET6_0_OR_GREATER
|
|
|
|
|
|
// Not supported for .NET 6.0 due to Windows DLL requirements
|
2023-03-09 14:04:31 -05:00
|
|
|
|
return null;
|
2023-03-09 14:39:26 -05:00
|
|
|
|
#else
|
2023-03-09 17:16:39 -05:00
|
|
|
|
try
|
2023-03-09 14:39:26 -05:00
|
|
|
|
{
|
2023-03-09 17:16:39 -05:00
|
|
|
|
// Create a temp output directory
|
|
|
|
|
|
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
|
|
|
|
|
|
Directory.CreateDirectory(tempPath);
|
2023-03-09 14:39:26 -05:00
|
|
|
|
|
2023-03-09 17:16:39 -05:00
|
|
|
|
using (MpqArchive mpqArchive = new MpqArchive(file, FileAccess.Read))
|
2023-03-09 14:39:26 -05:00
|
|
|
|
{
|
2023-03-09 17:16:39 -05:00
|
|
|
|
// Try to open the listfile
|
|
|
|
|
|
string listfile = null;
|
|
|
|
|
|
MpqFileStream listStream = mpqArchive.OpenFile("(listfile)");
|
2023-03-09 14:39:26 -05:00
|
|
|
|
|
2023-03-09 17:16:39 -05:00
|
|
|
|
// If we can't read the listfile, we just return
|
|
|
|
|
|
if (!listStream.CanRead)
|
|
|
|
|
|
return null;
|
2023-03-09 14:39:26 -05:00
|
|
|
|
|
2023-03-09 17:16:39 -05:00
|
|
|
|
// Read the listfile in for processing
|
|
|
|
|
|
using (StreamReader sr = new StreamReader(listStream))
|
|
|
|
|
|
{
|
|
|
|
|
|
listfile = sr.ReadToEnd();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Split the listfile by newlines
|
|
|
|
|
|
string[] listfileLines = listfile.Replace("\r\n", "\n").Split('\n');
|
|
|
|
|
|
|
|
|
|
|
|
// Loop over each entry
|
|
|
|
|
|
foreach (string sub in listfileLines)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
string tempFile = Path.Combine(tempPath, sub);
|
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(tempFile));
|
|
|
|
|
|
mpqArchive.ExtractFile(sub, tempFile);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (includeDebug) Console.WriteLine(ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-03-09 14:39:26 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-09 17:16:39 -05:00
|
|
|
|
return tempPath;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (includeDebug) Console.WriteLine(ex);
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2022-12-22 21:58:26 -08:00
|
|
|
|
#endif
|
2020-09-10 21:10:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|