metaforce/hecl/driver/main.cpp

371 lines
9.3 KiB
C++
Raw Normal View History

2015-07-22 19:14:50 +00:00
#if _WIN32
2015-10-12 04:38:49 +00:00
#ifndef NOMINMAX
#define NOMINMAX
#endif
2017-11-06 06:56:17 +00:00
#define WIN_PAUSE 0
2015-08-31 03:36:24 +00:00
#include <objbase.h>
2015-07-22 19:14:50 +00:00
#endif
2015-08-31 03:36:24 +00:00
#include <clocale>
2017-12-29 07:56:31 +00:00
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cstdarg>
2015-08-16 23:01:35 +00:00
#include <signal.h>
2015-05-20 05:22:32 +00:00
#include <regex>
2015-05-26 04:42:20 +00:00
#include <list>
2016-03-04 23:02:44 +00:00
#include "hecl/Database.hpp"
2017-12-29 07:56:31 +00:00
#include "hecl/Blender/Connection.hpp"
2016-03-04 23:02:44 +00:00
#include "logvisor/logvisor.hpp"
2015-07-05 06:27:24 +00:00
2016-09-25 01:57:43 +00:00
logvisor::Module LogModule("hecl::Driver");
2015-05-17 04:55:29 +00:00
2015-06-10 02:40:03 +00:00
#include "ToolBase.hpp"
#include "ToolInit.hpp"
#include "ToolSpec.hpp"
2015-06-12 09:08:49 +00:00
#include "ToolExtract.hpp"
2015-06-10 02:40:03 +00:00
#include "ToolCook.hpp"
#include "ToolPackage.hpp"
2018-04-15 06:04:08 +00:00
#include "ToolImage.hpp"
2020-04-10 03:19:33 +00:00
#include "ToolInstallAddon.hpp"
2015-06-10 02:40:03 +00:00
#include "ToolHelp.hpp"
2015-05-15 22:39:43 +00:00
2015-07-25 23:01:02 +00:00
/* Static reference to dataspec additions
* (used by MSVC to definitively link DataSpecs) */
#include "DataSpecRegistry.hpp"
2015-07-22 19:14:50 +00:00
2015-05-26 04:42:20 +00:00
bool XTERM_COLOR = false;
2015-06-11 09:41:10 +00:00
/*
2015-06-10 02:40:03 +00:00
#define HECL_GIT 1234567
#define HECL_GIT_S "1234567"
#define HECL_BRANCH master
#define HECL_BRANCH_S "master"
2015-06-11 09:41:10 +00:00
*/
2015-06-10 02:40:03 +00:00
2015-05-20 05:22:32 +00:00
/* Main usage message */
2018-12-08 05:18:42 +00:00
static void printHelp(const hecl::SystemChar* pname) {
if (XTERM_COLOR)
2020-04-11 22:48:11 +00:00
fmt::print(FMT_STRING(_SYS_STR("" BOLD "HECL" NORMAL "")));
2018-12-08 05:18:42 +00:00
else
2020-04-11 22:48:11 +00:00
fmt::print(FMT_STRING(_SYS_STR("HECL")));
2018-04-15 06:04:08 +00:00
#if HECL_HAS_NOD
2020-04-10 03:19:33 +00:00
#define TOOL_LIST "extract|init|cook|package|image|installaddon|help"
2018-04-15 06:04:08 +00:00
#else
2020-04-10 03:19:33 +00:00
#define TOOL_LIST "extract|init|cook|package|installaddon|help"
2018-04-15 06:04:08 +00:00
#endif
2015-05-20 05:22:32 +00:00
#if HECL_GIT
2020-04-11 22:48:11 +00:00
fmt::print(FMT_STRING(_SYS_STR(" Commit " HECL_GIT_S " " HECL_BRANCH_S "\nUsage: {} " TOOL_LIST "\n")), pname);
2015-05-20 05:22:32 +00:00
#elif HECL_VER
2020-04-11 22:48:11 +00:00
fmt::print(FMT_STRING(_SYS_STR(" Version " HECL_VER_S "\nUsage: {} " TOOL_LIST "\n")), pname);
2015-05-20 05:22:32 +00:00
#else
2020-04-11 22:48:11 +00:00
fmt::print(FMT_STRING(_SYS_STR("\nUsage: {} " TOOL_LIST "\n")), pname);
2015-05-20 05:22:32 +00:00
#endif
}
/* Regex patterns */
2018-12-08 05:18:42 +00:00
static const hecl::SystemRegex regOPEN(_SYS_STR("-o([^\"]*|\\S*)"), std::regex::ECMAScript | std::regex::optimize);
static ToolBase* ToolPtr = nullptr;
2015-08-16 23:01:35 +00:00
/* SIGINT will gracefully close blender connections and delete blends in progress */
2018-12-08 05:18:42 +00:00
static void SIGINTHandler(int sig) {
if (ToolPtr)
ToolPtr->cancel();
hecl::blender::Connection::Shutdown();
logvisor::KillProcessTree();
exit(1);
2015-08-16 23:01:35 +00:00
}
2016-03-04 23:02:44 +00:00
static logvisor::Module AthenaLog("Athena");
static void AthenaExc(athena::error::Level level, const char* file, const char*, int line, fmt::string_view fmt,
fmt::format_args args) {
2019-07-20 04:22:58 +00:00
AthenaLog.vreport(logvisor::Level(level), fmt, args);
2015-07-26 02:52:02 +00:00
}
2017-02-24 08:27:07 +00:00
hecl::SystemString ExeDir;
#if _WIN32
static ToolPassInfo CreateInfo(int argc, const wchar_t** argv) {
#else
static ToolPassInfo CreateInfo(int argc, const char** argv) {
#endif
hecl::SystemChar cwdbuf[1024];
ToolPassInfo info;
info.pname = argv[0];
if (hecl::Getcwd(cwdbuf, static_cast<int>(std::size(cwdbuf)))) {
info.cwd = cwdbuf;
if (info.cwd.size() && info.cwd.back() != _SYS_STR('/') && info.cwd.back() != _SYS_STR('\\')) {
#if _WIN32
info.cwd += _SYS_STR('\\');
#else
info.cwd += _SYS_STR('/');
#endif
}
if (hecl::PathRelative(argv[0])) {
ExeDir = hecl::SystemString(cwdbuf) + _SYS_STR('/');
}
hecl::SystemString Argv0(argv[0]);
hecl::SystemString::size_type lastIdx = Argv0.find_last_of(_SYS_STR("/\\"));
if (lastIdx != hecl::SystemString::npos) {
ExeDir.insert(ExeDir.end(), Argv0.begin(), Argv0.begin() + lastIdx);
}
}
/* Concatenate args */
std::vector<hecl::SystemString> args;
args.reserve(argc - 2);
for (int i = 2; i < argc; ++i) {
args.emplace_back(argv[i]);
}
if (!args.empty()) {
/* Extract output argument */
for (auto it = args.cbegin(); it != args.cend();) {
const hecl::SystemString& arg = *it;
hecl::SystemRegexMatch oMatch;
if (std::regex_search(arg, oMatch, regOPEN)) {
const hecl::SystemString& token = oMatch[1].str();
if (token.size()) {
if (info.output.empty()) {
info.output = oMatch[1].str();
}
it = args.erase(it);
} else {
it = args.erase(it);
if (it == args.end()) {
break;
}
if (info.output.empty()) {
info.output = *it;
}
it = args.erase(it);
}
continue;
}
++it;
}
/* Iterate flags */
bool threadArg = false;
for (auto it = args.cbegin(); it != args.cend();) {
const hecl::SystemString& arg = *it;
if (threadArg) {
threadArg = false;
hecl::CpuCountOverride = int(hecl::StrToUl(arg.c_str(), nullptr, 0));
it = args.erase(it);
continue;
}
if (arg.size() < 2 || arg[0] != _SYS_STR('-') || arg[1] == _SYS_STR('-')) {
++it;
continue;
}
for (auto chit = arg.cbegin() + 1; chit != arg.cend(); ++chit) {
if (*chit == _SYS_STR('v'))
++info.verbosityLevel;
else if (*chit == _SYS_STR('f'))
info.force = true;
else if (*chit == _SYS_STR('y'))
info.yes = true;
else if (*chit == _SYS_STR('g'))
info.gui = true;
else if (*chit == _SYS_STR('j')) {
++chit;
if (*chit)
hecl::CpuCountOverride = int(hecl::StrToUl(&*chit, nullptr, 0));
else
threadArg = true;
break;
} else
info.flags.push_back(*chit);
}
it = args.erase(it);
}
/* Gather remaining args */
info.args.reserve(args.size());
for (const hecl::SystemString& arg : args)
info.args.push_back(arg);
}
return info;
}
static std::unique_ptr<hecl::Database::Project> FindProject(hecl::SystemStringView cwd) {
const hecl::ProjectRootPath rootPath = hecl::SearchForProject(cwd);
if (!rootPath) {
return nullptr;
}
const size_t ErrorRef = logvisor::ErrorCount;
auto newProj = std::make_unique<hecl::Database::Project>(rootPath);
if (logvisor::ErrorCount > ErrorRef) {
#if WIN_PAUSE
system("PAUSE");
#endif
return nullptr;
}
return newProj;
}
static std::unique_ptr<ToolBase> MakeSelectedTool(hecl::SystemString toolName, ToolPassInfo& info) {
hecl::SystemString toolNameLower = toolName;
hecl::ToLower(toolNameLower);
if (toolNameLower == _SYS_STR("init")) {
return std::make_unique<ToolInit>(info);
}
if (toolNameLower == _SYS_STR("spec")) {
return std::make_unique<ToolSpec>(info);
}
if (toolNameLower == _SYS_STR("extract")) {
return std::make_unique<ToolExtract>(info);
}
if (toolNameLower == _SYS_STR("cook")) {
return std::make_unique<ToolCook>(info);
}
if (toolNameLower == _SYS_STR("package") || toolNameLower == _SYS_STR("pack")) {
return std::make_unique<ToolPackage>(info);
}
#if HECL_HAS_NOD
if (toolNameLower == _SYS_STR("image")) {
return std::make_unique<ToolImage>(info);
}
#endif
2020-04-10 03:19:33 +00:00
if (toolNameLower == _SYS_STR("installaddon")) {
return std::make_unique<ToolInstallAddon>(info);
}
if (toolNameLower == _SYS_STR("help")) {
return std::make_unique<ToolHelp>(info);
}
auto fp = hecl::FopenUnique(toolName.c_str(), _SYS_STR("rb"));
if (fp == nullptr) {
2020-04-11 22:48:11 +00:00
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("unrecognized tool '{}'")), toolNameLower);
return nullptr;
}
fp.reset();
/* Shortcut-case: implicit extract */
info.args.insert(info.args.begin(), std::move(toolName));
return std::make_unique<ToolExtract>(info);
}
2016-06-17 20:44:49 +00:00
#if _WIN32
2015-06-10 02:40:03 +00:00
int wmain(int argc, const wchar_t** argv)
#else
2018-05-25 06:34:58 +00:00
/* SIGWINCH should do nothing */
static void SIGWINCHHandler(int sig) {}
int main(int argc, const char** argv)
2015-06-10 02:40:03 +00:00
#endif
{
2018-12-08 05:18:42 +00:00
if (argc > 1 && !hecl::StrCmp(argv[1], _SYS_STR("--dlpackage"))) {
2020-04-11 22:48:11 +00:00
fmt::print(FMT_STRING("{}\n"), HECL_DLPACKAGE);
2018-12-08 05:18:42 +00:00
return 100;
}
2018-01-02 00:58:13 +00:00
2015-08-31 03:36:24 +00:00
#if _WIN32
2018-12-08 05:18:42 +00:00
CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE);
2015-08-31 03:36:24 +00:00
#else
2018-12-08 05:18:42 +00:00
std::setlocale(LC_ALL, "en-US.UTF-8");
2015-08-31 03:36:24 +00:00
#endif
2016-03-24 02:51:57 +00:00
2018-12-08 05:18:42 +00:00
/* Xterm check */
2015-09-02 22:00:05 +00:00
#if _WIN32
2018-12-08 05:18:42 +00:00
const char* conemuANSI = getenv("ConEmuANSI");
if (conemuANSI && !strcmp(conemuANSI, "ON"))
XTERM_COLOR = true;
2015-09-02 22:00:05 +00:00
#else
2018-12-08 05:18:42 +00:00
const char* term = getenv("TERM");
if (term && !strncmp(term, "xterm", 5))
XTERM_COLOR = true;
signal(SIGWINCH, SIGWINCHHandler);
2015-09-02 22:00:05 +00:00
#endif
2018-12-08 05:18:42 +00:00
signal(SIGINT, SIGINTHandler);
2015-08-16 23:01:35 +00:00
2018-12-08 05:18:42 +00:00
logvisor::RegisterStandardExceptions();
logvisor::RegisterConsoleLogger();
atSetExceptionHandler(AthenaExc);
2018-12-08 05:18:42 +00:00
/* Basic usage check */
if (argc == 1) {
printHelp(argv[0]);
2015-07-22 19:14:50 +00:00
#if WIN_PAUSE
2018-12-08 05:18:42 +00:00
system("PAUSE");
2015-07-22 19:14:50 +00:00
#endif
2018-12-08 05:18:42 +00:00
return 0;
} else if (argc == 0) {
printHelp(_SYS_STR("hecl"));
2015-07-22 19:14:50 +00:00
#if WIN_PAUSE
2018-12-08 05:18:42 +00:00
system("PAUSE");
2015-07-22 19:14:50 +00:00
#endif
2018-12-08 05:18:42 +00:00
return 0;
}
/* Prepare DataSpecs */
HECLRegisterDataSpecs();
/* Assemble common tool pass info */
ToolPassInfo info = CreateInfo(argc, argv);
2018-12-08 05:18:42 +00:00
/* Attempt to find hecl project */
auto project = FindProject(info.cwd);
if (project != nullptr) {
info.project = project.get();
2018-12-08 05:18:42 +00:00
}
/* Construct selected tool */
const size_t MakeToolErrorRef = logvisor::ErrorCount;
auto tool = MakeSelectedTool(argv[1], info);
if (logvisor::ErrorCount > MakeToolErrorRef) {
2015-07-22 19:14:50 +00:00
#if WIN_PAUSE
2018-12-08 05:18:42 +00:00
system("PAUSE");
2015-07-16 02:03:38 +00:00
#endif
2018-12-08 05:18:42 +00:00
return 1;
}
if (info.verbosityLevel) {
2020-04-11 22:48:11 +00:00
LogModule.report(logvisor::Info, FMT_STRING(_SYS_STR("Constructed tool '{}' {}\n")), tool->toolName(),
info.verbosityLevel);
}
2018-12-08 05:18:42 +00:00
/* Run tool */
const size_t RunToolErrorRef = logvisor::ErrorCount;
2018-12-08 05:18:42 +00:00
ToolPtr = tool.get();
const int retval = tool->run();
2018-12-08 05:18:42 +00:00
ToolPtr = nullptr;
if (logvisor::ErrorCount > RunToolErrorRef) {
2018-12-08 05:18:42 +00:00
hecl::blender::Connection::Shutdown();
2015-07-22 19:14:50 +00:00
#if WIN_PAUSE
2018-12-08 05:18:42 +00:00
system("PAUSE");
2015-07-16 02:03:38 +00:00
#endif
2018-12-08 05:18:42 +00:00
return 1;
}
2018-12-08 05:18:42 +00:00
hecl::blender::Connection::Shutdown();
2015-07-22 19:14:50 +00:00
#if WIN_PAUSE
2018-12-08 05:18:42 +00:00
system("PAUSE");
2015-07-22 19:14:50 +00:00
#endif
2018-12-08 05:18:42 +00:00
return retval;
2015-05-15 22:39:43 +00:00
}