Magic_Game/core/Tool/Path.cpp

154 lines
3.5 KiB
C++
Raw Normal View History

#include "..\etool.h"
#include <algorithm>
#include <commdlg.h>
2018-01-30 16:45:38 +08:00
#define DEFINE_KNOWN_FOLDER(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
EXTERN_C const GUID DECLSPEC_SELECTANY name \
= { l, w1, w2,{ b1, b2, b3, b4, b5, b6, b7, b8 } }
2018-01-30 16:45:38 +08:00
DEFINE_KNOWN_FOLDER(FOLDERID_LocalAppData, 0xF1B32785, 0x6FBA, 0x4FCF, 0x9D, 0x55, 0x7B, 0x8E, 0x7F, 0x15, 0x70, 0x91);
2018-01-30 16:45:38 +08:00
e2d::String e2d::Path::getLocalAppDataPath()
{
typedef HRESULT(WINAPI* pFunSHGetKnownFolderPath)(const GUID& rfid, DWORD dwFlags, HANDLE hToken, PWSTR *ppszPath);
// <20><>ȡ AppData\Local <20>ļ<EFBFBD><C4BC>е<EFBFBD>·<EFBFBD><C2B7>
PWSTR pszPath = NULL;
HMODULE hModule = LoadLibrary(L"shell32.dll");
pFunSHGetKnownFolderPath SHGetKnownFolderPath = (pFunSHGetKnownFolderPath)GetProcAddress(hModule, "SHGetKnownFolderPath");
HRESULT hr = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &pszPath);
2018-01-30 16:45:38 +08:00
if (SUCCEEDED(hr))
{
String path = pszPath;
CoTaskMemFree(pszPath);
return path;
}
return L"";
2018-01-30 16:45:38 +08:00
}
e2d::String e2d::Path::getTempPath()
2018-01-30 16:45:38 +08:00
{
// <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD>ļ<EFBFBD>Ŀ¼
wchar_t path[_MAX_PATH];
if (0 == ::GetTempPath(_MAX_PATH, path))
{
return L"";
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ļ<EFBFBD>Ŀ¼
e2d::String tempFilePath;
tempFilePath << path << L"Easy2DGameTemp\\";
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
if (!Path::createFolder(tempFilePath))
{
return path;
}
// <20><>ȡ<EFBFBD><C8A1>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD>
String sGameName = Game::getName();
if (!sGameName.isEmpty())
{
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
if (!Path::createFolder(tempFilePath + sGameName + L"\\"))
{
return std::move(tempFilePath);
}
tempFilePath << sGameName << L"\\";
}
return std::move(tempFilePath);
2018-01-30 16:45:38 +08:00
}
e2d::String e2d::Path::getDefaultSavePath()
{
// <20><>ȡ AppData ·<><C2B7>
String path = Path::getLocalAppDataPath();
if (path.isEmpty())
{
WARN_IF(true, "Cannot get local AppData path!");
return std::move(path);
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
if (!Path::createFolder(path + L"\\Easy2DGameData"))
{
return std::move(path);
}
path << L"\\Easy2DGameData";
// <20><>ȡ<EFBFBD><C8A1>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD>
String sGameName = Game::getName();
if (!sGameName.isEmpty())
{
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
if (!Path::createFolder(path + L"\\" + sGameName))
{
return std::move(path);
}
path << L"\\" << sGameName;
}
path << L"\\";
return std::move(path);
}
e2d::String e2d::Path::getFileExtension(String filePath)
2018-01-30 16:45:38 +08:00
{
String fileExtension;
// <20>ҵ<EFBFBD><D2B5>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> '.' <20><>λ<EFBFBD><CEBB>
int pos = filePath.findLastOf(L'.');
// <20>ж<EFBFBD> pos <20>Ƿ<EFBFBD><C7B7>Ǹ<EFBFBD><C7B8><EFBFBD>Чλ<D0A7><CEBB>
if (pos != -1)
{
// <20><>ȡ<EFBFBD><C8A1>չ<EFBFBD><D5B9>
fileExtension = filePath.subtract(pos);
// ת<><D7AA>ΪСд<D0A1><D0B4>ĸ
fileExtension = fileExtension.toLower();
}
return fileExtension;
2018-01-30 16:45:38 +08:00
}
e2d::String e2d::Path::getSaveFilePath(const String title, const String defExt)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ի<EFBFBD><D4BB><EFBFBD>
OPENFILENAME ofn = { 0 };
wchar_t strFilename[MAX_PATH] = { 0 }; // <20><><EFBFBD>ڽ<EFBFBD><DABD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
ofn.lStructSize = sizeof(OPENFILENAME); // <20><EFBFBD><E1B9B9><EFBFBD><EFBFBD>С
ofn.hwndOwner = Window::getHWnd(); // <20><><EFBFBD>ھ<EFBFBD><DABE><EFBFBD>
ofn.lpstrFilter = L"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>\0*.*\0\0"; // <20><><EFBFBD>ù<EFBFBD><C3B9><EFBFBD>
ofn.nFilterIndex = 1; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ofn.lpstrFile = strFilename; // <20><><EFBFBD>շ<EFBFBD><D5B7>ص<EFBFBD><D8B5>ļ<EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
ofn.nMaxFile = sizeof(strFilename); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ofn.lpstrInitialDir = NULL; // <20><>ʼĿ¼ΪĬ<CEAA><C4AC>
ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
ofn.lpstrTitle = title; // <20><><EFBFBD><EFBFBD>
ofn.lpstrDefExt = defExt; // Ĭ<><C4AC>׷<EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD>չ<EFBFBD><D5B9>
if (GetSaveFileName(&ofn))
{
return strFilename;
}
return L"";
}
bool e2d::Path::createFolder(String strDirPath)
{
if (strDirPath.isEmpty())
{
WARN_IF(true, "Path::createFolder Failed: Invalid directory path!");
return false;
}
if (-1 == ::_waccess(strDirPath, 0))
{
if (0 != ::_wmkdir(strDirPath))
{
return false;
}
}
return true;
}