From 0cf6c6fb94eb5524fc33e29099d71b82399372b1 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Thu, 24 May 2018 15:47:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=B7=AF=E5=BE=84=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Common/Image.cpp | 11 +++++++-- core/Tool/Music.cpp | 9 ++++++- core/Tool/Path.cpp | 57 ++++++++++++++++++++++++++++++++++++++++--- core/e2dtool.h | 17 +++++++++++-- 4 files changed, 85 insertions(+), 9 deletions(-) diff --git a/core/Common/Image.cpp b/core/Common/Image.cpp index bceb4654..7235386e 100644 --- a/core/Common/Image.cpp +++ b/core/Common/Image.cpp @@ -1,5 +1,6 @@ #include "..\e2dcommon.h" #include "..\e2dbase.h" +#include "..\e2dtool.h" #include static std::map s_mBitmapsFromFile; @@ -160,6 +161,12 @@ bool e2d::Image::preload(const String& filePath) return true; } + String actualFilePath = Path::checkFilePath(filePath); + if (actualFilePath.isEmpty()) + { + return false; + } + HRESULT hr = S_OK; IWICBitmapDecoder *pDecoder = nullptr; @@ -170,7 +177,7 @@ bool e2d::Image::preload(const String& filePath) // 创建解码器 hr = Renderer::getIWICImagingFactory()->CreateDecoderFromFilename( - (LPCWSTR)filePath, + (LPCWSTR)actualFilePath, nullptr, GENERIC_READ, WICDecodeMetadataCacheOnLoad, @@ -218,7 +225,7 @@ bool e2d::Image::preload(const String& filePath) // 保存图片指针和图片的 Hash 名 s_mBitmapsFromFile.insert( std::map::value_type( - filePath.getHashCode(), + actualFilePath.getHashCode(), pBitmap) ); } diff --git a/core/Tool/Music.cpp b/core/Tool/Music.cpp index e1fb154e..5118e831 100644 --- a/core/Tool/Music.cpp +++ b/core/Tool/Music.cpp @@ -85,6 +85,13 @@ bool e2d::Music::open(const e2d::String& filePath) return false; } + String actualFilePath = Path::checkFilePath(filePath); + if (actualFilePath.isEmpty()) + { + WARN("MusicInfo::open File not found."); + return false; + } + if (!s_pXAudio2) { WARN("IXAudio2 nullptr pointer error!"); @@ -93,7 +100,7 @@ bool e2d::Music::open(const e2d::String& filePath) // 定位 wave 文件 wchar_t pFilePath[MAX_PATH]; - if (!_findMediaFileCch(pFilePath, MAX_PATH, (const wchar_t *)filePath)) + if (!_findMediaFileCch(pFilePath, MAX_PATH, (const wchar_t *)actualFilePath)) { WARN("Failed to find media file: %s", pFilePath); return false; diff --git a/core/Tool/Path.cpp b/core/Tool/Path.cpp index d523b394..cc74da3b 100644 --- a/core/Tool/Path.cpp +++ b/core/Tool/Path.cpp @@ -1,5 +1,6 @@ #include "..\e2dtool.h" #include +#include #include #define DEFINE_KNOWN_FOLDER(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \ @@ -12,6 +13,7 @@ DEFINE_KNOWN_FOLDER(FOLDERID_LocalAppData, 0xF1B32785, 0x6FBA, 0x4FCF, 0x9D, 0x5 static e2d::String s_sLocalAppDataPath; static e2d::String s_sTempPath; static e2d::String s_sDataSavePath; +static std::list s_vPathList; bool e2d::Path::__init(const String& gameName) { @@ -40,7 +42,7 @@ bool e2d::Path::__init(const String& gameName) { s_sDataSavePath << gameName << L"\\"; } - if (!Path::existsFolder(s_sDataSavePath)) + if (!Path::exists(s_sDataSavePath)) { if (!Path::createFolder(s_sDataSavePath)) { @@ -62,7 +64,7 @@ bool e2d::Path::__init(const String& gameName) s_sTempPath << gameName << L"\\"; } - if (!Path::existsFolder(s_sTempPath)) + if (!Path::exists(s_sTempPath)) { if (!Path::createFolder(s_sTempPath)) { @@ -73,11 +75,54 @@ bool e2d::Path::__init(const String& gameName) return true; } +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); + } +} + e2d::String e2d::Path::getTempPath() { return s_sTempPath; } +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(); +} + e2d::String e2d::Path::getDataSavePath() { return s_sDataSavePath; @@ -150,7 +195,11 @@ bool e2d::Path::createFolder(const String& dirPath) return true; } -bool e2d::Path::existsFolder(const String & dirPath) +bool e2d::Path::exists(const String & path) { - return ::_waccess((const wchar_t *)dirPath, 0) == 0; + if (path.isEmpty() || path.getLength() >= MAX_PATH) + { + return false; + } + return ::_waccess((const wchar_t *)path, 0) == 0; } diff --git a/core/e2dtool.h b/core/e2dtool.h index be9f6318..f3aa21e3 100644 --- a/core/e2dtool.h +++ b/core/e2dtool.h @@ -442,12 +442,25 @@ class Path friend class Game; public: + // 添加搜索路径 + static void add( + String path + ); + + // 检查文件路径 + static String checkFilePath( + const String& path + ); + // 获取数据的默认保存路径 static String getDataSavePath(); // 获取临时文件目录 static String getTempPath(); + // 获取执行程序的绝对路径 + static String getExecutableFilePath(); + // 获取文件扩展名 static String getFileExtension( const String& filePath @@ -464,8 +477,8 @@ public: const String& dirPath /* 文件夹路径 */ ); - // 判断文件夹是否存在 - static bool existsFolder( + // 判断文件或文件夹是否存在 + static bool exists( const String& dirPath /* 文件夹路径 */ );