metaforce/hecl/driver/main.cpp

320 lines
8.3 KiB
C++
Raw Normal View History

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