|
@@ -1,6 +1,3 @@
|
|
|
-#include "StdInc.h"
|
|
|
-#include "VCMIDirs.h"
|
|
|
-
|
|
|
/*
|
|
|
* VCMIDirs.cpp, part of VCMI engine
|
|
|
*
|
|
@@ -11,216 +8,504 @@
|
|
|
*
|
|
|
*/
|
|
|
|
|
|
-static VCMIDirs VCMIDirsGlobal;
|
|
|
-
|
|
|
-VCMIDirs::VCMIDirs()
|
|
|
-{
|
|
|
- // initialize local directory and create folders to which VCMI needs write access
|
|
|
- boost::filesystem::create_directory(userDataPath());
|
|
|
- boost::filesystem::create_directory(userCachePath());
|
|
|
- boost::filesystem::create_directory(userConfigPath());
|
|
|
- boost::filesystem::create_directory(userSavePath());
|
|
|
-}
|
|
|
+#include "StdInc.h"
|
|
|
+#include "VCMIDirs.h"
|
|
|
|
|
|
-VCMIDirs & VCMIDirs::get()
|
|
|
-{
|
|
|
- return VCMIDirsGlobal;
|
|
|
-}
|
|
|
+namespace bfs = boost::filesystem;
|
|
|
|
|
|
-//FIXME: find way to at least decrease size of this ifdef (along with cleanup in CMake)
|
|
|
-#if defined(_WIN32)
|
|
|
+bfs::path IVCMIDirs::userSavePath() const { return userDataPath() / "Saves"; }
|
|
|
|
|
|
-std::string VCMIDirs::userCachePath() const
|
|
|
+void IVCMIDirs::init()
|
|
|
{
|
|
|
- return userDataPath();
|
|
|
+ // TODO: Log errors
|
|
|
+ bfs::create_directory(userDataPath());
|
|
|
+ bfs::create_directory(userCachePath());
|
|
|
+ bfs::create_directory(userConfigPath());
|
|
|
+ bfs::create_directory(userSavePath());
|
|
|
}
|
|
|
|
|
|
-std::string VCMIDirs::userConfigPath() const
|
|
|
+#ifdef VCMI_WINDOWS
|
|
|
+
|
|
|
+#include <Windows.h>
|
|
|
+#include <Shlobj.h>
|
|
|
+#include <Shellapi.h>
|
|
|
+
|
|
|
+// Generates script file named _temp.bat in 'to' directory and runs it
|
|
|
+// Script will:
|
|
|
+// - Wait util 'exeName' ends.
|
|
|
+// - Copy all files from 'from' to 'to'
|
|
|
+// - Ask user to replace files existed in 'to'.
|
|
|
+// - Run 'exeName'
|
|
|
+// - Delete itself.
|
|
|
+bool StartBatchCopyDataProgram(
|
|
|
+ const bfs::path& from, const bfs::path& to, const bfs::path& exeName,
|
|
|
+ const bfs::path& currentPath = bfs::current_path())
|
|
|
{
|
|
|
- return userDataPath() + "/config";
|
|
|
+ static const char base[] =
|
|
|
+ "@echo off" "\n"
|
|
|
+ "echo Preparing to move VCMI data system." "\n"
|
|
|
+
|
|
|
+ ":CLIENT_RUNNING_LOOP" "\n"
|
|
|
+ "TASKLIST | FIND /I %1% > nul" "\n"
|
|
|
+ "IF ERRORLEVEL 1 (" "\n"
|
|
|
+ "GOTO CLIENT_NOT_RUNNING" "\n"
|
|
|
+ ") ELSE (" "\n"
|
|
|
+ "echo %1% is still running..." "\n"
|
|
|
+ "echo Waiting until process ends..." "\n"
|
|
|
+ "ping 1.1.1.1 -n 1 -w 3000 > nul" "\n" // Sleep ~3 seconds. I love Windows :)
|
|
|
+ "goto :CLIENT_RUNNING_LOOP" "\n"
|
|
|
+ ")" "\n"
|
|
|
+
|
|
|
+ ":CLIENT_NOT_RUNNING" "\n"
|
|
|
+ "echo %1% turned off..." "\n"
|
|
|
+ "echo Attempt to move datas." "\n"
|
|
|
+ "echo From: %2%" "\n"
|
|
|
+ "echo To: %4%" "\n"
|
|
|
+ "echo Please resolve any conflicts..." "\n"
|
|
|
+ "move /-Y %3% %4%" "\n" // Move all files from %3% to %4%.
|
|
|
+ // /-Y ask what to do when file exists in %4%
|
|
|
+ ":REMOVE_OLD_DIR" "\n"
|
|
|
+ "rd %2% || rem" "\n" // Remove empty directory. Sets error flag if fail.
|
|
|
+ "IF ERRORLEVEL 145 (" "\n" // Directory not empty
|
|
|
+ "echo Directory %2% is not empty." "\n"
|
|
|
+ "echo Please move rest of files manually now." "\n"
|
|
|
+ "pause" "\n" // Press any key to continue...
|
|
|
+ "goto REMOVE_OLD_DIR" "\n"
|
|
|
+ ")" "\n"
|
|
|
+ "echo Game data updated succefully." "\n"
|
|
|
+ "echo Please update your shortcuts." "\n"
|
|
|
+ "echo Press any key to start a game . . ." "\n"
|
|
|
+ "pause > nul" "\n"
|
|
|
+ "%5%" "\n"
|
|
|
+ "del \"%%~f0\"&exit" "\n" // Script deletes itself
|
|
|
+ ;
|
|
|
+
|
|
|
+ const auto startGameString =
|
|
|
+ bfs::equivalent(currentPath, from) ?
|
|
|
+ (boost::format("start \"\" %1%") % (to / exeName)) : // Start game in new path.
|
|
|
+ (boost::format("start \"\" /D %1% %2%") % currentPath % (to / exeName)); // Start game in 'currentPath"
|
|
|
+
|
|
|
+ const bfs::path bathFilename = to / "_temp.bat";
|
|
|
+ bfs::ofstream bathFile(bathFilename, bfs::ofstream::trunc | bfs::ofstream::out);
|
|
|
+ if (!bathFile.is_open())
|
|
|
+ return false;
|
|
|
+ bathFile << (boost::format(base) % exeName % from % (from / "*.*") % to % startGameString.str()).str();
|
|
|
+ bathFile.close();
|
|
|
+
|
|
|
+ std::system(("start \"Updating VCMI datas\" /D \"" + to.string() + "\" \"" + bathFilename.string() + '\"').c_str());
|
|
|
+ // start won't block std::system
|
|
|
+ // /D start bat in other directory insteand of current directory.
|
|
|
+
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
-std::string VCMIDirs::userSavePath() const
|
|
|
+class VCMIDirsWIN32 : public IVCMIDirs
|
|
|
{
|
|
|
- return userDataPath() + "/Games";
|
|
|
-}
|
|
|
+ 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;
|
|
|
+
|
|
|
+ boost::filesystem::path clientPath() const override;
|
|
|
+ boost::filesystem::path serverPath() const override;
|
|
|
|
|
|
-std::string VCMIDirs::userDataPath() const
|
|
|
+ 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;
|
|
|
+
|
|
|
+ void init() override;
|
|
|
+ protected:
|
|
|
+ boost::filesystem::path oldUserDataPath() const;
|
|
|
+ boost::filesystem::path oldUserSavePath() const;
|
|
|
+};
|
|
|
+
|
|
|
+void VCMIDirsWIN32::init()
|
|
|
{
|
|
|
- const std::string homeDir = std::getenv("userprofile");
|
|
|
- return homeDir + "\\vcmi";
|
|
|
- //return dataPaths()[0];
|
|
|
+ // Call base (init dirs)
|
|
|
+ IVCMIDirs::init();
|
|
|
+
|
|
|
+ // Moves one directory (from) contents to another directory (to)
|
|
|
+ // Shows user the "moving file dialog" and ask to resolve conflits.
|
|
|
+ // If necessary updates current directory.
|
|
|
+ auto moveDirIfExists = [](const bfs::path& from, const bfs::path& to) -> bool
|
|
|
+ {
|
|
|
+ if (!bfs::is_directory(from))
|
|
|
+ return true; // Nothing to do here. Flies away.
|
|
|
+
|
|
|
+ if (bfs::is_empty(from))
|
|
|
+ {
|
|
|
+ if (bfs::current_path() == from)
|
|
|
+ bfs::current_path(to);
|
|
|
+
|
|
|
+ bfs::remove(from);
|
|
|
+ return true; // 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 :/
|
|
|
+ auto makeDoubleNulled = [](const bfs::path& path) -> std::unique_ptr<wchar_t[]>
|
|
|
+ {
|
|
|
+ const std::wstring& pathStr = path.native();
|
|
|
+ std::unique_ptr<wchar_t[]> result(new wchar_t[pathStr.length() + 2]);
|
|
|
+
|
|
|
+ size_t i = 0;
|
|
|
+ for (const wchar_t ch : pathStr)
|
|
|
+ result[i++] = ch;
|
|
|
+ result[i++] = L'\0';
|
|
|
+ result[i++] = L'\0';
|
|
|
+
|
|
|
+ return result;
|
|
|
+ };
|
|
|
+
|
|
|
+ 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.
|
|
|
+ return false;
|
|
|
+ else if (fileOp.fAnyOperationsAborted) // TODO: Log warn. User aborted operation. User should move files.
|
|
|
+ return false;
|
|
|
+ else if (!bfs::is_empty(from)) // TODO: Log warn. Some files not moved. User should try to move files.
|
|
|
+ return false;
|
|
|
+
|
|
|
+ if (bfs::current_path() == from)
|
|
|
+ bfs::current_path(to);
|
|
|
+
|
|
|
+ // TODO: Log fact that we moved files succefully.
|
|
|
+ bfs::remove(from);
|
|
|
+ return true;
|
|
|
+ };
|
|
|
+
|
|
|
+ // Retrieves the fully qualified path for the file that contains the specified module.
|
|
|
+ // The module must have been loaded by the current process.
|
|
|
+ // If this parameter is nullptr, retrieves the path of the executable file of the current process.
|
|
|
+ auto getModulePath = [](HMODULE hModule) -> bfs::path
|
|
|
+ {
|
|
|
+ wchar_t exePathW[MAX_PATH];
|
|
|
+ DWORD nSize = GetModuleFileNameW(hModule, exePathW, MAX_PATH);
|
|
|
+ DWORD error = GetLastError();
|
|
|
+ // WARN: Windows XP don't set ERROR_INSUFFICIENT_BUFFER error.
|
|
|
+ if (nSize != 0 && error != ERROR_INSUFFICIENT_BUFFER)
|
|
|
+ return bfs::path(std::wstring(exePathW, nSize));
|
|
|
+ // TODO: Error handling
|
|
|
+ return bfs::path();
|
|
|
+ };
|
|
|
+
|
|
|
+ // Moves one directory contents to another directory
|
|
|
+ // Shows user the "moving file dialog" and ask to resolve conflicts.
|
|
|
+ // It takes into account that 'from' path can contain current executable.
|
|
|
+ // If necessary closes program and starts update script.
|
|
|
+ auto advancedMoveDirIfExists = [getModulePath, moveDirIfExists](const bfs::path& from, const bfs::path& to) -> bool
|
|
|
+ {
|
|
|
+ const bfs::path executablePath = getModulePath(nullptr);
|
|
|
+
|
|
|
+ // VCMI cann't determine executable path.
|
|
|
+ // Use standard way to move directory and exit function.
|
|
|
+ if (executablePath.empty())
|
|
|
+ return moveDirIfExists(from, to);
|
|
|
+
|
|
|
+ const bfs::path executableName = executablePath.filename();
|
|
|
+
|
|
|
+ // Current executabl isn't in 'from' path.
|
|
|
+ // Use standard way to move directory and exit function.
|
|
|
+ if (!bfs::equivalent(executablePath, from / executableName))
|
|
|
+ return moveDirIfExists(from, to);
|
|
|
+
|
|
|
+ // Try standard way to move directory.
|
|
|
+ // I don't know how other systems, but Windows 8.1 allow to move running executable.
|
|
|
+ if (moveDirIfExists(from, to))
|
|
|
+ return true;
|
|
|
+
|
|
|
+ // Start copying script and exit program.
|
|
|
+ if (StartBatchCopyDataProgram(from, to, executableName))
|
|
|
+ exit(ERROR_SUCCESS);
|
|
|
+
|
|
|
+ // Everything failed :C
|
|
|
+ return false;
|
|
|
+ };
|
|
|
+
|
|
|
+ moveDirIfExists(oldUserSavePath(), userSavePath());
|
|
|
+ advancedMoveDirIfExists(oldUserDataPath(), userDataPath());
|
|
|
}
|
|
|
|
|
|
-std::string VCMIDirs::libraryPath() const
|
|
|
+bfs::path VCMIDirsWIN32::userDataPath() const
|
|
|
{
|
|
|
+ wchar_t profileDir[MAX_PATH];
|
|
|
+
|
|
|
+ if (SHGetSpecialFolderPathW(nullptr, profileDir, CSIDL_MYDOCUMENTS, FALSE) != FALSE)
|
|
|
+ return bfs::path(profileDir) / "My Games\\vcmi";
|
|
|
+
|
|
|
return ".";
|
|
|
}
|
|
|
|
|
|
-std::string VCMIDirs::clientPath() const
|
|
|
+bfs::path VCMIDirsWIN32::oldUserDataPath() const
|
|
|
{
|
|
|
- return libraryPath() + "\\" + "VCMI_client.exe";
|
|
|
-}
|
|
|
+ wchar_t profileDir[MAX_PATH];
|
|
|
+
|
|
|
+ if (SHGetSpecialFolderPathW(nullptr, profileDir, CSIDL_PROFILE, FALSE) == FALSE) // WinAPI way failed
|
|
|
+ {
|
|
|
+#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
|
|
|
+ else
|
|
|
+ return "."; // Every thing failed, return current directory.
|
|
|
+ }
|
|
|
+ else
|
|
|
+ return bfs::path(profileDir) / "vcmi";
|
|
|
|
|
|
-std::string VCMIDirs::serverPath() const
|
|
|
-{
|
|
|
- return libraryPath() + "\\" + "VCMI_server.exe";
|
|
|
+ //return dataPaths()[0] ???;
|
|
|
}
|
|
|
+bfs::path VCMIDirsWIN32::oldUserSavePath() const { return userDataPath() / "Games"; }
|
|
|
|
|
|
-std::vector<std::string> VCMIDirs::dataPaths() const
|
|
|
-{
|
|
|
- return std::vector<std::string>(1, ".");
|
|
|
-}
|
|
|
+bfs::path VCMIDirsWIN32::userCachePath() const { return userDataPath(); }
|
|
|
+bfs::path VCMIDirsWIN32::userConfigPath() const { return userDataPath() / "config"; }
|
|
|
|
|
|
-std::string VCMIDirs::libraryName(std::string basename) const
|
|
|
+std::vector<bfs::path> VCMIDirsWIN32::dataPaths() const
|
|
|
{
|
|
|
- return basename + ".dll";
|
|
|
+ return std::vector<bfs::path>(1, bfs::path("."));
|
|
|
}
|
|
|
|
|
|
-#elif defined(__APPLE__)
|
|
|
+bfs::path VCMIDirsWIN32::clientPath() const { return binaryPath() / "VCMI_client.exe"; }
|
|
|
+bfs::path VCMIDirsWIN32::serverPath() const { return binaryPath() / "VCMI_server.exe"; }
|
|
|
|
|
|
-std::string VCMIDirs::userCachePath() const
|
|
|
-{
|
|
|
- return userDataPath();
|
|
|
-}
|
|
|
+bfs::path VCMIDirsWIN32::libraryPath() const { return "."; }
|
|
|
+bfs::path VCMIDirsWIN32::binaryPath() const { return "."; }
|
|
|
|
|
|
-std::string VCMIDirs::userConfigPath() const
|
|
|
+std::string VCMIDirsWIN32::genHelpString() const
|
|
|
{
|
|
|
- return userDataPath() + "/config";
|
|
|
-}
|
|
|
|
|
|
-std::string VCMIDirs::userSavePath() const
|
|
|
-{
|
|
|
- return userDataPath() + "/Games";
|
|
|
+ std::vector<std::string> tempVec;
|
|
|
+ for (const bfs::path& path : dataPaths())
|
|
|
+ tempVec.push_back(path.string());
|
|
|
+ std::string gdStringA = boost::algorithm::join(tempVec, ";");
|
|
|
+
|
|
|
+
|
|
|
+ return
|
|
|
+ " game data: " + gdStringA + "\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?
|
|
|
}
|
|
|
|
|
|
-std::string VCMIDirs::userDataPath() const
|
|
|
+std::string VCMIDirsWIN32::libraryName(const std::string& basename) const { return basename + ".dll"; }
|
|
|
+#elif defined(VCMI_UNIX)
|
|
|
+class IVCMIDirsUNIX : public IVCMIDirs
|
|
|
{
|
|
|
- // 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();
|
|
|
+ public:
|
|
|
+ boost::filesystem::path clientPath() const override;
|
|
|
+ boost::filesystem::path serverPath() const override;
|
|
|
|
|
|
- // ...so here goes a bit of hardcode instead
|
|
|
- std::string home_dir = ".";
|
|
|
- if (getenv("HOME") != nullptr )
|
|
|
- home_dir = getenv("HOME");
|
|
|
+ std::string genHelpString() const override;
|
|
|
+};
|
|
|
|
|
|
- return boost::filesystem::path(home_dir + "/Library/Application Support/vcmi").string();
|
|
|
-}
|
|
|
+bfs::path IVCMIDirsUNIX::clientPath() const { return binaryPath() / "vcmiclient"; }
|
|
|
+bfs::path IVCMIDirsUNIX::serverPath() const { return binaryPath() / "vcmiserver"; }
|
|
|
|
|
|
-std::string VCMIDirs::libraryPath() const
|
|
|
+std::string IVCMIDirsUNIX::genHelpString() const
|
|
|
{
|
|
|
- return ".";
|
|
|
-}
|
|
|
+ std::vector<std::string> tempVec;
|
|
|
+ for (const bfs::path& path : dataPaths())
|
|
|
+ tempVec.push_back(path.string());
|
|
|
+ std::string gdStringA = boost::algorithm::join(tempVec, ":");
|
|
|
|
|
|
-std::string VCMIDirs::clientPath() const
|
|
|
-{
|
|
|
- return "./vcmiclient";
|
|
|
-}
|
|
|
|
|
|
-std::string VCMIDirs::serverPath() const
|
|
|
-{
|
|
|
- return "./vcmiserver";
|
|
|
+ return
|
|
|
+ " game data: " + gdStringA + "\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?
|
|
|
}
|
|
|
|
|
|
-std::vector<std::string> VCMIDirs::dataPaths() const
|
|
|
+#ifdef VCMI_APPLE
|
|
|
+class VCMIDirsOSX : public IVCMIDirsUNIX
|
|
|
{
|
|
|
- return std::vector<std::string>(1, "../Data");
|
|
|
-}
|
|
|
+ public:
|
|
|
+ boost::filesystem::path userDataPath() const override;
|
|
|
+ boost::filesystem::path userCachePath() const override;
|
|
|
+ boost::filesystem::path userConfigPath() const override;
|
|
|
|
|
|
-std::string VCMIDirs::libraryName(std::string basename) const
|
|
|
-{
|
|
|
- return "lib" + basename + ".dylib";
|
|
|
-}
|
|
|
+ std::vector<boost::filesystem::path> dataPaths() const override;
|
|
|
|
|
|
-#else
|
|
|
+ boost::filesystem::path libraryPath() const override;
|
|
|
+ boost::filesystem::path binaryPath() const override;
|
|
|
|
|
|
-std::string VCMIDirs::libraryName(std::string basename) const
|
|
|
-{
|
|
|
- return "lib" + basename + ".so";
|
|
|
-}
|
|
|
+ std::string libraryName(const std::string& basename) const override;
|
|
|
|
|
|
-std::string VCMIDirs::libraryPath() const
|
|
|
+ void init() override;
|
|
|
+};
|
|
|
+
|
|
|
+void VCMIDirsOSX::init()
|
|
|
{
|
|
|
- return M_LIB_DIR;
|
|
|
+ // Call base (init dirs)
|
|
|
+ IVCMIDirsUNIX::init();
|
|
|
+
|
|
|
+ auto moveDirIfExists = [](const bfs::path& from, const bfs::path& to)
|
|
|
+ {
|
|
|
+ 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)
|
|
|
+ {
|
|
|
+ const boost::filesystem::path& srcFilePath = file->path();
|
|
|
+ const boost::filesystem::path dstFilePath = to / srcFilePath.filename();
|
|
|
+
|
|
|
+ // TODO: Aplication should ask user what to do when file exists:
|
|
|
+ // replace/ignore/stop process/replace all/ignore all
|
|
|
+ if (!boost::filesystem::exists(dstFilePath))
|
|
|
+ bfs::rename(srcFilePath, dstFilePath);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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());
|
|
|
}
|
|
|
|
|
|
-std::string VCMIDirs::clientPath() const
|
|
|
+bfs::path VCMIDirsOSX::userDataPath() const
|
|
|
{
|
|
|
- return std::string(M_BIN_DIR) + "/" + "vcmiclient";
|
|
|
+ // 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();
|
|
|
+
|
|
|
+ // ...so here goes a bit of hardcode instead
|
|
|
+
|
|
|
+ const char* homeDir = getenv("HOME"); // Should be std::getenv?
|
|
|
+ if (homeDir == nullptr)
|
|
|
+ homeDir = ".";
|
|
|
+ return bfs::path(homeDir) / "Library" / "Application Support" / "vcmi";
|
|
|
}
|
|
|
+bfs::path VCMIDirsOSX::userCachePath() const { return userDataPath(); }
|
|
|
+bfs::path VCMIDirsOSX::userConfigPath() const { return userDataPath() / "config"; }
|
|
|
|
|
|
-std::string VCMIDirs::serverPath() const
|
|
|
+std::vector<bfs::path> VCMIDirsOSX::dataPaths() const
|
|
|
{
|
|
|
- return std::string(M_BIN_DIR) + "/" + "vcmiserver";
|
|
|
+ return std::vector<bfs::path>(1, "../Data");
|
|
|
}
|
|
|
|
|
|
-// $XDG_DATA_HOME, default: $HOME/.local/share
|
|
|
-std::string VCMIDirs::userDataPath() const
|
|
|
+bfs::path VCMIDirsOSX::libraryPath() const { return "."; }
|
|
|
+bfs::path VCMIDirsOSX::binaryPath() const { return "."; }
|
|
|
+
|
|
|
+std::string libraryName(const std::string& basename) { return "lib" + basename + ".dylib"; }
|
|
|
+#elif defined(VCMI_LINUX)
|
|
|
+class VCMIDirsLinux : public IVCMIDirsUNIX
|
|
|
{
|
|
|
-#ifdef __ANDROID__
|
|
|
- // on Android HOME will be set to something like /sdcard/data/Android/is.xyz.vcmi/files/
|
|
|
- return std::string(getenv("HOME"));
|
|
|
-#else
|
|
|
- if (getenv("XDG_DATA_HOME") != nullptr )
|
|
|
- return std::string(getenv("XDG_DATA_HOME")) + "/vcmi";
|
|
|
- if (getenv("HOME") != nullptr )
|
|
|
- return std::string(getenv("HOME")) + "/.local/share" + "/vcmi";
|
|
|
- return ".";
|
|
|
-#endif
|
|
|
-}
|
|
|
+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;
|
|
|
|
|
|
-std::string VCMIDirs::userSavePath() const
|
|
|
+ boost::filesystem::path libraryPath() const override;
|
|
|
+ boost::filesystem::path binaryPath() const override;
|
|
|
+
|
|
|
+ std::string libraryName(const std::string& basename) const override;
|
|
|
+};
|
|
|
+
|
|
|
+bfs::path VCMIDirsLinux::userDataPath() const
|
|
|
{
|
|
|
- return userDataPath() + "/Saves";
|
|
|
+ // $XDG_DATA_HOME, default: $HOME/.local/share
|
|
|
+ const char* homeDir;
|
|
|
+ if ((homeDir = getenv("XDG_DATA_HOME")))
|
|
|
+ return homeDir;
|
|
|
+ else if ((homeDir = getenv("HOME")))
|
|
|
+ return bfs::path(homeDir) / ".local" / "share" / "vcmi";
|
|
|
+ else
|
|
|
+ return ".";
|
|
|
}
|
|
|
-
|
|
|
-// $XDG_CACHE_HOME, default: $HOME/.cache
|
|
|
-std::string VCMIDirs::userCachePath() const
|
|
|
+bfs::path VCMIDirsLinux::userCachePath() const
|
|
|
{
|
|
|
-#ifdef __ANDROID__
|
|
|
- return userDataPath() + "/cache";
|
|
|
-#else
|
|
|
- if (getenv("XDG_CACHE_HOME") != nullptr )
|
|
|
- return std::string(getenv("XDG_CACHE_HOME")) + "/vcmi";
|
|
|
- if (getenv("HOME") != nullptr )
|
|
|
- return std::string(getenv("HOME")) + "/.cache" + "/vcmi";
|
|
|
- return ".";
|
|
|
-#endif
|
|
|
+ // $XDG_CACHE_HOME, default: $HOME/.cache
|
|
|
+ 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";
|
|
|
+ else
|
|
|
+ return ".";
|
|
|
}
|
|
|
-
|
|
|
-// $XDG_CONFIG_HOME, default: $HOME/.config
|
|
|
-std::string VCMIDirs::userConfigPath() const
|
|
|
+bfs::path VCMIDirsLinux::userConfigPath() const
|
|
|
{
|
|
|
-#ifdef __ANDROID__
|
|
|
- return userDataPath() + "/config";
|
|
|
-#else
|
|
|
- if (getenv("XDG_CONFIG_HOME") != nullptr )
|
|
|
- return std::string(getenv("XDG_CONFIG_HOME")) + "/vcmi";
|
|
|
- if (getenv("HOME") != nullptr )
|
|
|
- return std::string(getenv("HOME")) + "/.config" + "/vcmi";
|
|
|
- return ".";
|
|
|
-#endif
|
|
|
+ // $XDG_CONFIG_HOME, default: $HOME/.config
|
|
|
+ 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";
|
|
|
+ else
|
|
|
+ return ".";
|
|
|
}
|
|
|
|
|
|
-// $XDG_DATA_DIRS, default: /usr/local/share/:/usr/share/
|
|
|
-std::vector<std::string> VCMIDirs::dataPaths() const
|
|
|
+std::vector<bfs::path> VCMIDirsLinux::dataPaths() const
|
|
|
{
|
|
|
+ // $XDG_DATA_DIRS, default: /usr/local/share/:/usr/share/
|
|
|
+
|
|
|
// construct list in reverse.
|
|
|
// in specification first directory has highest priority
|
|
|
// in vcmi fs last directory has highest priority
|
|
|
+ std::vector<bfs::path> ret;
|
|
|
|
|
|
- std::vector<std::string> ret;
|
|
|
-#ifdef __ANDROID__
|
|
|
- ret.push_back(userDataPath());
|
|
|
-#else
|
|
|
- if (getenv("HOME") != nullptr ) // compatibility, should be removed after 0.96
|
|
|
- ret.push_back(std::string(getenv("HOME")) + "/.vcmi");
|
|
|
+ const char* tempResult;
|
|
|
ret.push_back(M_DATA_DIR);
|
|
|
|
|
|
- if (getenv("XDG_DATA_DIRS") != nullptr)
|
|
|
+ if ((tempResult = getenv("XDG_DATA_DIRS")) != nullptr)
|
|
|
{
|
|
|
- std::string dataDirsEnv = getenv("XDG_DATA_DIRS");
|
|
|
+ std::string dataDirsEnv = tempResult;
|
|
|
std::vector<std::string> dataDirs;
|
|
|
boost::split(dataDirs, dataDirsEnv, boost::is_any_of(":"));
|
|
|
for (auto & entry : boost::adaptors::reverse(dataDirs))
|
|
@@ -231,21 +516,61 @@ std::vector<std::string> VCMIDirs::dataPaths() const
|
|
|
ret.push_back("/usr/share/");
|
|
|
ret.push_back("/usr/local/share/");
|
|
|
}
|
|
|
-#endif
|
|
|
+
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
-#endif
|
|
|
+bfs::path VCMIDirsLinux::libraryPath() const { return M_LIB_DIR; }
|
|
|
+bfs::path VCMIDirsLinux::binaryPath() const { return M_BIN_DIR; }
|
|
|
|
|
|
-std::string VCMIDirs::genHelpString() const
|
|
|
+std::string VCMIDirsLinux::libraryName(const std::string& basename) const { return "lib" + basename + ".so"; }
|
|
|
+#ifdef VCMI_ANDROID
|
|
|
+class VCMIDirsAndroid : public VCMIDirsLinux
|
|
|
{
|
|
|
- return
|
|
|
- " game data: " + boost::algorithm::join(dataPaths(), ":") + "\n" +
|
|
|
- " libraries: " + libraryPath() + "\n" +
|
|
|
- " server: " + serverPath() + "\n" +
|
|
|
- "\n" +
|
|
|
- " user data: " + userDataPath() + "\n" +
|
|
|
- " user cache: " + userCachePath() + "\n" +
|
|
|
- " user config: " + userConfigPath() + "\n" +
|
|
|
- " user saves: " + userSavePath() + "\n";
|
|
|
+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/
|
|
|
+bfs::path VCMIDirsAndroid::userDataPath() const { return getenv("HOME"); }
|
|
|
+bfs::path VCMIDirsAndroid::userCachePath() const { return userDataPath() / "cache"; }
|
|
|
+bfs::path VCMIDirsAndroid::userConfigPath() const { return userDataPath() / "config"; }
|
|
|
+
|
|
|
+std::vector<bfs::path> VCMIDirsAndroid::dataPaths() const
|
|
|
+{
|
|
|
+ return std::vector<bfs::path>(1, userDataPath());
|
|
|
}
|
|
|
+#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
|
|
|
+ static VCMIDirsWIN32 singleton;
|
|
|
+ #elif defined(VCMI_ANDROID)
|
|
|
+ static VCMIDirsAndroid singleton;
|
|
|
+ #elif defined(VCMI_LINUX)
|
|
|
+ static VCMIDirsLinux singleton;
|
|
|
+ #elif defined(VCMI_APPLE)
|
|
|
+ static VCMIDirsOSX singleton;
|
|
|
+ #endif
|
|
|
+ static bool initialized = false;
|
|
|
+ if (!initialized)
|
|
|
+ {
|
|
|
+ std::locale::global(boost::locale::generator().generate("en_US.UTF-8"));
|
|
|
+ boost::filesystem::path::imbue(std::locale());
|
|
|
+
|
|
|
+ singleton.init();
|
|
|
+ initialized = true;
|
|
|
+ }
|
|
|
+ return singleton;
|
|
|
+ }
|
|
|
+}
|