Magic_Game/core/Tool/Path.cpp

229 lines
5.4 KiB
C++
Raw Normal View History

2018-04-21 21:24:46 +08:00
#include "..\e2dtool.h"
#include <algorithm>
2018-05-24 15:47:38 +08:00
#include <list>
#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_sDataSavePath;
2018-05-24 15:47:38 +08:00
static std::list<e2d::String> s_vPathList;
2018-04-26 21:47:56 +08:00
2018-07-03 01:49:20 +08:00
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 = nullptr;
HMODULE hModule = LoadLibrary(L"shell32.dll");
pFunSHGetKnownFolderPath SHGetKnownFolderPath = (pFunSHGetKnownFolderPath)GetProcAddress(hModule, "SHGetKnownFolderPath");
HRESULT hr = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &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
{
WARN("Get local AppData path failed!");
return false;
}
2018-07-03 01:49:20 +08:00
return true;
}
void e2d::Path::setGameFolderName(const String & name)
{
if (name.isEmpty())
return;
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>ݵ<EFBFBD>Ĭ<EFBFBD>ϱ<EFBFBD><CFB1><EFBFBD>·<EFBFBD><C2B7>
2018-07-03 01:49:20 +08:00
if (!s_sLocalAppDataPath.isEmpty())
{
2018-07-03 01:49:20 +08:00
s_sDataSavePath = s_sLocalAppDataPath + L"\\Easy2DGameData\\" << name << L"\\";
if (!Path::exists(s_sDataSavePath) && !Path::createFolder(s_sDataSavePath))
2018-04-26 21:47:56 +08:00
{
s_sDataSavePath = L"";
2018-04-26 21:47:56 +08:00
}
2018-07-03 01:49:20 +08:00
s_sDataSavePath << L"Data.ini";
}
2018-04-26 21:47:56 +08:00
// <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD>ļ<EFBFBD>Ŀ¼
wchar_t path[_MAX_PATH];
2018-07-03 01:49:20 +08:00
if (0 != ::GetTempPath(_MAX_PATH, path))
{
2018-07-03 01:49:20 +08:00
s_sTempPath << path << L"\\Easy2DGameTemp\\" << name << L"\\";
2018-04-26 21:47:56 +08:00
2018-07-03 01:49:20 +08:00
if (!Path::exists(s_sTempPath) && !Path::createFolder(s_sTempPath))
2018-04-26 21:47:56 +08:00
{
s_sTempPath = L"";
2018-04-26 21:47:56 +08:00
}
}
2018-04-26 21:47:56 +08:00
}
2018-05-24 15:47:38 +08:00
void e2d::Path::add(String path)
{
path.replace(L"/", L"\\");
if (path[path.getLength() - 1] != L'\\')
{
path << L"\\";
}
auto iter = std::find(s_vPathList.cbegin(), s_vPathList.cend(), path);
if (iter == s_vPathList.cend())
{
s_vPathList.push_front(path);
}
}
2018-04-26 21:47:56 +08:00
e2d::String e2d::Path::getTempPath()
{
return s_sTempPath;
}
2018-05-24 15:47:38 +08:00
e2d::String e2d::Path::getExecutableFilePath()
{
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
}
e2d::String e2d::Path::searchForFile(const String& path)
2018-05-24 15:47:38 +08:00
{
if (Path::exists(path))
{
return path;
}
else
{
for (auto& resPath : s_vPathList)
{
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)
{
String destFilePath = s_sTempPath + 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::getDataSavePath()
2018-04-26 21:47:56 +08:00
{
return s_sDataSavePath;
}
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::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;
}