增加路径搜索功能

This commit is contained in:
Nomango 2018-05-24 15:47:38 +08:00
parent 868238e42b
commit 0cf6c6fb94
4 changed files with 85 additions and 9 deletions

View File

@ -1,5 +1,6 @@
#include "..\e2dcommon.h"
#include "..\e2dbase.h"
#include "..\e2dtool.h"
#include <map>
static std::map<size_t, ID2D1Bitmap*> 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<size_t, ID2D1Bitmap*>::value_type(
filePath.getHashCode(),
actualFilePath.getHashCode(),
pBitmap)
);
}

View File

@ -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;

View File

@ -1,5 +1,6 @@
#include "..\e2dtool.h"
#include <algorithm>
#include <list>
#include <commdlg.h>
#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<e2d::String> 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;
}

View File

@ -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 /* 文件夹路径 */
);