增加Path类初始化

This commit is contained in:
Nomango 2018-04-26 21:47:56 +08:00
parent 245a6c6e71
commit fca3ba9f25
4 changed files with 117 additions and 74 deletions

View File

@ -56,17 +56,22 @@ bool e2d::Game::init(String sGameName)
if (!Music::__init()) if (!Music::__init())
{ {
WARN_IF(true, "Music::__init Failed!"); WARN_IF(true, "Music::__init Failed!");
goto music_fail; Music::__uninit();
}
// ±£´æÓÎÏ·Ãû³Æ
s_sGameName = sGameName;
// ³õʼ»¯Â·¾¶
if (!Path::__init())
{
WARN_IF(true, "Path::__init Failed!");
} }
// 初始化成功 // 初始化成功
s_sGameName = sGameName;
s_bInitialized = true; s_bInitialized = true;
goto succeeded; goto succeeded;
music_fail:
Music::__uninit();
input_fail: input_fail:
Input::__uninit(); Input::__uninit();

View File

@ -9,11 +9,15 @@
DEFINE_KNOWN_FOLDER(FOLDERID_LocalAppData, 0xF1B32785, 0x6FBA, 0x4FCF, 0x9D, 0x55, 0x7B, 0x8E, 0x7F, 0x15, 0x70, 0x91); DEFINE_KNOWN_FOLDER(FOLDERID_LocalAppData, 0xF1B32785, 0x6FBA, 0x4FCF, 0x9D, 0x55, 0x7B, 0x8E, 0x7F, 0x15, 0x70, 0x91);
e2d::String e2d::Path::getLocalAppDataPath() static e2d::String s_sLocalAppDataPath;
static e2d::String s_sTempPath;
static e2d::String s_sDefaultSavePath;
bool e2d::Path::__init()
{ {
// 获取 AppData\Local 文件夹的路径
typedef HRESULT(WINAPI* pFunSHGetKnownFolderPath)(const GUID& rfid, DWORD dwFlags, HANDLE hToken, PWSTR *ppszPath); typedef HRESULT(WINAPI* pFunSHGetKnownFolderPath)(const GUID& rfid, DWORD dwFlags, HANDLE hToken, PWSTR *ppszPath);
// 获取 AppData\Local 文件夹的路径
PWSTR pszPath = NULL; PWSTR pszPath = NULL;
HMODULE hModule = LoadLibrary(L"shell32.dll"); HMODULE hModule = LoadLibrary(L"shell32.dll");
pFunSHGetKnownFolderPath SHGetKnownFolderPath = (pFunSHGetKnownFolderPath)GetProcAddress(hModule, "SHGetKnownFolderPath"); pFunSHGetKnownFolderPath SHGetKnownFolderPath = (pFunSHGetKnownFolderPath)GetProcAddress(hModule, "SHGetKnownFolderPath");
@ -21,77 +25,108 @@ e2d::String e2d::Path::getLocalAppDataPath()
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
String path = pszPath; s_sLocalAppDataPath = pszPath;
CoTaskMemFree(pszPath); CoTaskMemFree(pszPath);
return path;
} }
return L""; else
{
WARN_IF(true, "Cannot get local AppData path!");
}
// 获取游戏名称
String sGameName = Game::getName();
// 获取默认保存路径
bool bInitSavePath = false;
do
{
String localAppPath = s_sLocalAppDataPath;
if (localAppPath.isEmpty())
{
break;
}
else
{
s_sDefaultSavePath = localAppPath;
}
localAppPath << L"\\Easy2DGameData";
if (Path::createFolder(localAppPath))
{
s_sDefaultSavePath = localAppPath;
}
else
{
break;
}
if (!sGameName.isEmpty())
{
localAppPath << L"\\" << sGameName;
// 创建文件夹
if (Path::createFolder(localAppPath))
{
s_sDefaultSavePath = localAppPath;
}
}
s_sDefaultSavePath << L"\\";
bInitSavePath = true;
} while (0);
// 获取临时文件目录
bool bInitTempPath = false;
do
{
wchar_t path[_MAX_PATH];
if (0 == ::GetTempPath(_MAX_PATH, path))
{
break;
}
else
{
s_sTempPath = path;
}
// 创建临时文件目录
String tempPath;
tempPath << s_sTempPath << L"\\Easy2DGameTemp";
// 创建文件夹
if (Path::createFolder(tempPath))
{
s_sTempPath = path;
}
else
{
break;
}
if (!sGameName.isEmpty())
{
tempPath << L"\\" << sGameName;
// 创建文件夹
if (Path::createFolder(tempPath))
{
s_sTempPath = tempPath;
}
}
s_sTempPath << L"\\";
bInitTempPath = true;
} while (0);
return SUCCEEDED(hr) && bInitSavePath && bInitTempPath;
} }
e2d::String e2d::Path::getTempPath() e2d::String e2d::Path::getTempPath()
{ {
// 获取临时文件目录 return s_sTempPath;
wchar_t path[_MAX_PATH];
if (0 == ::GetTempPath(_MAX_PATH, path))
{
return L"";
}
// 创建临时文件目录
e2d::String tempFilePath;
tempFilePath << path << L"Easy2DGameTemp\\";
// 创建文件夹
if (!Path::createFolder(tempFilePath))
{
return path;
}
// 获取游戏名称
String sGameName = Game::getName();
if (!sGameName.isEmpty())
{
// 创建文件夹
if (!Path::createFolder(tempFilePath + sGameName + L"\\"))
{
return std::move(tempFilePath);
}
tempFilePath << sGameName << L"\\";
}
return std::move(tempFilePath);
} }
e2d::String e2d::Path::getDefaultSavePath() e2d::String e2d::Path::getDefaultSavePath()
{ {
// 获取 AppData 路径 return s_sDefaultSavePath;
String path = Path::getLocalAppDataPath();
if (path.isEmpty())
{
WARN_IF(true, "Cannot get local AppData path!");
return std::move(path);
}
// 创建文件夹
if (!Path::createFolder(path + L"\\Easy2DGameData"))
{
return std::move(path);
}
path << L"\\Easy2DGameData";
// 获取游戏名称
String sGameName = Game::getName();
if (!sGameName.isEmpty())
{
// 创建文件夹
if (!Path::createFolder(path + L"\\" + sGameName))
{
return std::move(path);
}
path << L"\\" << sGameName;
}
path << L"\\";
return std::move(path);
} }
e2d::String e2d::Path::getFileExtension(String filePath) e2d::String e2d::Path::getFileExtension(String filePath)

View File

@ -14,7 +14,7 @@ class Game
public: public:
// 初始化游戏 // 初始化游戏
static bool init( static bool init(
String sGameName = L"E2DGame" /* 游戏英文名称 */ String sGameName = L"" /* 游戏英文名称 */
); );
// 启动游戏 // 启动游戏

View File

@ -292,16 +292,15 @@ public:
// 路径工具 // 路径工具
class Path class Path
{ {
friend Game;
public: public:
// 获取系统的 AppData Local 路径 // 获取数据的默认保存路径
static String getLocalAppDataPath(); static String getDefaultSavePath();
// 获取临时文件目录 // 获取临时文件目录
static String getTempPath(); static String getTempPath();
// 获取数据的默认保存路径
static String getDefaultSavePath();
// 获取文件扩展名 // 获取文件扩展名
static String getFileExtension( static String getFileExtension(
String filePath String filePath
@ -317,6 +316,10 @@ public:
static bool createFolder( static bool createFolder(
String strDirPath /* 文件夹路径 */ String strDirPath /* 文件夹路径 */
); );
private:
// 初始化
static bool __init();
}; };
} }