Files
BinaryObjectScanner/BinaryObjectScanner.FileType/MPQ.cs

87 lines
2.7 KiB
C#
Raw Normal View History

using System;
using System.IO;
using BinaryObjectScanner.Interfaces;
#if NET48
using StormLibSharp;
2022-12-22 21:58:26 -08:00
#endif
2023-03-13 21:49:25 -04:00
namespace BinaryObjectScanner.FileType
{
/// <summary>
2022-12-26 12:58:03 -08:00
/// MoPaQ game data archive
/// </summary>
2023-03-09 15:07:35 -05:00
public class MPQ : IExtractable
{
/// <inheritdoc/>
2023-03-09 17:16:39 -05:00
public string Extract(string file, bool includeDebug)
{
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:39:26 -05:00
// TODO: Add stream opening support
/// <inheritdoc/>
2023-03-09 17:16:39 -05:00
public string Extract(Stream stream, string file, bool includeDebug)
{
2023-03-09 14:39:26 -05:00
#if NET6_0_OR_GREATER
// Not supported for .NET 6.0 due to Windows DLL requirements
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
}
}
}