2018-04-21 21:24:46 +08:00
|
|
|
|
#include "..\e2dtool.h"
|
2018-03-28 17:56:04 +08:00
|
|
|
|
#include <algorithm>
|
2018-05-24 15:47:38 +08:00
|
|
|
|
#include <list>
|
2018-03-28 17:56:04 +08:00
|
|
|
|
#include <commdlg.h>
|
2018-01-30 16:45:38 +08:00
|
|
|
|
|
2018-03-28 17:56:04 +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
|
|
|
|
|
2018-03-28 17:56:04 +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;
|
2018-05-24 14:33:16 +08:00
|
|
|
|
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-05-24 14:33:16 +08:00
|
|
|
|
bool e2d::Path::__init(const String& gameName)
|
2018-02-28 19:46:03 +08:00
|
|
|
|
{
|
2018-04-26 21:47:56 +08:00
|
|
|
|
// <20><>ȡ AppData\Local <20>ļ<EFBFBD><C4BC>е<EFBFBD>·<EFBFBD><C2B7>
|
2018-03-28 17:56:04 +08:00
|
|
|
|
typedef HRESULT(WINAPI* pFunSHGetKnownFolderPath)(const GUID& rfid, DWORD dwFlags, HANDLE hToken, PWSTR *ppszPath);
|
2018-02-28 19:46:03 +08:00
|
|
|
|
|
2018-05-17 15:22:14 +08:00
|
|
|
|
PWSTR pszPath = nullptr;
|
2018-03-28 17:56:04 +08:00
|
|
|
|
HMODULE hModule = LoadLibrary(L"shell32.dll");
|
|
|
|
|
|
pFunSHGetKnownFolderPath SHGetKnownFolderPath = (pFunSHGetKnownFolderPath)GetProcAddress(hModule, "SHGetKnownFolderPath");
|
2018-05-17 15:22:14 +08:00
|
|
|
|
HRESULT hr = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &pszPath);
|
2018-01-30 16:45:38 +08:00
|
|
|
|
|
2018-03-28 17:56:04 +08:00
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
2018-04-26 21:47:56 +08:00
|
|
|
|
s_sLocalAppDataPath = pszPath;
|
2018-03-28 17:56:04 +08:00
|
|
|
|
CoTaskMemFree(pszPath);
|
|
|
|
|
|
}
|
2018-04-26 21:47:56 +08:00
|
|
|
|
else
|
2018-03-28 17:56:04 +08:00
|
|
|
|
{
|
2018-05-24 14:33:16 +08:00
|
|
|
|
WARN("Get local AppData path failed!");
|
|
|
|
|
|
return false;
|
2018-03-28 17:56:04 +08:00
|
|
|
|
}
|
2018-04-26 21:47:56 +08:00
|
|
|
|
|
2018-05-24 14:33:16 +08:00
|
|
|
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>ݵ<EFBFBD>Ĭ<EFBFBD>ϱ<EFBFBD><CFB1><EFBFBD>·<EFBFBD><C2B7>
|
|
|
|
|
|
s_sDataSavePath = s_sLocalAppDataPath + L"\\Easy2DGameData\\";
|
|
|
|
|
|
if (!gameName.isEmpty())
|
2018-03-28 17:56:04 +08:00
|
|
|
|
{
|
2018-05-24 14:33:16 +08:00
|
|
|
|
s_sDataSavePath << gameName << L"\\";
|
|
|
|
|
|
}
|
2018-05-24 15:47:38 +08:00
|
|
|
|
if (!Path::exists(s_sDataSavePath))
|
2018-05-24 14:33:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (!Path::createFolder(s_sDataSavePath))
|
2018-04-26 21:47:56 +08:00
|
|
|
|
{
|
2018-05-24 14:33:16 +08:00
|
|
|
|
s_sDataSavePath = L"";
|
2018-04-26 21:47:56 +08:00
|
|
|
|
}
|
2018-05-24 14:33:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
s_sDataSavePath << L"Data.ini";
|
2018-04-26 21:47:56 +08:00
|
|
|
|
|
|
|
|
|
|
// <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD>ļ<EFBFBD>Ŀ¼
|
2018-05-24 14:33:16 +08:00
|
|
|
|
wchar_t path[_MAX_PATH];
|
|
|
|
|
|
if (0 == ::GetTempPath(_MAX_PATH, path))
|
2018-03-28 17:56:04 +08:00
|
|
|
|
{
|
2018-05-24 14:33:16 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-04-26 21:47:56 +08:00
|
|
|
|
|
2018-05-24 14:33:16 +08:00
|
|
|
|
s_sTempPath << path << L"\\Easy2DGameTemp\\";
|
|
|
|
|
|
if (!gameName.isEmpty())
|
|
|
|
|
|
{
|
|
|
|
|
|
s_sTempPath << gameName << L"\\";
|
|
|
|
|
|
}
|
2018-04-26 21:47:56 +08:00
|
|
|
|
|
2018-05-24 15:47:38 +08:00
|
|
|
|
if (!Path::exists(s_sTempPath))
|
2018-05-24 14:33:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (!Path::createFolder(s_sTempPath))
|
2018-04-26 21:47:56 +08:00
|
|
|
|
{
|
2018-05-24 14:33:16 +08:00
|
|
|
|
s_sTempPath = L"";
|
2018-04-26 21:47:56 +08:00
|
|
|
|
}
|
2018-05-24 14:33:16 +08:00
|
|
|
|
}
|
2018-04-26 21:47:56 +08:00
|
|
|
|
|
2018-05-24 14:33:16 +08:00
|
|
|
|
return true;
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
return String(szPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
return String();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
e2d::String e2d::Path::checkFilePath(const String& path)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Path::exists(path))
|
|
|
|
|
|
{
|
|
|
|
|
|
return path;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
for (auto& resPath : s_vPathList)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Path::exists(resPath + path))
|
|
|
|
|
|
{
|
|
|
|
|
|
return resPath + path;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return String();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-24 14:33:16 +08:00
|
|
|
|
e2d::String e2d::Path::getDataSavePath()
|
2018-04-26 21:47:56 +08:00
|
|
|
|
{
|
2018-05-24 14:33:16 +08:00
|
|
|
|
return s_sDataSavePath;
|
2018-02-28 19:46:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-07 15:48:06 +08:00
|
|
|
|
e2d::String e2d::Path::getFileExtension(const String& filePath)
|
2018-01-30 16:45:38 +08:00
|
|
|
|
{
|
2018-03-28 17:56:04 +08:00
|
|
|
|
String fileExtension;
|
|
|
|
|
|
// <20>ҵ<EFBFBD><D2B5>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> '.' <20><>λ<EFBFBD><CEBB>
|
2018-05-03 22:21:01 +08:00
|
|
|
|
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)
|
2018-03-28 17:56:04 +08:00
|
|
|
|
{
|
|
|
|
|
|
// <20><>ȡ<EFBFBD><C8A1>չ<EFBFBD><D5B9>
|
2018-05-03 22:21:01 +08:00
|
|
|
|
fileExtension = filePath.subtract(static_cast<int>(pos));
|
2018-03-28 17:56:04 +08:00
|
|
|
|
// ת<><D7AA>ΪСд<D0A1><D0B4>ĸ
|
|
|
|
|
|
fileExtension = fileExtension.toLower();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return fileExtension;
|
2018-01-30 16:45:38 +08:00
|
|
|
|
}
|
2018-03-11 23:56:40 +08:00
|
|
|
|
|
2018-05-07 15:48:06 +08:00
|
|
|
|
e2d::String e2d::Path::getSaveFilePath(const String& title, const String& defExt)
|
2018-03-11 23:56:40 +08:00
|
|
|
|
{
|
2018-03-28 17:56:04 +08:00
|
|
|
|
// <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>
|
2018-05-17 15:22:14 +08:00
|
|
|
|
ofn.lpstrInitialDir = nullptr; // <20><>ʼĿ¼ΪĬ<CEAA><C4AC>
|
2018-03-28 17:56:04 +08:00
|
|
|
|
ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
|
2018-05-24 14:33:16 +08:00
|
|
|
|
ofn.lpstrTitle = (LPCWSTR)title; // <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
ofn.lpstrDefExt = (LPCWSTR)defExt; // Ĭ<><C4AC><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD>չ<EFBFBD><D5B9>
|
2018-03-28 17:56:04 +08:00
|
|
|
|
|
|
|
|
|
|
if (GetSaveFileName(&ofn))
|
2018-03-11 23:56:40 +08:00
|
|
|
|
{
|
2018-03-28 17:56:04 +08:00
|
|
|
|
return strFilename;
|
2018-03-11 23:56:40 +08:00
|
|
|
|
}
|
2018-03-28 17:56:04 +08:00
|
|
|
|
return L"";
|
2018-03-11 23:56:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-07 15:48:06 +08:00
|
|
|
|
bool e2d::Path::createFolder(const String& dirPath)
|
2018-03-11 23:56:40 +08:00
|
|
|
|
{
|
2018-05-24 14:33:16 +08:00
|
|
|
|
if (dirPath.isEmpty() || dirPath.getLength() >= MAX_PATH)
|
2018-03-28 17:56:04 +08:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-24 14:33:16 +08:00
|
|
|
|
wchar_t tmpDirPath[_MAX_PATH] = { 0 };
|
|
|
|
|
|
int length = dirPath.getLength();
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < length; ++i)
|
2018-03-28 17:56:04 +08:00
|
|
|
|
{
|
2018-05-24 14:33:16 +08:00
|
|
|
|
tmpDirPath[i] = dirPath.at(i);
|
|
|
|
|
|
if (tmpDirPath[i] == L'\\' || tmpDirPath[i] == L'/' || i == (length - 1))
|
2018-03-28 17:56:04 +08:00
|
|
|
|
{
|
2018-05-24 14:33:16 +08:00
|
|
|
|
if (::_waccess(tmpDirPath, 0) != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (::_wmkdir(tmpDirPath) != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-03-28 17:56:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
2018-03-11 23:56:40 +08:00
|
|
|
|
}
|
2018-05-24 14:33:16 +08:00
|
|
|
|
|
2018-05-24 15:47:38 +08:00
|
|
|
|
bool e2d::Path::exists(const String & path)
|
2018-05-24 14:33:16 +08:00
|
|
|
|
{
|
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;
|
2018-05-24 14:33:16 +08:00
|
|
|
|
}
|