reone/tools/biftool.cpp

99 lines
2.9 KiB
C++
Raw Normal View History

2020-08-03 01:06:55 +00:00
/*
* Copyright (c) 2020 The reone project contributors
2020-08-03 01:06:55 +00:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-08-02 15:47:59 +00:00
#include "tools.h"
#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
2020-12-22 15:36:22 +00:00
#include "../src/common/log.h"
#include "../src/resource/biffile.h"
#include "../src/resource/util.h"
2020-08-02 15:47:59 +00:00
2020-08-08 04:27:15 +00:00
using namespace std;
using namespace reone::resource;
2020-08-02 15:47:59 +00:00
namespace fs = boost::filesystem;
namespace reone {
namespace tools {
void BifTool::list(const fs::path &path, const fs::path &keyPath) const {
if (!fs::exists(keyPath)) {
2020-08-08 04:27:15 +00:00
throw runtime_error("BIF: key file does not exist: " + keyPath.string());
2020-08-02 15:47:59 +00:00
}
KeyFile key;
key.load(keyPath);
auto &files = key.files();
2020-08-08 04:27:15 +00:00
string filename(path.filename().string());
2020-08-02 15:47:59 +00:00
int bifIdx = getFileIndexByFilename(files, filename);
if (bifIdx == -1) {
2020-08-08 04:27:15 +00:00
throw runtime_error("BIF: filename not found in Key file: " + filename);
2020-08-02 15:47:59 +00:00
}
for (auto &key : key.keys()) {
if (key.bifIdx == bifIdx) {
info(boost::format("%16s\t%4s") % key.resRef % getExtByResType(key.resType));
}
}
}
2020-08-08 04:27:15 +00:00
int BifTool::getFileIndexByFilename(const vector<KeyFile::FileEntry> &files, const string &filename) const {
2020-08-02 15:47:59 +00:00
for (int i = 0; i < files.size(); ++i) {
if (boost::contains(files[i].filename, filename)) {
return i;
}
}
return -1;
}
void BifTool::extract(const fs::path &path, const fs::path &keyPath, const fs::path &destPath) const {
if (!fs::exists(keyPath)) {
2020-08-08 04:27:15 +00:00
throw runtime_error("BIF: key file does not exist: " + keyPath.string());
2020-08-02 15:47:59 +00:00
}
KeyFile key;
key.load(keyPath);
auto &files = key.files();
2020-08-08 04:27:15 +00:00
string filename(path.filename().string());
2020-08-02 15:47:59 +00:00
int bifIdx = getFileIndexByFilename(files, filename);
if (bifIdx == -1) {
2020-08-08 04:27:15 +00:00
throw runtime_error("BIF: filename not found in Key file: " + filename);
2020-08-02 15:47:59 +00:00
}
BifFile bif;
bif.load(path);
for (auto &key : key.keys()) {
if (key.bifIdx != bifIdx) continue;
info(boost::format("Extracting %16s\t%4s") % key.resRef % getExtByResType(key.resType));
fs::path resPath(destPath);
resPath.append(key.resRef + "." + getExtByResType(key.resType));
ByteArray data(bif.getResourceData(key.resIdx));
2020-08-08 04:27:15 +00:00
fs::ofstream res(resPath, ios::binary);
2020-08-02 15:47:59 +00:00
res.write(data.data(), data.size());
}
}
} // namespace tools
} // namespace reone