Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include <HDMapping/Assert.h>
#include <HDMapping/HDMAPPING_ConfigureInfo.hpp>
#include <HDMapping/SessionPaths.h>

#ifdef _WIN32
#include "resource.h"
Expand Down Expand Up @@ -1293,7 +1294,7 @@ bool save_project_settings(const std::string& file_name, const ProjectSettings&
nlohmann::json jsession_file_names;
for (const auto& pc : _project_settings.session_file_names)
{
nlohmann::json jfn{ { "session_file_name", pc } };
nlohmann::json jfn{ { "session_file_name", toSessionPath(pc, file_name) } };
jsession_file_names.push_back(jfn);
}
jj["session_file_names"] = jsession_file_names;
Expand Down Expand Up @@ -1481,7 +1482,7 @@ bool loadProject(const std::string& file_name, ProjectSettings& _project_setting

for (const auto& fn_json : data["session_file_names"])
{
const std::string fn = fn_json["session_file_name"];
const std::string fn = resolveProjectPath(fn_json["session_file_name"].get<std::string>(), file_name);
_project_settings.session_file_names.push_back(fn);
std::cout << "'" << fn << "'";
if (!fs::exists(fn))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <portable-file-dialogs.h>

#include <HDMapping/HDMAPPING_ConfigureInfo.hpp>
#include <HDMapping/SessionPaths.h>

#ifdef _WIN32
#include "resource.h"
Expand Down Expand Up @@ -326,7 +327,7 @@ bool save_project_settings(const std::string& file_name, const ProjectSettings&
nlohmann::json jsession_file_names;
for (const auto& pc : _project_settings.session_file_names)
{
nlohmann::json jfn{ { "session_file_name", pc } };
nlohmann::json jfn{ { "session_file_name", toSessionPath(pc, file_name) } };
jsession_file_names.push_back(jfn);
}
jj["session_file_names"] = jsession_file_names;
Expand Down Expand Up @@ -443,7 +444,7 @@ bool loadProject(const std::string& file_name, ProjectSettings& _project_setting

for (const auto& fn_json : data["session_file_names"])
{
const std::string fn = fn_json["session_file_name"];
const std::string fn = resolveProjectPath(fn_json["session_file_name"].get<std::string>(), file_name);
_project_settings.session_file_names.push_back(fn);
std::cout << "'" << fn << "'";
if (!fs::exists(fn))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <portable-file-dialogs.h>

#include <HDMapping/HDMAPPING_ConfigureInfo.hpp>
#include <HDMapping/SessionPaths.h>

#ifdef _WIN32
#include "resource.h"
Expand Down Expand Up @@ -1617,7 +1618,7 @@ bool save_project_settings(const std::string& file_name, const ProjectSettings&
nlohmann::json jsession_file_names;
for (const auto& pc : _project_settings.session_file_names)
{
nlohmann::json jfn{ { "session_file_name", pc } };
nlohmann::json jfn{ { "session_file_name", toSessionPath(pc, file_name) } };
jsession_file_names.push_back(jfn);
}
jj["session_file_names"] = jsession_file_names;
Expand Down Expand Up @@ -1749,7 +1750,7 @@ bool loadProject(const std::string& file_name, ProjectSettings& _project_setting

for (const auto& fn_json : data["session_file_names"])
{
const std::string fn = fn_json["session_file_name"];
const std::string fn = resolveProjectPath(fn_json["session_file_name"].get<std::string>(), file_name);
_project_settings.session_file_names.push_back(fn);
std::cout << "'" << fn << "'";
if (!fs::exists(fn))
Expand Down
49 changes: 38 additions & 11 deletions shared/include/HDMapping/SessionPaths.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
// store the paths of the files they reference relative to the session file's
// folder, so a session keeps working after its folder is moved or copied to
// another computer. In memory the paths stay absolute: writers convert with
// toSessionPath(), readers with resolveSessionPath().
// toSessionPath(), readers with resolveSessionPath(). Step 3 projects (.mjp)
// store their session files the same way and read them with resolveProjectPath().

// Path to write into session_file for `path`: relative to the session file's
// folder with '/' separators, or absolute when there is no relative path (e.g.
Expand All @@ -32,25 +33,51 @@ inline std::string toSessionPath(const std::string& path, const std::string& ses
return (relative_path.empty() ? absolute_path : relative_path).generic_string();
}

// Absolute path for a path read from session_file. Relative paths resolve
// against the session file's folder, absolute ones (older sessions) are used as
// they are. If the result doesn't exist, the file of the same name next to the
// session file is used instead, so sessions moved together with their data keep
// loading.
inline std::string resolveSessionPath(const std::string& stored_path, const std::string& session_file)
//! Absolute path for a path read from `file`: relative paths resolve against the
//! folder of `file`, absolute ones (older files) are used as they are.
inline std::string resolveStoredPath(const std::string& stored_path, const std::string& file)
{
namespace fs = std::filesystem;
// Sessions written on Windows use '\' separators
// Files written on Windows use '\' separators
std::string normalized = stored_path;
std::replace(normalized.begin(), normalized.end(), '\\', '/');
const fs::path path(normalized);

std::error_code ec;
const fs::path session_dir = fs::absolute(session_file, ec).parent_path();
const fs::path resolved = path.is_absolute() ? path : (session_dir / path).lexically_normal();
const fs::path dir = fs::absolute(file, ec).parent_path();
return (path.is_absolute() ? path : (dir / path).lexically_normal()).string();
}

// Absolute path for a path read from session_file, see resolveStoredPath(). If
// the result doesn't exist, the file of the same name next to the session file
// is used instead, so sessions moved together with their data keep loading.
inline std::string resolveSessionPath(const std::string& stored_path, const std::string& session_file)
{
namespace fs = std::filesystem;
const fs::path resolved = resolveStoredPath(stored_path, session_file);

std::error_code ec;
if (fs::exists(resolved, ec))
{
return resolved.string();
}
return (session_dir / path.filename()).string();
return (fs::absolute(session_file, ec).parent_path() / resolved.filename()).string();
}

//! Absolute path of a session file read from a step 3 project, see
//! resolveStoredPath(). Older projects kept paths given on the command line as
//! they were, relative to the current folder; such a path is used when the
//! project-relative one doesn't exist.
inline std::string resolveProjectPath(const std::string& stored_path, const std::string& project_file)
{
namespace fs = std::filesystem;
const std::string resolved = resolveStoredPath(stored_path, project_file);

std::error_code ec;
if (!fs::exists(resolved, ec) && fs::exists(stored_path, ec))
{
const fs::path absolute_path = fs::absolute(stored_path, ec);
return ec ? stored_path : absolute_path.string();
}
return resolved;
}
Loading