aboutsummaryrefslogtreecommitdiff
path: root/libs/bsapacker/src/ArchiveBuilderHelper.cpp
blob: c5c956ba0b561d65c04e347016e9113c497b4760 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <bsapacker/ArchiveBuilderHelper.h>

#include <algorithm>
#include <ranges>

#include "SettingsService.h"

#include <QDebug>

using std::filesystem::path;
using std::filesystem::directory_entry;
using std::filesystem::directory_iterator;
using std::filesystem::recursive_directory_iterator;

namespace BsaPacker
{
	const std::set<std::string> ArchiveBuilderHelper::INCOMPRESSIBLE_TYPES = { ".wav", ".ogg", ".mp3" };

	ArchiveBuilderHelper::ArchiveBuilderHelper(const ISettingsService* settingsService)
		: m_SettingsService(settingsService)
	{
	}
	uint32_t ArchiveBuilderHelper::getFileCount(const path& rootDirectory) const
	{
		uint32_t count = 0;
		for(auto& p : recursive_directory_iterator(rootDirectory)) {
			if (p.is_regular_file()) {
				count++;
			}
		}
		return count;
	}

	std::vector<path::string_type> ArchiveBuilderHelper::getRootDirectoryFilenames(const path& rootDirectory) const
	{
		std::vector<path::string_type> filenames;
		for (const auto& entry : directory_iterator(rootDirectory)) {
			filenames.push_back(entry.path().filename().native());
		}
		return filenames;
	}

	bool ArchiveBuilderHelper::isFileIgnorable(const path& filepath, const std::vector<path::string_type>& rootDirFilenames) const
	{
		return this->doesPathContainFiles(filepath, rootDirFilenames) || // ignore files within mod directory
			this->isExtensionBlacklisted(filepath); // ignore user blacklisted file types
	}

	bool ArchiveBuilderHelper::isIncompressible(const path& filename) const
	{
		if (!this->m_SettingsService->GetPluginSetting(SettingsService::SETTING_COMPRESS_ARCHIVES).toBool()) {
			return true;
		}

		const auto& extension = filename.extension().string();
		const auto& count = ArchiveBuilderHelper::INCOMPRESSIBLE_TYPES.count(extension);
		const auto& result = count > 0;
		return result;
	}

	bool ArchiveBuilderHelper::isExtensionBlacklisted(const path& filepath) const
	{
		const auto& setting = this->m_SettingsService->GetPluginSetting(SettingsService::SETTING_BLACKLISTED_FILES).toString().toStdString();
		const auto &extension = filepath.extension().string();
		const auto count = std::ranges::count(
				setting | std::views::split(' '),
				extension,
				[](auto r)
				{ return std::string_view(r.data(), r.size()); });
		return count > 0;
	}

	bool ArchiveBuilderHelper::doesPathContainFiles(const path& filepath, const std::vector<path::string_type>& files) const
	{
		return std::find(files.begin(), files.end(), filepath.filename()) != files.end();
	}
} // namespace BsaPacker