2018-09-05 13:33:39 +08:00
|
|
|
|
#include "..\e2dtool.h"
|
2018-09-05 13:17:07 +08:00
|
|
|
|
#include "..\e2dmodule.h"
|
2018-09-02 14:03:04 +08:00
|
|
|
|
#include <shobjidl.h>
|
2018-07-08 02:41:44 +08:00
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
std::list<e2d::String> e2d::File::search_paths_;
|
2018-07-08 02:41:44 +08:00
|
|
|
|
|
|
|
|
|
|
e2d::File::File()
|
2018-09-04 22:42:34 +08:00
|
|
|
|
: file_path_()
|
|
|
|
|
|
, attributes_(0)
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
e2d::File::File(const String & file_name)
|
|
|
|
|
|
: file_path_(file_name)
|
|
|
|
|
|
, attributes_(0)
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
this->Open(file_name);
|
2018-07-08 02:41:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
e2d::File::~File()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
bool e2d::File::Open(const String & file_name)
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
|
|
|
|
|
auto FindFile = [=](const String & path) -> bool
|
|
|
|
|
|
{
|
|
|
|
|
|
if (::_waccess((const wchar_t*)path, 0) == 0)
|
|
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
attributes_ = ::GetFileAttributes((LPCTSTR)path);
|
2018-07-08 02:41:44 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (FindFile(file_name))
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
file_path_ = file_name;
|
2018-07-08 02:41:44 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
for (const auto& resPath : search_paths_)
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (FindFile(resPath + file_name))
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
file_path_ = resPath + file_name;
|
2018-07-08 02:41:44 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
bool e2d::File::Exists() const
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
return ::_waccess((const wchar_t*)file_path_, 0) == 0;
|
2018-07-08 02:41:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
bool e2d::File::IsFolder() const
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
return (attributes_ & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
2018-07-08 02:41:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
const e2d::String& e2d::File::GetPath() const
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
return file_path_;
|
2018-07-08 02:41:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
e2d::String e2d::File::GetExtension() const
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
|
|
|
|
|
String fileExtension;
|
|
|
|
|
|
// <20>ҵ<EFBFBD><D2B5>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> '.' <20><>λ<EFBFBD><CEBB>
|
2018-09-04 22:42:34 +08:00
|
|
|
|
size_t pos = std::wstring(file_path_).find_last_of(L'.');
|
2018-07-08 02:41:44 +08:00
|
|
|
|
// <20>ж<EFBFBD> pos <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>Чλ<D0A7><CEBB>
|
|
|
|
|
|
if (pos != std::wstring::npos)
|
|
|
|
|
|
{
|
|
|
|
|
|
// <20><>ȡ<EFBFBD><C8A1>չ<EFBFBD><D5B9>
|
2018-09-04 22:42:34 +08:00
|
|
|
|
fileExtension = file_path_.Subtract(static_cast<int>(pos));
|
2018-07-08 02:41:44 +08:00
|
|
|
|
// ת<><D7AA>ΪСд<D0A1><D0B4>ĸ
|
2018-09-04 22:42:34 +08:00
|
|
|
|
fileExtension = fileExtension.ToLower();
|
2018-07-08 02:41:44 +08:00
|
|
|
|
}
|
2018-08-28 00:06:10 +08:00
|
|
|
|
return std::move(fileExtension);
|
2018-07-08 02:41:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
bool e2d::File::Delete()
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (::DeleteFile((LPCWSTR)file_path_))
|
2018-07-08 02:41:44 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
e2d::File e2d::File::Extract(int resource_name, const String & resource_type, const String& dest_file_name)
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
|
2018-09-04 22:42:34 +08:00
|
|
|
|
HANDLE hFile = ::CreateFile(
|
|
|
|
|
|
static_cast<LPCWSTR>(dest_file_name),
|
|
|
|
|
|
GENERIC_WRITE,
|
|
|
|
|
|
NULL,
|
|
|
|
|
|
NULL,
|
|
|
|
|
|
CREATE_ALWAYS,
|
|
|
|
|
|
FILE_ATTRIBUTE_TEMPORARY,
|
|
|
|
|
|
NULL
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2018-07-08 02:41:44 +08:00
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
|
|
return std::move(File());
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD>ļ<EFBFBD><C4BC>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD>ڴ桢<DAB4>õ<EFBFBD><C3B5><EFBFBD>Դ<EFBFBD><D4B4>С
|
2018-09-04 22:42:34 +08:00
|
|
|
|
HRSRC hRes = ::FindResource(NULL, MAKEINTRESOURCE(resource_name), (LPCWSTR)resource_type);
|
2018-07-08 02:41:44 +08:00
|
|
|
|
HGLOBAL hMem = ::LoadResource(NULL, hRes);
|
|
|
|
|
|
DWORD dwSize = ::SizeofResource(NULL, hRes);
|
|
|
|
|
|
|
|
|
|
|
|
if (hRes && hMem && dwSize)
|
|
|
|
|
|
{
|
|
|
|
|
|
// д<><D0B4><EFBFBD>ļ<EFBFBD>
|
|
|
|
|
|
DWORD dwWrite = 0;
|
|
|
|
|
|
::WriteFile(hFile, hMem, dwSize, &dwWrite, NULL);
|
|
|
|
|
|
::CloseHandle(hFile);
|
2018-09-04 22:42:34 +08:00
|
|
|
|
return File(dest_file_name);
|
2018-07-08 02:41:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
::CloseHandle(hFile);
|
2018-09-04 22:42:34 +08:00
|
|
|
|
::DeleteFile(static_cast<LPCWSTR>(dest_file_name));
|
2018-07-08 02:41:44 +08:00
|
|
|
|
return std::move(File());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
void e2d::File::AddSearchPath(const String & path)
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
|
|
|
|
|
String tmp = path;
|
2018-09-04 22:42:34 +08:00
|
|
|
|
tmp.Replace(L"/", L"\\");
|
|
|
|
|
|
if (tmp[tmp.GetLength() - 1] != L'\\')
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
|
|
|
|
|
tmp << L"\\";
|
|
|
|
|
|
}
|
2018-09-04 22:42:34 +08:00
|
|
|
|
auto iter = std::find(search_paths_.cbegin(), search_paths_.cend(), tmp);
|
|
|
|
|
|
if (iter == search_paths_.cend())
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
search_paths_.push_front(path);
|
2018-07-08 02:41:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
bool e2d::File::CreateFolder(const String & dir_path)
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (dir_path.IsEmpty() || dir_path.GetLength() >= MAX_PATH)
|
2018-07-08 02:41:44 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
wchar_t tmpDirPath[_MAX_PATH] = { 0 };
|
2018-09-04 22:42:34 +08:00
|
|
|
|
int length = dir_path.GetLength();
|
2018-07-08 02:41:44 +08:00
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < length; ++i)
|
|
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
tmpDirPath[i] = dir_path.At(i);
|
2018-07-08 02:41:44 +08:00
|
|
|
|
if (tmpDirPath[i] == L'\\' || tmpDirPath[i] == L'/' || i == (length - 1))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (::_waccess(tmpDirPath, 0) != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (::_wmkdir(tmpDirPath) != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
e2d::File e2d::File::ShowOpenDialog(const String & title, const String & filter)
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
String file_path;
|
2018-09-02 14:03:04 +08:00
|
|
|
|
HRESULT hr = ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
|
|
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
|
|
|
|
|
IFileOpenDialog *pFileOpen;
|
|
|
|
|
|
|
|
|
|
|
|
hr = ::CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
|
|
|
|
|
|
IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));
|
|
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (!title.IsEmpty())
|
2018-09-02 14:03:04 +08:00
|
|
|
|
{
|
|
|
|
|
|
pFileOpen->SetTitle(LPCWSTR(title));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (!filter.IsEmpty())
|
2018-09-02 14:03:04 +08:00
|
|
|
|
{
|
|
|
|
|
|
COMDLG_FILTERSPEC rgSpec[] =
|
|
|
|
|
|
{
|
|
|
|
|
|
{ L"", LPCWSTR(filter) }
|
|
|
|
|
|
};
|
|
|
|
|
|
pFileOpen->SetFileTypes(1, rgSpec);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
COMDLG_FILTERSPEC rgSpec[] =
|
|
|
|
|
|
{
|
|
|
|
|
|
{ L"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>", L"*.*" }
|
|
|
|
|
|
};
|
|
|
|
|
|
pFileOpen->SetFileTypes(1, rgSpec);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
Game::GetInstance()->Pause();
|
2018-09-02 14:03:04 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
HWND hWnd = Window::GetInstance()->GetHWnd();
|
2018-09-02 14:03:04 +08:00
|
|
|
|
hr = pFileOpen->Show(hWnd);
|
|
|
|
|
|
}
|
2018-09-04 22:42:34 +08:00
|
|
|
|
Game::GetInstance()->Resume();
|
2018-09-02 14:03:04 +08:00
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
|
|
|
|
|
IShellItem *pItem;
|
|
|
|
|
|
hr = pFileOpen->GetResult(&pItem);
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
|
|
|
|
|
PWSTR pszFilePath;
|
|
|
|
|
|
hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
|
|
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
file_path = pszFilePath;
|
2018-09-02 14:03:04 +08:00
|
|
|
|
::CoTaskMemFree(pszFilePath);
|
|
|
|
|
|
}
|
|
|
|
|
|
pItem->Release();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
pFileOpen->Release();
|
|
|
|
|
|
}
|
|
|
|
|
|
::CoUninitialize();
|
|
|
|
|
|
}
|
2018-09-04 22:42:34 +08:00
|
|
|
|
return std::move(File(file_path));
|
2018-09-02 14:03:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
e2d::File e2d::File::ShowSaveDialog(const String & title, const String& def_file, const String & def_ext)
|
2018-09-02 14:03:04 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
String file_path;
|
2018-09-02 14:03:04 +08:00
|
|
|
|
HRESULT hr = ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
|
|
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
2018-09-02 14:03:04 +08:00
|
|
|
|
IFileSaveDialog *pFileSave;
|
|
|
|
|
|
|
|
|
|
|
|
hr = ::CoCreateInstance(CLSID_FileSaveDialog, NULL, CLSCTX_ALL,
|
|
|
|
|
|
IID_IFileSaveDialog, reinterpret_cast<void**>(&pFileSave));
|
|
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (!title.IsEmpty())
|
2018-09-02 14:03:04 +08:00
|
|
|
|
{
|
|
|
|
|
|
pFileSave->SetTitle(LPCWSTR(title));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (!def_file.IsEmpty())
|
2018-09-02 14:03:04 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
pFileSave->SetFileName(LPCWSTR(def_file));
|
2018-09-02 14:03:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (!def_ext.IsEmpty())
|
2018-09-02 14:03:04 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
pFileSave->SetDefaultExtension(LPCWSTR(def_ext));
|
2018-09-02 14:03:04 +08:00
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
String spec = L"*." + def_ext;
|
2018-09-02 14:03:04 +08:00
|
|
|
|
COMDLG_FILTERSPEC rgSpec[] =
|
|
|
|
|
|
{
|
|
|
|
|
|
{ L"", LPCWSTR(spec) }
|
|
|
|
|
|
};
|
|
|
|
|
|
pFileSave->SetFileTypes(1, rgSpec);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
COMDLG_FILTERSPEC rgSpec[] =
|
|
|
|
|
|
{
|
|
|
|
|
|
{ L"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>", L"*.*" }
|
|
|
|
|
|
};
|
|
|
|
|
|
pFileSave->SetFileTypes(1, rgSpec);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
Game::GetInstance()->Pause();
|
2018-09-02 14:03:04 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
HWND hWnd = Window::GetInstance()->GetHWnd();
|
2018-09-02 14:03:04 +08:00
|
|
|
|
hr = pFileSave->Show(hWnd);
|
|
|
|
|
|
}
|
2018-09-04 22:42:34 +08:00
|
|
|
|
Game::GetInstance()->Resume();
|
2018-09-02 14:03:04 +08:00
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
|
|
|
|
|
IShellItem *pItem;
|
|
|
|
|
|
hr = pFileSave->GetResult(&pItem);
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
|
|
|
|
|
PWSTR pszFilePath;
|
|
|
|
|
|
hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
|
|
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
file_path = pszFilePath;
|
2018-09-02 14:03:04 +08:00
|
|
|
|
::CoTaskMemFree(pszFilePath);
|
|
|
|
|
|
}
|
|
|
|
|
|
pItem->Release();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
pFileSave->Release();
|
|
|
|
|
|
}
|
|
|
|
|
|
::CoUninitialize();
|
2018-07-08 02:41:44 +08:00
|
|
|
|
}
|
2018-09-04 22:42:34 +08:00
|
|
|
|
return std::move(File(file_path));
|
2018-07-08 02:41:44 +08:00
|
|
|
|
}
|