wabbajack/Wabbajack.Compression.BSA/BSADispatch.cs

64 lines
2.4 KiB
C#
Raw Normal View History

2021-09-27 12:42:46 +00:00
using System;
using System.IO;
using System.Threading.Tasks;
using Wabbajack.Common;
using Wabbajack.Common.FileSignatures;
using Wabbajack.Compression.BSA.Interfaces;
using Wabbajack.Compression.BSA.TES3Archive;
using Wabbajack.DTOs.BSA.ArchiveStates;
using Wabbajack.DTOs.Streams;
using Wabbajack.Paths;
using Wabbajack.Paths.IO;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.Compression.BSA;
public static class BSADispatch
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
private static readonly SignatureChecker BSASignatures = new(FileType.BSA, FileType.BA2, FileType.TES3);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public static async ValueTask<IReader> Open(AbsolutePath filename)
{
return await BSASignatures.MatchesAsync(filename) switch
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
FileType.TES3 => await Reader.Load(new NativeFileStreamFactory(filename)),
FileType.BSA => await TES5Archive.Reader.Load(new NativeFileStreamFactory(filename)),
FileType.BA2 => await BA2Archive.Reader.Load(new NativeFileStreamFactory(filename)),
2021-10-23 16:51:17 +00:00
_ => throw new InvalidDataException("Filename is not a .bsa or .ba2")
};
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public static async ValueTask<IReader> Open(IStreamFactory factory)
{
await using var stream = await factory.GetStream();
return await BSASignatures.MatchesAsync(stream) switch
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
FileType.TES3 => await Reader.Load(factory),
FileType.BSA => await TES5Archive.Reader.Load(factory),
FileType.BA2 => await BA2Archive.Reader.Load(factory),
2021-10-23 16:51:17 +00:00
_ => throw new InvalidDataException("Filename is not a .bsa or .ba2")
};
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public static async ValueTask<IReader> Open(IStreamFactory factory, FileType sig)
{
await using var stream = await factory.GetStream();
return sig switch
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
FileType.TES3 => await Reader.Load(factory),
FileType.BSA => await TES5Archive.Reader.Load(factory),
FileType.BA2 => await BA2Archive.Reader.Load(factory),
2021-10-23 16:51:17 +00:00
_ => throw new InvalidDataException("Filename is not a .bsa or .ba2")
};
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public static IBuilder CreateBuilder(IArchive oldState, TemporaryFileManager manager)
{
return oldState switch
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
TES3State tes3 => new Builder(tes3),
BSAState bsa => TES5Archive.Builder.Create(bsa, manager),
BA2State ba2 => BA2Archive.Builder.Create(ba2, manager),
2021-10-23 16:51:17 +00:00
_ => throw new NotImplementedException()
};
2021-09-27 12:42:46 +00:00
}
}