change: refactory File class.

This commit is contained in:
Nomango 2018-10-16 14:57:28 +08:00
parent 709f2bfe8d
commit 4ed453468d
7 changed files with 66 additions and 77 deletions

View File

@ -306,12 +306,9 @@ namespace easy2d
const String& file_name const String& file_name
); );
// 文件或文件夹是否存在 // 文件是否存在
bool Exists() const; bool Exists() const;
// 是否是文件夹
bool IsFolder() const;
// 删除文件 // 删除文件
bool Delete(); bool Delete();
@ -323,8 +320,7 @@ namespace easy2d
// 释放资源到临时文件目录 // 释放资源到临时文件目录
static File Extract( static File Extract(
int resource_name, /* 资源名称 */ const Resource& res, /* 资源 */
const String& resource_type, /* 资源类型 */
const String& dest_file_name /* 目标文件名 */ const String& dest_file_name /* 目标文件名 */
); );
@ -333,11 +329,6 @@ namespace easy2d
const String& path const String& path
); );
// 创建文件夹
static bool CreateFolder(
const String& dir_path /* 文件夹路径 */
);
// 弹出打开文件对话框 // 弹出打开文件对话框
static File ShowOpenDialog( static File ShowOpenDialog(
const String& title = L"打开", /* 对话框标题 */ const String& title = L"打开", /* 对话框标题 */
@ -352,8 +343,7 @@ namespace easy2d
); );
protected: protected:
DWORD attributes_; String file_path_;
String file_path_;
static std::list<String> search_paths_; static std::list<String> search_paths_;
}; };

View File

@ -179,7 +179,7 @@ namespace easy2d
~String(); ~String();
// 获取字符串长度 // 获取字符串长度
int GetLength() const; int Length() const;
// 获取该字符串的 Hash 值 // 获取该字符串的 Hash 值
size_t GetHash() const; size_t GetHash() const;

View File

@ -183,7 +183,7 @@ void easy2d::Graphics::DrawDebugInfo()
ThrowIfFailed( ThrowIfFailed(
write_factory_->CreateTextLayout( write_factory_->CreateTextLayout(
(const wchar_t *)fps_text, (const wchar_t *)fps_text,
(UINT32)fps_text.GetLength(), (UINT32)fps_text.Length(),
fps_text_format_, fps_text_format_,
0, 0,
0, 0,

View File

@ -400,7 +400,7 @@ void easy2d::Text::CreateLayout()
return; return;
} }
UINT32 length = (UINT32)text_.GetLength(); UINT32 length = (UINT32)text_.Length();
auto writeFactory = Device::GetGraphics()->GetWriteFactory(); auto writeFactory = Device::GetGraphics()->GetWriteFactory();
// 对文本自动换行情况下进行处理 // 对文本自动换行情况下进行处理

View File

@ -26,13 +26,11 @@ std::list<easy2d::String> easy2d::File::search_paths_;
easy2d::File::File() easy2d::File::File()
: file_path_() : file_path_()
, attributes_(0)
{ {
} }
easy2d::File::File(const String & file_name) easy2d::File::File(const String & file_name)
: file_path_(file_name) : file_path_(file_name)
, attributes_(0)
{ {
this->Open(file_name); this->Open(file_name);
} }
@ -46,13 +44,10 @@ bool easy2d::File::Open(const String & file_name)
if (file_name.IsEmpty()) if (file_name.IsEmpty())
return false; return false;
auto FindFile = [=](const String & path) -> bool auto FindFile = [](const String & path) -> bool
{ {
if (::_waccess((const wchar_t*)path, 0) == 0) if (::PathFileExists((const wchar_t*)path))
{
attributes_ = ::GetFileAttributes((LPCTSTR)path);
return true; return true;
}
return false; return false;
}; };
@ -61,15 +56,13 @@ bool easy2d::File::Open(const String & file_name)
file_path_ = file_name; file_path_ = file_name;
return true; return true;
} }
else
for (const auto& path : search_paths_)
{ {
for (const auto& path : search_paths_) if (FindFile(path + file_name))
{ {
if (FindFile(path + file_name)) file_path_ = path + file_name;
{ return true;
file_path_ = path + file_name;
return true;
}
} }
} }
return false; return false;
@ -77,12 +70,9 @@ bool easy2d::File::Open(const String & file_name)
bool easy2d::File::Exists() const bool easy2d::File::Exists() const
{ {
return ::_waccess((const wchar_t*)file_path_, 0) == 0; if (::PathFileExists(static_cast<LPCWSTR>(file_path_)))
} return true;
return false;
bool easy2d::File::IsFolder() const
{
return (attributes_ & FILE_ATTRIBUTE_DIRECTORY) != 0;
} }
const easy2d::String& easy2d::File::GetPath() const const easy2d::String& easy2d::File::GetPath() const
@ -108,15 +98,15 @@ easy2d::String easy2d::File::GetExtension() const
bool easy2d::File::Delete() bool easy2d::File::Delete()
{ {
if (::DeleteFile((LPCWSTR)file_path_)) if (::DeleteFile(static_cast<LPCWSTR>(file_path_)))
return true; return true;
return false; return false;
} }
easy2d::File easy2d::File::Extract(int resource_name, const String & resource_type, const String& dest_file_name) easy2d::File easy2d::File::Extract(const Resource& res, const String& dest_file_name)
{ {
// ´´½¨Îļþ File file;
HANDLE file = ::CreateFile( HANDLE file_handle = ::CreateFile(
static_cast<LPCWSTR>(dest_file_name), static_cast<LPCWSTR>(dest_file_name),
GENERIC_WRITE, GENERIC_WRITE,
NULL, NULL,
@ -126,11 +116,11 @@ easy2d::File easy2d::File::Extract(int resource_name, const String & resource_ty
NULL NULL
); );
if (file == INVALID_HANDLE_VALUE) if (file_handle == INVALID_HANDLE_VALUE)
return std::move(File()); return std::move(file);
// 查找资源文件中、加载资源到内存、得到资源大小 // 查找资源文件中、加载资源到内存、得到资源大小
HRSRC res = ::FindResource(NULL, MAKEINTRESOURCE(resource_name), (LPCWSTR)resource_type); HRSRC res = ::FindResource(NULL, MAKEINTRESOURCE(res.id), static_cast<LPCWSTR>(res.type));
HGLOBAL res_data = ::LoadResource(NULL, res); HGLOBAL res_data = ::LoadResource(NULL, res);
DWORD res_size = ::SizeofResource(NULL, res); DWORD res_size = ::SizeofResource(NULL, res);
@ -138,23 +128,26 @@ easy2d::File easy2d::File::Extract(int resource_name, const String & resource_ty
{ {
// 写入文件 // 写入文件
DWORD written_bytes = 0; DWORD written_bytes = 0;
::WriteFile(file, res_data, res_size, &written_bytes, NULL); ::WriteFile(file_handle, res_data, res_size, &written_bytes, NULL);
::CloseHandle(file); ::CloseHandle(file_handle);
return File(dest_file_name);
file.Open(dest_file_name);
} }
else else
{ {
::CloseHandle(file); ::CloseHandle(file_handle);
::DeleteFile(static_cast<LPCWSTR>(dest_file_name)); ::DeleteFile(static_cast<LPCWSTR>(dest_file_name));
return std::move(File());
} }
::FreeResource(res_data);
return std::move(file);
} }
void easy2d::File::AddSearchPath(const String & path) void easy2d::File::AddSearchPath(const String & path)
{ {
String tmp = path; String tmp = path;
tmp.Replace(L"/", L"\\"); tmp.Replace(L"/", L"\\");
if (tmp.At(tmp.GetLength() - 1) != L'\\') if (tmp.At(tmp.Length() - 1) != L'\\')
{ {
tmp << L"\\"; tmp << L"\\";
} }
@ -165,31 +158,6 @@ void easy2d::File::AddSearchPath(const String & path)
} }
} }
bool easy2d::File::CreateFolder(const String & dir_path)
{
if (dir_path.IsEmpty() || dir_path.GetLength() >= MAX_PATH)
return false;
wchar_t tmp_dir_path[MAX_PATH] = { 0 };
int length = dir_path.GetLength();
for (int i = 0; i < length; ++i)
{
tmp_dir_path[i] = dir_path.At(i);
if (tmp_dir_path[i] == L'\\' || tmp_dir_path[i] == L'/' || i == (length - 1))
{
if (::_waccess(tmp_dir_path, 0) != 0)
{
if (::_wmkdir(tmp_dir_path) != 0)
{
return false;
}
}
}
}
return true;
}
easy2d::File easy2d::File::ShowOpenDialog(const String & title, const String & filter) easy2d::File easy2d::File::ShowOpenDialog(const String & title, const String & filter)
{ {
String file_path; String file_path;

View File

@ -22,6 +22,37 @@
#include "..\e2dmodule.h" #include "..\e2dmodule.h"
#include <shlobj.h> #include <shlobj.h>
namespace
{
// ´´½¨Ö¸¶¨Îļþ¼Ð
bool CreateFolder(const easy2d::String & dir_path)
{
if (dir_path.IsEmpty() || dir_path.Length() >= MAX_PATH)
return false;
wchar_t tmp_dir_path[MAX_PATH] = { 0 };
int length = dir_path.Length();
for (int i = 0; i < length; ++i)
{
tmp_dir_path[i] = dir_path.At(i);
if (tmp_dir_path[i] == L'\\' || tmp_dir_path[i] == L'/' || i == (length - 1))
{
if (::_waccess(tmp_dir_path, 0) != 0)
{
if (::_wmkdir(tmp_dir_path) != 0)
{
return false;
}
}
}
}
return true;
}
}
const easy2d::String& easy2d::Path::GetDataPath() const easy2d::String& easy2d::Path::GetDataPath()
{ {
static String data_path; static String data_path;
@ -37,7 +68,7 @@ const easy2d::String& easy2d::Path::GetDataPath()
data_path = local_app_data_path + L"\\Easy2DGameData\\" << folder_name << L"\\"; data_path = local_app_data_path + L"\\Easy2DGameData\\" << folder_name << L"\\";
File file(data_path); File file(data_path);
if (!file.Exists() && !File::CreateFolder(data_path)) if (!file.Exists() && !CreateFolder(data_path))
{ {
data_path = L""; data_path = L"";
} }
@ -62,7 +93,7 @@ const easy2d::String& easy2d::Path::GetTemporaryPath()
temp_path << path << L"\\Easy2DGameTemp\\" << folder_name << L"\\"; temp_path << path << L"\\Easy2DGameTemp\\" << folder_name << L"\\";
File file(temp_path); File file(temp_path);
if (!file.Exists() && !File::CreateFolder(temp_path)) if (!file.Exists() && !CreateFolder(temp_path))
{ {
temp_path = L""; temp_path = L"";
} }

View File

@ -363,7 +363,7 @@ bool easy2d::String::IsEmpty() const
return string_.empty(); return string_.empty();
} }
int easy2d::String::GetLength() const int easy2d::String::Length() const
{ {
return static_cast<int>(string_.size()); return static_cast<int>(string_.size());
} }