2020-05-11 12:52:23 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2019-10-06 21:58:36 +00:00
|
|
|
|
using System.Text;
|
2020-05-25 16:24:16 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2020-03-23 12:57:18 +00:00
|
|
|
|
using Wabbajack.Common;
|
2019-10-06 21:58:36 +00:00
|
|
|
|
|
2020-06-27 17:04:59 +00:00
|
|
|
|
namespace Compression.BSA
|
2019-10-06 21:58:36 +00:00
|
|
|
|
{
|
|
|
|
|
public static class BSADispatch
|
|
|
|
|
{
|
2020-05-25 16:24:16 +00:00
|
|
|
|
public static async ValueTask<IBSAReader> OpenRead(AbsolutePath filename)
|
2019-10-06 21:58:36 +00:00
|
|
|
|
{
|
2019-11-16 00:01:37 +00:00
|
|
|
|
var fourcc = "";
|
2020-05-25 16:24:16 +00:00
|
|
|
|
using (var file = await filename.OpenRead())
|
2019-10-06 21:58:36 +00:00
|
|
|
|
{
|
2019-11-16 00:01:37 +00:00
|
|
|
|
fourcc = Encoding.ASCII.GetString(new BinaryReader(file).ReadBytes(4));
|
|
|
|
|
}
|
2019-10-06 21:58:36 +00:00
|
|
|
|
|
2020-02-19 23:50:12 +00:00
|
|
|
|
if (fourcc == TES3Reader.TES3_MAGIC)
|
2020-05-25 16:24:16 +00:00
|
|
|
|
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")
|
2020-05-25 16:24:16 +00:00
|
|
|
|
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);
|
2019-10-06 21:58:36 +00:00
|
|
|
|
}
|
2020-05-11 12:52:23 +00:00
|
|
|
|
|
|
|
|
|
private static HashSet<string> MagicStrings = new HashSet<string> {TES3Reader.TES3_MAGIC, "BSA\0", "BTDX"};
|
2020-05-25 16:24:16 +00:00
|
|
|
|
public static async ValueTask<bool> MightBeBSA(AbsolutePath filename)
|
2020-05-11 12:52:23 +00:00
|
|
|
|
{
|
2020-05-25 16:24:16 +00:00
|
|
|
|
using var file = await filename.OpenRead();
|
2020-05-11 12:52:23 +00:00
|
|
|
|
var fourcc = Encoding.ASCII.GetString(new BinaryReader(file).ReadBytes(4));
|
|
|
|
|
return MagicStrings.Contains(fourcc);
|
|
|
|
|
}
|
2019-10-06 21:58:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|