Skip to content

Commit

Permalink
feat(core): add real path helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Milerius committed Sep 16, 2019
1 parent b86fc3d commit 9d2f03c
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 4 deletions.
10 changes: 6 additions & 4 deletions modules/core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
##! shared sources between the module and his unit tests
add_library(antara_core_shared_sources INTERFACE)
target_include_directories(antara_core_shared_sources INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(antara_core_shared_sources INTERFACE antara::default_settings)
add_library(antara_core_shared_sources STATIC)
target_sources(antara_core_shared_sources PRIVATE antara/gaming/core/real.path.cpp)
target_include_directories(antara_core_shared_sources PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(antara_core_shared_sources PUBLIC antara::default_settings)
add_library(antara::core ALIAS antara_core_shared_sources)

##! antara core tests
add_executable(antara_core_tests)
target_sources(antara_core_tests PUBLIC
antara/gaming/core/antara.core.tests.cpp
antara/gaming/core/antara.core.version.tests.cpp)
antara/gaming/core/antara.core.version.tests.cpp
antara/gaming/core/antara.core.real.path.tests.cpp)
target_link_libraries(antara_core_tests PRIVATE doctest PUBLIC antara::core)
set_target_properties(antara_core_tests
PROPERTIES
Expand Down
35 changes: 35 additions & 0 deletions modules/core/antara/gaming/core/antara.core.real.path.tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/******************************************************************************
* Copyright © 2013-2019 The Komodo Platform Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Komodo Platform software, including this file may be copied, modified, *
* propagated or distributed except according to the terms contained in the *
* LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/

#include <doctest/doctest.h>
#include "antara/gaming/core/real.path.hpp"

namespace antara::gaming::core::tests
{
TEST_CASE("binary_real_path not empty")
{
auto result = binary_real_path().string();
MESSAGE("binary real path " << result);
CHECK_FALSE(result.empty());
}

TEST_CASE("assets_real_path not empty")
{
auto result = assets_real_path().string();
MESSAGE("assets real path " << result);
CHECK_FALSE(result.empty());
}
}
32 changes: 32 additions & 0 deletions modules/core/antara/gaming/core/details/emscripten/real.path.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/******************************************************************************
* Copyright © 2013-2019 The Komodo Platform Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Komodo Platform software, including this file may be copied, modified, *
* propagated or distributed except according to the terms contained in the *
* LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/

#pragma once

#include <filesystem>

namespace antara::gaming::core::details
{
std::filesystem::path binary_real_path() noexcept
{
return std::filesystem::current_path();
}

std::filesystem::path assets_real_path() noexcept
{
return binary_real_path() / "assets";
}
}
34 changes: 34 additions & 0 deletions modules/core/antara/gaming/core/details/linux/real.path.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/******************************************************************************
* Copyright © 2013-2019 The Komodo Platform Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Komodo Platform software, including this file may be copied, modified, *
* propagated or distributed except according to the terms contained in the *
* LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/

#pragma once

#include <filesystem>
#include <string>
#include <unistd.h> //! getpid()

namespace antara::gaming::core::details
{
std::filesystem::path binary_real_path() noexcept
{
return std::filesystem::read_symlink("/proc/" + std::to_string(getpid()) + "/exe");
}

std::filesystem::path assets_real_path() noexcept
{
return binary_real_path().parent_path().parent_path() / "share/assets/";
}
}
57 changes: 57 additions & 0 deletions modules/core/antara/gaming/core/details/osx/real.path.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/******************************************************************************
* Copyright © 2013-2019 The Komodo Platform Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Komodo Platform software, including this file may be copied, modified, *
* propagated or distributed except according to the terms contained in the *
* LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/

#pragma once

#include <mach-o/dyld.h>
#include <climits>
#include <string>
#include <filesystem>
#include <array>

namespace antara::gaming::core::details
{
static std::string &replace_all_mute(std::string &s,
const std::string &from, const std::string &to) noexcept
{
if (!from.empty())
for (std::size_t pos = 0; (pos = s.find(from, pos) + 1); pos += to.size())
s.replace(--pos, from.size(), to);
return s;
}

static std::string replace_all_copy(std::string s,
const std::string &from, const std::string &to) noexcept
{
return replace_all_mute(s, from, to);
}

std::filesystem::path binary_real_path() noexcept
{
std::array<char, PATH_MAX + 1> dir_name_buffer{};
auto size = static_cast<uint32_t>(dir_name_buffer.size());
[[maybe_unused]] int result = _NSGetExecutablePath(dir_name_buffer.data(), &size);
assert(result == 0);
std::string tmp_path(dir_name_buffer.data());
auto final_path = replace_all_copy(tmp_path, "./", "");
return std::filesystem::path(final_path);
}

std::filesystem::path assets_real_path() noexcept
{
return binary_real_path().parent_path().parent_path() / "Resources/assets";
}
}
38 changes: 38 additions & 0 deletions modules/core/antara/gaming/core/details/windows/real.path.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/******************************************************************************
* Copyright © 2013-2019 The Komodo Platform Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Komodo Platform software, including this file may be copied, modified, *
* propagated or distributed except according to the terms contained in the *
* LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/

#pragma once

#include <windows.h> //! HMODULE, GetModuleHandleW, GetModuleFileNameW
#include <filesystem>
#include <string>

namespace nephtys::resources::details
{
std::filesystem::path binary_real_path() noexcept
{
HMODULE hModule = GetModuleHandleW(NULL);
assert(hModule != nullptr);
WCHAR path[MAX_PATH];
auto result = GetModuleFileNameW(hModule, path, MAX_PATH);
return std::filesystem::path(path);
}

std::filesystem::path assets_real_path() noexcept
{
return binary_real_path().parent_path() / "assets";
}
}
46 changes: 46 additions & 0 deletions modules/core/antara/gaming/core/real.path.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/******************************************************************************
* Copyright © 2013-2019 The Komodo Platform Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Komodo Platform software, including this file may be copied, modified, *
* propagated or distributed except according to the terms contained in the *
* LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/

#include "antara/gaming/core/real.path.hpp"

#ifdef _WIN32

#include "antara/gaming/core/details/windows/real.path.hpp"

#elif __APPLE__

#include "antara/gaming/core/details/osx/real.path.hpp"

#elif __linux__

#include "nephtys/resources/details/linux/real.path.hpp"

#elif EMSCRIPTEN
#include "antara/gaming/core/details/emscripten/real.path.hpp"
#endif

namespace antara::gaming::core
{
std::filesystem::path binary_real_path() noexcept
{
return details::binary_real_path();
}

std::filesystem::path assets_real_path() noexcept
{
return details::assets_real_path();
}
}
25 changes: 25 additions & 0 deletions modules/core/antara/gaming/core/real.path.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/******************************************************************************
* Copyright © 2013-2019 The Komodo Platform Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Komodo Platform software, including this file may be copied, modified, *
* propagated or distributed except according to the terms contained in the *
* LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/

#pragma once

#include <filesystem>

namespace antara::gaming::core
{
std::filesystem::path binary_real_path() noexcept;
std::filesystem::path assets_real_path() noexcept;
}

0 comments on commit 9d2f03c

Please sign in to comment.