2018-10-03 22:02:46 +08:00
|
|
|
|
// Copyright (c) 2016-2018 Easy2D - Nomango
|
|
|
|
|
|
//
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
|
//
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
|
//
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
|
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-10-16 14:13:15 +08:00
|
|
|
|
std::list<easy2d::String> easy2d::File::search_paths_;
|
2018-07-08 02:41:44 +08:00
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
easy2d::File::File()
|
2018-09-04 22:42:34 +08:00
|
|
|
|
: file_path_()
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
easy2d::File::File(const String & file_name)
|
2018-09-04 22:42:34 +08:00
|
|
|
|
: file_path_(file_name)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
easy2d::File::~File()
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
bool easy2d::File::Open(const String & file_name)
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
2018-09-07 23:47:21 +08:00
|
|
|
|
if (file_name.IsEmpty())
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
2018-10-16 14:57:28 +08:00
|
|
|
|
auto FindFile = [](const String & path) -> bool
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
2018-10-16 14:57:28 +08:00
|
|
|
|
if (::PathFileExists((const wchar_t*)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;
|
|
|
|
|
|
}
|
2018-10-16 14:57:28 +08:00
|
|
|
|
|
|
|
|
|
|
for (const auto& path : search_paths_)
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
2018-10-16 14:57:28 +08:00
|
|
|
|
if (FindFile(path + file_name))
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
2018-10-16 14:57:28 +08:00
|
|
|
|
file_path_ = path + file_name;
|
|
|
|
|
|
return true;
|
2018-07-08 02:41:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
bool easy2d::File::Exists() const
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
2018-10-16 14:57:28 +08:00
|
|
|
|
if (::PathFileExists(static_cast<LPCWSTR>(file_path_)))
|
|
|
|
|
|
return true;
|
|
|
|
|
|
return false;
|
2018-07-08 02:41:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
const easy2d::String& easy2d::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-10-16 14:13:15 +08:00
|
|
|
|
easy2d::String easy2d::File::GetExtension() const
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
2018-09-11 17:54:21 +08:00
|
|
|
|
String file_ext;
|
2018-07-08 02:41:44 +08:00
|
|
|
|
// <20>ҵ<EFBFBD><D2B5>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> '.' <20><>λ<EFBFBD><CEBB>
|
2018-09-11 17:54:21 +08:00
|
|
|
|
size_t pos = file_path_.operator std::wstring().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-11 17:54:21 +08:00
|
|
|
|
file_ext = file_path_.Subtract(static_cast<int>(pos));
|
2018-07-08 02:41:44 +08:00
|
|
|
|
// ת<><D7AA>ΪСд<D0A1><D0B4>ĸ
|
2018-09-11 17:54:21 +08:00
|
|
|
|
file_ext = file_ext.ToLower();
|
2018-07-08 02:41:44 +08:00
|
|
|
|
}
|
2018-09-11 17:54:21 +08:00
|
|
|
|
return std::move(file_ext);
|
2018-07-08 02:41:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
bool easy2d::File::Delete()
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
2018-10-16 14:57:28 +08:00
|
|
|
|
if (::DeleteFile(static_cast<LPCWSTR>(file_path_)))
|
2018-07-08 02:41:44 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:57:28 +08:00
|
|
|
|
easy2d::File easy2d::File::Extract(const Resource& res, const String& dest_file_name)
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
2018-10-16 14:57:28 +08:00
|
|
|
|
File file;
|
|
|
|
|
|
HANDLE file_handle = ::CreateFile(
|
2018-09-04 22:42:34 +08:00
|
|
|
|
static_cast<LPCWSTR>(dest_file_name),
|
|
|
|
|
|
GENERIC_WRITE,
|
|
|
|
|
|
NULL,
|
|
|
|
|
|
NULL,
|
|
|
|
|
|
CREATE_ALWAYS,
|
|
|
|
|
|
FILE_ATTRIBUTE_TEMPORARY,
|
|
|
|
|
|
NULL
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2018-10-16 14:57:28 +08:00
|
|
|
|
if (file_handle == INVALID_HANDLE_VALUE)
|
|
|
|
|
|
return std::move(file);
|
2018-07-08 02:41:44 +08:00
|
|
|
|
|
|
|
|
|
|
// <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-10-17 00:16:26 +08:00
|
|
|
|
HRSRC res_info = ::FindResource(NULL, MAKEINTRESOURCE(res.id), static_cast<LPCWSTR>(res.type));
|
|
|
|
|
|
HGLOBAL res_data = ::LoadResource(NULL, res_info);
|
|
|
|
|
|
DWORD res_size = ::SizeofResource(NULL, res_info);
|
2018-07-08 02:41:44 +08:00
|
|
|
|
|
2018-10-17 00:16:26 +08:00
|
|
|
|
if (res_info && res_data && res_size)
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
|
|
|
|
|
// д<><D0B4><EFBFBD>ļ<EFBFBD>
|
2018-09-11 17:54:21 +08:00
|
|
|
|
DWORD written_bytes = 0;
|
2018-10-16 14:57:28 +08:00
|
|
|
|
::WriteFile(file_handle, res_data, res_size, &written_bytes, NULL);
|
|
|
|
|
|
::CloseHandle(file_handle);
|
|
|
|
|
|
|
|
|
|
|
|
file.Open(dest_file_name);
|
2018-07-08 02:41:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2018-10-16 14:57:28 +08:00
|
|
|
|
::CloseHandle(file_handle);
|
2018-09-04 22:42:34 +08:00
|
|
|
|
::DeleteFile(static_cast<LPCWSTR>(dest_file_name));
|
2018-07-08 02:41:44 +08:00
|
|
|
|
}
|
2018-10-16 14:57:28 +08:00
|
|
|
|
|
|
|
|
|
|
::FreeResource(res_data);
|
|
|
|
|
|
return std::move(file);
|
2018-07-08 02:41:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
void easy2d::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"\\");
|
2018-10-16 14:57:28 +08:00
|
|
|
|
if (tmp.At(tmp.Length() - 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-10-16 14:13:15 +08:00
|
|
|
|
easy2d::File easy2d::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))
|
|
|
|
|
|
{
|
2018-09-07 00:28:54 +08:00
|
|
|
|
IFileOpenDialog *file_open;
|
2018-09-02 14:03:04 +08:00
|
|
|
|
|
|
|
|
|
|
hr = ::CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
|
2018-09-07 00:28:54 +08:00
|
|
|
|
IID_IFileOpenDialog, reinterpret_cast<void**>(&file_open));
|
2018-09-02 14:03:04 +08:00
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (!title.IsEmpty())
|
2018-09-02 14:03:04 +08:00
|
|
|
|
{
|
2018-09-07 00:28:54 +08:00
|
|
|
|
file_open->SetTitle(LPCWSTR(title));
|
2018-09-02 14:03:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (!filter.IsEmpty())
|
2018-09-02 14:03:04 +08:00
|
|
|
|
{
|
2018-09-07 00:28:54 +08:00
|
|
|
|
COMDLG_FILTERSPEC spec[] =
|
2018-09-02 14:03:04 +08:00
|
|
|
|
{
|
|
|
|
|
|
{ L"", LPCWSTR(filter) }
|
|
|
|
|
|
};
|
2018-09-07 00:28:54 +08:00
|
|
|
|
file_open->SetFileTypes(1, spec);
|
2018-09-02 14:03:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2018-09-07 00:28:54 +08:00
|
|
|
|
COMDLG_FILTERSPEC spec[] =
|
2018-09-02 14:03:04 +08:00
|
|
|
|
{
|
|
|
|
|
|
{ L"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>", L"*.*" }
|
|
|
|
|
|
};
|
2018-09-07 00:28:54 +08:00
|
|
|
|
file_open->SetFileTypes(1, spec);
|
2018-09-02 14:03:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-03 18:04:04 +08:00
|
|
|
|
hr = file_open->Show(nullptr);
|
2018-09-02 14:03:04 +08:00
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
2018-09-07 00:28:54 +08:00
|
|
|
|
IShellItem *item;
|
|
|
|
|
|
hr = file_open->GetResult(&item);
|
2018-09-02 14:03:04 +08:00
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
2018-09-07 00:28:54 +08:00
|
|
|
|
PWSTR str_file_path;
|
|
|
|
|
|
hr = item->GetDisplayName(SIGDN_FILESYSPATH, &str_file_path);
|
2018-09-02 14:03:04 +08:00
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
2018-09-07 00:28:54 +08:00
|
|
|
|
file_path = str_file_path;
|
|
|
|
|
|
::CoTaskMemFree(str_file_path);
|
2018-09-02 14:03:04 +08:00
|
|
|
|
}
|
2018-09-07 00:28:54 +08:00
|
|
|
|
item->Release();
|
2018-09-02 14:03:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-09-07 00:28:54 +08:00
|
|
|
|
file_open->Release();
|
2018-09-02 14:03:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
::CoUninitialize();
|
|
|
|
|
|
}
|
2018-09-04 22:42:34 +08:00
|
|
|
|
return std::move(File(file_path));
|
2018-09-02 14:03:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
easy2d::File easy2d::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-07 00:28:54 +08:00
|
|
|
|
IFileSaveDialog *file_save;
|
2018-09-02 14:03:04 +08:00
|
|
|
|
|
|
|
|
|
|
hr = ::CoCreateInstance(CLSID_FileSaveDialog, NULL, CLSCTX_ALL,
|
2018-09-07 00:28:54 +08:00
|
|
|
|
IID_IFileSaveDialog, reinterpret_cast<void**>(&file_save));
|
2018-09-02 14:03:04 +08:00
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (!title.IsEmpty())
|
2018-09-02 14:03:04 +08:00
|
|
|
|
{
|
2018-09-07 00:28:54 +08:00
|
|
|
|
file_save->SetTitle(LPCWSTR(title));
|
2018-09-02 14:03:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (!def_file.IsEmpty())
|
2018-09-02 14:03:04 +08:00
|
|
|
|
{
|
2018-09-07 00:28:54 +08:00
|
|
|
|
file_save->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-07 00:28:54 +08:00
|
|
|
|
file_save->SetDefaultExtension(LPCWSTR(def_ext));
|
2018-09-02 14:03:04 +08:00
|
|
|
|
|
2018-09-07 00:28:54 +08:00
|
|
|
|
String ext = L"*." + def_ext;
|
|
|
|
|
|
COMDLG_FILTERSPEC spec[] =
|
2018-09-02 14:03:04 +08:00
|
|
|
|
{
|
2018-09-07 00:28:54 +08:00
|
|
|
|
{ L"", LPCWSTR(ext) }
|
2018-09-02 14:03:04 +08:00
|
|
|
|
};
|
2018-09-07 00:28:54 +08:00
|
|
|
|
file_save->SetFileTypes(1, spec);
|
2018-09-02 14:03:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2018-09-07 00:28:54 +08:00
|
|
|
|
COMDLG_FILTERSPEC spec[] =
|
2018-09-02 14:03:04 +08:00
|
|
|
|
{
|
|
|
|
|
|
{ L"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>", L"*.*" }
|
|
|
|
|
|
};
|
2018-09-07 00:28:54 +08:00
|
|
|
|
file_save->SetFileTypes(1, spec);
|
2018-09-02 14:03:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-03 18:04:04 +08:00
|
|
|
|
hr = file_save->Show(nullptr);
|
2018-09-02 14:03:04 +08:00
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
2018-09-07 00:28:54 +08:00
|
|
|
|
IShellItem *item;
|
|
|
|
|
|
hr = file_save->GetResult(&item);
|
2018-09-02 14:03:04 +08:00
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
2018-09-07 00:28:54 +08:00
|
|
|
|
PWSTR str_file_path;
|
|
|
|
|
|
hr = item->GetDisplayName(SIGDN_FILESYSPATH, &str_file_path);
|
2018-09-02 14:03:04 +08:00
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
2018-09-07 00:28:54 +08:00
|
|
|
|
file_path = str_file_path;
|
|
|
|
|
|
::CoTaskMemFree(str_file_path);
|
2018-09-02 14:03:04 +08:00
|
|
|
|
}
|
2018-09-07 00:28:54 +08:00
|
|
|
|
item->Release();
|
2018-09-02 14:03:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-09-07 00:28:54 +08:00
|
|
|
|
file_save->Release();
|
2018-09-02 14:03:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
::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
|
|
|
|
}
|