From e7f0939030ed4dc55300faa68b1c1a135cd1af0a Mon Sep 17 00:00:00 2001 From: Haibo Date: Sun, 27 May 2018 01:28:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20Path::extractResource=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95=EF=BC=8C=E6=8F=90=E5=8F=96=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E6=96=87=E4=BB=B6=E5=88=B0=20Temp=20?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Common/Image.cpp | 2 +- core/Tool/Music.cpp | 2 +- core/Tool/Path.cpp | 22 +++++++++++++++++++++- core/e2dcommon.h | 6 +++--- core/e2dmacros.h | 4 ++-- core/e2dtool.h | 11 +++++++++-- 6 files changed, 37 insertions(+), 10 deletions(-) diff --git a/core/Common/Image.cpp b/core/Common/Image.cpp index 7235386e..6a5b60e4 100644 --- a/core/Common/Image.cpp +++ b/core/Common/Image.cpp @@ -161,7 +161,7 @@ bool e2d::Image::preload(const String& filePath) return true; } - String actualFilePath = Path::checkFilePath(filePath); + String actualFilePath = Path::searchForFile(filePath); if (actualFilePath.isEmpty()) { return false; diff --git a/core/Tool/Music.cpp b/core/Tool/Music.cpp index 5118e831..e57d581a 100644 --- a/core/Tool/Music.cpp +++ b/core/Tool/Music.cpp @@ -85,7 +85,7 @@ bool e2d::Music::open(const e2d::String& filePath) return false; } - String actualFilePath = Path::checkFilePath(filePath); + String actualFilePath = Path::searchForFile(filePath); if (actualFilePath.isEmpty()) { WARN("MusicInfo::open File not found."); diff --git a/core/Tool/Path.cpp b/core/Tool/Path.cpp index cc74da3b..b36bdc70 100644 --- a/core/Tool/Path.cpp +++ b/core/Tool/Path.cpp @@ -104,7 +104,7 @@ e2d::String e2d::Path::getExecutableFilePath() return String(); } -e2d::String e2d::Path::checkFilePath(const String& path) +e2d::String e2d::Path::searchForFile(const String& path) { if (Path::exists(path)) { @@ -123,6 +123,26 @@ e2d::String e2d::Path::checkFilePath(const String& path) return String(); } +e2d::String e2d::Path::extractResource(int resNameId, const String & resType, const String & destFileName) +{ + String destFilePath = s_sTempPath + destFileName; + // 创建文件 + HANDLE hFile = ::CreateFile((LPCWSTR)destFilePath, GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, NULL); + if (hFile == INVALID_HANDLE_VALUE) + return String(); + + // 查找资源文件中、加载资源到内存、得到资源大小 + HRSRC hRes = ::FindResource(NULL, MAKEINTRESOURCE(resNameId), (LPCWSTR)resType); + HGLOBAL hMem = ::LoadResource(NULL, hRes); + DWORD dwSize = ::SizeofResource(NULL, hRes); + + // 写入文件 + DWORD dwWrite = 0; + ::WriteFile(hFile, hMem, dwSize, &dwWrite, NULL); + ::CloseHandle(hFile); + return destFilePath; +} + e2d::String e2d::Path::getDataSavePath() { return s_sDataSavePath; diff --git a/core/e2dcommon.h b/core/e2dcommon.h index fec59d01..643b2d44 100644 --- a/core/e2dcommon.h +++ b/core/e2dcommon.h @@ -236,8 +236,8 @@ public: friend String operator+ (const wchar_t*, const String &); // 类型转换操作符 - E2D_EXPLICIT operator const wchar_t* () const; - E2D_EXPLICIT operator wchar_t* () const; + E2D_OP_EXPLICIT operator const wchar_t* () const; + E2D_OP_EXPLICIT operator wchar_t* () const; // 比较运算符 bool operator== (const String &) const; @@ -392,7 +392,7 @@ public: void operator() (void) const; - operator bool() const; + E2D_OP_EXPLICIT operator bool() const; protected: std::function _func; diff --git a/core/e2dmacros.h b/core/e2dmacros.h index 7755a0b4..f2c0bf82 100644 --- a/core/e2dmacros.h +++ b/core/e2dmacros.h @@ -79,7 +79,7 @@ #endif #if _MSC_VER >= 1800 -# define E2D_EXPLICIT explicit +# define E2D_OP_EXPLICIT explicit #else -# define E2D_EXPLICIT +# define E2D_OP_EXPLICIT #endif \ No newline at end of file diff --git a/core/e2dtool.h b/core/e2dtool.h index ef4884a5..e73010a5 100644 --- a/core/e2dtool.h +++ b/core/e2dtool.h @@ -447,11 +447,18 @@ public: String path ); - // 检查文件路径 - static String checkFilePath( + // 检索文件路径 + static String searchForFile( const String& path ); + // 提取资源文件,返回提取后的文件路径 + static String extractResource( + int resNameId, /* 资源名称 */ + const String& resType, /* 资源类型 */ + const String& destFileName /* 目标文件名 */ + ); + // 获取数据的默认保存路径 static String getDataSavePath();