aboutsummaryrefslogtreecommitdiff
path: root/libs/bsapacker/src/qlibbsarch
diff options
context:
space:
mode:
Diffstat (limited to 'libs/bsapacker/src/qlibbsarch')
-rw-r--r--libs/bsapacker/src/qlibbsarch/BSArchive.h38
-rw-r--r--libs/bsapacker/src/qlibbsarch/BSArchiveAuto.h91
-rw-r--r--libs/bsapacker/src/qlibbsarch/BSArchiveEntries.h23
-rw-r--r--libs/bsapacker/src/qlibbsarch/QLibbsarch.h42
4 files changed, 194 insertions, 0 deletions
diff --git a/libs/bsapacker/src/qlibbsarch/BSArchive.h b/libs/bsapacker/src/qlibbsarch/BSArchive.h
new file mode 100644
index 0000000..a159ffd
--- /dev/null
+++ b/libs/bsapacker/src/qlibbsarch/BSArchive.h
@@ -0,0 +1,38 @@
+#pragma once
+
+#include <qlibbsarch/QLibbsarch.h>
+#include "BSArchiveEntries.h"
+#include <QDataStream>
+
+class BSArchive
+{
+public:
+ BSArchive();
+ virtual ~BSArchive();
+
+ void open(const QString& archivePath);
+ void close();
+ void create(const QString &archiveName, const bsa_archive_type_e &type, const BSArchiveEntries &entries);
+ void save();
+ void addFileFromDiskRoot(const QString &rootDir, const QString &filename);
+ void addFileFromDiskRoot(const QString &rootDir, const QStringList &files);
+ void addFileFromDisk(const QString& pathInArchive, const QString& filePath);
+ void addFileFromMemory(const QString& filename, const QByteArray& data);
+ void setCompressed(bool value);
+ void setShareData(bool value);
+ bsa_file_record_t findFileRecord (const QString& filename);
+ QByteArray extractFileDataByRecord(bsa_file_record_t record);
+ QByteArray extractFileDataByFilename(const QString& filename);
+ void extract(const QString& filename, const QString& saveAs);
+ QStringList listFiles();
+ void free();
+ void reset();
+
+ void setDDSCallback(bsa_file_dds_info_proc_t fileDDSInfoProc, void *context);
+
+ bsa_archive_t getArchive() const;
+
+private:
+ bool _openedArchive = false;
+ bsa_archive_t _archive;
+};
diff --git a/libs/bsapacker/src/qlibbsarch/BSArchiveAuto.h b/libs/bsapacker/src/qlibbsarch/BSArchiveAuto.h
new file mode 100644
index 0000000..d632fa4
--- /dev/null
+++ b/libs/bsapacker/src/qlibbsarch/BSArchiveAuto.h
@@ -0,0 +1,91 @@
+#pragma once
+
+#include "BSArchive.h"
+#include "BSArchiveEntries.h"
+#include <QMap>
+
+/*!
+ * \brief A convenience class for BSArchive and BSArchiveEntries. Its performance is worse than using these
+ * two classes separately, but it removes the need to manually handle the BSArchiveEntries.
+ */
+class BSArchiveAuto
+{
+public:
+ /*!
+ * \brief Constructor
+ * \param rootDirectory The root directory of the BSA. This directory is the one containing folders such as textures and meshes.
+ */
+ BSArchiveAuto(const QString &_rootDirectory);
+ /*!
+ * \brief Opens an existing archive
+ * \param archivePath The path of the archive
+ */
+ void open(const QString &archivePath);
+ /*!
+ * \brief Creates a BSA in memory
+ * \param archiveName The BSA name
+ * \param type The BSA type
+ */
+ void create(const QString& archiveName, const bsa_archive_type_e& type);
+ /*!
+ * \brief Adds a file from the disk to the BSA. Also adds it to the BSA entries.
+ * \param rootDir The root directory of the file.
+ * \param filename The complete path to the file.
+ */
+ void addFileFromDiskRoot(const QString& filename);
+ /*!
+ * \brief Adds files from the disk to the BSA. Also adds them to the BSA entries.
+ * \param rootDir The root directory of the file. This directory is the one containing folders such as textures and meshes.
+ * \param files A list containing the complete paths to the files.
+ */
+ void addFileFromDiskRoot(const QStringList& files);
+ /*!
+ * \brief Adds file from the memory to the BSA. Also adds it to the BSA entries.
+ * \param filename The name of the file. It connot be a full path, and has to be a relative path.
+ * \param data The file bytes data.
+ */
+ void addFileFromMemory(const QString& filename, const QByteArray& data);
+ /*!
+ * \brief Adds a file from the disk to the BSA. Also adds it to the BSA entries.
+ * \param saveAs The path of the file in the BSA
+ * \param diskPath The path of the file on disk
+ */
+ void addFileFromDisk(const QString& saveAs, const QString& diskPath);
+ /*!
+ * \brief Adds files from the disk to the BSA. Also adds it to the BSA entries.
+ * \param map A map containing "save as" path as keys and disk path as values
+ */
+ void addFileFromDisk(const QMap<QString, QString>& map);
+ /*!
+ * \brief Extracts all files from the BSA to the destination.
+ * \param destinationDirectory The directory where all files will be extracted.
+ * \param overwriteExistingFiles Whether files in archive will overwrite existing loose files or not
+ */
+ void extractAll(const QString &destinationDirectory, const bool &overwriteExistingFiles);
+ /*!
+ * \brief Saves the archive to the disk
+ */
+ void save();
+
+ void setShareData(const bool state);
+ void setCompressed(const bool state);
+
+ void reset();
+ /*!
+ * \brief Required in order to create FO4 DDS archive
+ * \param fileDDSInfoProc The function that will process the DDS file
+ */
+ void setDDSCallback(bsa_file_dds_info_proc_t fileDDSInfoProc, void *context);
+
+private:
+ /*!
+ * \brief The key will store the "save as" path, while the value will hold the disk path
+ */
+ QMap<QString, QString> _filesFromDisk;
+ QStringList _filesFromDiskRoot;
+ QMap<QString, QByteArray> _filesfromMemory;
+ QDir _rootDirectory;
+
+ BSArchive _archive;
+ BSArchiveEntries _entries;
+};
diff --git a/libs/bsapacker/src/qlibbsarch/BSArchiveEntries.h b/libs/bsapacker/src/qlibbsarch/BSArchiveEntries.h
new file mode 100644
index 0000000..3f3cb63
--- /dev/null
+++ b/libs/bsapacker/src/qlibbsarch/BSArchiveEntries.h
@@ -0,0 +1,23 @@
+#pragma once
+
+#include "QLibbsarch.h"
+
+class BSArchiveEntries
+{
+public:
+ BSArchiveEntries();
+ explicit BSArchiveEntries(const QStringList& QSLEntries);
+ explicit BSArchiveEntries(const bsa_entry_list_t& entries);
+ ~BSArchiveEntries();
+
+ void add(const QString& filepath);
+ void free();
+ void reset();
+ uint32_t count() ;
+ QStringList list();
+
+ bsa_entry_list_t getEntries() const;
+
+private:
+ bsa_entry_list_t _entries;
+};
diff --git a/libs/bsapacker/src/qlibbsarch/QLibbsarch.h b/libs/bsapacker/src/qlibbsarch/QLibbsarch.h
new file mode 100644
index 0000000..7af53f4
--- /dev/null
+++ b/libs/bsapacker/src/qlibbsarch/QLibbsarch.h
@@ -0,0 +1,42 @@
+#pragma once
+
+#include <libbsarch/libbsarch.h>
+#include <string>
+#include <stdexcept>
+#include <QDebug>
+#include <QDir>
+#include <QStringList>
+
+namespace QLibBsarch
+{
+ constexpr bool enableDebugLog = true;
+
+#define LOG_LIBBSARCH \
+ if constexpr (QLibBsarch::enableDebugLog) \
+ qDebug() << "[QLIBBSARCH] " << __FUNCTION__ << ' '
+
+#define PREPARE_PATH_LIBBSARCH(qstring) reinterpret_cast<const wchar_t *>(QDir::toNativeSeparators(qstring).utf16())
+
+ inline const std::string wcharToString(const wchar_t *text) { return QString::fromWCharArray(text).toStdString(); }
+
+ inline void checkResult(const bsa_result_message_s &result)
+ {
+ if (result.code == BSA_RESULT_EXCEPTION)
+ {
+ const std::string &error = QLibBsarch::wcharToString(result.text);
+ LOG_LIBBSARCH << QString::fromStdString(error);
+ throw std::runtime_error(error);
+ }
+ }
+
+ inline void checkResult(const bsa_result_message_buffer_s &result)
+ {
+ if (result.message.code == BSA_RESULT_EXCEPTION)
+ {
+ const std::string &error = QLibBsarch::wcharToString(result.message.text);
+ LOG_LIBBSARCH << QString::fromStdString(error);
+ throw std::runtime_error(error);
+ }
+ }
+
+} // namespace QLibBsarch