Magic_Game/core/Tool/Path.cpp

189 lines
3.9 KiB
C++
Raw Normal View History

2018-04-21 21:24:46 +08:00
#include "..\e2dtool.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
2018-04-26 21:47:56 +08:00
static e2d::String s_sLocalAppDataPath;
static e2d::String s_sTempPath;
static e2d::String s_sDefaultSavePath;
bool e2d::Path::__init()
{
2018-04-26 21:47:56 +08:00
// <20><>ȡ AppData\Local <20>ļ<EFBFBD><C4BC>е<EFBFBD>·<EFBFBD><C2B7>
typedef HRESULT(WINAPI* pFunSHGetKnownFolderPath)(const GUID& rfid, DWORD dwFlags, HANDLE hToken, PWSTR *ppszPath);
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))
{
2018-04-26 21:47:56 +08:00
s_sLocalAppDataPath = pszPath;
CoTaskMemFree(pszPath);
}
2018-04-26 21:47:56 +08:00
else
{
2018-04-26 21:47:56 +08:00
WARN_IF(true, "Cannot get local AppData path!");
}
// <20><>ȡ<EFBFBD><C8A1>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD>
String sGameName = Game::getName();
2018-04-26 21:47:56 +08:00
// <20><>ȡĬ<C8A1>ϱ<EFBFBD><CFB1><EFBFBD>·<EFBFBD><C2B7>
bool bInitSavePath = false;
do
{
2018-04-26 21:47:56 +08:00
String localAppPath = s_sLocalAppDataPath;
if (localAppPath.isEmpty())
{
break;
}
else
{
2018-04-26 21:47:56 +08:00
s_sDefaultSavePath = localAppPath;
}
2018-01-30 16:45:38 +08:00
2018-04-26 21:47:56 +08:00
localAppPath << L"\\Easy2DGameData";
if (Path::createFolder(localAppPath))
{
s_sDefaultSavePath = localAppPath;
}
else
{
break;
}
2018-04-26 21:47:56 +08:00
if (!sGameName.isEmpty())
{
localAppPath << L"\\" << sGameName;
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
if (Path::createFolder(localAppPath))
{
s_sDefaultSavePath = localAppPath;
}
}
2018-04-26 21:47:56 +08:00
s_sDefaultSavePath << L"\\";
bInitSavePath = true;
} while (0);
// <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD>ļ<EFBFBD>Ŀ¼
bool bInitTempPath = false;
do
{
2018-04-26 21:47:56 +08:00
wchar_t path[_MAX_PATH];
if (0 == ::GetTempPath(_MAX_PATH, path))
{
break;
}
else
{
s_sTempPath = path;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ļ<EFBFBD>Ŀ¼
String tempPath;
tempPath << s_sTempPath << L"\\Easy2DGameTemp";
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
2018-04-26 21:47:56 +08:00
if (Path::createFolder(tempPath))
{
2018-04-26 21:47:56 +08:00
s_sTempPath = path;
}
2018-04-26 21:47:56 +08:00
else
{
break;
}
if (!sGameName.isEmpty())
{
tempPath << L"\\" << sGameName;
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
if (Path::createFolder(tempPath))
{
s_sTempPath = tempPath;
}
}
s_sTempPath << L"\\";
bInitTempPath = true;
} while (0);
return SUCCEEDED(hr) && bInitSavePath && bInitTempPath;
}
e2d::String e2d::Path::getTempPath()
{
return s_sTempPath;
}
e2d::String e2d::Path::getDefaultSavePath()
{
return s_sDefaultSavePath;
}
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>
2018-05-01 17:19:55 +08:00
int pos = filePath.getWString().find_last_of(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;
}