change: refactory File class.
This commit is contained in:
parent
709f2bfe8d
commit
4ed453468d
|
|
@ -306,12 +306,9 @@ namespace easy2d
|
|||
const String& file_name
|
||||
);
|
||||
|
||||
// 文件或文件夹是否存在
|
||||
// 文件是否存在
|
||||
bool Exists() const;
|
||||
|
||||
// 是否是文件夹
|
||||
bool IsFolder() const;
|
||||
|
||||
// 删除文件
|
||||
bool Delete();
|
||||
|
||||
|
|
@ -323,8 +320,7 @@ namespace easy2d
|
|||
|
||||
// 释放资源到临时文件目录
|
||||
static File Extract(
|
||||
int resource_name, /* 资源名称 */
|
||||
const String& resource_type, /* 资源类型 */
|
||||
const Resource& res, /* 资源 */
|
||||
const String& dest_file_name /* 目标文件名 */
|
||||
);
|
||||
|
||||
|
|
@ -333,11 +329,6 @@ namespace easy2d
|
|||
const String& path
|
||||
);
|
||||
|
||||
// 创建文件夹
|
||||
static bool CreateFolder(
|
||||
const String& dir_path /* 文件夹路径 */
|
||||
);
|
||||
|
||||
// 弹出打开文件对话框
|
||||
static File ShowOpenDialog(
|
||||
const String& title = L"打开", /* 对话框标题 */
|
||||
|
|
@ -352,8 +343,7 @@ namespace easy2d
|
|||
);
|
||||
|
||||
protected:
|
||||
DWORD attributes_;
|
||||
String file_path_;
|
||||
String file_path_;
|
||||
|
||||
static std::list<String> search_paths_;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -179,7 +179,7 @@ namespace easy2d
|
|||
~String();
|
||||
|
||||
// 获取字符串长度
|
||||
int GetLength() const;
|
||||
int Length() const;
|
||||
|
||||
// 获取该字符串的 Hash 值
|
||||
size_t GetHash() const;
|
||||
|
|
|
|||
|
|
@ -183,7 +183,7 @@ void easy2d::Graphics::DrawDebugInfo()
|
|||
ThrowIfFailed(
|
||||
write_factory_->CreateTextLayout(
|
||||
(const wchar_t *)fps_text,
|
||||
(UINT32)fps_text.GetLength(),
|
||||
(UINT32)fps_text.Length(),
|
||||
fps_text_format_,
|
||||
0,
|
||||
0,
|
||||
|
|
|
|||
|
|
@ -400,7 +400,7 @@ void easy2d::Text::CreateLayout()
|
|||
return;
|
||||
}
|
||||
|
||||
UINT32 length = (UINT32)text_.GetLength();
|
||||
UINT32 length = (UINT32)text_.Length();
|
||||
auto writeFactory = Device::GetGraphics()->GetWriteFactory();
|
||||
|
||||
// 对文本自动换行情况下进行处理
|
||||
|
|
|
|||
|
|
@ -26,13 +26,11 @@ std::list<easy2d::String> easy2d::File::search_paths_;
|
|||
|
||||
easy2d::File::File()
|
||||
: file_path_()
|
||||
, attributes_(0)
|
||||
{
|
||||
}
|
||||
|
||||
easy2d::File::File(const String & file_name)
|
||||
: file_path_(file_name)
|
||||
, attributes_(0)
|
||||
{
|
||||
this->Open(file_name);
|
||||
}
|
||||
|
|
@ -46,13 +44,10 @@ bool easy2d::File::Open(const String & file_name)
|
|||
if (file_name.IsEmpty())
|
||||
return false;
|
||||
|
||||
auto FindFile = [=](const String & path) -> bool
|
||||
auto FindFile = [](const String & path) -> bool
|
||||
{
|
||||
if (::_waccess((const wchar_t*)path, 0) == 0)
|
||||
{
|
||||
attributes_ = ::GetFileAttributes((LPCTSTR)path);
|
||||
if (::PathFileExists((const wchar_t*)path))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
|
|
@ -61,15 +56,13 @@ bool easy2d::File::Open(const String & file_name)
|
|||
file_path_ = file_name;
|
||||
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;
|
||||
|
|
@ -77,12 +70,9 @@ bool easy2d::File::Open(const String & file_name)
|
|||
|
||||
bool easy2d::File::Exists() const
|
||||
{
|
||||
return ::_waccess((const wchar_t*)file_path_, 0) == 0;
|
||||
}
|
||||
|
||||
bool easy2d::File::IsFolder() const
|
||||
{
|
||||
return (attributes_ & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
||||
if (::PathFileExists(static_cast<LPCWSTR>(file_path_)))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
const easy2d::String& easy2d::File::GetPath() const
|
||||
|
|
@ -108,15 +98,15 @@ easy2d::String easy2d::File::GetExtension() const
|
|||
|
||||
bool easy2d::File::Delete()
|
||||
{
|
||||
if (::DeleteFile((LPCWSTR)file_path_))
|
||||
if (::DeleteFile(static_cast<LPCWSTR>(file_path_)))
|
||||
return true;
|
||||
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)
|
||||
{
|
||||
// ´´½¨Îļþ
|
||||
HANDLE file = ::CreateFile(
|
||||
File file;
|
||||
HANDLE file_handle = ::CreateFile(
|
||||
static_cast<LPCWSTR>(dest_file_name),
|
||||
GENERIC_WRITE,
|
||||
NULL,
|
||||
|
|
@ -126,11 +116,11 @@ easy2d::File easy2d::File::Extract(int resource_name, const String & resource_ty
|
|||
NULL
|
||||
);
|
||||
|
||||
if (file == INVALID_HANDLE_VALUE)
|
||||
return std::move(File());
|
||||
if (file_handle == INVALID_HANDLE_VALUE)
|
||||
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);
|
||||
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;
|
||||
::WriteFile(file, res_data, res_size, &written_bytes, NULL);
|
||||
::CloseHandle(file);
|
||||
return File(dest_file_name);
|
||||
::WriteFile(file_handle, res_data, res_size, &written_bytes, NULL);
|
||||
::CloseHandle(file_handle);
|
||||
|
||||
file.Open(dest_file_name);
|
||||
}
|
||||
else
|
||||
{
|
||||
::CloseHandle(file);
|
||||
::CloseHandle(file_handle);
|
||||
::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)
|
||||
{
|
||||
String tmp = path;
|
||||
tmp.Replace(L"/", L"\\");
|
||||
if (tmp.At(tmp.GetLength() - 1) != L'\\')
|
||||
if (tmp.At(tmp.Length() - 1) != 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)
|
||||
{
|
||||
String file_path;
|
||||
|
|
|
|||
|
|
@ -22,6 +22,37 @@
|
|||
#include "..\e2dmodule.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()
|
||||
{
|
||||
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"\\";
|
||||
|
||||
File file(data_path);
|
||||
if (!file.Exists() && !File::CreateFolder(data_path))
|
||||
if (!file.Exists() && !CreateFolder(data_path))
|
||||
{
|
||||
data_path = L"";
|
||||
}
|
||||
|
|
@ -62,7 +93,7 @@ const easy2d::String& easy2d::Path::GetTemporaryPath()
|
|||
temp_path << path << L"\\Easy2DGameTemp\\" << folder_name << L"\\";
|
||||
|
||||
File file(temp_path);
|
||||
if (!file.Exists() && !File::CreateFolder(temp_path))
|
||||
if (!file.Exists() && !CreateFolder(temp_path))
|
||||
{
|
||||
temp_path = L"";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -363,7 +363,7 @@ bool easy2d::String::IsEmpty() const
|
|||
return string_.empty();
|
||||
}
|
||||
|
||||
int easy2d::String::GetLength() const
|
||||
int easy2d::String::Length() const
|
||||
{
|
||||
return static_cast<int>(string_.size());
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue