#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2013 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License // If the library is compiled as a DLL, this ensures symbols are exported #define NUCLEX_STORAGE_SOURCE 1 #include "Nuclex/Storage/FileSystem/WindowsFileManager.h" #if defined(NUCLEX_STORAGE_WIN32) #include "Nuclex/Storage/FileSystem/ContainerFileCodecChain.h" #include "Generic/GenericWindowsContainer.h" #include "Windows/WindowsPathHelper.h" #include "Windows/WindowsFileApi.h" #include namespace Nuclex { namespace Storage { namespace FileSystem { // ------------------------------------------------------------------------------------------- // WindowsFileManager::WindowsFileManager(const std::string &applicationName) : staticDataContainer(nullptr), personalContainer(nullptr), saveGameContainer(nullptr), localContainer(nullptr), temporaryContainer(nullptr) { typedef GenericWindowsContainer TContainer; this->staticDataContainer = new TContainer(this->Codecs, getInstallDirectory()); this->personalContainer = new TContainer( this->Codecs, getPersonalDirectory(applicationName) ); this->saveGameContainer = new TContainer( this->Codecs, getSaveGameDirectory(applicationName) ); this->localContainer = new TContainer( this->Codecs, getLocalDirectory(applicationName) ); this->temporaryContainer = new TContainer( this->Codecs, getTemporaryDirectory(applicationName) ); } // ------------------------------------------------------------------------------------------- // WindowsFileManager::~WindowsFileManager() { delete this->temporaryContainer; delete this->localContainer; delete this->saveGameContainer; delete this->personalContainer; delete this->staticDataContainer; } // ------------------------------------------------------------------------------------------- // const Container &WindowsFileManager::GetInstallContainer() const { return *this->staticDataContainer; } // ------------------------------------------------------------------------------------------- // Container &WindowsFileManager::GetPersonalContainer() const { WindowsFileApi::CreateDirectoryRecursively(this->personalContainer->GetNativePath()); return *this->personalContainer; } // ------------------------------------------------------------------------------------------- // Container &WindowsFileManager::GetSaveGameContainer() const { WindowsFileApi::CreateDirectoryRecursively(this->saveGameContainer->GetNativePath()); return *this->saveGameContainer; } // ------------------------------------------------------------------------------------------- // Container &WindowsFileManager::GetLocalContainer() const { WindowsFileApi::CreateDirectoryRecursively(this->localContainer->GetNativePath()); return *this->localContainer; } // ------------------------------------------------------------------------------------------- // Container &WindowsFileManager::GetTemporaryContainer() const { WindowsFileApi::CreateDirectoryRecursively(this->temporaryContainer->GetNativePath()); return *this->temporaryContainer; } // ------------------------------------------------------------------------------------------- // std::string WindowsFileManager::getInstallDirectory() { std::string installDirectory = WindowsPathHelper::RemoveLastElement( WindowsFileApi::GetModuleFileName() ); WindowsPathHelper::RemoveTrailingPathSeparator(installDirectory); return installDirectory; } // ------------------------------------------------------------------------------------------- // std::shared_ptr WindowsFileManager::GetFromNativePath( const std::string &path ) const { typedef GenericWindowsContainer TContainer; return std::shared_ptr(new TContainer(this->Codecs, path)); } // ------------------------------------------------------------------------------------------- // std::string WindowsFileManager::getPersonalDirectory(const std::string &applicationName) { std::string dataDirectory = WindowsFileApi::GetSpecialFolderPath(CSIDL_APPDATA); std::string applicationDirectory = WindowsPathHelper::CombinePaths( dataDirectory, applicationName ); WindowsPathHelper::RemoveTrailingPathSeparator(applicationDirectory); return applicationDirectory; } // ------------------------------------------------------------------------------------------- // std::string WindowsFileManager::getSaveGameDirectory(const std::string &applicationName) { bool isVistaOrLater = isAtLeastWindowsVersion(6, 0); std::string applicationDirectory; if(isVistaOrLater) { // Windows Vista added the saved games directory std::string savedGameDirectory = WindowsFileApi::GetKnownFolder(FOLDERID_SavedGames); applicationDirectory = WindowsPathHelper::CombinePaths( savedGameDirectory, applicationName ); } else { // Windows XP requires us to use a different directory std::string personalDirectory = WindowsFileApi::GetSpecialFolderPath(CSIDL_PERSONAL); std::string myGamesDirectory = WindowsPathHelper::CombinePaths( personalDirectory, "My Games" ); applicationDirectory = WindowsPathHelper::CombinePaths(myGamesDirectory, applicationName); } WindowsPathHelper::RemoveTrailingPathSeparator(applicationDirectory); return applicationDirectory; } // ------------------------------------------------------------------------------------------- // std::string WindowsFileManager::getLocalDirectory(const std::string &applicationName) { std::string dataDirectory = WindowsFileApi::GetSpecialFolderPath(CSIDL_LOCAL_APPDATA); std::string applicationDirectory = WindowsPathHelper::CombinePaths( dataDirectory, applicationName ); WindowsPathHelper::RemoveTrailingPathSeparator(applicationDirectory); return applicationDirectory; } // ------------------------------------------------------------------------------------------- // std::string WindowsFileManager::getTemporaryDirectory(const std::string &applicationName) { std::string temporaryDirectory = WindowsFileApi::GetTempPath(); std::string applicationDirectory = WindowsPathHelper::CombinePaths( temporaryDirectory, applicationName ); WindowsPathHelper::RemoveTrailingPathSeparator(applicationDirectory); return applicationDirectory; } // ------------------------------------------------------------------------------------------- // bool WindowsFileManager::isAtLeastWindowsVersion(int major, int minor) { OSVERSIONINFOW osVersionInfo = {0}; osVersionInfo.dwOSVersionInfoSize = sizeof(osVersionInfo); BOOL result = ::GetVersionExW(&osVersionInfo); if(result == FALSE) { throw std::runtime_error("Could not determine operating system version"); } if(osVersionInfo.dwMajorVersion == static_cast(major)) { return (osVersionInfo.dwMinorVersion >= static_cast(minor)); } else { return (osVersionInfo.dwMajorVersion > static_cast(major)); } } // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Storage::FileSystem #endif // defined(NUCLEX_STORAGE_WIN32)