Magic_Game/core/Tool/Path.cpp

228 lines
5.4 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
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
2018-07-04 12:49:05 +08:00
e2d::String e2d::Path::_tempPath;
e2d::String e2d::Path::_dataPath;
std::list<e2d::String> e2d::Path::_paths;
2018-04-26 21:47:56 +08:00
2018-07-03 01:49:20 +08:00
2018-07-03 01:52:11 +08:00
void e2d::Path::addSearchPath(String path)
2018-05-24 15:47:38 +08:00
{
path.replace(L"/", L"\\");
if (path[path.getLength() - 1] != L'\\')
{
path << L"\\";
}
2018-07-04 12:49:05 +08:00
auto iter = std::find(_paths.cbegin(), _paths.cend(), path);
if (iter == _paths.cend())
2018-05-24 15:47:38 +08:00
{
2018-07-04 12:49:05 +08:00
_paths.push_front(path);
2018-05-24 15:47:38 +08:00
}
}
2018-07-04 17:00:21 +08:00
e2d::String e2d::Path::getDataPath()
{
if (_dataPath.isEmpty())
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵı<DDB5><C4B1><EFBFBD>·<EFBFBD><C2B7>
String localAppDataPath = Path::getLocalAppDataPath();
String gameName = Game::getInstance()->getConfig()->getGameName();
2018-07-04 17:00:21 +08:00
if (!localAppDataPath.isEmpty() && !gameName.isEmpty())
{
_dataPath = localAppDataPath + L"\\Easy2DGameData\\" << gameName << L"\\";
if (!Path::exists(_dataPath) && !Path::createFolder(_dataPath))
{
_dataPath = L"";
}
}
_dataPath << L"Data.ini";
}
return _dataPath;
}
2018-04-26 21:47:56 +08:00
e2d::String e2d::Path::getTempPath()
{
2018-07-04 17:00:21 +08:00
if (_tempPath.isEmpty())
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
wchar_t path[_MAX_PATH];
String gameName = Game::getInstance()->getConfig()->getGameName();
2018-07-04 17:00:21 +08:00
if (0 != ::GetTempPath(_MAX_PATH, path) && !gameName.isEmpty())
{
_tempPath << path << L"\\Easy2DGameTemp\\" << gameName << L"\\";
if (!Path::exists(_tempPath) && !Path::createFolder(_tempPath))
{
_tempPath = L"";
}
}
}
2018-07-04 12:49:05 +08:00
return _tempPath;
}
e2d::String e2d::Path::getLocalAppDataPath()
{
static String localAppDataPath;
if (localAppDataPath.isEmpty())
{
// <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
}
2018-07-04 12:49:05 +08:00
e2d::String e2d::Path::getCurrentFilePath()
2018-05-24 15:47:38 +08:00
{
TCHAR szPath[_MAX_PATH] = { 0 };
if (::GetModuleFileName(nullptr, szPath, _MAX_PATH) != 0)
{
2018-07-03 01:49:20 +08:00
return std::move(String(szPath));
2018-05-24 15:47:38 +08:00
}
2018-07-03 01:49:20 +08:00
return std::move(String());
2018-05-24 15:47:38 +08:00
}
2018-07-03 01:52:11 +08:00
e2d::String e2d::Path::findFile(const String& path)
2018-05-24 15:47:38 +08:00
{
if (Path::exists(path))
{
return path;
}
else
{
2018-07-04 12:49:05 +08:00
for (auto& resPath : _paths)
2018-05-24 15:47:38 +08:00
{
if (Path::exists(resPath + path))
{
return resPath + path;
}
}
}
2018-07-03 01:49:20 +08:00
return std::move(String());
2018-05-24 15:47:38 +08:00
}
e2d::String e2d::Path::extractResource(int resNameId, const String & resType, const String & destFileName)
{
2018-07-04 12:49:05 +08:00
String destFilePath = _tempPath + destFileName;
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
HANDLE hFile = ::CreateFile((LPCWSTR)destFilePath, GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, NULL);
if (hFile == INVALID_HANDLE_VALUE)
2018-07-03 01:49:20 +08:00
return std::move(String());
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD>ļ<EFBFBD><C4BC>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD>ڴ桢<DAB4>õ<EFBFBD><C3B5><EFBFBD>Դ<EFBFBD><D4B4>С
HRSRC hRes = ::FindResource(NULL, MAKEINTRESOURCE(resNameId), (LPCWSTR)resType);
HGLOBAL hMem = ::LoadResource(NULL, hRes);
DWORD dwSize = ::SizeofResource(NULL, hRes);
if (hRes && hMem && dwSize)
{
// д<><D0B4><EFBFBD>ļ<EFBFBD>
DWORD dwWrite = 0;
::WriteFile(hFile, hMem, dwSize, &dwWrite, NULL);
::CloseHandle(hFile);
return destFilePath;
}
else
{
::CloseHandle(hFile);
::DeleteFile((LPCWSTR)destFilePath);
2018-07-03 01:49:20 +08:00
return std::move(String());
}
}
e2d::String e2d::Path::getFileExtension(const 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>
size_t pos = filePath.getWString().find_last_of(L'.');
// <20>ж<EFBFBD> pos <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>Чλ<D0A7><CEBB>
if (pos != std::wstring::npos)
{
// <20><>ȡ<EFBFBD><C8A1>չ<EFBFBD><D5B9>
fileExtension = filePath.subtract(static_cast<int>(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::getInstance()->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 = nullptr; // <20><>ʼĿ¼ΪĬ<CEAA><C4AC>
ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
ofn.lpstrTitle = (LPCWSTR)title; // <20><><EFBFBD><EFBFBD>
ofn.lpstrDefExt = (LPCWSTR)defExt; // Ĭ<><C4AC>׷<EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD>չ<EFBFBD><D5B9>
if (GetSaveFileName(&ofn))
{
return strFilename;
}
2018-07-03 01:49:20 +08:00
return std::move(String());
}
bool e2d::Path::createFolder(const String& dirPath)
{
if (dirPath.isEmpty() || dirPath.getLength() >= MAX_PATH)
{
return false;
}
wchar_t tmpDirPath[_MAX_PATH] = { 0 };
int length = dirPath.getLength();
for (int i = 0; i < length; ++i)
{
tmpDirPath[i] = dirPath.at(i);
if (tmpDirPath[i] == L'\\' || tmpDirPath[i] == L'/' || i == (length - 1))
{
if (::_waccess(tmpDirPath, 0) != 0)
{
if (::_wmkdir(tmpDirPath) != 0)
{
return false;
}
}
}
}
return true;
}
2018-05-24 15:47:38 +08:00
bool e2d::Path::exists(const String & path)
{
2018-05-24 15:47:38 +08:00
if (path.isEmpty() || path.getLength() >= MAX_PATH)
{
return false;
}
return ::_waccess((const wchar_t *)path, 0) == 0;
}