From fca3ba9f2536f43d71ebfdd90ddba76f493cfaa7 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Thu, 26 Apr 2018 21:47:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Path=E7=B1=BB=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Base/Game.cpp | 15 +++-- core/Tool/Path.cpp | 161 +++++++++++++++++++++++++++------------------ core/e2dbase.h | 2 +- core/e2dtool.h | 13 ++-- 4 files changed, 117 insertions(+), 74 deletions(-) diff --git a/core/Base/Game.cpp b/core/Base/Game.cpp index 73ee7718..1eee9bf1 100644 --- a/core/Base/Game.cpp +++ b/core/Base/Game.cpp @@ -56,17 +56,22 @@ bool e2d::Game::init(String sGameName) if (!Music::__init()) { 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; goto succeeded; -music_fail: - Music::__uninit(); - input_fail: Input::__uninit(); diff --git a/core/Tool/Path.cpp b/core/Tool/Path.cpp index 2b9db6a7..79484d1b 100644 --- a/core/Tool/Path.cpp +++ b/core/Tool/Path.cpp @@ -9,11 +9,15 @@ 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); - // 获取 AppData\Local 文件夹的路径 PWSTR pszPath = NULL; HMODULE hModule = LoadLibrary(L"shell32.dll"); pFunSHGetKnownFolderPath SHGetKnownFolderPath = (pFunSHGetKnownFolderPath)GetProcAddress(hModule, "SHGetKnownFolderPath"); @@ -21,77 +25,108 @@ e2d::String e2d::Path::getLocalAppDataPath() if (SUCCEEDED(hr)) { - String path = pszPath; + s_sLocalAppDataPath = 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() { - // 获取临时文件目录 - 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); + return s_sTempPath; } e2d::String e2d::Path::getDefaultSavePath() { - // 获取 AppData 路径 - 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); + return s_sDefaultSavePath; } e2d::String e2d::Path::getFileExtension(String filePath) diff --git a/core/e2dbase.h b/core/e2dbase.h index 5e4baa0f..6ae50333 100644 --- a/core/e2dbase.h +++ b/core/e2dbase.h @@ -14,7 +14,7 @@ class Game public: // 初始化游戏 static bool init( - String sGameName = L"E2DGame" /* 游戏英文名称 */ + String sGameName = L"" /* 游戏英文名称 */ ); // 启动游戏 diff --git a/core/e2dtool.h b/core/e2dtool.h index cba6c90b..9cce01e1 100644 --- a/core/e2dtool.h +++ b/core/e2dtool.h @@ -292,16 +292,15 @@ public: // 路径工具 class Path { + friend Game; + public: - // 获取系统的 AppData Local 路径 - static String getLocalAppDataPath(); + // 获取数据的默认保存路径 + static String getDefaultSavePath(); // 获取临时文件目录 static String getTempPath(); - // 获取数据的默认保存路径 - static String getDefaultSavePath(); - // 获取文件扩展名 static String getFileExtension( String filePath @@ -317,6 +316,10 @@ public: static bool createFolder( String strDirPath /* 文件夹路径 */ ); + +private: + // 初始化 + static bool __init(); }; } \ No newline at end of file