metaforce/Editor/main.cpp

237 lines
7.9 KiB
C++
Raw Normal View History

2016-03-04 23:04:53 +00:00
#include "logvisor/logvisor.hpp"
#include "boo/boo.hpp"
#include "specter/specter.hpp"
#include "hecl/CVarManager.hpp"
#include "Runtime/CBasics.hpp"
#include "ViewManager.hpp"
2017-11-19 07:10:54 +00:00
#include "hecl/hecl.hpp"
2018-01-10 06:20:34 +00:00
#include "hecl/CVarCommons.hpp"
2018-01-11 09:38:08 +00:00
#include "hecl/Console.hpp"
#include "fmt/chrono.h"
2015-11-21 01:16:07 +00:00
static logvisor::Module AthenaLog("Athena");
2019-12-14 23:50:29 +00:00
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:27:21 +00:00
AthenaLog.vreport(logvisor::Level(level), fmt, args);
}
2018-12-08 05:30:43 +00:00
namespace urde {
2016-03-05 00:03:41 +00:00
static logvisor::Module Log{"URDE"};
2016-02-15 17:35:20 +00:00
2018-12-08 05:30:43 +00:00
static hecl::SystemString CPUFeatureString(const zeus::CPUInfo& cpuInf) {
hecl::SystemString features;
auto AddFeature = [&features](const hecl::SystemChar* str) {
if (!features.empty())
features += _SYS_STR(", ");
features += str;
};
if (cpuInf.AESNI)
AddFeature(_SYS_STR("AES-NI"));
if (cpuInf.SSE1)
AddFeature(_SYS_STR("SSE"));
if (cpuInf.SSE2)
AddFeature(_SYS_STR("SSE2"));
if (cpuInf.SSE3)
AddFeature(_SYS_STR("SSE3"));
if (cpuInf.SSSE3)
AddFeature(_SYS_STR("SSSE3"));
if (cpuInf.SSE4a)
AddFeature(_SYS_STR("SSE4a"));
if (cpuInf.SSE41)
AddFeature(_SYS_STR("SSE4.1"));
if (cpuInf.SSE42)
AddFeature(_SYS_STR("SSE4.2"));
if (cpuInf.AVX)
AddFeature(_SYS_STR("AVX"));
if (cpuInf.AVX2)
AddFeature(_SYS_STR("AVX2"));
return features;
2017-12-12 02:06:19 +00:00
}
2018-12-08 05:30:43 +00:00
struct Application : boo::IApplicationCallback {
2020-09-06 01:32:12 +00:00
hecl::Runtime::FileStoreManager& m_fileMgr;
hecl::CVarManager& m_cvarManager;
hecl::CVarCommons& m_cvarCommons;
2018-12-08 05:30:43 +00:00
std::unique_ptr<ViewManager> m_viewManager;
2018-12-08 05:30:43 +00:00
std::atomic_bool m_running = {true};
2015-11-22 04:35:24 +00:00
2020-09-06 01:32:12 +00:00
Application(hecl::Runtime::FileStoreManager& fileMgr, hecl::CVarManager& cvarMgr, hecl::CVarCommons& cvarCmns)
: m_fileMgr(fileMgr)
, m_cvarManager(cvarMgr)
, m_cvarCommons(cvarCmns)
2019-12-14 23:50:29 +00:00
, m_viewManager(std::make_unique<ViewManager>(m_fileMgr, m_cvarManager)) {}
2015-11-22 04:35:24 +00:00
2018-12-08 05:30:43 +00:00
virtual ~Application() = default;
int appMain(boo::IApplication* app) override {
2018-12-08 05:30:43 +00:00
initialize(app);
m_viewManager->init(app);
while (m_running.load()) {
if (!m_viewManager->proc())
break;
2015-11-21 01:16:07 +00:00
}
2018-12-08 05:30:43 +00:00
m_viewManager->stop();
m_viewManager->projectManager().saveProject();
m_cvarManager.serialize();
m_viewManager.reset();
return 0;
}
void appQuitting(boo::IApplication*) override { m_running.store(false); }
void appFilesOpen(boo::IApplication*, const std::vector<boo::SystemString>& paths) override {
2018-12-08 05:30:43 +00:00
for (const auto& path : paths) {
hecl::ProjectRootPath projPath = hecl::SearchForProject(path);
if (projPath) {
m_viewManager->deferOpenProject(path);
break;
}
2018-01-07 05:19:49 +00:00
}
2018-12-08 05:30:43 +00:00
}
void initialize(boo::IApplication* app) {
zeus::detectCPU();
2020-09-06 01:32:12 +00:00
2018-12-08 05:30:43 +00:00
for (const boo::SystemString& arg : app->getArgs()) {
if (arg.find(_SYS_STR("--verbosity=")) == 0 || arg.find(_SYS_STR("-v=")) == 0) {
hecl::SystemUTF8Conv utf8Arg(arg.substr(arg.find_last_of('=') + 1));
hecl::VerbosityLevel = atoi(utf8Arg.c_str());
2020-04-11 22:51:39 +00:00
hecl::LogModule.report(logvisor::Info, FMT_STRING("Set verbosity level to {}"), hecl::VerbosityLevel);
2018-12-08 05:30:43 +00:00
}
2018-01-07 05:19:49 +00:00
}
2018-01-11 09:38:08 +00:00
2018-12-08 05:30:43 +00:00
const zeus::CPUInfo& cpuInf = zeus::cpuFeatures();
2020-04-11 22:51:39 +00:00
Log.report(logvisor::Info, FMT_STRING("CPU Name: {}"), cpuInf.cpuBrand);
Log.report(logvisor::Info, FMT_STRING("CPU Vendor: {}"), cpuInf.cpuVendor);
Log.report(logvisor::Info, FMT_STRING(_SYS_STR("CPU Features: {}")), CPUFeatureString(cpuInf));
2018-12-08 05:30:43 +00:00
}
std::string getGraphicsApi() const { return m_cvarCommons.getGraphicsApi(); }
uint32_t getSamples() const { return m_cvarCommons.getSamples(); }
uint32_t getAnisotropy() const { return m_cvarCommons.getAnisotropy(); }
bool getDeepColor() const { return m_cvarCommons.getDeepColor(); }
int64_t getTargetFrameTime() { return m_cvarCommons.getVariableFrameTime() ? 0 : 1000000000L / 60; }
2015-11-21 01:16:07 +00:00
};
2018-12-08 05:30:43 +00:00
} // namespace urde
2015-11-21 01:16:07 +00:00
2017-02-24 08:28:44 +00:00
static hecl::SystemChar CwdBuf[1024];
hecl::SystemString ExeDir;
2018-12-08 05:30:43 +00:00
static void SetupBasics(bool logging) {
auto result = zeus::validateCPU();
if (!result.first) {
2017-12-12 02:06:19 +00:00
#if _WIN32 && !WINDOWS_STORE
2020-04-11 22:51:39 +00:00
std::wstring msg = fmt::format(FMT_STRING(L"ERROR: This build of URDE requires the following CPU features:\n{}\n"),
2019-12-14 23:50:29 +00:00
urde::CPUFeatureString(result.second));
2019-07-28 01:21:31 +00:00
MessageBoxW(nullptr, msg.c_str(), L"CPU error", MB_OK | MB_ICONERROR);
2017-12-12 02:06:19 +00:00
#else
2020-04-11 22:51:39 +00:00
fmt::print(stderr, FMT_STRING("ERROR: This build of URDE requires the following CPU features:\n{}\n"),
2019-07-20 04:27:21 +00:00
urde::CPUFeatureString(result.second));
2017-12-12 02:06:19 +00:00
#endif
2018-12-08 05:30:43 +00:00
exit(1);
}
2017-12-12 02:06:19 +00:00
2018-12-08 05:30:43 +00:00
logvisor::RegisterStandardExceptions();
if (logging)
logvisor::RegisterConsoleLogger();
atSetExceptionHandler(AthenaExc);
2017-10-24 03:12:10 +00:00
}
2018-12-08 05:30:43 +00:00
static bool IsClientLoggingEnabled(int argc, const boo::SystemChar** argv) {
for (int i = 1; i < argc; ++i)
if (!hecl::StrNCmp(argv[i], _SYS_STR("-l"), 2))
return true;
return false;
2017-11-19 07:10:54 +00:00
}
2017-12-06 03:26:15 +00:00
#if !WINDOWS_STORE
2015-11-21 01:16:07 +00:00
#if _WIN32
int wmain(int argc, const boo::SystemChar** argv)
#else
int main(int argc, const boo::SystemChar** argv)
#endif
{
2018-12-08 05:30:43 +00:00
if (argc > 1 && !hecl::StrCmp(argv[1], _SYS_STR("--dlpackage"))) {
2020-04-11 22:51:39 +00:00
fmt::print(FMT_STRING("{}\n"), URDE_DLPACKAGE);
2018-12-08 05:30:43 +00:00
return 100;
}
SetupBasics(IsClientLoggingEnabled(argc, argv));
2020-09-06 01:32:12 +00:00
hecl::Runtime::FileStoreManager fileMgr{_SYS_STR("urde")};
hecl::CVarManager cvarMgr{fileMgr};
hecl::CVarCommons cvarCmns{cvarMgr};
std::vector<boo::SystemString> args;
for (int i = 1; i < argc; ++i)
args.push_back(argv[i]);
cvarMgr.parseCommandLine(args);
2020-09-06 01:32:12 +00:00
hecl::SystemStringView logFile = hecl::SystemStringConv(cvarCmns.getLogFile()).sys_str();
hecl::SystemString logFilePath;
if (!logFile.empty()) {
std::time_t time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
char buf[100];
std::strftime(buf, 100, "%Y-%m-%d_%H-%M-%S", std::localtime(&time));
hecl::SystemString timeStr = hecl::SystemStringConv(buf).c_str();
logFilePath = fmt::format(FMT_STRING(_SYS_STR("{}/{}-{}")), fileMgr.getStoreRoot(), timeStr, logFile);
2020-09-06 01:32:12 +00:00
logvisor::RegisterFileLogger(logFilePath.c_str());
}
2018-12-08 05:30:43 +00:00
if (hecl::SystemChar* cwd = hecl::Getcwd(CwdBuf, 1024)) {
if (hecl::PathRelative(argv[0]))
ExeDir = hecl::SystemString(cwd) + _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);
}
2019-02-04 00:01:44 +00:00
/* Handle -j argument */
hecl::SetCpuCountOverride(argc, argv);
2020-09-06 01:32:12 +00:00
urde::Application appCb(fileMgr, cvarMgr, cvarCmns);
2018-12-08 05:30:43 +00:00
int ret = boo::ApplicationRun(boo::IApplication::EPlatformType::Auto, appCb, _SYS_STR("urde"), _SYS_STR("URDE"), argc,
argv, appCb.getGraphicsApi(), appCb.getSamples(), appCb.getAnisotropy(),
appCb.getDeepColor(), appCb.getTargetFrameTime(), false);
2018-12-08 05:30:43 +00:00
// printf("IM DYING!!\n");
return ret;
2015-11-21 01:16:07 +00:00
}
2017-12-06 03:26:15 +00:00
#endif
2015-11-21 01:16:07 +00:00
2017-12-06 03:26:15 +00:00
#if WINDOWS_STORE
2017-12-07 04:13:12 +00:00
#include "boo/UWPViewProvider.hpp"
2017-10-24 03:12:10 +00:00
using namespace Windows::ApplicationModel::Core;
2018-12-08 05:30:43 +00:00
[Platform::MTAThread] int WINAPIV main(Platform::Array<Platform::String ^> ^ params) {
SetupBasics(false);
urde::Application appCb;
auto viewProvider =
ref new boo::ViewProvider(appCb, _SYS_STR("urde"), _SYS_STR("URDE"), _SYS_STR("urde"), params, false);
CoreApplication::Run(viewProvider);
return 0;
2017-10-24 03:12:10 +00:00
}
#elif _WIN32
2018-12-08 05:30:43 +00:00
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR lpCmdLine, int) {
int argc = 0;
const boo::SystemChar** argv;
if (lpCmdLine[0])
argv = (const wchar_t**)(CommandLineToArgvW(lpCmdLine, &argc));
static boo::SystemChar selfPath[1024];
GetModuleFileNameW(nullptr, selfPath, 1024);
static const boo::SystemChar* booArgv[32] = {};
booArgv[0] = selfPath;
for (int i = 0; i < argc; ++i)
booArgv[i + 1] = argv[i];
const DWORD outType = GetFileType(GetStdHandle(STD_OUTPUT_HANDLE));
if (IsClientLoggingEnabled(argc + 1, booArgv) && outType == FILE_TYPE_UNKNOWN)
2018-12-08 05:30:43 +00:00
logvisor::CreateWin32Console();
return wmain(argc + 1, booArgv);
2015-11-21 01:16:07 +00:00
}
#endif