增加 Path::extractResource 方法,提取资源中的文件到 Temp 目录下

This commit is contained in:
Haibo 2018-05-27 01:28:01 +08:00
parent 5f2583d4ba
commit e7f0939030
6 changed files with 37 additions and 10 deletions

View File

@ -161,7 +161,7 @@ bool e2d::Image::preload(const String& filePath)
return true; return true;
} }
String actualFilePath = Path::checkFilePath(filePath); String actualFilePath = Path::searchForFile(filePath);
if (actualFilePath.isEmpty()) if (actualFilePath.isEmpty())
{ {
return false; return false;

View File

@ -85,7 +85,7 @@ bool e2d::Music::open(const e2d::String& filePath)
return false; return false;
} }
String actualFilePath = Path::checkFilePath(filePath); String actualFilePath = Path::searchForFile(filePath);
if (actualFilePath.isEmpty()) if (actualFilePath.isEmpty())
{ {
WARN("MusicInfo::open File not found."); WARN("MusicInfo::open File not found.");

View File

@ -104,7 +104,7 @@ e2d::String e2d::Path::getExecutableFilePath()
return String(); return String();
} }
e2d::String e2d::Path::checkFilePath(const String& path) e2d::String e2d::Path::searchForFile(const String& path)
{ {
if (Path::exists(path)) if (Path::exists(path))
{ {
@ -123,6 +123,26 @@ e2d::String e2d::Path::checkFilePath(const String& path)
return String(); 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() e2d::String e2d::Path::getDataSavePath()
{ {
return s_sDataSavePath; return s_sDataSavePath;

View File

@ -236,8 +236,8 @@ public:
friend String operator+ (const wchar_t*, const String &); friend String operator+ (const wchar_t*, const String &);
// ÀàÐÍת»»²Ù×÷·û // ÀàÐÍת»»²Ù×÷·û
E2D_EXPLICIT operator const wchar_t* () const; E2D_OP_EXPLICIT operator const wchar_t* () const;
E2D_EXPLICIT operator wchar_t* () const; E2D_OP_EXPLICIT operator wchar_t* () const;
// ±È½ÏÔËËã·û // ±È½ÏÔËËã·û
bool operator== (const String &) const; bool operator== (const String &) const;
@ -392,7 +392,7 @@ public:
void operator() (void) const; void operator() (void) const;
operator bool() const; E2D_OP_EXPLICIT operator bool() const;
protected: protected:
std::function<void()> _func; std::function<void()> _func;

View File

@ -79,7 +79,7 @@
#endif #endif
#if _MSC_VER >= 1800 #if _MSC_VER >= 1800
# define E2D_EXPLICIT explicit # define E2D_OP_EXPLICIT explicit
#else #else
# define E2D_EXPLICIT # define E2D_OP_EXPLICIT
#endif #endif

View File

@ -447,11 +447,18 @@ public:
String path String path
); );
// 检文件路径 // 检文件路径
static String checkFilePath( static String searchForFile(
const String& path const String& path
); );
// 提取资源文件,返回提取后的文件路径
static String extractResource(
int resNameId, /* 资源名称 */
const String& resType, /* 资源类型 */
const String& destFileName /* 目标文件名 */
);
// 获取数据的默认保存路径 // 获取数据的默认保存路径
static String getDataSavePath(); static String getDataSavePath();