2009-12-31 19:55:23 +00:00
|
|
|
/*
|
|
|
|
OpenMW - The completely unofficial reimplementation of Morrowind
|
|
|
|
Copyright (C) 2008-2010 Nicolay Korslund
|
|
|
|
Email: < korslund@gmail.com >
|
2018-03-08 20:23:24 +00:00
|
|
|
WWW: https://openmw.org/
|
2009-12-31 19:55:23 +00:00
|
|
|
|
|
|
|
This file (bsa_file.h) is part of the OpenMW package.
|
|
|
|
|
|
|
|
OpenMW is distributed as free software: you can redistribute it
|
|
|
|
and/or modify it under the terms of the GNU General Public License
|
|
|
|
version 3, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
version 3 along with this program. If not, see
|
2018-03-08 20:23:24 +00:00
|
|
|
https://www.gnu.org/licenses/ .
|
2009-12-31 19:55:23 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2011-06-19 14:42:35 +00:00
|
|
|
#ifndef BSA_BSA_FILE_H
|
|
|
|
#define BSA_BSA_FILE_H
|
2009-12-31 19:55:23 +00:00
|
|
|
|
2020-10-22 20:39:59 +00:00
|
|
|
#include <cstdint>
|
2022-06-19 11:28:33 +00:00
|
|
|
#include <filesystem>
|
2010-01-02 10:50:35 +00:00
|
|
|
#include <string>
|
2009-12-31 19:55:23 +00:00
|
|
|
#include <vector>
|
2012-07-15 14:41:19 +00:00
|
|
|
|
2022-07-02 22:02:29 +00:00
|
|
|
#include <components/files/conversion.hpp>
|
2022-07-16 16:19:56 +00:00
|
|
|
#include <components/files/istreamptr.hpp>
|
2012-07-15 14:41:19 +00:00
|
|
|
|
2011-06-19 14:42:35 +00:00
|
|
|
namespace Bsa
|
|
|
|
{
|
|
|
|
|
2024-01-30 00:29:38 +00:00
|
|
|
enum class BsaVersion : std::uint32_t
|
2023-03-02 15:49:09 +00:00
|
|
|
{
|
2024-01-30 00:29:38 +00:00
|
|
|
Unknown = 0x0,
|
|
|
|
Uncompressed = 0x100,
|
|
|
|
Compressed = 0x415342, // B, S, A,
|
|
|
|
BA2GNRL, // used by FO4, BSA which contains files
|
|
|
|
BA2DX10 // used by FO4, BSA which contains textures
|
2023-03-02 15:49:09 +00:00
|
|
|
};
|
|
|
|
|
2009-12-31 19:55:23 +00:00
|
|
|
/**
|
|
|
|
This class is used to read "Bethesda Archive Files", or BSAs.
|
|
|
|
*/
|
2012-07-15 14:41:19 +00:00
|
|
|
class BSAFile
|
|
|
|
{
|
2022-09-22 18:26:05 +00:00
|
|
|
public:
|
2021-04-07 16:58:46 +00:00
|
|
|
#pragma pack(push)
|
|
|
|
#pragma pack(1)
|
|
|
|
struct Hash
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2021-04-07 16:58:46 +00:00
|
|
|
uint32_t low, high;
|
2022-09-22 18:26:05 +00:00
|
|
|
};
|
2021-04-07 16:58:46 +00:00
|
|
|
#pragma pack(pop)
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2012-07-15 14:41:19 +00:00
|
|
|
/// Represents one file entry in the archive
|
|
|
|
struct FileStruct
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2021-04-07 16:58:46 +00:00
|
|
|
void setNameInfos(size_t index, std::vector<char>* stringBuf)
|
|
|
|
{
|
2021-05-02 06:43:44 +00:00
|
|
|
namesOffset = static_cast<uint32_t>(index);
|
2021-04-07 16:58:46 +00:00
|
|
|
namesBuffer = stringBuf;
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2012-07-15 14:41:19 +00:00
|
|
|
// File size and offset in file. We store the offset from the
|
|
|
|
// beginning of the file, not the offset into the data buffer
|
|
|
|
// (which is what is stored in the archive.)
|
|
|
|
uint32_t fileSize, offset;
|
2021-04-07 16:58:46 +00:00
|
|
|
Hash hash;
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2012-07-15 14:41:19 +00:00
|
|
|
// Zero-terminated file name
|
2022-10-05 21:45:17 +00:00
|
|
|
const char* name() const { return &(*namesBuffer)[namesOffset]; }
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2021-04-07 16:58:46 +00:00
|
|
|
uint32_t namesOffset = 0;
|
|
|
|
std::vector<char>* namesBuffer = nullptr;
|
2022-09-22 18:26:05 +00:00
|
|
|
};
|
2012-07-15 14:41:19 +00:00
|
|
|
typedef std::vector<FileStruct> FileList;
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2019-01-16 19:19:15 +00:00
|
|
|
protected:
|
2021-04-07 16:58:46 +00:00
|
|
|
bool mHasChanged = false;
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2012-07-15 14:41:19 +00:00
|
|
|
/// Table of files in this archive
|
2019-01-16 19:19:15 +00:00
|
|
|
FileList mFiles;
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2012-07-15 14:41:19 +00:00
|
|
|
/// Filename string buffer
|
2019-01-16 19:19:15 +00:00
|
|
|
std::vector<char> mStringBuf;
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2012-07-15 14:41:19 +00:00
|
|
|
/// True when an archive has been loaded
|
2019-01-16 19:19:15 +00:00
|
|
|
bool mIsLoaded;
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2012-07-15 14:41:19 +00:00
|
|
|
/// Used for error messages
|
2022-06-19 11:28:33 +00:00
|
|
|
std::filesystem::path mFilepath;
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2012-07-15 14:41:19 +00:00
|
|
|
/// Error handling
|
2022-09-10 17:38:29 +00:00
|
|
|
[[noreturn]] void fail(const std::string& msg) const;
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2012-07-15 14:41:19 +00:00
|
|
|
/// Read header information from the input source
|
2019-01-16 19:19:15 +00:00
|
|
|
virtual void readHeader();
|
2021-04-07 16:58:46 +00:00
|
|
|
virtual void writeHeader();
|
2022-09-22 18:26:05 +00:00
|
|
|
|
|
|
|
public:
|
2012-07-15 14:41:19 +00:00
|
|
|
/* -----------------------------------
|
|
|
|
* BSA management methods
|
|
|
|
* -----------------------------------
|
2022-09-22 18:26:05 +00:00
|
|
|
*/
|
|
|
|
|
2012-07-15 14:41:19 +00:00
|
|
|
BSAFile()
|
2019-01-16 19:19:15 +00:00
|
|
|
: mIsLoaded(false)
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2021-04-07 16:58:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~BSAFile()
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2019-01-16 19:19:15 +00:00
|
|
|
close();
|
|
|
|
}
|
2012-07-15 14:41:19 +00:00
|
|
|
|
|
|
|
/// Open an archive file.
|
2022-06-19 11:28:33 +00:00
|
|
|
void open(const std::filesystem::path& file);
|
2012-07-15 14:41:19 +00:00
|
|
|
|
2021-04-07 16:58:46 +00:00
|
|
|
void close();
|
|
|
|
|
2012-07-15 14:41:19 +00:00
|
|
|
/* -----------------------------------
|
|
|
|
* Archive file routines
|
|
|
|
* -----------------------------------
|
|
|
|
*/
|
|
|
|
|
2016-02-08 14:55:05 +00:00
|
|
|
/** Open a file contained in the archive.
|
|
|
|
* @note Thread safe.
|
|
|
|
*/
|
2022-07-16 16:19:56 +00:00
|
|
|
Files::IStreamPtr getFile(const FileStruct* file);
|
2015-03-17 20:59:39 +00:00
|
|
|
|
2022-02-26 14:18:36 +00:00
|
|
|
void addFile(const std::string& filename, std::istream& file);
|
2021-04-07 16:58:46 +00:00
|
|
|
|
2012-07-15 14:41:19 +00:00
|
|
|
/// Get a list of all files
|
2016-02-08 14:55:05 +00:00
|
|
|
/// @note Thread safe.
|
2012-07-15 14:41:19 +00:00
|
|
|
const FileList& getList() const
|
2019-01-16 19:19:15 +00:00
|
|
|
{
|
|
|
|
return mFiles;
|
|
|
|
}
|
2020-12-29 20:45:59 +00:00
|
|
|
|
2022-06-19 11:28:33 +00:00
|
|
|
std::string getFilename() const
|
2020-12-29 20:45:59 +00:00
|
|
|
{
|
2022-07-02 22:02:29 +00:00
|
|
|
return Files::pathToUnicodeString(mFilepath);
|
2020-12-29 20:45:59 +00:00
|
|
|
}
|
2023-03-02 15:49:09 +00:00
|
|
|
|
|
|
|
// checks version of BSA from file header
|
|
|
|
static BsaVersion detectVersion(const std::filesystem::path& filePath);
|
2009-12-31 19:55:23 +00:00
|
|
|
};
|
|
|
|
|
2011-06-19 14:42:35 +00:00
|
|
|
}
|
|
|
|
|
2009-12-31 19:55:23 +00:00
|
|
|
#endif
|