2013-03-02 21:41:25 +03:00
|
|
|
/*
|
|
|
|
* VCMIDirs.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-08-09 18:22:37 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "VCMIDirs.h"
|
2014-03-04 17:51:10 +03:00
|
|
|
|
2014-08-11 00:42:39 +03:00
|
|
|
namespace bfs = boost::filesystem;
|
2014-03-04 17:51:10 +03:00
|
|
|
|
2014-08-11 00:42:39 +03:00
|
|
|
bfs::path IVCMIDirs::userSavePath() const { return userDataPath() / "Saves"; }
|
|
|
|
|
|
|
|
void IVCMIDirs::init()
|
|
|
|
{
|
|
|
|
// TODO: Log errors
|
|
|
|
bfs::create_directory(userDataPath());
|
|
|
|
bfs::create_directory(userCachePath());
|
|
|
|
bfs::create_directory(userConfigPath());
|
|
|
|
bfs::create_directory(userSavePath());
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef VCMI_WINDOWS
|
|
|
|
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <Shlobj.h>
|
|
|
|
#include <Shellapi.h>
|
2014-08-09 20:54:12 +03:00
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
class VCMIDirsWIN32 : public IVCMIDirs
|
2013-03-02 21:41:25 +03:00
|
|
|
{
|
2014-08-09 20:54:12 +03:00
|
|
|
public:
|
|
|
|
boost::filesystem::path userDataPath() const override;
|
|
|
|
boost::filesystem::path userCachePath() const override;
|
|
|
|
boost::filesystem::path userConfigPath() const override;
|
2013-03-02 21:41:25 +03:00
|
|
|
|
2014-08-09 20:54:12 +03:00
|
|
|
std::vector<boost::filesystem::path> dataPaths() const override;
|
2013-03-02 21:41:25 +03:00
|
|
|
|
2014-08-09 20:54:12 +03:00
|
|
|
boost::filesystem::path clientPath() const override;
|
|
|
|
boost::filesystem::path serverPath() const override;
|
2013-08-24 23:11:51 +03:00
|
|
|
|
2014-08-09 20:54:12 +03:00
|
|
|
boost::filesystem::path libraryPath() const override;
|
|
|
|
boost::filesystem::path binaryPath() const override;
|
2013-03-02 21:41:25 +03:00
|
|
|
|
2014-08-09 20:54:12 +03:00
|
|
|
std::string libraryName(const std::string& basename) const override;
|
2013-03-02 21:41:25 +03:00
|
|
|
|
2014-08-09 20:54:12 +03:00
|
|
|
std::string genHelpString() const override;
|
|
|
|
|
2014-08-11 00:42:39 +03:00
|
|
|
void init() override;
|
|
|
|
};
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
void VCMIDirsWIN32::init()
|
2014-08-09 20:54:12 +03:00
|
|
|
{
|
2014-08-11 00:42:39 +03:00
|
|
|
// Call base (init dirs)
|
|
|
|
IVCMIDirs::init();
|
|
|
|
|
|
|
|
auto moveDirIfExists = [](const bfs::path& from, const bfs::path& to)
|
2014-08-09 18:22:37 +03:00
|
|
|
{
|
2014-08-11 00:42:39 +03:00
|
|
|
if (!bfs::is_directory(from))
|
|
|
|
return; // Nothing to do here. Flies away.
|
|
|
|
|
|
|
|
if (bfs::is_empty(from))
|
|
|
|
{
|
|
|
|
bfs::remove(from);
|
|
|
|
return; // Nothing to do here. Flies away.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bfs::is_directory(to))
|
|
|
|
{
|
|
|
|
// IVCMIDirs::init() should create all destination directories.
|
|
|
|
// TODO: Log fact, that we shouldn't be here.
|
|
|
|
bfs::create_directory(to);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Why the hell path strings should be end with double null :/
|
2014-08-11 22:24:31 +03:00
|
|
|
auto makeDoubleNulled = [](const bfs::path& path) -> std::unique_ptr<wchar_t[]>
|
2014-08-11 00:42:39 +03:00
|
|
|
{
|
2014-08-11 22:24:31 +03:00
|
|
|
const std::wstring& pathStr = path.native();
|
|
|
|
std::unique_ptr<wchar_t[]> result(new wchar_t[pathStr.length() + 2]);
|
2014-08-11 00:42:39 +03:00
|
|
|
|
|
|
|
size_t i = 0;
|
2014-08-11 22:24:31 +03:00
|
|
|
for (const wchar_t ch : pathStr)
|
2014-08-11 00:42:39 +03:00
|
|
|
result[i++] = ch;
|
|
|
|
result[i++] = L'\0';
|
|
|
|
result[i++] = L'\0';
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
auto fromDNulled = makeDoubleNulled(from / L"*.*");
|
|
|
|
auto toDNulled = makeDoubleNulled(to);
|
|
|
|
|
|
|
|
SHFILEOPSTRUCTW fileOp;
|
|
|
|
fileOp.hwnd = GetConsoleWindow();
|
|
|
|
fileOp.wFunc = FO_MOVE;
|
|
|
|
fileOp.pFrom = fromDNulled.get();
|
|
|
|
fileOp.pTo = toDNulled.get();
|
|
|
|
fileOp.fFlags = 0;
|
|
|
|
fileOp.hNameMappings = nullptr;
|
|
|
|
fileOp.lpszProgressTitle = nullptr;
|
|
|
|
|
|
|
|
const int errorCode = SHFileOperationW(&fileOp);
|
|
|
|
if (errorCode != 0); // TODO: Log error. User should try to move files.
|
|
|
|
else if (fileOp.fAnyOperationsAborted); // TODO: Log warn. User aborted operation. User should move files.
|
2014-08-11 00:42:39 +03:00
|
|
|
else if (!bfs::is_empty(from)); // TODO: Log warn. Some files not moved. User should try to move files.
|
|
|
|
else // TODO: Log fact that we moved files succefully.
|
|
|
|
bfs::remove(from);
|
|
|
|
};
|
|
|
|
|
|
|
|
moveDirIfExists(userDataPath() / "Games", userSavePath());
|
2013-03-02 21:41:25 +03:00
|
|
|
}
|
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
bfs::path VCMIDirsWIN32::userDataPath() const
|
2013-03-02 21:41:25 +03:00
|
|
|
{
|
2014-08-11 22:24:31 +03:00
|
|
|
wchar_t profileDir[MAX_PATH];
|
2014-08-09 20:54:12 +03:00
|
|
|
|
|
|
|
// The user's profile folder. A typical path is C:\Users\username.
|
|
|
|
// FIXME: Applications should not create files or folders at this level;
|
|
|
|
// they should put their data under the locations referred to by CSIDL_APPDATA or CSIDL_LOCAL_APPDATA.
|
2014-08-11 22:24:31 +03:00
|
|
|
if (SHGetSpecialFolderPathW(nullptr, profileDir, CSIDL_PROFILE, FALSE) == FALSE) // WinAPI way failed
|
2014-08-09 18:22:37 +03:00
|
|
|
{
|
2014-08-11 22:24:31 +03:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER >= 1700
|
|
|
|
wchar_t* buffer;
|
|
|
|
size_t bufferSize;
|
|
|
|
errno_t result = _wdupenv_s(&buffer, &bufferSize, L"userprofile");
|
|
|
|
if (result == 0)
|
|
|
|
{
|
|
|
|
bfs::path result(std::wstring(buffer, bufferSize));
|
|
|
|
free(buffer);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
const char* profileDirA;
|
|
|
|
if (profileDirA = std::getenv("userprofile")) // STL way succeed
|
|
|
|
return bfs::path(profileDirA) / "vcmi";
|
|
|
|
#endif
|
2014-08-09 20:54:12 +03:00
|
|
|
else
|
|
|
|
return "."; // Every thing failed, return current directory.
|
|
|
|
}
|
|
|
|
else
|
2014-08-11 22:24:31 +03:00
|
|
|
return bfs::path(profileDir) / "vcmi";
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-09 20:54:12 +03:00
|
|
|
//return dataPaths()[0] ???;
|
|
|
|
}
|
2014-08-11 22:24:31 +03:00
|
|
|
bfs::path VCMIDirsWIN32::userCachePath() const { return userDataPath(); }
|
|
|
|
bfs::path VCMIDirsWIN32::userConfigPath() const { return userDataPath() / "config"; }
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
std::vector<bfs::path> VCMIDirsWIN32::dataPaths() const
|
2014-08-09 20:54:12 +03:00
|
|
|
{
|
|
|
|
return std::vector<bfs::path>(1, bfs::path("."));
|
|
|
|
}
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
bfs::path VCMIDirsWIN32::clientPath() const { return binaryPath() / "VCMI_client.exe"; }
|
|
|
|
bfs::path VCMIDirsWIN32::serverPath() const { return binaryPath() / "VCMI_server.exe"; }
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
bfs::path VCMIDirsWIN32::libraryPath() const { return "."; }
|
|
|
|
bfs::path VCMIDirsWIN32::binaryPath() const { return "."; }
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
std::string VCMIDirsWIN32::genHelpString() const
|
2014-08-09 20:54:12 +03:00
|
|
|
{
|
2013-03-02 21:41:25 +03:00
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
std::vector<std::string> tempVec;
|
2014-08-09 20:54:12 +03:00
|
|
|
for (const bfs::path& path : dataPaths())
|
2014-08-11 22:24:31 +03:00
|
|
|
tempVec.push_back(path.string());
|
|
|
|
std::string gdStringA = boost::algorithm::join(tempVec, ";");
|
2014-08-09 20:54:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
return
|
2014-08-11 22:24:31 +03:00
|
|
|
" game data: " + gdStringA + "\n"
|
2014-08-11 00:42:39 +03:00
|
|
|
" 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?
|
2013-03-02 21:41:25 +03:00
|
|
|
}
|
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
std::string VCMIDirsWIN32::libraryName(const std::string& basename) const { return basename + ".dll"; }
|
2014-08-11 00:42:39 +03:00
|
|
|
#elif defined(VCMI_UNIX)
|
2014-08-11 22:24:31 +03:00
|
|
|
class IVCMIDirsUNIX : public IVCMIDirs
|
2013-03-02 21:41:25 +03:00
|
|
|
{
|
2014-08-09 20:54:12 +03:00
|
|
|
public:
|
|
|
|
boost::filesystem::path clientPath() const override;
|
|
|
|
boost::filesystem::path serverPath() const override;
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-09 20:54:12 +03:00
|
|
|
std::string genHelpString() const override;
|
|
|
|
};
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
bfs::path IVCMIDirsUNIX::clientPath() const { return binaryPath() / "vcmiclient"; }
|
|
|
|
bfs::path IVCMIDirsUNIX::clientPath() const { return binaryPath() / "vcmiserver"; }
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
std::string IVCMIDirsUNIX::genHelpString() const
|
2014-08-09 20:54:12 +03:00
|
|
|
{
|
2014-08-11 22:24:31 +03:00
|
|
|
std::vector<std::string> tempVec;
|
2014-08-09 20:54:12 +03:00
|
|
|
for (const bfs::path& path : dataPaths())
|
2014-08-11 22:24:31 +03:00
|
|
|
tempVec.push_back(path.string());
|
|
|
|
std::string gdStringA = boost::algorithm::join(tempVec, ":");
|
2014-08-09 20:54:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
return
|
2014-08-11 22:24:31 +03:00
|
|
|
" game data: " + gdStringA + "\n"
|
2014-08-11 00:42:39 +03:00
|
|
|
" libraries: " + libraryPath().string() + "\n"
|
|
|
|
" server: " + serverPath().string() + "\n"
|
|
|
|
"\n"
|
|
|
|
" user data: " + userDataPath().string() + "\n"
|
|
|
|
" user cache: " + userCachePath().string() + "\n"
|
|
|
|
" user config: " + userConfigPath().string() + "\n"
|
2014-08-09 20:54:12 +03:00
|
|
|
" user saves: " + userSavePath().string() + "\n"; // Should end without new-line?
|
|
|
|
}
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-11 00:42:39 +03:00
|
|
|
#ifdef VCMI_APPLE
|
2014-08-11 22:24:31 +03:00
|
|
|
class VCMIDirsOSX : public IVCMIDirsUNIX
|
2014-08-09 20:54:12 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
boost::filesystem::path userDataPath() const override;
|
|
|
|
boost::filesystem::path userCachePath() const override;
|
|
|
|
boost::filesystem::path userConfigPath() const override;
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-09 20:54:12 +03:00
|
|
|
std::vector<boost::filesystem::path> dataPaths() const override;
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-09 20:54:12 +03:00
|
|
|
boost::filesystem::path libraryPath() const override;
|
|
|
|
boost::filesystem::path binaryPath() const override;
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-09 20:54:12 +03:00
|
|
|
std::string libraryName(const std::string& basename) const override;
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-11 00:42:39 +03:00
|
|
|
void init() override;
|
|
|
|
};
|
2013-03-02 21:41:25 +03:00
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
void VCMIDirsOSX::init()
|
2014-08-09 20:54:12 +03:00
|
|
|
{
|
2014-08-11 00:42:39 +03:00
|
|
|
// Call base (init dirs)
|
2014-08-11 22:24:31 +03:00
|
|
|
IVCMIDirsUNIX::init();
|
2014-08-11 00:42:39 +03:00
|
|
|
|
|
|
|
auto moveDirIfExists = [](const bfs::path& from, const bfs::path& to)
|
2014-08-09 18:22:37 +03:00
|
|
|
{
|
2014-08-11 00:42:39 +03:00
|
|
|
if (!bfs::is_directory(from))
|
|
|
|
return; // Nothing to do here. Flies away.
|
|
|
|
|
|
|
|
if (bfs::is_empty(from))
|
|
|
|
{
|
|
|
|
bfs::remove(from);
|
|
|
|
return; // Nothing to do here. Flies away.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bfs::is_directory(to))
|
|
|
|
{
|
|
|
|
// IVCMIDirs::init() should create all destination directories.
|
|
|
|
// TODO: Log fact, that we shouldn't be here.
|
|
|
|
bfs::create_directory(to);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (bfs::directory_iterator file(from); file != bfs::directory_iterator(); ++file)
|
|
|
|
{
|
2014-08-11 22:24:31 +03:00
|
|
|
const boost::filesystem::path& srcFilePath = file->path();
|
|
|
|
const boost::filesystem::path dstFilePath = to / srcFilePath.filename();
|
2014-08-11 00:42:39 +03:00
|
|
|
|
|
|
|
// TODO: Aplication should ask user what to do when file exists:
|
|
|
|
// replace/ignore/stop process/replace all/ignore all
|
2014-08-11 22:24:31 +03:00
|
|
|
if (!boost::filesystem::exists(dstFilePath))
|
|
|
|
bfs::rename(srcFilePath, dstFilePath);
|
2014-08-11 00:42:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!bfs::is_empty(from)); // TODO: Log warn. Some files not moved. User should try to move files.
|
|
|
|
else
|
|
|
|
bfs::remove(from);
|
|
|
|
};
|
|
|
|
|
|
|
|
moveDirIfExists(userDataPath() / "Games", userSavePath());
|
2013-03-02 21:41:25 +03:00
|
|
|
}
|
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
bfs::path VCMIDirsOSX::userDataPath() const
|
2013-03-02 21:41:25 +03:00
|
|
|
{
|
2014-08-09 20:54:12 +03:00
|
|
|
// 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];
|
|
|
|
// UserPath = path([urls[0] path] + "/vcmi").string();
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-09 20:54:12 +03:00
|
|
|
// ...so here goes a bit of hardcode instead
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
const char* homeDir = getenv("HOME"); // Should be std::getenv?
|
|
|
|
if (homeDir == nullptr)
|
|
|
|
homeDir = ".";
|
|
|
|
return bfs::path(homeDir) / "Library" / "Application Support" / "vcmi";
|
2014-08-09 20:54:12 +03:00
|
|
|
}
|
2014-08-11 22:24:31 +03:00
|
|
|
bfs::path VCMIDirsOSX::userCachePath() const { return userDataPath(); }
|
|
|
|
bfs::path VCMIDirsOSX::userConfigPath() const { return userDataPath() / "config"; }
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
std::vector<bfs::path> VCMIDirsOSX::dataPaths() const
|
2014-08-09 20:54:12 +03:00
|
|
|
{
|
|
|
|
return std::vector<bfs::path>(1, "../Data");
|
|
|
|
}
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
bfs::path VCMIDirsOSX::libraryPath() const { return "."; }
|
|
|
|
bfs::path VCMIDirsOSX::binaryPath() const { return "."; }
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-09 20:54:12 +03:00
|
|
|
std::string libraryName(const std::string& basename) { return "lib" + basename + ".dylib"; }
|
2014-08-11 00:42:39 +03:00
|
|
|
#elif defined(VCMI_LINUX)
|
2014-08-11 22:24:31 +03:00
|
|
|
class VCMIDirsLinux : public IVCMIDirsUNIX
|
2014-08-09 20:54:12 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
boost::filesystem::path userDataPath() const override;
|
|
|
|
boost::filesystem::path userCachePath() const override;
|
|
|
|
boost::filesystem::path userConfigPath() const override;
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-09 20:54:12 +03:00
|
|
|
std::vector<boost::filesystem::path> dataPaths() const override;
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-09 20:54:12 +03:00
|
|
|
boost::filesystem::path libraryPath() const override;
|
|
|
|
boost::filesystem::path binaryPath() const override;
|
|
|
|
|
|
|
|
std::string libraryName(const std::string& basename) const override;
|
|
|
|
};
|
2014-03-04 17:51:10 +03:00
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
bfs::path VCMIDirsLinux::userDataPath() const
|
2014-03-04 17:51:10 +03:00
|
|
|
{
|
2014-08-09 20:54:12 +03:00
|
|
|
// $XDG_DATA_HOME, default: $HOME/.local/share
|
2014-08-11 22:24:31 +03:00
|
|
|
const char* homeDir;
|
|
|
|
if (homeDir = getenv("XDG_DATA_HOME"))
|
|
|
|
return homeDir;
|
|
|
|
else if (homeDir = getenv("HOME"))
|
|
|
|
return bfs::path(homeDir) / ".local" / "share" / "vcmi";
|
2014-08-09 20:54:12 +03:00
|
|
|
else
|
|
|
|
return ".";
|
|
|
|
}
|
2014-08-11 22:24:31 +03:00
|
|
|
bfs::path VCMIDirsLinux::userCachePath() const
|
2014-08-09 20:54:12 +03:00
|
|
|
{
|
|
|
|
// $XDG_CACHE_HOME, default: $HOME/.cache
|
2014-08-11 22:24:31 +03:00
|
|
|
const char* tempResult;
|
|
|
|
if (tempResult = getenv("XDG_CACHE_HOME"))
|
|
|
|
return bfs::path(tempResult) / "vcmi";
|
|
|
|
else if (tempResult = getenv("HOME"))
|
|
|
|
return bfs::path(tempResult) / ".cache" / "vcmi";
|
2014-08-09 20:54:12 +03:00
|
|
|
else
|
|
|
|
return ".";
|
|
|
|
}
|
2014-08-11 22:24:31 +03:00
|
|
|
bfs::path VCMIDirsLinux::userConfigPath() const
|
2014-08-09 20:54:12 +03:00
|
|
|
{
|
|
|
|
// $XDG_CONFIG_HOME, default: $HOME/.config
|
2014-08-11 22:24:31 +03:00
|
|
|
const char* tempResult;
|
|
|
|
if (tempResult = getenv("XDG_CONFIG_HOME"))
|
|
|
|
return bfs::path(tempResult) / "vcmi";
|
|
|
|
else if (tempResult = getenv("HOME"))
|
|
|
|
return bfs::path(tempResult) / ".config" / "vcmi";
|
2014-08-09 20:54:12 +03:00
|
|
|
else
|
|
|
|
return ".";
|
|
|
|
}
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
std::vector<bfs::path> VCMIDirsLinux::dataPaths() const
|
2014-08-09 20:54:12 +03:00
|
|
|
{
|
|
|
|
// $XDG_DATA_DIRS, default: /usr/local/share/:/usr/share/
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-09 20:54:12 +03:00
|
|
|
// construct list in reverse.
|
|
|
|
// in specification first directory has highest priority
|
|
|
|
// in vcmi fs last directory has highest priority
|
|
|
|
std::vector<bfs::path> ret;
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
const char* tempResult;
|
2014-08-09 20:54:12 +03:00
|
|
|
ret.push_back(M_DATA_DIR);
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
if ((tempResult = getenv("XDG_DATA_DIRS")) != nullptr)
|
2014-08-09 20:54:12 +03:00
|
|
|
{
|
2014-08-11 22:24:31 +03:00
|
|
|
std::string dataDirsEnv = tempResult;
|
2014-08-09 20:54:12 +03:00
|
|
|
std::vector<std::string> dataDirs;
|
|
|
|
boost::split(dataDirs, dataDirsEnv, boost::is_any_of(":"));
|
|
|
|
for (auto & entry : boost::adaptors::reverse(dataDirs))
|
|
|
|
ret.push_back(entry + "/vcmi");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret.push_back("/usr/share/");
|
|
|
|
ret.push_back("/usr/local/share/");
|
|
|
|
}
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-09 20:54:12 +03:00
|
|
|
return ret;
|
|
|
|
}
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
bfs::path VCMIDirsLinux::libraryPath() const { return M_LIB_PATH; }
|
|
|
|
bfs::path VCMIDirsLinux::binaryPath() const { return M_BIN_DIR; }
|
2014-08-09 18:22:37 +03:00
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
std::string VCMIDirsLinux::libraryName(const std::string& basename) const { return "lib" + basename + ".so"; }
|
2014-08-11 00:42:39 +03:00
|
|
|
#ifdef VCMI_ANDROID
|
2014-08-11 22:24:31 +03:00
|
|
|
class VCMIDirsAndroid : public VCMIDirsLinux
|
2014-08-09 20:54:12 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
boost::filesystem::path userDataPath() const override;
|
|
|
|
boost::filesystem::path userCachePath() const override;
|
|
|
|
boost::filesystem::path userConfigPath() const override;
|
|
|
|
|
|
|
|
std::vector<boost::filesystem::path> dataPaths() const override;
|
|
|
|
};
|
|
|
|
|
|
|
|
// on Android HOME will be set to something like /sdcard/data/Android/is.xyz.vcmi/files/
|
2014-08-11 22:24:31 +03:00
|
|
|
bfs::path VCMIDirsAndroid::userDataPath() const { return getenv("HOME"); }
|
|
|
|
bfs::path VCMIDirsAndroid::userCachePath() const { return userDataPath() / "cache"; }
|
|
|
|
bfs::path VCMIDirsAndroid::userConfigPath() const { return userDataPath() / "config"; }
|
2014-08-09 20:54:12 +03:00
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
std::vector<bfs::path> VCMIDirsAndroid::dataPaths() const
|
2014-08-09 20:54:12 +03:00
|
|
|
{
|
|
|
|
return std::vector<bfs::path>(1, userDataPath());
|
|
|
|
}
|
2014-08-11 00:42:39 +03:00
|
|
|
#endif // VCMI_ANDROID
|
|
|
|
#endif // VCMI_APPLE, VCMI_LINUX
|
|
|
|
#endif // VCMI_WINDOWS, VCMI_UNIX
|
|
|
|
|
|
|
|
// Getters for interfaces are separated for clarity.
|
|
|
|
namespace VCMIDirs
|
|
|
|
{
|
|
|
|
const IVCMIDirs& get()
|
|
|
|
{
|
|
|
|
#ifdef VCMI_WINDOWS
|
2014-08-11 22:24:31 +03:00
|
|
|
static VCMIDirsWIN32 singleton;
|
2014-08-11 00:42:39 +03:00
|
|
|
#elif defined(VCMI_ANDROID)
|
2014-08-11 22:24:31 +03:00
|
|
|
static VCMIDirsAndroid singleton;
|
2014-08-11 00:42:39 +03:00
|
|
|
#elif defined(VCMI_LINUX)
|
2014-08-11 22:24:31 +03:00
|
|
|
static VCMIDirsLinux singleton;
|
2014-08-11 00:42:39 +03:00
|
|
|
#elif defined(VCMI_APPLE)
|
2014-08-11 22:24:31 +03:00
|
|
|
static VCMIDirsOSX singleton;
|
2014-08-11 00:42:39 +03:00
|
|
|
#endif
|
|
|
|
static bool initialized = false;
|
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
singleton.init();
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
}
|