Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

2.8 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file    PathResolver.cpp
 * @brief   Cross-platform path resolution implementation
 *
 * @author  ESEngine Team
 * @date    2026
 *
 * @copyright Copyright (c) 2026 ESEngine Team
 *            Licensed under the Apache License, Version 2.0.
 */

#include "PathResolver.hpp"
#include "../core/Log.hpp"

#include <algorithm>

namespace esengine {

std::string PathResolver::editorRoot_;
std::string PathResolver::projectRoot_;
bool PathResolver::initialized_ = false;

void PathResolver::init() {
    if (initialized_) return;

    // Web has no executable/source directory; paths resolve relative to the VFS root.
    editorRoot_ = getExecutableDirectory();
    projectRoot_ = editorRoot_;
    ES_LOG_INFO("PathResolver initialized - Editor root: {}", editorRoot_);

    initialized_ = true;
}

void PathResolver::shutdown() {
    if (!initialized_) return;

    editorRoot_.clear();
    projectRoot_.clear();
    initialized_ = false;
}

void PathResolver::setProjectRoot(const std::string& path) {
    projectRoot_ = normalizePath(path);
    ES_LOG_INFO("Project root set to: {}", projectRoot_);
}

const std::string& PathResolver::getProjectRoot() {
    return projectRoot_;
}

const std::string& PathResolver::getEditorRoot() {
    return editorRoot_;
}

std::string PathResolver::resolve(const std::string& path, PathType type) {
    if (type == PathType::Absolute || isAbsolutePath(path)) {
        return normalizePath(path);
    }

    std::string basePath;
    switch (type) {
        case PathType::Editor:
            basePath = editorRoot_;
            break;
        case PathType::Project:
            basePath = projectRoot_;
            break;
        default:
            basePath = editorRoot_;
            break;
    }

    if (basePath.empty()) {
        return normalizePath(path);
    }

    return normalizePath(basePath + "/" + path);
}

std::string PathResolver::editorPath(const std::string& relativePath) {
    return resolve(relativePath, PathType::Editor);
}

std::string PathResolver::projectPath(const std::string& relativePath) {
    return resolve(relativePath, PathType::Project);
}

bool PathResolver::isAbsolutePath(const std::string& path) {
    if (path.empty()) return false;
    return path[0] == '/';
}

std::string PathResolver::getExecutableDirectory() {
    // Web has no executable directory; assets resolve from the VFS root.
    return "";
}

std::string PathResolver::normalizePath(const std::string& path) {
    if (path.empty()) return path;

    std::string result = path;

    std::replace(result.begin(), result.end(), '\\', '/');

    while (result.length() > 1 && result.back() == '/') {
        result.pop_back();
    }

    return result;
}

}  // namespace esengine