mirror of
https://github.com/vcmi/vcmi.git
synced 2025-11-27 22:49:25 +02:00
519
lib/VCMIDirs.cpp
519
lib/VCMIDirs.cpp
@@ -11,89 +11,51 @@
|
|||||||
#include "StdInc.h"
|
#include "StdInc.h"
|
||||||
#include "VCMIDirs.h"
|
#include "VCMIDirs.h"
|
||||||
|
|
||||||
namespace bfs = boost::filesystem;
|
namespace bfs = boost::filesystem; // Should be in each cpp file
|
||||||
|
|
||||||
namespace VCMIDirs
|
|
||||||
{
|
|
||||||
namespace detail
|
|
||||||
{
|
|
||||||
bfs::path g_user_data_path;
|
|
||||||
bfs::path g_user_cache_path;
|
|
||||||
bfs::path g_user_config_path;
|
|
||||||
bfs::path g_user_save_path;
|
|
||||||
|
|
||||||
bfs::path g_library_path;
|
|
||||||
bfs::path g_client_path;
|
|
||||||
bfs::path g_server_path;
|
|
||||||
|
|
||||||
std::vector<bfs::path> g_data_paths;
|
|
||||||
|
|
||||||
std::string g_help_string;
|
|
||||||
|
|
||||||
extern bfs::path GetDataPath();
|
|
||||||
extern bfs::path GetCachePath();
|
|
||||||
extern bfs::path GetConfigPath();
|
|
||||||
extern bfs::path GetUserSavePath();
|
|
||||||
|
|
||||||
extern bfs::path GetLibraryPath();
|
|
||||||
extern bfs::path GetClientPath();
|
|
||||||
extern bfs::path GetServerPath();
|
|
||||||
extern std::vector<bfs::path> GetDataPaths();
|
|
||||||
|
|
||||||
extern std::string GetHelpString();
|
|
||||||
}
|
|
||||||
|
|
||||||
void InitAllPathes(void)
|
|
||||||
{
|
|
||||||
detail::g_user_data_path = detail::GetDataPath();
|
|
||||||
detail::g_user_cache_path = detail::GetCachePath();
|
|
||||||
detail::g_user_config_path = detail::GetConfigPath();
|
|
||||||
detail::g_user_save_path = detail::GetUserSavePath();
|
|
||||||
|
|
||||||
detail::g_library_path = detail::GetLibraryPath();
|
|
||||||
detail::g_client_path = detail::GetClientPath();
|
|
||||||
detail::g_server_path = detail::GetServerPath();
|
|
||||||
|
|
||||||
detail::g_data_paths = detail::GetDataPaths();
|
|
||||||
|
|
||||||
detail::g_help_string = detail::GetHelpString();
|
|
||||||
|
|
||||||
// initialize local directory and create folders to which VCMI needs write access
|
|
||||||
bfs::create_directory(detail::g_user_data_path);
|
|
||||||
bfs::create_directory(detail::g_user_cache_path);
|
|
||||||
bfs::create_directory(detail::g_user_config_path);
|
|
||||||
bfs::create_directory(detail::g_user_save_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace detail
|
|
||||||
{
|
|
||||||
struct InitAllPathes_ctor
|
|
||||||
{
|
|
||||||
InitAllPathes_ctor() { InitAllPathes(); }
|
|
||||||
} InitAllPathes_ctor_global_obj;
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO: Remove _VCMIDirs
|
|
||||||
static _VCMIDirs _VCMIDirsGlobal;
|
|
||||||
_VCMIDirs & get()
|
|
||||||
{
|
|
||||||
puts("VCMIDirs::get() - used of deprecated function :#");
|
|
||||||
return _VCMIDirsGlobal;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME: find way to at least decrease size of this ifdef (along with cleanup in CMake)
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
// This part should be moved into separate file (for example: VCMIDirs_win32.cpp)
|
// File: VCMIDirs_win32.h
|
||||||
|
//#include "IVCMIDirs.h"
|
||||||
|
|
||||||
|
class VCMIDirs_win32 : public IVCMIDirs
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
boost::filesystem::path userDataPath() const override;
|
||||||
|
boost::filesystem::path userCachePath() const override;
|
||||||
|
boost::filesystem::path userConfigPath() const override;
|
||||||
|
boost::filesystem::path userSavePath() const override;
|
||||||
|
|
||||||
|
std::vector<boost::filesystem::path> dataPaths() const override;
|
||||||
|
|
||||||
|
boost::filesystem::path clientPath() const override;
|
||||||
|
boost::filesystem::path serverPath() const override;
|
||||||
|
|
||||||
|
boost::filesystem::path libraryPath() const override;
|
||||||
|
boost::filesystem::path binaryPath() const override;
|
||||||
|
|
||||||
|
std::string libraryName(const std::string& basename) const override;
|
||||||
|
|
||||||
|
std::string genHelpString() const override;
|
||||||
|
};
|
||||||
|
// End of file: VCMIDirs_win32.h
|
||||||
|
|
||||||
|
// File: VCMIDirs_win32.cpp
|
||||||
|
//#include "StdInc.h"
|
||||||
|
//#include "VCMIDirs_win32"
|
||||||
// WinAPI
|
// WinAPI
|
||||||
#include <Windows.h>
|
#include <Windows.h> // WideCharToMultiByte
|
||||||
#include <Shlobj.h>
|
#include <Shlobj.h> // SHGetSpecialFolderPathW
|
||||||
|
|
||||||
namespace VCMIDirs
|
namespace VCMIDirs
|
||||||
{
|
{
|
||||||
namespace detail
|
const IVCMIDirs* get()
|
||||||
{
|
{
|
||||||
bfs::path GetDataPath()
|
static VCMIDirs_win32 singleton;
|
||||||
|
return &singleton;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bfs::path VCMIDirs_win32::userDataPath() const
|
||||||
{
|
{
|
||||||
const char* profile_dir_a;
|
const char* profile_dir_a;
|
||||||
wchar_t profile_dir_w[MAX_PATH];
|
wchar_t profile_dir_w[MAX_PATH];
|
||||||
@@ -103,6 +65,7 @@ namespace VCMIDirs
|
|||||||
// they should put their data under the locations referred to by CSIDL_APPDATA or CSIDL_LOCAL_APPDATA.
|
// they should put their data under the locations referred to by CSIDL_APPDATA or CSIDL_LOCAL_APPDATA.
|
||||||
if (SHGetSpecialFolderPathW(nullptr, profile_dir_w, CSIDL_PROFILE, FALSE) == FALSE) // WinAPI way failed
|
if (SHGetSpecialFolderPathW(nullptr, profile_dir_w, CSIDL_PROFILE, FALSE) == FALSE) // WinAPI way failed
|
||||||
{
|
{
|
||||||
|
// FIXME: Create macro for MS Visual Studio.
|
||||||
if (profile_dir_a = std::getenv("userprofile")) // STL way succeed
|
if (profile_dir_a = std::getenv("userprofile")) // STL way succeed
|
||||||
return bfs::path(profile_dir_a) / "vcmi";
|
return bfs::path(profile_dir_a) / "vcmi";
|
||||||
else
|
else
|
||||||
@@ -113,43 +76,22 @@ namespace VCMIDirs
|
|||||||
|
|
||||||
//return dataPaths()[0] ???;
|
//return dataPaths()[0] ???;
|
||||||
}
|
}
|
||||||
|
bfs::path VCMIDirs_win32::userCachePath() const { return userDataPath(); }
|
||||||
|
bfs::path VCMIDirs_win32::userConfigPath() const { return userDataPath() / "config"; }
|
||||||
|
bfs::path VCMIDirs_win32::userSavePath() const { return userDataPath() / "Games"; }
|
||||||
|
|
||||||
bfs::path GetCachePath()
|
std::vector<bfs::path> VCMIDirs_win32::dataPaths() const
|
||||||
{
|
|
||||||
return GetDataPath();
|
|
||||||
}
|
|
||||||
|
|
||||||
bfs::path GetConfigPath()
|
|
||||||
{
|
|
||||||
return GetDataPath() / "config";
|
|
||||||
}
|
|
||||||
|
|
||||||
bfs::path GetUserSavePath()
|
|
||||||
{
|
|
||||||
return GetDataPath() / "Games";
|
|
||||||
}
|
|
||||||
|
|
||||||
bfs::path GetLibraryPath()
|
|
||||||
{
|
|
||||||
return ".";
|
|
||||||
}
|
|
||||||
|
|
||||||
bfs::path GetClientPath()
|
|
||||||
{
|
|
||||||
return GetLibraryPath() / "VCMI_client.exe";
|
|
||||||
}
|
|
||||||
|
|
||||||
bfs::path GetServerPath()
|
|
||||||
{
|
|
||||||
return GetLibraryPath() / "VCMI_server.exe";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<bfs::path> GetDataPaths()
|
|
||||||
{
|
{
|
||||||
return std::vector<bfs::path>(1, bfs::path("."));
|
return std::vector<bfs::path>(1, bfs::path("."));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string GetHelpString()
|
bfs::path VCMIDirs_win32::clientPath() const { return binaryPath() / "VCMI_client.exe"; }
|
||||||
|
bfs::path VCMIDirs_win32::serverPath() const { return binaryPath() / "VCMI_server.exe"; }
|
||||||
|
|
||||||
|
bfs::path VCMIDirs_win32::libraryPath() const { return "."; }
|
||||||
|
bfs::path VCMIDirs_win32::binaryPath() const { return "."; }
|
||||||
|
|
||||||
|
std::string VCMIDirs_win32::genHelpString() const
|
||||||
{
|
{
|
||||||
// I think this function should have multiple versions
|
// I think this function should have multiple versions
|
||||||
// 1. For various arguments
|
// 1. For various arguments
|
||||||
@@ -184,37 +126,100 @@ namespace VCMIDirs
|
|||||||
};
|
};
|
||||||
|
|
||||||
std::vector<std::string> temp_vec;
|
std::vector<std::string> temp_vec;
|
||||||
for (const bfs::path& path : GetDataPaths())
|
for (const bfs::path& path : dataPaths())
|
||||||
temp_vec.push_back(utf8_convert(path));
|
temp_vec.push_back(utf8_convert(path));
|
||||||
std::string gd_string_a = boost::algorithm::join(temp_vec, L";");
|
std::string gd_string_a = boost::algorithm::join(temp_vec, L";");
|
||||||
|
|
||||||
|
|
||||||
return
|
return
|
||||||
" game data: " + gd_string_a + "\n" +
|
" game data: " + gd_string_a + "\n" +
|
||||||
" libraries: " + utf8_convert(GetLibraryPath()) + "\n" +
|
" libraries: " + utf8_convert(libraryPath()) + "\n" +
|
||||||
" server: " + utf8_convert(GetServerPath()) + "\n" +
|
" server: " + utf8_convert(serverPath()) + "\n" +
|
||||||
"\n" +
|
"\n" +
|
||||||
" user data: " + utf8_convert(GetDataPath()) + "\n" +
|
" user data: " + utf8_convert(userDataPath()) + "\n" +
|
||||||
" user cache: " + utf8_convert(GetCachePath()) + "\n" +
|
" user cache: " + utf8_convert(userCachePath()) + "\n" +
|
||||||
" user config: " + utf8_convert(GetConfigPath()) + "\n" +
|
" user config: " + utf8_convert(userConfigPath()) + "\n" +
|
||||||
" user saves: " + utf8_convert(GetUserSavePath()) + "\n"; // Should end without new-line?
|
" user saves: " + utf8_convert(userSavePath()) + "\n"; // Should end without new-line?
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string libraryName(const std::string& basename)
|
std::string libraryName(const std::string& basename) { return basename + ".dll"; }
|
||||||
|
// End of file: VCMIDirs_win32.cpp
|
||||||
|
#else // UNIX
|
||||||
|
// File: IVCMIDirs_unix.h
|
||||||
|
//#include "IVCMIDirs.h"
|
||||||
|
|
||||||
|
class IVCMIDirs_unix : public IVCMIDirs
|
||||||
{
|
{
|
||||||
return basename + ".dll";
|
public:
|
||||||
}
|
boost::filesystem::path clientPath() const override;
|
||||||
}
|
boost::filesystem::path serverPath() const override;
|
||||||
|
|
||||||
#elif defined (__APPLE__)
|
std::string genHelpString() const override;
|
||||||
// This part should be moved to separate file (for example: VCMIDirs_apple.cpp)
|
};
|
||||||
|
// End of file: IVCMIDirs_unix.h
|
||||||
|
|
||||||
|
// File: IVCMIDirs_unix.cpp
|
||||||
|
//#include "StdInc.h"
|
||||||
|
//#include "IVCMIDirs_unix.h"
|
||||||
|
|
||||||
|
bfs::path IVCMIDirs_unix::clientPath() const { return binaryPath() / "vcmiclient"; }
|
||||||
|
bfs::path IVCMIDirs_unix::clientPath() const { return binaryPath() / "vcmiserver"; }
|
||||||
|
|
||||||
|
std::string IVCMIDirs_unix::genHelpString() const
|
||||||
|
{
|
||||||
|
std::vector<std::string> temp_vec;
|
||||||
|
for (const bfs::path& path : dataPaths())
|
||||||
|
temp_vec.push_back(path.string());
|
||||||
|
std::string gd_string_a = boost::algorithm::join(temp_vec, L";");
|
||||||
|
|
||||||
|
|
||||||
|
return
|
||||||
|
" game data: " + gd_string_a + "\n" +
|
||||||
|
" libraries: " + libraryPath().string() + "\n" +
|
||||||
|
" server: " + serverPath().string() + "\n" +
|
||||||
|
"\n" +
|
||||||
|
" user data: " + userDataPath().string() + "\n" +
|
||||||
|
" user cache: " + userCachePath().string() + "\n" +
|
||||||
|
" user config: " + userConfigPath().string() + "\n" +
|
||||||
|
" user saves: " + userSavePath().string() + "\n"; // Should end without new-line?
|
||||||
|
}
|
||||||
|
// End of file: IVCMIDirs_unix.cpp
|
||||||
|
|
||||||
|
#ifdef __APPLE__
|
||||||
|
// File: VCMIDirs_OSX.h
|
||||||
|
//#include "IVCMIDirs_unix.h"
|
||||||
|
|
||||||
|
class VCMIDirs_OSX : public IVCMIDirs_unix
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
boost::filesystem::path userDataPath() const override;
|
||||||
|
boost::filesystem::path userCachePath() const override;
|
||||||
|
boost::filesystem::path userConfigPath() const override;
|
||||||
|
boost::filesystem::path userSavePath() const override;
|
||||||
|
|
||||||
|
std::vector<boost::filesystem::path> dataPaths() const override;
|
||||||
|
|
||||||
|
boost::filesystem::path libraryPath() const override;
|
||||||
|
boost::filesystem::path binaryPath() const override;
|
||||||
|
|
||||||
|
std::string libraryName(const std::string& basename) const override;
|
||||||
|
};
|
||||||
|
// End of file: VCMIDirs_OSX.h
|
||||||
|
|
||||||
|
// File: VCMIDirs_OSX.cpp
|
||||||
|
//#include "StdInc.h"
|
||||||
|
//#include "VCMIDirs_OSX.h"
|
||||||
|
|
||||||
namespace VCMIDirs
|
namespace VCMIDirs
|
||||||
{
|
{
|
||||||
namespace detail
|
const IVCMIDirs* get()
|
||||||
{
|
{
|
||||||
bfs::path GetDataPath()
|
static VCMIDirs_OSX singleton;
|
||||||
|
return &singleton;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bfs::path VCMIDirs_OSX::userDataPath() const
|
||||||
{
|
{
|
||||||
// This is Cocoa code that should be normally used to get path to Application Support folder but can't use it here for now...
|
// This is Cocoa code that should be normally used to get path to Application Support folder but can't use it here for now...
|
||||||
// NSArray* urls = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask];
|
// NSArray* urls = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask];
|
||||||
@@ -227,158 +232,68 @@ namespace VCMIDirs
|
|||||||
home_dir = ".";
|
home_dir = ".";
|
||||||
return bfs::path(home_dir) / "Library" / "Application Support" / "vcmi";
|
return bfs::path(home_dir) / "Library" / "Application Support" / "vcmi";
|
||||||
}
|
}
|
||||||
|
bfs::path VCMIDirs_OSX::userCachePath() const { return userDataPath(); }
|
||||||
|
bfs::path VCMIDirs_OSX::userConfigPath() const { return userDataPath() / "config"; }
|
||||||
|
bfs::path VCMIDirs_OSX::userSavePath() const { return userDataPath() / "Games"; }
|
||||||
|
|
||||||
bfs::path GetCachePath()
|
std::vector<bfs::path> VCMIDirs_OSX::dataPaths() const
|
||||||
{
|
{
|
||||||
return GetDataPath();
|
return std::vector<bfs::path>(1, "../Data");
|
||||||
}
|
}
|
||||||
|
|
||||||
bfs::path GetConfigPath()
|
bfs::path VCMIDirs_OSX::libraryPath() const { return "."; }
|
||||||
{
|
bfs::path VCMIDirs_OSX::binaryPath() const { return "."; }
|
||||||
return GetDataPath() / "config";
|
|
||||||
}
|
|
||||||
|
|
||||||
bfs::path GetUserSavePath()
|
std::string libraryName(const std::string& basename) { return "lib" + basename + ".dylib"; }
|
||||||
{
|
// End of file: VCMIDirs_OSX.cpp
|
||||||
return GetDataPath() / "Games";
|
|
||||||
}
|
|
||||||
|
|
||||||
bfs::path GetLibraryPath()
|
|
||||||
{
|
|
||||||
return ".";
|
|
||||||
}
|
|
||||||
|
|
||||||
bfs::path GetClientPath()
|
|
||||||
{
|
|
||||||
return "./vcmiclient";
|
|
||||||
}
|
|
||||||
|
|
||||||
bfs::path GetServerPath()
|
|
||||||
{
|
|
||||||
return "./vcmiserver";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<bfs::path> GetDataPaths()
|
|
||||||
{
|
|
||||||
return std::vector<std::string>(1, "../Data");
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string GetHelpString()
|
|
||||||
{
|
|
||||||
std::vector<std::string> temp_vec;
|
|
||||||
for (const bfs::path& path : GetDataPaths())
|
|
||||||
temp_vec.push_back(path.string());
|
|
||||||
|
|
||||||
return
|
|
||||||
" game data: " + boost::algorithm::join(temp_vec, ":") + "\n" +
|
|
||||||
" libraries: " + GetLibraryPath().string() + "\n" +
|
|
||||||
" server: " + GetServerPath().string() + "\n" +
|
|
||||||
"\n" +
|
|
||||||
" user data: " + GetDataPath().string() + "\n" +
|
|
||||||
" user cache: " + GetCachePath().string() + "\n" +
|
|
||||||
" user config: " + GetConfigPath().string() + "\n" +
|
|
||||||
" user saves: " + GetUserSavePath().string() + "\n"; // Should end without new-line?
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string libraryName(const std::string& basename)
|
|
||||||
{
|
|
||||||
return "lib" + basename + ".dylib";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#elif defined __ANDROID__
|
|
||||||
// This part should be moved to separate file (for example: VCMIDirs_android.cpp)
|
|
||||||
|
|
||||||
namespace VCMIDirs
|
|
||||||
{
|
|
||||||
namespace detail
|
|
||||||
{
|
|
||||||
bfs::path GetDataPath()
|
|
||||||
{
|
|
||||||
// on Android HOME will be set to something like /sdcard/data/Android/is.xyz.vcmi/files/
|
|
||||||
return getenv("HOME");
|
|
||||||
}
|
|
||||||
|
|
||||||
bfs::path GetCachePath()
|
|
||||||
{
|
|
||||||
return GetDataPath() / "cache";
|
|
||||||
}
|
|
||||||
|
|
||||||
bfs::path GetConfigPath()
|
|
||||||
{
|
|
||||||
return GetDataPath() / "config";
|
|
||||||
}
|
|
||||||
|
|
||||||
bfs::path GetUserSavePath()
|
|
||||||
{
|
|
||||||
return GetDataPath() / "Saves"; // Why different than other platforms???
|
|
||||||
}
|
|
||||||
|
|
||||||
bfs::path GetLibraryPath()
|
|
||||||
{
|
|
||||||
return M_LIB_DIR;
|
|
||||||
}
|
|
||||||
|
|
||||||
bfs::path GetClientPath()
|
|
||||||
{
|
|
||||||
return bfs::path(M_BIN_DIR) / "vcmiclient";
|
|
||||||
}
|
|
||||||
|
|
||||||
bfs::path GetServerPath()
|
|
||||||
{
|
|
||||||
return bfs::path(M_BIN_DIR) / "vcmiserver";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<bfs::path> GetDataPaths()
|
|
||||||
{
|
|
||||||
return std::vector<bfs::path>(1, GetDataPath());
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string GetHelpString()
|
|
||||||
{
|
|
||||||
std::vector<std::string> temp_vec;
|
|
||||||
for (const bfs::path& path : GetDataPaths())
|
|
||||||
temp_vec.push_back(path.string());
|
|
||||||
|
|
||||||
return
|
|
||||||
" game data: " + boost::algorithm::join(temp_vec, ":") + "\n" +
|
|
||||||
" libraries: " + GetLibraryPath().string() + "\n" +
|
|
||||||
" server: " + GetServerPath().string() + "\n" +
|
|
||||||
"\n" +
|
|
||||||
" user data: " + GetDataPath().string() + "\n" +
|
|
||||||
" user cache: " + GetCachePath().string() + "\n" +
|
|
||||||
" user config: " + GetConfigPath().string() + "\n" +
|
|
||||||
" user saves: " + GetUserSavePath().string() + "\n"; // Should end without new-line?
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string libraryName(const std::string& basename)
|
|
||||||
{
|
|
||||||
return "lib" + basename + ".so";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
// This part should be moved to separate file (for example: VCMIDirs_default.cpp)
|
// File: VCMIDirs_Linux.h
|
||||||
|
//#include "IVCMIDirs_unix.h"
|
||||||
|
|
||||||
|
class VCMIDirs_Linux : public IVCMIDirs_unix
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
boost::filesystem::path userDataPath() const override;
|
||||||
|
boost::filesystem::path userCachePath() const override;
|
||||||
|
boost::filesystem::path userConfigPath() const override;
|
||||||
|
boost::filesystem::path userSavePath() const override;
|
||||||
|
|
||||||
|
std::vector<boost::filesystem::path> dataPaths() const override;
|
||||||
|
|
||||||
|
boost::filesystem::path libraryPath() const override;
|
||||||
|
boost::filesystem::path binaryPath() const override;
|
||||||
|
|
||||||
|
std::string libraryName(const std::string& basename) const override;
|
||||||
|
};
|
||||||
|
// End of file: VCMIDirs_Linux.h
|
||||||
|
|
||||||
|
// File: VCMIDirs_Linux.cpp
|
||||||
|
//#include "StdInc.h"
|
||||||
|
//#include "VCMIDirs_Linux.h"
|
||||||
|
|
||||||
namespace VCMIDirs
|
namespace VCMIDirs
|
||||||
{
|
{
|
||||||
namespace detail
|
const IVCMIDirs* get()
|
||||||
|
{
|
||||||
|
static VCMIDirs_Linux singleton;
|
||||||
|
return &singleton;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bfs::path VCMIDirs_Linux::userDataPath() const
|
||||||
{
|
{
|
||||||
// $XDG_DATA_HOME, default: $HOME/.local/share
|
// $XDG_DATA_HOME, default: $HOME/.local/share
|
||||||
bfs::path GetDataPath()
|
|
||||||
{
|
|
||||||
const char* home_dir;
|
const char* home_dir;
|
||||||
if (home_dir = getenv("XDG_DATA_HOME"))
|
if (home_dir = getenv("XDG_DATA_HOME"))
|
||||||
return = home_dir;
|
return home_dir;
|
||||||
else if (home_dir = getenv("HOME"))
|
else if (home_dir = getenv("HOME"))
|
||||||
return = bfs::path(home_dir) / ".local" / "share" / "vcmi";
|
return bfs::path(home_dir) / ".local" / "share" / "vcmi";
|
||||||
else
|
else
|
||||||
return ".";
|
return ".";
|
||||||
}
|
}
|
||||||
|
bfs::path VCMIDirs_Linux::userCachePath() const
|
||||||
// $XDG_CACHE_HOME, default: $HOME/.cache
|
|
||||||
bfs::path GetCachePath()
|
|
||||||
{
|
{
|
||||||
|
// $XDG_CACHE_HOME, default: $HOME/.cache
|
||||||
const char* home_dir;
|
const char* home_dir;
|
||||||
if (home_dir = getenv("XDG_CACHE_HOME"))
|
if (home_dir = getenv("XDG_CACHE_HOME"))
|
||||||
return bfs::path(home_dir) / "vcmi";
|
return bfs::path(home_dir) / "vcmi";
|
||||||
@@ -387,40 +302,23 @@ namespace VCMIDirs
|
|||||||
else
|
else
|
||||||
return ".";
|
return ".";
|
||||||
}
|
}
|
||||||
|
bfs::path VCMIDirs_Linux::userConfigPath() const
|
||||||
// $XDG_CONFIG_HOME, default: $HOME/.config
|
|
||||||
bfs::path GetConfigPath()
|
|
||||||
{
|
{
|
||||||
|
// $XDG_CONFIG_HOME, default: $HOME/.config
|
||||||
const char* home_dir;
|
const char* home_dir;
|
||||||
if (home_dir = getenv("XDG_CONFIG_HOME"))
|
if (home_dir = getenv("XDG_CONFIG_HOME"))
|
||||||
return bfs:path(home_dir) / "vcmi";
|
return bfs::path(home_dir) / "vcmi";
|
||||||
else if (home_dir = getenv("HOME"))
|
else if (home_dir = getenv("HOME"))
|
||||||
return bfs::path(home_dir) / ".config" / "vcmi";
|
return bfs::path(home_dir) / ".config" / "vcmi";
|
||||||
else
|
else
|
||||||
return ".";
|
return ".";
|
||||||
}
|
}
|
||||||
|
bfs::path VCMIDirs_Linux::userSavePath() const
|
||||||
bfs::path GetUserSavePath()
|
|
||||||
{
|
{
|
||||||
return GetDataPath() / "Saves"; // Why different than other platforms???
|
return userDataPath() / "Saves";
|
||||||
}
|
}
|
||||||
|
|
||||||
bfs::path GetLibraryPath()
|
std::vector<bfs::path> VCMIDirs_Linux::dataPaths() const
|
||||||
{
|
|
||||||
return M_LIB_DIR;
|
|
||||||
}
|
|
||||||
|
|
||||||
bfs::path GetClientPath()
|
|
||||||
{
|
|
||||||
return bfs::path(M_BIN_DIR) / "vcmiclient";
|
|
||||||
}
|
|
||||||
|
|
||||||
bfs::path GetServerPath()
|
|
||||||
{
|
|
||||||
return bfs::path(M_BIN_DIR) / "vcmiserver";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<bfs::path> GetDataPaths()
|
|
||||||
{
|
{
|
||||||
// $XDG_DATA_DIRS, default: /usr/local/share/:/usr/share/
|
// $XDG_DATA_DIRS, default: /usr/local/share/:/usr/share/
|
||||||
|
|
||||||
@@ -434,7 +332,7 @@ namespace VCMIDirs
|
|||||||
ret.push_back(bfs::path(home_dir) / ".vcmi");
|
ret.push_back(bfs::path(home_dir) / ".vcmi");
|
||||||
ret.push_back(M_DATA_DIR);
|
ret.push_back(M_DATA_DIR);
|
||||||
|
|
||||||
if (home_dir = getenv("XDG_DATA_DIRS") != nullptr)
|
if ((home_dir = getenv("XDG_DATA_DIRS")) != nullptr)
|
||||||
{
|
{
|
||||||
std::string dataDirsEnv = home_dir;
|
std::string dataDirsEnv = home_dir;
|
||||||
std::vector<std::string> dataDirs;
|
std::vector<std::string> dataDirs;
|
||||||
@@ -451,27 +349,50 @@ namespace VCMIDirs
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string GetHelpString()
|
bfs::path VCMIDirs_Linux::libraryPath() const { return M_LIB_PATH; }
|
||||||
{
|
bfs::path VCMIDirs_Linux::binaryPath() const { return M_BIN_DIR; }
|
||||||
std::vector<std::string> temp_vec;
|
|
||||||
for (const bfs::path& path : GetDataPaths())
|
|
||||||
temp_vec.push_back(path.string());
|
|
||||||
|
|
||||||
return
|
std::string VCMIDirs_Linux::libraryName(const std::string& basename) const { return "lib" + basename + ".so"; }
|
||||||
" game data: " + boost::algorithm::join(temp_vec, ":") + "\n" +
|
// End of file VCMIDirs_Linux.cpp
|
||||||
" libraries: " + GetLibraryPath().string() + "\n" +
|
#ifdef __ANDROID__
|
||||||
" server: " + GetServerPath().string() + "\n" +
|
// File: VCMIDirs_Android.h
|
||||||
"\n" +
|
//#include "VCMIDirs_Linux.h"
|
||||||
" user data: " + GetDataPath().string() + "\n" +
|
class VCMIDirs_Android : public VCMIDirs_Linux
|
||||||
" user cache: " + GetCachePath().string() + "\n" +
|
{
|
||||||
" user config: " + GetConfigPath().string() + "\n" +
|
public:
|
||||||
" user saves: " + GetUserSavePath().string() + "\n"; // Should end without new-line?
|
boost::filesystem::path userDataPath() const override;
|
||||||
|
boost::filesystem::path userCachePath() const override;
|
||||||
|
boost::filesystem::path userConfigPath() const override;
|
||||||
|
boost::filesystem::path userSavePath() const override;
|
||||||
|
|
||||||
|
std::vector<boost::filesystem::path> dataPaths() const override;
|
||||||
|
};
|
||||||
|
// End of file: VCMIDirs_Android.h
|
||||||
|
|
||||||
|
// File: VCMIDirs_Android.cpp
|
||||||
|
//#include "StdInc.h"
|
||||||
|
//#include "VCMIDirs_Android.h"
|
||||||
|
|
||||||
|
namespace VCMIDirs
|
||||||
|
{
|
||||||
|
const IVCMIDirs* get()
|
||||||
|
{
|
||||||
|
static VCMIDirs_Android singleton;
|
||||||
|
return &singleton;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string libraryName(const std::string& basename)
|
// on Android HOME will be set to something like /sdcard/data/Android/is.xyz.vcmi/files/
|
||||||
|
bfs::path VCMIDirs_Android::userDataPath() const { return getenv("HOME"); }
|
||||||
|
bfs::path VCMIDirs_Android::userCachePath() const { return userDataPath() / "cache"; }
|
||||||
|
bfs::path VCMIDirs_Android::userConfigPath() const { return userDataPath() / "config"; }
|
||||||
|
bfs::path VCMIDirs_Android::userSavePath() const { return userDataPath() / "Saves"; }
|
||||||
|
|
||||||
|
std::vector<bfs::path> VCMIDirs_Android::dataPaths() const
|
||||||
{
|
{
|
||||||
return "lib" + basename + ".so";
|
return std::vector<bfs::path>(1, userDataPath());
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// End of file: VCMIDirs_Android.cpp
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
122
lib/VCMIDirs.h
122
lib/VCMIDirs.h
@@ -7,112 +7,56 @@
|
|||||||
* Full text of license available in license.txt file, in main folder
|
* Full text of license available in license.txt file, in main folder
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
// Boost
|
|
||||||
#include <boost/filesystem/path.hpp>
|
|
||||||
|
|
||||||
// STL C++
|
// STL C++
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
// TODO: Remove _VCMIDirs
|
// Boost
|
||||||
class _VCMIDirs;
|
#include <boost/filesystem/path.hpp>
|
||||||
|
|
||||||
// Where to find the various VCMI files. This is mostly useful for linux.
|
// TODO: File should be rename to IVCMIDirs.h
|
||||||
namespace VCMIDirs
|
|
||||||
{
|
|
||||||
namespace detail
|
|
||||||
{
|
|
||||||
extern DLL_LINKAGE boost::filesystem::path g_user_data_path;
|
|
||||||
extern DLL_LINKAGE boost::filesystem::path g_user_cache_path;
|
|
||||||
extern DLL_LINKAGE boost::filesystem::path g_user_config_path;
|
|
||||||
extern DLL_LINKAGE boost::filesystem::path g_user_save_path;
|
|
||||||
|
|
||||||
extern DLL_LINKAGE boost::filesystem::path g_library_path;
|
class IVCMIDirs
|
||||||
extern DLL_LINKAGE boost::filesystem::path g_client_path;
|
|
||||||
extern DLL_LINKAGE boost::filesystem::path g_server_path;
|
|
||||||
|
|
||||||
extern DLL_LINKAGE std::vector<boost::filesystem::path> g_data_paths;
|
|
||||||
|
|
||||||
extern DLL_LINKAGE std::string g_help_string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// deprecated: get singleton instance
|
|
||||||
extern DLL_LINKAGE _VCMIDirs & get();
|
|
||||||
|
|
||||||
// Path to user-specific data directory
|
|
||||||
inline const boost::filesystem::path& userDataPath() { return detail::g_user_data_path; }
|
|
||||||
|
|
||||||
// Path to "cache" directory, can be used for any non-essential files
|
|
||||||
inline const boost::filesystem::path& userCachePath() { return detail::g_user_cache_path; }
|
|
||||||
|
|
||||||
// Path to writeable directory with user configs
|
|
||||||
inline const boost::filesystem::path& userConfigPath() { return detail::g_user_config_path; }
|
|
||||||
|
|
||||||
// Path to saved games
|
|
||||||
inline const boost::filesystem::path& userSavePath() { return detail::g_user_save_path; }
|
|
||||||
|
|
||||||
// Paths to global system-wide data directories. First items have higher priority
|
|
||||||
inline const std::vector<boost::filesystem::path>& dataPaths() { return detail::g_data_paths; }
|
|
||||||
|
|
||||||
// Full path to client executable, including server name (e.g. /usr/bin/vcmiclient)
|
|
||||||
inline const boost::filesystem::path& clientPath() { return detail::g_client_path; }
|
|
||||||
|
|
||||||
// Full path to server executable, including server name (e.g. /usr/bin/vcmiserver)
|
|
||||||
inline const boost::filesystem::path& serverPath() { return detail::g_server_path; }
|
|
||||||
|
|
||||||
// Path where vcmi libraries can be found (in AI and Scripting subdirectories)
|
|
||||||
inline const boost::filesystem::path& libraryPath() { return detail::g_library_path; }
|
|
||||||
|
|
||||||
// Returns system-specific name for dynamic libraries ( StupidAI => "libStupidAI.so" or "StupidAI.dll")
|
|
||||||
extern DLL_LINKAGE std::string libraryName(const std::string& basename);
|
|
||||||
//extern DLL_LINKAGE std::string libraryName(const char* basename);
|
|
||||||
//extern DLL_LINKAGE std::string libraryName(std::string&& basename);
|
|
||||||
|
|
||||||
|
|
||||||
inline const std::string& genHelpString() { return detail::g_help_string; }
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Remove _VCMIDirs
|
|
||||||
// This class is deprecated
|
|
||||||
class DLL_LINKAGE _VCMIDirs
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// deprecated: Path to user-specific data directory
|
// Path to user-specific data directory
|
||||||
std::string userDataPath() const { return VCMIDirs::userDataPath().string(); }
|
virtual boost::filesystem::path userDataPath() const = 0;
|
||||||
|
|
||||||
/// deprecated: Path to "cache" directory, can be used for any non-essential files
|
// Path to "cache" directory, can be used for any non-essential files
|
||||||
std::string userCachePath() const { return VCMIDirs::userCachePath().string(); }
|
virtual boost::filesystem::path userCachePath() const = 0;
|
||||||
|
|
||||||
/// deprecated: Path to writeable directory with user configs
|
// Path to writeable directory with user configs
|
||||||
std::string userConfigPath() const { return VCMIDirs::userConfigPath().string(); }
|
virtual boost::filesystem::path userConfigPath() const = 0;
|
||||||
|
|
||||||
/// deprecated: Path to saved games
|
// Path to saved games
|
||||||
std::string userSavePath() const { return VCMIDirs::userSavePath().string(); }
|
virtual boost::filesystem::path userSavePath() const = 0;
|
||||||
|
|
||||||
/// deprecated: Paths to global system-wide data directories. First items have higher priority
|
// Paths to global system-wide data directories. First items have higher priority
|
||||||
std::vector<std::string> dataPaths() const
|
virtual std::vector<boost::filesystem::path> dataPaths() const = 0;
|
||||||
{
|
|
||||||
std::vector<std::string> result;
|
|
||||||
for (const auto& path : VCMIDirs::dataPaths())
|
|
||||||
result.push_back(path.string());
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// deprecated: Full path to client executable, including server name (e.g. /usr/bin/vcmiclient)
|
// Full path to client executable, including server name (e.g. /usr/bin/vcmiclient)
|
||||||
std::string clientPath() const { return VCMIDirs::clientPath().string(); }
|
virtual boost::filesystem::path clientPath() const = 0;
|
||||||
|
|
||||||
/// deprecated: Full path to server executable, including server name (e.g. /usr/bin/vcmiserver)
|
// Full path to server executable, including server name (e.g. /usr/bin/vcmiserver)
|
||||||
std::string serverPath() const { return VCMIDirs::serverPath().string(); }
|
virtual boost::filesystem::path serverPath() const = 0;
|
||||||
|
|
||||||
/// deprecated: Path where vcmi libraries can be found (in AI and Scripting subdirectories)
|
// Path where vcmi libraries can be found (in AI and Scripting subdirectories)
|
||||||
std::string libraryPath() const { return VCMIDirs::libraryPath().string(); }
|
virtual boost::filesystem::path libraryPath() const = 0;
|
||||||
|
|
||||||
/// deprecated: Returns system-specific name for dynamic libraries ( StupidAI => "libStupidAI.so" or "StupidAI.dll")
|
// Path where vcmi binaries can be found
|
||||||
std::string libraryName(std::string basename) const { return VCMIDirs::libraryName(basename); }
|
virtual boost::filesystem::path binaryPath() const = 0;
|
||||||
|
|
||||||
/// deprecated:
|
// Returns system-specific name for dynamic libraries ( StupidAI => "libStupidAI.so" or "StupidAI.dll")
|
||||||
std::string genHelpString() const { return VCMIDirs::genHelpString(); }
|
virtual std::string libraryName(const std::string& basename) const = 0;
|
||||||
|
// virtual std::string libraryName(const char* basename) const = 0; ?
|
||||||
|
// virtual std::string libraryName(std::string&& basename) const = 0;?
|
||||||
|
|
||||||
|
virtual std::string genHelpString() const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
namespace VCMIDirs
|
||||||
|
{
|
||||||
|
extern DLL_LINKAGE const IVCMIDirs* get();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user