wabbajack/Compression.BSA/BSADispatch.cs

37 lines
1.3 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
2020-03-23 12:57:18 +00:00
using Wabbajack.Common;
namespace Compression.BSA
{
public static class BSADispatch
{
public static async ValueTask<IBSAReader> OpenRead(AbsolutePath filename)
{
2019-11-16 00:01:37 +00:00
var fourcc = "";
using (var file = await filename.OpenRead())
{
2019-11-16 00:01:37 +00:00
fourcc = Encoding.ASCII.GetString(new BinaryReader(file).ReadBytes(4));
}
if (fourcc == TES3Reader.TES3_MAGIC)
return await TES3Reader.Load(filename);
2019-11-16 00:01:37 +00:00
if (fourcc == "BSA\0")
2020-06-26 19:04:16 +00:00
return await BSAReader.LoadWithRetry(filename);
2019-11-16 00:01:37 +00:00
if (fourcc == "BTDX")
return await BA2Reader.Load(filename);
2019-11-16 00:01:37 +00:00
throw new InvalidDataException("Filename is not a .bsa or .ba2, magic " + fourcc);
}
private static HashSet<string> MagicStrings = new HashSet<string> {TES3Reader.TES3_MAGIC, "BSA\0", "BTDX"};
public static async ValueTask<bool> MightBeBSA(AbsolutePath filename)
{
using var file = await filename.OpenRead();
var fourcc = Encoding.ASCII.GetString(new BinaryReader(file).ReadBytes(4));
return MagicStrings.Contains(fourcc);
}
}
}