2020-08-03 13:16:58 +00:00
|
|
|
/*
|
2021-01-02 15:06:11 +00:00
|
|
|
* Copyright (c) 2020-2021 The reone project contributors
|
2020-08-03 13:16:58 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "program.h"
|
|
|
|
|
2021-02-09 07:53:53 +00:00
|
|
|
#include <algorithm>
|
2020-08-03 13:16:58 +00:00
|
|
|
#include <iostream>
|
2021-02-09 07:53:53 +00:00
|
|
|
#include <unordered_map>
|
2020-08-03 13:16:58 +00:00
|
|
|
|
2020-10-01 09:53:36 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include <boost/program_options.hpp>
|
2020-08-03 13:16:58 +00:00
|
|
|
|
2020-11-10 06:44:48 +00:00
|
|
|
#include "../src/common/pathutil.h"
|
2020-08-03 13:16:58 +00:00
|
|
|
|
2021-02-09 07:53:53 +00:00
|
|
|
#include "tools.h"
|
|
|
|
#include "types.h"
|
2021-01-17 17:48:16 +00:00
|
|
|
|
2020-08-03 13:16:58 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2020-10-02 15:34:02 +00:00
|
|
|
using namespace reone::resource;
|
2020-08-03 13:16:58 +00:00
|
|
|
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
|
|
|
namespace reone {
|
|
|
|
|
|
|
|
namespace tools {
|
|
|
|
|
2021-02-09 01:38:37 +00:00
|
|
|
static const char kConfigFilename[] = "reone-tools.cfg";
|
2021-01-30 04:37:36 +00:00
|
|
|
|
2021-02-09 07:53:53 +00:00
|
|
|
static const unordered_map<string, Operation> g_operations {
|
|
|
|
{ "list", Operation::List },
|
|
|
|
{ "extract", Operation::Extract },
|
|
|
|
{ "to-json", Operation::ToJSON },
|
|
|
|
{ "to-tga", Operation::ToTGA },
|
2021-02-09 15:47:04 +00:00
|
|
|
{ "to-2da", Operation::To2DA }
|
2021-02-09 07:53:53 +00:00
|
|
|
};
|
|
|
|
|
2020-08-03 13:16:58 +00:00
|
|
|
Program::Program(int argc, char **argv) : _argc(argc), _argv(argv) {
|
|
|
|
}
|
|
|
|
|
|
|
|
int Program::run() {
|
2020-10-01 09:53:36 +00:00
|
|
|
initOptions();
|
2020-08-03 13:16:58 +00:00
|
|
|
loadOptions();
|
2021-01-29 13:21:23 +00:00
|
|
|
determineGameID();
|
2021-02-09 07:53:53 +00:00
|
|
|
loadTools();
|
2020-08-03 13:16:58 +00:00
|
|
|
|
2021-02-09 07:53:53 +00:00
|
|
|
switch (_operation) {
|
|
|
|
case Operation::None:
|
2020-08-03 13:16:58 +00:00
|
|
|
cout << _cmdLineOpts << endl;
|
|
|
|
break;
|
2021-02-09 07:53:53 +00:00
|
|
|
default: {
|
|
|
|
auto tool = getTool();
|
|
|
|
if (tool) {
|
|
|
|
tool->invoke(_operation, _target, _gamePath, _destPath);
|
|
|
|
} else {
|
|
|
|
cout << "Unable to choose a tool for the specified operation" << endl;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-08-03 13:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-01 09:53:36 +00:00
|
|
|
void Program::initOptions() {
|
2021-01-30 04:37:36 +00:00
|
|
|
_commonOpts.add_options()
|
|
|
|
("game", po::value<string>(), "path to game directory")
|
|
|
|
("dest", po::value<string>(), "path to destination directory");
|
|
|
|
|
|
|
|
_cmdLineOpts.add(_commonOpts).add_options()
|
2020-08-03 13:16:58 +00:00
|
|
|
("list", "list file contents")
|
|
|
|
("extract", "extract file contents")
|
2021-02-09 01:45:00 +00:00
|
|
|
("to-json", "convert 2DA, GFF or TLK file to JSON")
|
|
|
|
("to-tga", "convert TPC image to TGA")
|
2021-02-09 05:40:14 +00:00
|
|
|
("to-2da", "convert JSON to 2DA")
|
|
|
|
("to-gff", "convert JSON to GFF")
|
2021-01-17 17:48:16 +00:00
|
|
|
("target", po::value<string>(), "target name or path to input file");
|
2020-10-01 09:53:36 +00:00
|
|
|
}
|
2020-08-03 13:16:58 +00:00
|
|
|
|
2021-01-12 01:49:02 +00:00
|
|
|
static fs::path getDestination(const po::variables_map &vars) {
|
|
|
|
fs::path result;
|
|
|
|
if (vars.count("dest") > 0) {
|
|
|
|
result = vars["dest"].as<string>();
|
2021-01-17 17:48:16 +00:00
|
|
|
} else if (vars.count("target") > 0) {
|
|
|
|
result = fs::path(vars["target"].as<string>()).parent_path();
|
2021-01-12 01:49:02 +00:00
|
|
|
} else {
|
|
|
|
result = fs::current_path();
|
|
|
|
}
|
|
|
|
return move(result);
|
|
|
|
}
|
|
|
|
|
2020-10-01 09:53:36 +00:00
|
|
|
void Program::loadOptions() {
|
2020-08-03 13:16:58 +00:00
|
|
|
po::positional_options_description positional;
|
2021-01-17 17:48:16 +00:00
|
|
|
positional.add("target", 1);
|
2020-08-03 13:16:58 +00:00
|
|
|
|
|
|
|
po::parsed_options parsedCmdLineOpts = po::command_line_parser(_argc, _argv)
|
|
|
|
.options(_cmdLineOpts)
|
|
|
|
.positional(positional)
|
|
|
|
.run();
|
|
|
|
|
2020-10-01 09:53:36 +00:00
|
|
|
po::variables_map vars;
|
|
|
|
po::store(parsedCmdLineOpts, vars);
|
2021-01-30 04:37:36 +00:00
|
|
|
if (fs::exists(kConfigFilename)) {
|
2021-02-09 01:38:37 +00:00
|
|
|
po::store(po::parse_config_file<char>(kConfigFilename, _commonOpts), vars);
|
2021-01-30 04:37:36 +00:00
|
|
|
}
|
2020-10-01 09:53:36 +00:00
|
|
|
po::notify(vars);
|
2020-08-03 13:16:58 +00:00
|
|
|
|
2020-10-01 09:53:36 +00:00
|
|
|
_gamePath = vars.count("game") > 0 ? vars["game"].as<string>() : fs::current_path();
|
2021-01-12 01:49:02 +00:00
|
|
|
_destPath = getDestination(vars);
|
2021-01-17 17:48:16 +00:00
|
|
|
_target = vars.count("target") > 0 ? vars["target"].as<string>() : "";
|
2021-02-09 07:53:53 +00:00
|
|
|
|
|
|
|
// Determine operation from program options
|
|
|
|
for (auto &operation : g_operations) {
|
|
|
|
if (vars.count(operation.first)) {
|
|
|
|
_operation = operation.second;
|
|
|
|
break;
|
|
|
|
}
|
2020-08-03 13:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:21:23 +00:00
|
|
|
void Program::determineGameID() {
|
2020-10-01 09:53:36 +00:00
|
|
|
fs::path exePath = getPathIgnoreCase(_gamePath, "swkotor2.exe");
|
2021-01-29 13:21:23 +00:00
|
|
|
_gameId = exePath.empty() ? GameID::KotOR : GameID::TSL;
|
2020-10-01 09:53:36 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 07:53:53 +00:00
|
|
|
void Program::loadTools() {
|
2021-02-09 15:47:04 +00:00
|
|
|
// Tools are queried in the order of addition, whether they support a
|
|
|
|
// particular operation on a particular file, or not. The first tool
|
|
|
|
// to return true gets chosen.
|
|
|
|
|
2021-02-09 07:53:53 +00:00
|
|
|
_tools.push_back(make_shared<KeyBifTool>());
|
|
|
|
_tools.push_back(make_shared<ErfTool>());
|
|
|
|
_tools.push_back(make_shared<RimTool>());
|
|
|
|
_tools.push_back(make_shared<TwoDaTool>());
|
|
|
|
_tools.push_back(make_shared<TlkTool>());
|
|
|
|
_tools.push_back(make_shared<GffTool>());
|
|
|
|
_tools.push_back(make_shared<TpcTool>());
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<ITool> Program::getTool() const {
|
|
|
|
for (auto &tool : _tools) {
|
|
|
|
if (tool->supports(_operation, _target)) return tool;
|
2020-08-03 13:16:58 +00:00
|
|
|
}
|
2021-02-09 07:53:53 +00:00
|
|
|
return nullptr;
|
2020-08-03 13:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace tools
|
|
|
|
|
|
|
|
} // namespace reone
|