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"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
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-01-17 17:48:16 +00:00
|
|
|
#include "moduleprobe.h"
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
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();
|
2020-10-03 10:02:26 +00:00
|
|
|
initGameVersion();
|
2020-08-03 13:16:58 +00:00
|
|
|
|
|
|
|
switch (_command) {
|
|
|
|
case Command::List:
|
|
|
|
case Command::Extract:
|
|
|
|
case Command::Convert:
|
2021-01-17 17:48:16 +00:00
|
|
|
initFileTool();
|
|
|
|
switch (_command) {
|
|
|
|
case Command::List:
|
|
|
|
_tool->list(_target, _keyPath);
|
|
|
|
break;
|
|
|
|
case Command::Extract:
|
|
|
|
_tool->extract(_target, _keyPath, _destPath);
|
|
|
|
break;
|
|
|
|
case Command::Convert:
|
|
|
|
_tool->convert(_target, _destPath);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2021-01-18 01:03:36 +00:00
|
|
|
break;
|
2021-01-17 17:48:16 +00:00
|
|
|
case Command::ModuleProbe:
|
|
|
|
ModuleProbe().probe(_target, _gamePath, _destPath);
|
2020-08-03 13:16:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cout << _cmdLineOpts << endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-01 09:53:36 +00:00
|
|
|
void Program::initOptions() {
|
2020-08-03 13:16:58 +00:00
|
|
|
_cmdLineOpts.add_options()
|
|
|
|
("help", "print this message")
|
|
|
|
("list", "list file contents")
|
|
|
|
("extract", "extract file contents")
|
|
|
|
("convert", "convert 2DA or GFF file to JSON")
|
2021-01-17 17:48:16 +00:00
|
|
|
("modprobe", "probe module and produce a JSON file describing it")
|
2020-08-08 04:27:15 +00:00
|
|
|
("game", po::value<string>(), "path to game directory")
|
|
|
|
("dest", po::value<string>(), "path to destination directory")
|
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);
|
|
|
|
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>() : "";
|
2020-08-03 13:16:58 +00:00
|
|
|
_keyPath = getPathIgnoreCase(_gamePath, "chitin.key");
|
|
|
|
|
2020-10-01 09:53:36 +00:00
|
|
|
if (vars.count("help")) {
|
2020-08-03 13:16:58 +00:00
|
|
|
_command = Command::Help;
|
2020-10-01 09:53:36 +00:00
|
|
|
} else if (vars.count("list")) {
|
2020-08-03 13:16:58 +00:00
|
|
|
_command = Command::List;
|
2020-10-01 09:53:36 +00:00
|
|
|
} else if (vars.count("extract")) {
|
2020-08-03 13:16:58 +00:00
|
|
|
_command = Command::Extract;
|
2020-10-01 09:53:36 +00:00
|
|
|
} else if (vars.count("convert")) {
|
2020-08-03 13:16:58 +00:00
|
|
|
_command = Command::Convert;
|
2021-01-17 17:48:16 +00:00
|
|
|
} else if (vars.count("modprobe")) {
|
|
|
|
_command = Command::ModuleProbe;
|
2020-08-03 13:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-01 09:53:36 +00:00
|
|
|
void Program::initGameVersion() {
|
|
|
|
fs::path exePath = getPathIgnoreCase(_gamePath, "swkotor2.exe");
|
|
|
|
_version = exePath.empty() ? GameVersion::KotOR : GameVersion::TheSithLords;
|
|
|
|
}
|
|
|
|
|
2021-01-17 17:48:16 +00:00
|
|
|
void Program::initFileTool() {
|
2020-08-03 13:16:58 +00:00
|
|
|
switch (_command) {
|
|
|
|
case Command::List:
|
|
|
|
case Command::Extract:
|
|
|
|
case Command::Convert:
|
2021-01-17 17:48:16 +00:00
|
|
|
if (!fs::exists(_target)) {
|
|
|
|
throw runtime_error("Input file does not exist: " + _target);
|
2020-08-03 13:16:58 +00:00
|
|
|
}
|
2021-01-17 17:48:16 +00:00
|
|
|
_tool = getFileToolByPath(_version, _target);
|
2020-08-03 13:16:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
2021-01-17 17:48:16 +00:00
|
|
|
throw logic_error("Unsupported file tool command: " + to_string(static_cast<int>(_command)));
|
2020-08-03 13:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace tools
|
|
|
|
|
|
|
|
} // namespace reone
|