metaforce/hecl/driver/main.cpp

308 lines
8.0 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
2015-07-22 19:14:50 +00:00
#define WIN_PAUSE 1
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>
2015-05-15 22:39:43 +00:00
#include <stdio.h>
#include <string.h>
2015-05-26 04:42:20 +00:00
#include <stdlib.h>
2015-07-26 02:52:02 +00:00
#include <stdarg.h>
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>
#include "HECL/Database.hpp"
2015-07-28 02:25:33 +00:00
#include "../blender/BlenderConnection.hpp"
2015-07-05 06:27:24 +00:00
#include "LogVisor/LogVisor.hpp"
LogVisor::LogModule LogModule("HECLDriver");
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 "ToolAdd.hpp"
#include "ToolRemove.hpp"
#include "ToolGroup.hpp"
#include "ToolCook.hpp"
#include "ToolClean.hpp"
#include "ToolPackage.hpp"
#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) */
2015-07-22 19:14:50 +00:00
#include "../DataSpecRegistry.hpp"
2015-05-26 04:42:20 +00:00
bool XTERM_COLOR = false;
2015-07-05 06:27:24 +00:00
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 */
2015-06-10 02:40:03 +00:00
static void printHelp(const HECL::SystemChar* pname)
2015-05-15 22:39:43 +00:00
{
2015-05-26 04:42:20 +00:00
if (XTERM_COLOR)
2015-06-10 02:40:03 +00:00
HECL::Printf(_S("" BOLD "HECL" NORMAL ""));
2015-05-26 04:42:20 +00:00
else
2015-06-10 02:40:03 +00:00
HECL::Printf(_S("HECL"));
2015-05-20 05:22:32 +00:00
#if HECL_GIT
2015-06-10 02:40:03 +00:00
HECL::Printf(_S(" Commit " HECL_GIT_S " " HECL_BRANCH_S "\nUsage: %s init|add|remove|group|cook|clean|package|help\n"), pname);
2015-05-20 05:22:32 +00:00
#elif HECL_VER
2015-06-10 02:40:03 +00:00
HECL::Printf(_S(" Version " HECL_VER_S "\nUsage: %s init|add|remove|group|cook|clean|package|help\n"), pname);
2015-05-20 05:22:32 +00:00
#else
2015-06-10 02:40:03 +00:00
HECL::Printf(_S("\nUsage: %s init|add|remove|group|cook|clean|package|help\n"), pname);
2015-05-20 05:22:32 +00:00
#endif
}
/* Regex patterns */
static const HECL::SystemRegex regOPEN(_S("-o([^\"]*|\\S*)"), std::regex::ECMAScript|std::regex::optimize);
2015-08-16 23:01:35 +00:00
/* SIGINT will gracefully close blender connections and delete blends in progress */
static void SIGINTHandler(int sig)
{
HECL::BlenderConnection::Shutdown();
exit(1);
}
2015-09-02 22:00:05 +00:00
/* SIGWINCH should do nothing */
static void SIGWINCHHandler(int sig) {}
2015-07-26 02:52:02 +00:00
static LogVisor::LogModule AthenaLog("Athena");
2015-11-23 03:09:46 +00:00
static void AthenaExc(Athena::error::Level level, const char* file,
2015-07-26 02:52:02 +00:00
const char*, int line, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
2015-07-28 02:25:33 +00:00
AthenaLog.reportSource(LogVisor::Level(level), file, line, fmt, ap);
2015-07-26 02:52:02 +00:00
va_end(ap);
}
2015-06-10 02:40:03 +00:00
#if HECL_UCS2
int wmain(int argc, const wchar_t** argv)
#else
int main(int argc, const char** argv)
2015-06-10 02:40:03 +00:00
#endif
{
2015-08-31 03:36:24 +00:00
#if _WIN32
CoInitializeEx(nullptr, COINIT_MULTITHREADED);
#else
std::setlocale(LC_ALL, "en-US.UTF-8");
#endif
2015-05-26 04:42:20 +00:00
/* Xterm check */
2015-09-02 22:00:05 +00:00
#if _WIN32
const char* conemuANSI = getenv("ConEmuANSI");
if (conemuANSI && !strcmp(conemuANSI, "ON"))
XTERM_COLOR = true;
#else
2015-05-26 04:42:20 +00:00
const char* term = getenv("TERM");
if (term && !strncmp(term, "xterm", 5))
2015-05-26 04:42:20 +00:00
XTERM_COLOR = true;
2015-09-02 22:12:48 +00:00
signal(SIGWINCH, SIGWINCHHandler);
2015-09-02 22:00:05 +00:00
#endif
2015-08-16 23:01:35 +00:00
signal(SIGINT, SIGINTHandler);
LogVisor::RegisterConsoleLogger();
2015-07-26 02:52:02 +00:00
atSetExceptionHandler(AthenaExc);
2015-05-20 05:22:32 +00:00
/* Basic usage check */
if (argc == 1)
{
printHelp(argv[0]);
2015-07-22 19:14:50 +00:00
#if WIN_PAUSE
system("PAUSE");
#endif
return 0;
}
else if (argc == 0)
{
2015-06-10 02:40:03 +00:00
printHelp(_S("hecl"));
2015-07-22 19:14:50 +00:00
#if WIN_PAUSE
system("PAUSE");
#endif
return 0;
}
/* Prepare DataSpecs */
HECLRegisterDataSpecs();
2015-05-20 05:22:32 +00:00
/* Assemble common tool pass info */
2015-06-10 02:40:03 +00:00
ToolPassInfo info;
2015-05-20 05:22:32 +00:00
info.pname = argv[0];
2015-07-22 19:14:50 +00:00
HECL::SystemChar cwdbuf[1024];
if (HECL::Getcwd(cwdbuf, 1024))
{
2015-05-27 09:09:05 +00:00
info.cwd = cwdbuf;
if (info.cwd.size() && info.cwd.back() != _S('/') && info.cwd.back() != _S('\\'))
#if _WIN32
info.cwd += _S('\\');
#else
info.cwd += _S('/');
#endif
}
2015-05-20 05:22:32 +00:00
/* Concatenate args */
2015-10-04 04:35:18 +00:00
std::vector<HECL::SystemString> args;
args.reserve(argc-2);
2015-05-20 05:22:32 +00:00
for (int i=2 ; i<argc ; ++i)
2015-06-10 02:40:03 +00:00
args.push_back(HECL::SystemString(argv[i]));
2015-05-20 05:22:32 +00:00
if (!args.empty())
{
/* Extract output argument */
2015-10-04 04:35:18 +00:00
for (auto it = args.cbegin() ; it != args.cend() ;)
2015-05-20 05:22:32 +00:00
{
2015-06-10 02:40:03 +00:00
const HECL::SystemString& arg = *it;
HECL::SystemRegexMatch oMatch;
2015-05-26 04:42:20 +00:00
if (std::regex_search(arg, oMatch, regOPEN))
{
2015-06-10 02:40:03 +00:00
const HECL::SystemString& token = oMatch[1].str();
2015-05-26 04:42:20 +00:00
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;
2015-05-20 05:22:32 +00:00
}
2015-10-16 00:34:47 +00:00
/* Iterate flags */
2015-10-04 04:35:18 +00:00
for (auto it = args.cbegin() ; it != args.cend() ;)
2015-05-20 05:22:32 +00:00
{
2015-06-10 02:40:03 +00:00
const HECL::SystemString& arg = *it;
2015-10-22 02:01:08 +00:00
if (arg.size() < 2 || arg[0] != _S('-') || arg[1] == _S('-'))
2015-05-26 04:42:20 +00:00
{
2015-10-16 00:34:47 +00:00
++it;
2015-05-26 04:42:20 +00:00
continue;
}
2015-05-20 05:22:32 +00:00
2015-10-16 00:34:47 +00:00
for (auto chit = arg.cbegin() + 1 ; chit != arg.cend() ; ++chit)
2015-05-26 04:42:20 +00:00
{
2015-10-16 00:34:47 +00:00
if (*chit == _S('v'))
++info.verbosityLevel;
else if (*chit == _S('f'))
info.force = true;
else
info.flags.push_back(*chit);
2015-05-26 04:42:20 +00:00
}
2015-10-16 00:34:47 +00:00
it = args.erase(it);
2015-05-20 05:22:32 +00:00
}
/* Gather remaining args */
2015-10-04 04:35:18 +00:00
info.args.reserve(args.size());
2015-06-10 02:40:03 +00:00
for (const HECL::SystemString& arg : args)
2015-05-26 04:42:20 +00:00
info.args.push_back(arg);
2015-05-20 05:22:32 +00:00
}
2015-06-11 04:55:06 +00:00
/* Attempt to find hecl project */
2015-09-30 06:23:07 +00:00
HECL::ProjectRootPath rootPath = HECL::SearchForProject(info.cwd);
2015-06-11 04:55:06 +00:00
std::unique_ptr<HECL::Database::Project> project;
2015-09-30 06:23:07 +00:00
if (rootPath)
2015-06-11 04:55:06 +00:00
{
2015-07-26 02:52:02 +00:00
size_t ErrorRef = LogVisor::ErrorCount;
2015-09-30 06:23:07 +00:00
HECL::Database::Project* newProj = new HECL::Database::Project(rootPath);
2015-07-26 02:52:02 +00:00
if (LogVisor::ErrorCount > ErrorRef)
2015-06-11 04:55:06 +00:00
{
2015-07-22 19:14:50 +00:00
#if WIN_PAUSE
system("PAUSE");
#endif
2015-07-26 02:52:02 +00:00
delete newProj;
2015-06-11 04:55:06 +00:00
return -1;
}
2015-07-26 02:52:02 +00:00
project.reset(newProj);
info.project = newProj;
2015-06-11 04:55:06 +00:00
}
2015-05-20 05:22:32 +00:00
/* Construct selected tool */
2015-06-10 02:40:03 +00:00
HECL::SystemString toolName(argv[1]);
2015-06-11 04:55:06 +00:00
HECL::ToLower(toolName);
std::unique_ptr<ToolBase> tool;
2015-07-26 02:52:02 +00:00
size_t ErrorRef = LogVisor::ErrorCount;
if (toolName == _S("init"))
tool.reset(new ToolInit(info));
else if (toolName == _S("spec"))
tool.reset(new ToolSpec(info));
else if (toolName == _S("extract"))
tool.reset(new ToolExtract(info));
else if (toolName == _S("add"))
tool.reset(new ToolAdd(info));
else if (toolName == _S("remove") || toolName == _S("rm"))
tool.reset(new ToolRemove(info));
else if (toolName == _S("group"))
tool.reset(new ToolGroup(info));
else if (toolName == _S("cook"))
tool.reset(new ToolCook(info));
else if (toolName == _S("clean"))
tool.reset(new ToolClean(info));
else if (toolName == _S("package") || toolName == _S("pack"))
tool.reset(new ToolPackage(info));
else if (toolName == _S("help"))
tool.reset(new ToolHelp(info));
else
{
FILE* fp = HECL::Fopen(argv[1], _S("rb"));
if (!fp)
LogModule.report(LogVisor::Error, _S("unrecognized tool '%s'"), toolName.c_str());
else
{
/* Shortcut-case: implicit extract */
fclose(fp);
2015-10-04 04:35:18 +00:00
info.args.insert(info.args.begin(), argv[1]);
tool.reset(new ToolExtract(info));
}
}
2015-07-26 02:52:02 +00:00
if (LogVisor::ErrorCount > ErrorRef)
2015-05-20 05:22:32 +00:00
{
2015-07-22 19:14:50 +00:00
#if WIN_PAUSE
system("PAUSE");
2015-07-16 02:03:38 +00:00
#endif
2015-05-20 05:22:32 +00:00
return -1;
}
if (info.verbosityLevel)
2015-07-05 06:27:24 +00:00
LogModule.report(LogVisor::Info, _S("Constructed tool '%s' %d\n"),
tool->toolName().c_str(), info.verbosityLevel);
2015-05-20 05:22:32 +00:00
/* Run tool */
2015-07-26 02:52:02 +00:00
ErrorRef = LogVisor::ErrorCount;
int retval = tool->run();
if (LogVisor::ErrorCount > ErrorRef)
2015-05-20 05:22:32 +00:00
{
2015-07-28 02:25:33 +00:00
HECL::BlenderConnection::Shutdown();
2015-07-22 19:14:50 +00:00
#if WIN_PAUSE
system("PAUSE");
2015-07-16 02:03:38 +00:00
#endif
2015-05-20 05:22:32 +00:00
return -1;
}
2015-07-28 02:25:33 +00:00
HECL::BlenderConnection::Shutdown();
2015-07-22 19:14:50 +00:00
#if WIN_PAUSE
system("PAUSE");
#endif
2015-05-20 05:22:32 +00:00
return retval;
2015-05-15 22:39:43 +00:00
}