2014-07-02 20:28:29 -04:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include <commdlg.h>
|
|
|
|
#include <shlobj.h>
|
|
|
|
#include "FolderUtilities.h"
|
|
|
|
|
2015-07-05 19:21:49 -04:00
|
|
|
wstring FolderUtilities::_homeFolder = L"";
|
2015-07-05 22:23:44 -04:00
|
|
|
vector<wstring> FolderUtilities::_gameFolders = vector<wstring>();
|
2014-07-02 20:28:29 -04:00
|
|
|
|
2015-07-05 19:21:49 -04:00
|
|
|
void FolderUtilities::SetHomeFolder(wstring homeFolder)
|
|
|
|
{
|
|
|
|
_homeFolder = homeFolder;
|
|
|
|
CreateDirectory(homeFolder.c_str(), nullptr);
|
|
|
|
}
|
2014-07-02 20:28:29 -04:00
|
|
|
|
2015-07-05 19:21:49 -04:00
|
|
|
wstring FolderUtilities::GetHomeFolder()
|
|
|
|
{
|
|
|
|
if(_homeFolder.size() == 0) {
|
|
|
|
throw std::exception("Home folder not specified");
|
|
|
|
}
|
|
|
|
return _homeFolder;
|
2014-07-02 20:28:29 -04:00
|
|
|
}
|
|
|
|
|
2015-07-05 22:23:44 -04:00
|
|
|
void FolderUtilities::AddKnowGameFolder(wstring gameFolder)
|
|
|
|
{
|
|
|
|
bool alreadyExists = false;
|
|
|
|
for(wstring folder : _gameFolders) {
|
|
|
|
if(folder.compare(gameFolder) == 0) {
|
|
|
|
alreadyExists = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!alreadyExists) {
|
|
|
|
_gameFolders.push_back(gameFolder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<wstring> FolderUtilities::GetKnowGameFolders()
|
|
|
|
{
|
|
|
|
return _gameFolders;
|
|
|
|
}
|
|
|
|
|
2014-07-02 20:28:29 -04:00
|
|
|
wstring FolderUtilities::GetSaveFolder()
|
|
|
|
{
|
2015-07-05 19:21:49 -04:00
|
|
|
wstring folder = CombinePath(GetHomeFolder(), L"Saves\\");
|
2014-07-02 20:28:29 -04:00
|
|
|
CreateDirectory(folder.c_str(), nullptr);
|
|
|
|
return folder;
|
|
|
|
}
|
|
|
|
|
|
|
|
wstring FolderUtilities::GetSaveStateFolder()
|
|
|
|
{
|
2015-07-05 19:21:49 -04:00
|
|
|
wstring folder = CombinePath(GetHomeFolder(), L"SaveStates\\");
|
2014-07-02 20:28:29 -04:00
|
|
|
CreateDirectory(folder.c_str(), nullptr);
|
|
|
|
return folder;
|
|
|
|
}
|
|
|
|
|
|
|
|
wstring FolderUtilities::GetMovieFolder()
|
|
|
|
{
|
2015-07-05 19:21:49 -04:00
|
|
|
wstring folder = CombinePath(GetHomeFolder(), + L"Movies\\");
|
2014-07-02 20:28:29 -04:00
|
|
|
CreateDirectory(folder.c_str(), nullptr);
|
|
|
|
return folder;
|
|
|
|
}
|
|
|
|
|
2014-07-10 21:17:37 -04:00
|
|
|
wstring FolderUtilities::GetScreenshotFolder()
|
|
|
|
{
|
2015-07-05 19:21:49 -04:00
|
|
|
wstring folder = CombinePath(GetHomeFolder(), L"Screenshots\\");
|
2014-07-10 21:17:37 -04:00
|
|
|
CreateDirectory(folder.c_str(), nullptr);
|
|
|
|
return folder;
|
|
|
|
}
|
|
|
|
|
2014-07-02 20:28:29 -04:00
|
|
|
vector<wstring> FolderUtilities::GetFolders(wstring rootFolder)
|
|
|
|
{
|
|
|
|
HANDLE hFind;
|
|
|
|
WIN32_FIND_DATA data;
|
|
|
|
|
|
|
|
vector<wstring> folders;
|
|
|
|
|
|
|
|
hFind = FindFirstFile((rootFolder + L"*").c_str(), &data);
|
|
|
|
if(hFind != INVALID_HANDLE_VALUE) {
|
|
|
|
do {
|
|
|
|
if(data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && wcscmp(data.cFileName, L".") != 0 && wcscmp(data.cFileName, L"..") != 0) {
|
|
|
|
wstring subfolder = rootFolder + data.cFileName + L"\\";
|
|
|
|
folders.push_back(subfolder);
|
|
|
|
for(wstring folderName : GetFolders(subfolder.c_str())) {
|
|
|
|
folders.push_back(folderName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while(FindNextFile(hFind, &data));
|
|
|
|
FindClose(hFind);
|
|
|
|
}
|
|
|
|
|
|
|
|
return folders;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<wstring> FolderUtilities::GetFilesInFolder(wstring rootFolder, wstring mask, bool recursive)
|
|
|
|
{
|
|
|
|
HANDLE hFind;
|
|
|
|
WIN32_FIND_DATA data;
|
|
|
|
|
|
|
|
vector<wstring> folders;
|
|
|
|
vector<wstring> files;
|
2014-07-09 18:29:07 -04:00
|
|
|
if(rootFolder[rootFolder.size() - 1] != '/' && rootFolder[rootFolder.size() - 1] != '\\') {
|
|
|
|
rootFolder += L"/";
|
|
|
|
}
|
|
|
|
|
2014-07-02 20:28:29 -04:00
|
|
|
folders.push_back(rootFolder);
|
|
|
|
|
|
|
|
if(recursive) {
|
|
|
|
for(wstring subFolder : GetFolders(rootFolder)) {
|
|
|
|
folders.push_back(subFolder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(wstring folder : folders) {
|
|
|
|
hFind = FindFirstFile((folder + mask).c_str(), &data);
|
|
|
|
if(hFind != INVALID_HANDLE_VALUE) {
|
|
|
|
do {
|
|
|
|
files.push_back(folder + data.cFileName);
|
|
|
|
} while(FindNextFile(hFind, &data));
|
|
|
|
FindClose(hFind);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
|
|
|
wstring FolderUtilities::GetFilename(wstring filepath, bool includeExtension)
|
|
|
|
{
|
|
|
|
int index = filepath.find_last_of(L"/\\");
|
|
|
|
wstring filename = (index == std::string::basic_string::npos) ? filepath : filepath.substr(index + 1);
|
|
|
|
if(!includeExtension) {
|
|
|
|
filename = filename.substr(0, filename.find_last_of(L"."));
|
|
|
|
}
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
wstring FolderUtilities::GetFolderName(wstring filepath)
|
|
|
|
{
|
|
|
|
int index = filepath.find_last_of(L"/\\");
|
|
|
|
return filepath.substr(0, index);
|
|
|
|
}
|
|
|
|
|
2015-07-01 23:17:14 -04:00
|
|
|
wstring FolderUtilities::CombinePath(wstring folder, wstring filename)
|
2014-07-02 20:28:29 -04:00
|
|
|
{
|
2015-07-01 23:17:14 -04:00
|
|
|
#ifdef WIN32
|
|
|
|
wstring separator = L"\\";
|
|
|
|
#else
|
|
|
|
wstring separator = L"/";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(folder.find_last_of(separator) != folder.length() - 1) {
|
|
|
|
folder += separator;
|
2014-07-02 20:28:29 -04:00
|
|
|
}
|
|
|
|
|
2015-07-01 23:17:14 -04:00
|
|
|
return folder + filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t FolderUtilities::GetFileModificationTime(wstring filepath)
|
|
|
|
{
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fileAttrData = {0};
|
|
|
|
GetFileAttributesEx(filepath.c_str(), GetFileExInfoStandard, &fileAttrData);
|
|
|
|
return ((int64_t)fileAttrData.ftLastWriteTime.dwHighDateTime << 32) | (int64_t)fileAttrData.ftLastWriteTime.dwLowDateTime;
|
2014-07-02 20:28:29 -04:00
|
|
|
}
|