metaforce/Editor/ProjectManager.cpp

201 lines
5.4 KiB
C++
Raw Normal View History

2015-12-13 21:01:32 +00:00
#include "ProjectManager.hpp"
2016-01-04 05:31:02 +00:00
#include "ViewManager.hpp"
#include "DataSpecRegistry.hpp"
2017-12-29 08:08:12 +00:00
#include "hecl/Blender/Connection.hpp"
#include "version.h"
2015-12-13 21:01:32 +00:00
2018-12-08 05:30:43 +00:00
namespace urde {
2016-03-04 23:04:53 +00:00
static logvisor::Module Log("URDE::ProjectManager");
2016-04-14 21:42:47 +00:00
ProjectManager* ProjectManager::g_SharedManager = nullptr;
2016-01-04 05:31:02 +00:00
2018-12-08 05:30:43 +00:00
CToken ProjectResourcePool::GetObj(std::string_view name) {
CToken ret = CSimplePool::GetObj(name);
if (ret)
return ret;
2018-12-08 05:30:43 +00:00
hecl::ProjectPath path(*m_parent.project(), name);
SObjectTag tag =
static_cast<ProjectResourceFactoryBase&>(x18_factory).TagFromPath(path);
2018-12-08 05:30:43 +00:00
if (tag)
return CSimplePool::GetObj(tag);
2018-12-08 05:30:43 +00:00
return {};
}
2018-12-08 05:30:43 +00:00
CToken ProjectResourcePool::GetObj(std::string_view name, const CVParamTransfer& pvxfer) {
CToken ret = CSimplePool::GetObj(name, pvxfer);
if (ret)
return ret;
2018-12-08 05:30:43 +00:00
hecl::ProjectPath path(*m_parent.project(), name);
SObjectTag tag =
static_cast<ProjectResourceFactoryBase&>(x18_factory).TagFromPath(path);
2018-12-08 05:30:43 +00:00
if (tag)
return CSimplePool::GetObj(tag, pvxfer);
2018-12-08 05:30:43 +00:00
return {};
}
2016-01-04 05:31:02 +00:00
bool ProjectManager::m_registeredSpecs = false;
2018-12-08 05:30:43 +00:00
ProjectManager::ProjectManager(ViewManager& vm)
: m_vm(vm), m_clientProc(nullptr), m_factoryMP1(m_clientProc), m_objStore(m_factoryMP1, *this) {
if (!m_registeredSpecs) {
HECLRegisterDataSpecs();
m_registeredSpecs = true;
}
g_SharedManager = this;
2016-01-04 05:31:02 +00:00
}
2018-12-08 05:30:43 +00:00
bool ProjectManager::newProject(hecl::SystemStringView path) {
hecl::ProjectRootPath projPath = hecl::SearchForProject(path);
if (projPath) {
2020-04-11 22:51:39 +00:00
Log.report(logvisor::Warning, FMT_STRING(_SYS_STR("project already exists at '{}'")), path);
2018-12-08 05:30:43 +00:00
return false;
}
2016-01-04 05:31:02 +00:00
2018-12-08 05:30:43 +00:00
hecl::MakeDir(path.data());
m_proj = std::make_unique<hecl::Database::Project>(path);
2018-12-08 05:30:43 +00:00
if (!*m_proj) {
m_proj.reset();
return false;
}
2016-01-04 05:31:02 +00:00
2018-12-08 05:30:43 +00:00
m_vm.ProjectChanged(*m_proj);
m_vm.SetupEditorView();
saveProject();
2016-03-07 03:12:32 +00:00
m_vm.m_mainWindow->setTitle(fmt::format(FMT_STRING(_SYS_STR("{} - URDE {} [{}]")),
2021-03-29 22:32:55 +00:00
m_proj->getProjectRootPath().getLastComponent(), URDE_WC_DESCRIBE_SYS, m_vm.platformName()));
2018-12-08 05:30:43 +00:00
m_vm.DismissSplash();
m_vm.FadeInEditors();
2016-01-05 00:01:02 +00:00
2018-12-08 05:30:43 +00:00
return true;
2016-01-04 05:31:02 +00:00
}
2018-12-08 05:30:43 +00:00
bool ProjectManager::openProject(hecl::SystemStringView path) {
hecl::SystemString subPath;
hecl::ProjectRootPath projPath = hecl::SearchForProject(path, subPath);
if (!projPath) {
2020-04-11 22:51:39 +00:00
Log.report(logvisor::Warning, FMT_STRING(_SYS_STR("project doesn't exist at '{}'")), path);
2018-12-08 05:30:43 +00:00
return false;
}
2016-01-04 05:31:02 +00:00
m_proj = std::make_unique<hecl::Database::Project>(projPath);
2018-12-08 05:30:43 +00:00
if (!*m_proj) {
m_proj.reset();
return false;
}
athena::io::YAMLDocReader r;
const auto makeProj = [this, &r, &subPath](bool needsSave) {
m_vm.ProjectChanged(*m_proj);
if (needsSave)
m_vm.SetupEditorView();
else
m_vm.SetupEditorView(r);
const bool doRun = hecl::StringUtils::BeginsWith(subPath, _SYS_STR("out"));
if (doRun) {
m_mainMP1.emplace(nullptr, nullptr, m_vm.m_mainBooFactory, m_vm.m_mainCommandQueue, m_vm.m_renderTex);
m_vm.InitMP1(*m_mainMP1);
}
if (needsSave)
saveProject();
m_vm.m_mainWindow->setTitle(fmt::format(FMT_STRING(_SYS_STR("{} - URDE {} [{}]")),
2021-03-29 22:32:55 +00:00
m_proj->getProjectRootPath().getLastComponent(), URDE_WC_DESCRIBE_SYS,
m_vm.platformName()));
m_vm.DismissSplash();
m_vm.FadeInEditors();
m_vm.pushRecentProject(m_proj->getProjectRootPath().getAbsolutePath());
return true;
};
const hecl::ProjectPath urdeSpacesPath(*m_proj, _SYS_STR(".hecl/urde_spaces.yaml"));
2018-12-08 05:30:43 +00:00
athena::io::FileReader reader(urdeSpacesPath.getAbsolutePath());
if (!reader.isOpen()) {
return makeProj(true);
2018-12-08 05:30:43 +00:00
}
const auto readHandler = [](void* data, unsigned char* buffer, size_t size, size_t* size_read) {
auto* const reader = static_cast<athena::io::IStreamReader*>(data);
return athena::io::YAMLAthenaReader(reader, buffer, size, size_read);
};
yaml_parser_set_input(r.getParser(), readHandler, &reader);
2018-12-08 05:30:43 +00:00
if (!r.ValidateClassType("UrdeSpacesState")) {
return makeProj(true);
2018-12-08 05:30:43 +00:00
}
r.reset();
reader.seek(0, athena::SeekOrigin::Begin);
2018-12-08 05:30:43 +00:00
if (!r.parse(&reader)) {
return makeProj(true);
2018-12-08 05:30:43 +00:00
}
2016-01-05 00:01:02 +00:00
return makeProj(false);
2016-01-04 05:31:02 +00:00
}
2015-12-13 21:01:32 +00:00
2018-12-08 05:30:43 +00:00
bool ProjectManager::extractGame(hecl::SystemStringView path) { return false; }
2016-01-05 00:01:02 +00:00
2018-12-08 05:30:43 +00:00
bool ProjectManager::saveProject() {
if (!m_proj)
return false;
2016-01-05 00:01:02 +00:00
2018-12-08 05:30:43 +00:00
hecl::ProjectPath oldSpacesPath(*m_proj, _SYS_STR(".hecl/~urde_spaces.yaml"));
athena::io::FileWriter writer(oldSpacesPath.getAbsolutePath());
if (!writer.isOpen())
return false;
2016-01-05 00:01:02 +00:00
2018-12-08 05:30:43 +00:00
athena::io::YAMLDocWriter w("UrdeSpacesState");
m_vm.SaveEditorView(w);
if (!w.finish(&writer))
return false;
2016-01-05 00:01:02 +00:00
2018-12-08 05:30:43 +00:00
hecl::ProjectPath newSpacesPath(*m_proj, _SYS_STR(".hecl/urde_spaces.yaml"));
2016-01-05 00:01:02 +00:00
2018-12-08 05:30:43 +00:00
hecl::Unlink(newSpacesPath.getAbsolutePath().data());
hecl::Rename(oldSpacesPath.getAbsolutePath().data(), newSpacesPath.getAbsolutePath().data());
2016-01-16 03:58:11 +00:00
2018-12-08 05:30:43 +00:00
m_vm.pushRecentProject(m_proj->getProjectRootPath().getAbsolutePath());
return true;
2016-01-05 00:01:02 +00:00
}
2018-12-08 05:30:43 +00:00
void ProjectManager::mainUpdate() {
if (m_precooking) {
if (!m_factoryMP1.IsBusy())
m_precooking = false;
else
return;
}
2018-12-08 05:30:43 +00:00
if (m_mainMP1) {
if (m_mainMP1->Proc()) {
m_mainMP1->Shutdown();
m_mainMP1 = std::nullopt;
2017-02-06 03:21:58 +00:00
}
2018-12-08 05:30:43 +00:00
}
2016-04-24 04:03:30 +00:00
}
2018-12-08 05:30:43 +00:00
void ProjectManager::mainDraw() {
if (m_precooking)
return;
2018-12-08 05:30:43 +00:00
if (m_mainMP1)
m_mainMP1->Draw();
2016-09-14 05:54:09 +00:00
}
2018-12-08 05:30:43 +00:00
void ProjectManager::shutdown() {
if (m_mainMP1)
m_mainMP1->Shutdown();
m_clientProc.shutdown();
m_factoryMP1.Shutdown();
hecl::blender::Connection::Shutdown();
2016-04-24 04:03:30 +00:00
}
2018-12-08 05:30:43 +00:00
} // namespace urde