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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <vector>
#include <boost/program_options.hpp>
#include <components/bsa/ba2dx10file.hpp>
#include <components/bsa/ba2gnrlfile.hpp>
#include <components/bsa/compressedbsafile.hpp>
#include <components/files/configurationmanager.hpp>
#include <components/files/conversion.hpp>
#include <components/misc/strings/algorithm.hpp>
#include <components/misc/strings/conversion.hpp>
#define BSATOOL_VERSION 1.1
// Create local aliases for brevity
namespace bpo = boost::program_options;
struct Arguments
{
std::string mode;
std::filesystem::path filename;
std::filesystem::path extractfile;
std::filesystem::path addfile;
std::filesystem::path outdir;
bool longformat;
bool fullpath;
};
bool parseOptions(int argc, char** argv, Arguments& info)
{
bpo::options_description desc(R"(Inspect and extract files from Bethesda BSA archives
Usages:
bsatool list [-l] archivefile\n
List the files presents in the input archive.
bsatool extract [-f] archivefile [file_to_extract] [output_directory]
Extract a file from the input archive.
bsatool extractall archivefile [output_directory]
Extract all files from the input archive.
bsatool add [-a] archivefile file_to_add
Add a file to the input archive.
bsatool create [-c] archivefile
Create an archive.
Allowed options)");
auto addOption = desc.add_options();
addOption("help,h", "print help message.");
addOption("version,v", "print version information and quit.");
addOption("long,l", "Include extra information in archive listing.");
addOption("full-path,f", "Create directory hierarchy on file extraction (always true for extractall).");
// input-file is hidden and used as a positional argument
bpo::options_description hidden("Hidden Options");
auto addHiddenOption = hidden.add_options();
addHiddenOption("mode,m", bpo::value<std::string>(), "bsatool mode");
addHiddenOption("input-file,i", bpo::value<Files::MaybeQuotedPathContainer>(), "input file");
bpo::positional_options_description p;
p.add("mode", 1).add("input-file", 3);
// there might be a better way to do this
bpo::options_description all;
all.add(desc).add(hidden);
bpo::variables_map variables;
try
{
bpo::parsed_options validOpts = bpo::command_line_parser(argc, argv).options(all).positional(p).run();
bpo::store(validOpts, variables);
}
catch (std::exception& e)
{
std::cout << "ERROR parsing arguments: " << e.what() << "\n\n" << desc << std::endl;
return false;
}
bpo::notify(variables);
if (variables.count("help"))
{
std::cout << desc << std::endl;
return false;
}
if (variables.count("version"))
{
std::cout << "BSATool version " << BSATOOL_VERSION << std::endl;
return false;
}
if (!variables.count("mode"))
{
std::cout << "ERROR: no mode specified!\n\n" << desc << std::endl;
return false;
}
info.mode = variables["mode"].as<std::string>();
if (!(info.mode == "list" || info.mode == "extract" || info.mode == "extractall" || info.mode == "add"
|| info.mode == "create"))
{
std::cout << std::endl << "ERROR: invalid mode \"" << info.mode << "\"\n\n" << desc << std::endl;
return false;
}
if (!variables.count("input-file"))
{
std::cout << "\nERROR: missing BSA archive\n\n" << desc << std::endl;
return false;
}
auto inputFiles = variables["input-file"].as<Files::MaybeQuotedPathContainer>();
info.filename = inputFiles[0].u8string(); // This call to u8string is redundant, but required to build on MSVC 14.26
// due to implementation bugs.
// Default output to the working directory
info.outdir = std::filesystem::current_path();
if (info.mode == "extract")
{
if (inputFiles.size() < 2)
{
std::cout << "\nERROR: file to extract unspecified\n\n" << desc << std::endl;
return false;
}
if (inputFiles.size() > 1)
info.extractfile = inputFiles[1].u8string(); // This call to u8string is redundant, but required to build on
// MSVC 14.26 due to implementation bugs.
if (inputFiles.size() > 2)
info.outdir = inputFiles[2].u8string(); // This call to u8string is redundant, but required to build on
// MSVC 14.26 due to implementation bugs.
}
else if (info.mode == "add")
{
if (inputFiles.empty())
{
std::cout << "\nERROR: file to add unspecified\n\n" << desc << std::endl;
return false;
}
if (inputFiles.size() > 1)
info.addfile = inputFiles[1].u8string(); // This call to u8string is redundant, but required to build on
// MSVC 14.26 due to implementation bugs.
}
else if (inputFiles.size() > 1)
info.outdir = inputFiles[1].u8string(); // This call to u8string is redundant, but required to build on
// MSVC 14.26 due to implementation bugs.
info.longformat = variables.count("long") != 0;
info.fullpath = variables.count("full-path") != 0;
return true;
}
template <typename File>
int list(std::unique_ptr<File>& bsa, Arguments& info)
{
// List all files
const auto& files = bsa->getList();
for (const auto& file : files)
{
if (info.longformat)
{
// Long format
std::ios::fmtflags f(std::cout.flags());
std::cout << std::setw(50) << std::left << file.name();
std::cout << std::setw(8) << std::left << std::dec << file.mFileSize;
std::cout << "@ 0x" << std::hex << file.mOffset << std::endl;
std::cout.flags(f);
}
else
std::cout << file.name() << std::endl;
}
return 0;
}
template <typename File>
int extract(std::unique_ptr<File>& bsa, Arguments& info)
{
auto archivePath = info.extractfile.u8string();
Misc::StringUtils::replaceAll(archivePath, u8"/", u8"\\");
auto extractPath = info.extractfile.u8string();
Misc::StringUtils::replaceAll(extractPath, u8"\\", u8"/");
Files::IStreamPtr stream;
// Get a stream for the file to extract
for (auto it = bsa->getList().rbegin(); it != bsa->getList().rend(); ++it)
{
auto streamPath = Misc::StringUtils::stringToU8String(it->name());
if (Misc::StringUtils::ciEqual(streamPath, archivePath) || Misc::StringUtils::ciEqual(streamPath, extractPath))
{
stream = bsa->getFile(&*it);
break;
}
}
if (!stream)
{
std::cout << "ERROR: file '" << Misc::StringUtils::u8StringToString(archivePath) << "' not found\n";
std::cout << "In archive: " << Files::pathToUnicodeString(info.filename) << std::endl;
return 3;
}
// Get the target path (the path the file will be extracted to)
std::filesystem::path relPath(extractPath);
std::filesystem::path target;
if (info.fullpath)
target = info.outdir / relPath;
else
target = info.outdir / relPath.filename();
// Create the directory hierarchy
std::filesystem::create_directories(target.parent_path());
std::filesystem::file_status s = std::filesystem::status(target.parent_path());
if (!std::filesystem::is_directory(s))
{
std::cout << "ERROR: " << Files::pathToUnicodeString(target.parent_path()) << " is not a directory."
<< std::endl;
return 3;
}
std::ofstream out(target, std::ios::binary);
// Write the file to disk
std::cout << "Extracting " << Files::pathToUnicodeString(info.extractfile) << " to "
<< Files::pathToUnicodeString(target) << std::endl;
out << stream->rdbuf();
out.close();
return 0;
}
template <typename File>
int extractAll(std::unique_ptr<File>& bsa, Arguments& info)
{
for (const auto& file : bsa->getList())
{
std::string extractPath(file.name());
Misc::StringUtils::replaceAll(extractPath, "\\", "/");
// Get the target path (the path the file will be extracted to)
auto target = info.outdir;
target /= Misc::StringUtils::stringToU8String(extractPath);
// Create the directory hierarchy
std::filesystem::create_directories(target.parent_path());
std::filesystem::file_status s = std::filesystem::status(target.parent_path());
if (!std::filesystem::is_directory(s))
{
std::cout << "ERROR: " << target.parent_path() << " is not a directory." << std::endl;
return 3;
}
// Get a stream for the file to extract
Files::IStreamPtr data = bsa->getFile(&file);
std::ofstream out(target, std::ios::binary);
// Write the file to disk
std::cout << "Extracting " << Files::pathToUnicodeString(target) << std::endl;
out << data->rdbuf();
out.close();
}
return 0;
}
template <typename File>
int add(std::unique_ptr<File>& bsa, Arguments& info)
{
std::fstream stream(info.addfile, std::ios_base::binary | std::ios_base::out | std::ios_base::in);
bsa->addFile(Files::pathToUnicodeString(info.addfile), stream);
return 0;
}
template <typename File>
int call(Arguments& info)
{
std::unique_ptr<File> bsa = std::make_unique<File>();
if (info.mode == "create")
{
bsa->open(info.filename);
return 0;
}
bsa->open(info.filename);
if (info.mode == "list")
return list(bsa, info);
else if (info.mode == "extract")
return extract(bsa, info);
else if (info.mode == "extractall")
return extractAll(bsa, info);
else if (info.mode == "add")
return add(bsa, info);
else
{
std::cout << "Unsupported mode. That is not supposed to happen." << std::endl;
return 1;
}
}
int main(int argc, char** argv)
{
try
{
Arguments info;
if (!parseOptions(argc, argv, info))
return 1;
// Open file
// TODO: add a version argument for this mode after compressed BSA writing is a thing
if (info.mode == "create")
return call<Bsa::BSAFile>(info);
Bsa::BsaVersion bsaVersion = Bsa::BSAFile::detectVersion(info.filename);
switch (bsaVersion)
{
case Bsa::BsaVersion::Unknown:
break;
case Bsa::BsaVersion::Uncompressed:
return call<Bsa::BSAFile>(info);
case Bsa::BsaVersion::Compressed:
return call<Bsa::CompressedBSAFile>(info);
case Bsa::BsaVersion::BA2GNRL:
return call<Bsa::BA2GNRLFile>(info);
case Bsa::BsaVersion::BA2DX10:
return call<Bsa::BA2DX10File>(info);
}
throw std::runtime_error("Unrecognised BSA archive");
}
catch (std::exception& e)
{
std::cerr << "ERROR reading BSA archive\nDetails:\n" << e.what() << std::endl;
return 2;
}
}
|