Radium Engine  1.5.0
Resources.cpp
1 #include <Core/Resources/Resources.hpp>
2 
3 #include <Core/Utils/Log.hpp>
4 #include <Core/Utils/StdFilesystem.hpp>
5 
6 #include <algorithm>
7 #include <cpplocate/cpplocate.h>
8 #include <filesystem>
9 #include <stack>
10 
11 namespace Ra {
12 namespace Core {
13 namespace Resources {
14 
15 using namespace Ra::Core::Utils;
16 namespace fs = ::std::filesystem;
17 
18 // see https://www.doxygen.nl/manual/commands.html#cmdcond
21 fs::path searchPath( const std::string& pattern, const std::string& system, void* libSymbol ) {
22  std::string p = cpplocate::locatePath( pattern, system, libSymbol );
23  return fs::path( p ).lexically_normal();
24 }
25 
27 fs::path clean( const fs::path& path ) {
28  auto status = fs::status( path );
29  if ( status.type() == fs::file_type::not_found ) return "";
30  if ( status.type() == fs::file_type::directory ) return fs::canonical( path ) / "";
31  return fs::canonical( path );
32 }
34 
35 optional<std::string> getRadiumResourcesPath() {
36  auto p =
37  searchPath( "Resources/Radium/", "", reinterpret_cast<void*>( getRadiumResourcesPath ) );
38  p = clean( p / "Resources/Radium" );
39 
40  if ( p.empty() ) return {};
41  return p.string();
42 }
43 
44 optional<std::string> getRadiumPluginsPath() {
45  auto p = searchPath( "Plugins/lib", "", reinterpret_cast<void*>( getRadiumPluginsPath ) );
46  p = clean( p / "Plugins" / "lib" );
47 
48  if ( p.empty() ) return {};
49  return p.string();
50 }
51 
53 optional<std::string> getBasePath() {
54  return clean( cpplocate::getModulePath() ).string();
55 }
56 
57 optional<std::string> getResourcesPath( void* symbol, const std::string& pattern ) {
58  auto p = searchPath( pattern, "", symbol );
59  p = clean( p / pattern );
60  if ( p.empty() ) return {};
61  return p.string();
62 }
63 
65 namespace DataPath {
66 static std::stack<std::string> s_dataPaths;
67 }
69 
70 std::string getDataPath() {
71  if ( DataPath::s_dataPaths.empty() ) { return fs::current_path().string(); }
72  return DataPath::s_dataPaths.top();
73 }
74 
75 std::string popDataPath() {
76  if ( DataPath::s_dataPaths.empty() ) { return fs::current_path().string(); }
77  auto p = DataPath::s_dataPaths.top();
78  DataPath::s_dataPaths.pop();
79  return p;
80 }
81 
82 void pushDataPath( std::string datapath ) {
83  DataPath::s_dataPaths.emplace( std::move( datapath ) );
84  fs::create_directories( DataPath::s_dataPaths.top() );
85 }
86 
87 } // namespace Resources
88 } // namespace Core
89 } // namespace Ra
std::string getDataPath()
Get the current data path.
Definition: Resources.cpp:70
std::string popDataPath()
Pop the current data path.
Definition: Resources.cpp:75
optional< std::string > getRadiumPluginsPath()
Get the path of Radium embedded plugins.
Definition: Resources.cpp:44
optional< std::string > getBasePath()
this one is always found, use optional for consistency ?
Definition: Resources.cpp:53
void pushDataPath(std::string datapath)
Push a new data path.
Definition: Resources.cpp:82
optional< std::string > getResourcesPath(void *symbol, const std::string &pattern)
Search for general resource path.
Definition: Resources.cpp:57
optional< std::string > getRadiumResourcesPath()
Get the path of Radium internal resources.
Definition: Resources.cpp:35
Definition: Cage.cpp:3