Magic_Game/core/Tool/Path.cpp

93 lines
2.1 KiB
C++
Raw Normal View History

2018-04-21 21:24:46 +08:00
#include "..\e2dtool.h"
2018-01-30 16:45:38 +08:00
2018-07-04 12:49:05 +08:00
extern "C" const GUID DECLSPEC_SELECTANY FOLDERID_LocalAppData = {
0xF1B32785, 0x6FBA, 0x4FCF, { 0x9D, 0x55, 0x7B, 0x8E, 0x7F, 0x15, 0x70, 0x91 }
};
2018-01-30 16:45:38 +08:00
const e2d::String& e2d::Path::dataPath()
2018-07-04 17:00:21 +08:00
{
static String dataPath;
if (dataPath.empty())
2018-07-04 17:00:21 +08:00
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵı<DDB5><C4B1><EFBFBD>·<EFBFBD><C2B7>
String localAppDataPath = Path::localAppDataPath();
String title = Game::instance()->window()->title();
2018-08-19 15:11:20 +08:00
String folderName = String::parse(title.hash());
if (!localAppDataPath.empty())
2018-07-04 17:00:21 +08:00
{
2018-08-19 15:11:20 +08:00
dataPath = localAppDataPath + L"\\Easy2DGameData\\" << folderName << L"\\";
2018-07-04 17:00:21 +08:00
File file(dataPath);
if (!file.exists() && !File::createFolder(dataPath))
2018-07-04 17:00:21 +08:00
{
dataPath = L"";
2018-07-04 17:00:21 +08:00
}
}
dataPath << L"Data.ini";
2018-07-04 17:00:21 +08:00
}
return dataPath;
2018-07-04 17:00:21 +08:00
}
const e2d::String& e2d::Path::tempPath()
2018-04-26 21:47:56 +08:00
{
static String tempPath;
if (tempPath.empty())
2018-07-04 17:00:21 +08:00
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
wchar_t path[_MAX_PATH];
String title = Game::instance()->window()->title();
2018-08-19 15:11:20 +08:00
String folderName = String::parse(title.hash());
2018-07-04 17:00:21 +08:00
2018-08-19 15:11:20 +08:00
if (0 != ::GetTempPath(_MAX_PATH, path))
2018-07-04 17:00:21 +08:00
{
2018-08-19 15:11:20 +08:00
tempPath << path << L"\\Easy2DGameTemp\\" << folderName << L"\\";
2018-07-04 17:00:21 +08:00
File file(tempPath);
if (!file.exists() && !File::createFolder(tempPath))
2018-07-04 17:00:21 +08:00
{
tempPath = L"";
2018-07-04 17:00:21 +08:00
}
}
}
return tempPath;
2018-07-04 12:49:05 +08:00
}
const e2d::String& e2d::Path::localAppDataPath()
2018-07-04 12:49:05 +08:00
{
static String localAppDataPath;
if (localAppDataPath.empty())
2018-07-04 12:49:05 +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 = nullptr;
HMODULE hModule = LoadLibrary(L"shell32.dll");
pFunSHGetKnownFolderPath SHGetKnownFolderPath = (pFunSHGetKnownFolderPath)GetProcAddress(hModule, "SHGetKnownFolderPath");
HRESULT hr = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &pszPath);
if (SUCCEEDED(hr))
{
localAppDataPath = pszPath;
CoTaskMemFree(pszPath);
}
}
return localAppDataPath;
2018-04-26 21:47:56 +08:00
}
const e2d::String& e2d::Path::currentFilePath()
2018-05-24 15:47:38 +08:00
{
static String currFilePath;
if (currFilePath.empty())
2018-05-24 15:47:38 +08:00
{
TCHAR szPath[_MAX_PATH] = { 0 };
if (::GetModuleFileName(nullptr, szPath, _MAX_PATH) != 0)
2018-05-24 15:47:38 +08:00
{
currFilePath = szPath;
2018-05-24 15:47:38 +08:00
}
}
return currFilePath;
}