Skip to content

File ProjectPaths.hpp

File List > Assets > ProjectPaths.hpp

Go to the documentation of this file

#ifndef ENGINE_ASSETS_PROJECTPATHS_HPP_
#define ENGINE_ASSETS_PROJECTPATHS_HPP_

#include "ECS/Registry.hpp"
#include <filesystem>
#include <string>

namespace Engine {

struct ProjectContext {
    std::string projectFolder;
    ProjectContext() = default;
    explicit ProjectContext(std::string folder) : projectFolder(std::move(folder)) {}
};

inline std::filesystem::path ResolveAssetPath(Registry& registry, const std::string& path) {
    if (path.empty()) {
        return path;
    }
    std::filesystem::path p(path);
    if (p.is_absolute() && std::filesystem::exists(p)) {
        return p;
    }
    if (registry.HasResource<ProjectContext>()) {
        const auto& proj = registry.GetResource<ProjectContext>().projectFolder;
        if (!proj.empty()) {
            std::filesystem::path full = std::filesystem::path(proj) / p;
            if (std::filesystem::exists(full)) {
                return full;
            }
            std::filesystem::path fromFilename = std::filesystem::path(proj) / p.filename();
            if (std::filesystem::exists(fromFilename)) {
                return fromFilename;
            }
            return full;
        }
    }
    if (std::filesystem::exists(p)) {
        return p;
    }
    return p;
}

inline std::filesystem::path ResolveProjectPath(Registry& registry, const std::string& path) {
    return ResolveAssetPath(registry, path);
}

inline std::string ToProjectRelativePath(Registry& registry, const std::string& path) {
    if (path.empty()) {
        return path;
    }
    if (registry.HasResource<ProjectContext>()) {
        const auto& proj = registry.GetResource<ProjectContext>().projectFolder;
        if (!proj.empty()) {
            std::error_code ec;
            std::filesystem::path rel = std::filesystem::relative(path, proj, ec);
            if (!ec && !rel.empty() && rel.string().find("..") == std::string::npos) {
                return rel.string();
            }
            std::filesystem::path fromFilename =
                std::filesystem::path(proj) / std::filesystem::path(path).filename();
            if (std::filesystem::exists(fromFilename)) {
                return std::filesystem::path(path).filename().string();
            }
        }
    }
    return path;
}

} // namespace Engine

#endif // ENGINE_ASSETS_PROJECTPATHS_HPP_