metaforce/Editor/ProjectManager.cpp

176 lines
4.1 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"
2015-12-13 21:01:32 +00:00
2016-01-05 09:53:16 +00:00
namespace URDE
2015-12-13 21:01:32 +00:00
{
2016-01-05 09:53:16 +00:00
static LogVisor::LogModule Log("URDE::ProjectManager");
2016-01-04 05:31:02 +00:00
bool ProjectManager::m_registeredSpecs = false;
ProjectManager::ProjectManager(ViewManager &vm)
: m_vm(vm)
{
if (!m_registeredSpecs)
{
HECLRegisterDataSpecs();
m_registeredSpecs = true;
}
}
bool ProjectManager::newProject(const HECL::SystemString& path)
{
HECL::ProjectRootPath projPath = HECL::SearchForProject(path);
if (projPath)
{
Log.report(LogVisor::Warning, _S("project already exists at '%s'"), path.c_str());
return false;
}
2016-01-05 00:01:02 +00:00
HECL::MakeDir(path.c_str());
2016-01-04 05:31:02 +00:00
m_proj.reset(new HECL::Database::Project(path));
if (!*m_proj)
{
m_proj.reset();
return false;
}
2016-01-05 00:01:02 +00:00
m_vm.SetupEditorView();
saveProject();
m_vm.m_mainWindow->setTitle(m_proj->getProjectRootPath().getLastComponent());
m_vm.DismissSplash();
m_vm.FadeInEditors();
2016-01-04 05:31:02 +00:00
return true;
}
bool ProjectManager::openProject(const HECL::SystemString& path)
{
HECL::ProjectRootPath projPath = HECL::SearchForProject(path);
if (!projPath)
{
Log.report(LogVisor::Warning, _S("project doesn't exist at '%s'"), path.c_str());
return false;
}
m_proj.reset(new HECL::Database::Project(projPath));
if (!*m_proj)
{
m_proj.reset();
return false;
}
2016-01-05 09:53:16 +00:00
#ifdef URDE_BINARY_CONFIGS
HECL::ProjectPath urdeSpacesPath(*m_proj, _S(".hecl/urde_spaces.bin"));
Athena::io::FileReader r(urdeSpacesPath.getAbsolutePath(), 32 * 1024, false);
2016-01-05 00:01:02 +00:00
if (r.hasError())
goto makeDefault;
m_vm.SetupEditorView(r);
#else
2016-01-05 09:53:16 +00:00
HECL::ProjectPath urdeSpacesPath(*m_proj, _S(".hecl/urde_spaces.yaml"));
FILE* fp = HECL::Fopen(urdeSpacesPath.getAbsolutePath().c_str(), _S("r"));
2016-01-05 00:01:02 +00:00
Athena::io::YAMLDocReader r;
if (!fp)
goto makeDefault;
yaml_parser_set_input_file(r.getParser(), fp);
2016-01-05 09:53:16 +00:00
if (!r.ValidateClassType(r.getParser(), "UrdeSpacesState"))
2016-01-04 05:31:02 +00:00
{
2016-01-05 00:01:02 +00:00
fclose(fp);
goto makeDefault;
}
2016-01-04 05:31:02 +00:00
2016-01-05 00:01:02 +00:00
r.reset();
fseek(fp, 0, SEEK_SET);
yaml_parser_set_input_file(r.getParser(), fp);
if (!r.parse())
{
fclose(fp);
goto makeDefault;
2016-01-04 05:31:02 +00:00
}
2016-01-05 00:01:02 +00:00
fclose(fp);
m_vm.SetupEditorView(r);
#endif
2016-01-04 05:31:02 +00:00
2016-01-05 00:01:02 +00:00
m_vm.m_mainWindow->setTitle(m_proj->getProjectRootPath().getLastComponent());
m_vm.DismissSplash();
m_vm.FadeInEditors();
2016-01-16 03:58:11 +00:00
m_vm.pushRecentProject(m_proj->getProjectRootPath().getAbsolutePath());
2016-01-05 00:01:02 +00:00
return true;
makeDefault:
m_vm.SetupEditorView();
saveProject();
m_vm.m_mainWindow->setTitle(m_proj->getProjectRootPath().getLastComponent());
m_vm.DismissSplash();
m_vm.FadeInEditors();
2016-01-04 05:31:02 +00:00
return true;
}
bool ProjectManager::extractGame(const HECL::SystemString& path)
{
return false;
}
2015-12-13 21:01:32 +00:00
2016-01-05 00:01:02 +00:00
bool ProjectManager::saveProject()
{
if (!m_proj)
return false;
2016-01-07 00:40:27 +00:00
#ifdef URDE_BINARY_CONFIGS
2016-01-05 09:53:16 +00:00
HECL::ProjectPath oldSpacesPath(*m_proj, _S(".hecl/~urde_spaces.bin"));
2016-01-05 04:20:45 +00:00
Athena::io::FileWriter w(oldSpacesPath.getAbsolutePath(), true, false);
if (w.hasError())
2016-01-05 00:01:02 +00:00
return false;
2016-01-05 04:20:45 +00:00
m_vm.SaveEditorView(w);
w.close();
2016-01-05 09:53:16 +00:00
HECL::ProjectPath newSpacesPath(*m_proj, _S(".hecl/urde_spaces.bin"));
2016-01-05 00:01:02 +00:00
#else
2016-01-05 09:53:16 +00:00
HECL::ProjectPath oldSpacesPath(*m_proj, _S(".hecl/~urde_spaces.yaml"));
2016-01-05 00:01:02 +00:00
FILE* fp = HECL::Fopen(oldSpacesPath.getAbsolutePath().c_str(), _S("w"));
if (!fp)
return false;
2016-01-05 09:53:16 +00:00
Athena::io::YAMLDocWriter w("UrdeSpacesState");
2016-01-05 00:01:02 +00:00
yaml_emitter_set_output_file(w.getEmitter(), fp);
if (!w.open())
{
fclose(fp);
return false;
}
m_vm.SaveEditorView(w);
if (!w.finish())
{
fclose(fp);
return false;
}
w.close();
fclose(fp);
2016-01-05 09:53:16 +00:00
HECL::ProjectPath newSpacesPath(*m_proj, _S(".hecl/urde_spaces.yaml"));
2016-01-05 00:01:02 +00:00
2016-01-05 04:20:45 +00:00
#endif
2016-01-05 00:01:02 +00:00
HECL::Unlink(newSpacesPath.getAbsolutePath().c_str());
HECL::Rename(oldSpacesPath.getAbsolutePath().c_str(),
newSpacesPath.getAbsolutePath().c_str());
2016-01-16 03:58:11 +00:00
m_vm.pushRecentProject(m_proj->getProjectRootPath().getAbsolutePath());
2016-01-05 00:01:02 +00:00
return true;
}
2015-12-13 21:01:32 +00:00
}