Skin Packager: Cosmetics

This commit is contained in:
Birunthan Mohanathas 2012-06-17 11:32:52 +03:00
parent a44a96f27b
commit c8f8405d26
2 changed files with 77 additions and 81 deletions

View File

@ -258,21 +258,21 @@ bool CDialogPackage::CreatePackage()
}; };
if (!m_ZipFile || if (!m_ZipFile ||
!AddFileToPackage(ConvertToAscii(tempFile).c_str(), "RMSKIN.ini") || !AddFileToPackage(tempFile, L"RMSKIN.ini") ||
(!c_Dialog->m_HeaderFile.empty() && !AddFileToPackage(ConvertToAscii(c_Dialog->m_HeaderFile.c_str()).c_str(), "RMSKIN.bmp"))) (!c_Dialog->m_HeaderFile.empty() && !AddFileToPackage(c_Dialog->m_HeaderFile.c_str(), L"RMSKIN.bmp")))
{ {
std::wstring error = L"Unable to create package."; std::wstring error = L"Unable to create package.";
error += L"\n\nClick OK to close Packager."; error += L"\n\nClick OK to close Packager.";
MessageBox(NULL, error.c_str(), L"Rainmeter Skin Packager", MB_ERROR); MessageBox(c_Dialog->GetWindow(), error.c_str(), L"Rainmeter Skin Packager", MB_OK | MB_ICONERROR);
DeleteFile(tempFile); DeleteFile(tempFile);
return cleanup(); return cleanup();
} }
// Add skin // Add skin
{ {
std::string zipPrefix = "Skins\\"; std::wstring zipPrefix = L"Skins\\" + m_SkinFolder.first;
zipPrefix += ConvertToAscii(m_SkinFolder.first.c_str()); zipPrefix += L'\\';
if (!AddFolderToPackage(m_SkinFolder.second, L"", zipPrefix.c_str(), true)) if (!AddFolderToPackage(m_SkinFolder.second, L"", zipPrefix.c_str()))
{ {
return cleanup(); return cleanup();
} }
@ -281,18 +281,16 @@ bool CDialogPackage::CreatePackage()
// Add themes // Add themes
for (auto iter = m_ThemeFolders.cbegin(); iter != m_ThemeFolders.cend(); ++iter) for (auto iter = m_ThemeFolders.cbegin(); iter != m_ThemeFolders.cend(); ++iter)
{ {
std::string realPath = ConvertToAscii((*iter).second.c_str()); std::wstring realPath = (*iter).second + L"Rainmeter.thm";
realPath += "Rainmeter.thm"; std::wstring zipPath = L"Themes\\" + (*iter).first;
std::string zipPath = "Themes\\"; zipPath += L"\\Rainmeter.thm";
zipPath += ConvertToAscii((*iter).first.c_str());
zipPath += "\\Rainmeter.thm";
if (!AddFileToPackage(realPath.c_str(), zipPath.c_str())) if (!AddFileToPackage(realPath.c_str(), zipPath.c_str()))
{ {
std::wstring error = L"Error adding theme '"; std::wstring error = L"Error adding theme '";
error += (*iter).first; error += (*iter).first;
error += L"'."; error += L"'.";
error += L"\n\nClick OK to close Packager."; error += L"\n\nClick OK to close Packager.";
MessageBox(NULL, error.c_str(), L"Rainmeter Skin Packager", MB_ERROR); MessageBox(c_Dialog->GetWindow(), error.c_str(), L"Rainmeter Skin Packager", MB_OK | MB_ICONERROR);
return cleanup(); return cleanup();
} }
} }
@ -303,16 +301,15 @@ bool CDialogPackage::CreatePackage()
// Add 32bit and 64bit versions // Add 32bit and 64bit versions
for (int i = 0; i < 2; ++i) for (int i = 0; i < 2; ++i)
{ {
std::string realPath = ConvertToAscii((i == 0) ? (*iter).second.first.c_str() : (*iter).second.second.c_str()); const std::wstring& realPath = (i == 0) ? (*iter).second.first : (*iter).second.second;
std::string zipPath = (i == 0) ? "Plugins\\32bit\\" : "Plugins\\64bit\\"; std::wstring zipPath = ((i == 0) ? L"Plugins\\32bit\\" : L"Plugins\\64bit\\") + (*iter).first;
zipPath += ConvertToAscii((*iter).first.c_str());
if (!AddFileToPackage(realPath.c_str(), zipPath.c_str())) if (!AddFileToPackage(realPath.c_str(), zipPath.c_str()))
{ {
std::wstring error = L"Error adding plugin '"; std::wstring error = L"Error adding plugin '";
error += (*iter).first; error += (*iter).first;
error += L"'."; error += L"'.";
error += L"\n\nClick OK to close Packager."; error += L"\n\nClick OK to close Packager.";
MessageBox(NULL, error.c_str(), L"Rainmeter Skin Packager", MB_ERROR); MessageBox(c_Dialog->GetWindow(), error.c_str(), L"Rainmeter Skin Packager", MB_OK | MB_ICONERROR);
return cleanup(); return cleanup();
} }
} }
@ -332,7 +329,7 @@ bool CDialogPackage::CreatePackage()
{ {
std::wstring error = L"Unable to create package."; std::wstring error = L"Unable to create package.";
error += L"\n\nClick OK to close Packager."; error += L"\n\nClick OK to close Packager.";
MessageBox(NULL, error.c_str(), L"Rainmeter Skin Packager", MB_ERROR); MessageBox(c_Dialog->GetWindow(), error.c_str(), L"Rainmeter Skin Packager", MB_OK | MB_ICONERROR);
return false; return false;
} }
@ -343,9 +340,6 @@ unsigned __stdcall CDialogPackage::PackagerThreadProc(void* pParam)
{ {
CDialogPackage* dialog = (CDialogPackage*)pParam; CDialogPackage* dialog = (CDialogPackage*)pParam;
// Wait a bit so new style MessageBox dialogs work
Sleep(100);
if (dialog->CreatePackage()) if (dialog->CreatePackage())
{ {
// Stop the progress bar // Stop the progress bar
@ -353,7 +347,10 @@ unsigned __stdcall CDialogPackage::PackagerThreadProc(void* pParam)
// SendMessage(item, PBM_SETMARQUEE, (WPARAM)FALSE, 0); // SendMessage(item, PBM_SETMARQUEE, (WPARAM)FALSE, 0);
FlashWindow(dialog->m_Window, TRUE); FlashWindow(dialog->m_Window, TRUE);
MessageBox(NULL, L"The .rmskin file has been successfully created.", L"Rainmeter Skin Packager", MB_OK | MB_ICONINFORMATION);
std::wstring message = L"The skin package has been successfully created.";
message += L"\n\nClick OK to close Packager.";
MessageBox(c_Dialog->GetWindow(), message.c_str(), L"Rainmeter Skin Packager", MB_OK | MB_ICONINFORMATION);
} }
else else
{ {
@ -365,84 +362,84 @@ unsigned __stdcall CDialogPackage::PackagerThreadProc(void* pParam)
return 0; return 0;
} }
bool CDialogPackage::AddFileToPackage(const char* realPath, const char* zipPath) bool CDialogPackage::AddFileToPackage(const WCHAR* filePath, const WCHAR* zipPath)
{ {
HANDLE file = CreateFile(filePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE)
{
return false;
}
// Set zip file time
zip_fileinfo zi = {0}; zip_fileinfo zi = {0};
FILETIME lastWriteTime;
FILETIME localTime;
GetFileTime(file, NULL, NULL, &lastWriteTime);
FileTimeToLocalFileTime(&lastWriteTime, &localTime);
FileTimeToDosDateTime(&localTime, ((LPWORD)&zi.dosDate) + 1, ((LPWORD)&zi.dosDate) + 0);
FILETIME ftLocal; std::string zipPathAscii = ConvertToAscii(zipPath);
WIN32_FIND_DATAA ff32; int open = zipOpenNewFileInZip(m_ZipFile, zipPathAscii.c_str(), &zi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION);
HANDLE hFind = FindFirstFileA(realPath, &ff32); if (open != ZIP_OK)
if (hFind != INVALID_HANDLE_VALUE)
{ {
FileTimeToLocalFileTime(&ff32.ftLastWriteTime, &ftLocal); return false;
FileTimeToDosDateTime(&ftLocal, ((LPWORD)&zi.dosDate) + 1, ((LPWORD)&zi.dosDate) + 0);
FindClose(hFind);
} }
int err = zipOpenNewFileInZip(m_ZipFile, zipPath, &zi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION); bool result = true;
if (err != ZIP_OK) return false; do
FILE* fin = fopen(realPath, "rb");
if (fin)
{ {
size_t readSize; const DWORD bufferSize = 16 * 1024;
do BYTE buffer[bufferSize];
DWORD readSize;
if (!ReadFile(file, buffer, bufferSize, &readSize, NULL))
{ {
const size_t bufferSize = 16 * 1024; result = false;
BYTE buffer[bufferSize]; }
readSize = fread(buffer, 1, bufferSize, fin); else if (readSize != 0)
if (readSize < bufferSize && feof(fin) == 0) {
{ result = zipWriteInFileInZip(m_ZipFile, buffer, (UINT)readSize) == ZIP_OK;
err = ZIP_ERRNO; }
} else
else if (readSize > 0) {
{ // EOF
err = zipWriteInFileInZip(m_ZipFile, buffer, (UINT)readSize); break;
if (err < 0)
{
err = ZIP_ERRNO;
}
}
} }
while ((err == ZIP_OK) && (readSize > 0));
fclose(fin);
}
else
{
err = ZIP_ERRNO;
} }
while (result);
if (zipCloseFileInZip(m_ZipFile) != ZIP_OK) return false; CloseHandle(file);
return err == ZIP_OK; return zipCloseFileInZip(m_ZipFile) == ZIP_OK && result;
} }
bool CDialogPackage::AddFolderToPackage(const std::wstring& path, std::wstring base, const char* zipPrefix, bool recursive) bool CDialogPackage::AddFolderToPackage(const std::wstring& path, std::wstring base, const WCHAR* zipPrefix)
{ {
std::wstring filter = path + base; std::wstring currentPath = path + base;
std::string asciiBase = ConvertToAscii(filter.c_str()); currentPath += L'*';
filter += L'*';
WIN32_FIND_DATA fd; WIN32_FIND_DATA fd;
HANDLE hFind = FindFirstFileEx( HANDLE hFind = FindFirstFileEx(
filter.c_str(), currentPath.c_str(),
FindExInfoStandard, FindExInfoStandard,
&fd, &fd,
FindExSearchNameMatch, FindExSearchNameMatch,
NULL, NULL,
0); 0);
if (hFind == INVALID_HANDLE_VALUE) return false; if (hFind == INVALID_HANDLE_VALUE)
{
return false;
}
bool ret = true; currentPath.pop_back(); // Remove *
bool result = true;
std::list<std::wstring> folders; std::list<std::wstring> folders;
do do
{ {
if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
{ {
// Ignore hidden folders // Ignore hidden files and folders
continue; continue;
} }
@ -455,19 +452,19 @@ bool CDialogPackage::AddFolderToPackage(const std::wstring& path, std::wstring b
} }
else else
{ {
std::string asciiFile = asciiBase + ConvertToAscii(fd.cFileName); std::wstring filePath = currentPath + fd.cFileName;
std::string zipFile = zipPrefix; std::wstring zipPath = zipPrefix;
zipFile += &asciiFile[path.length() - 1]; zipPath.append(filePath, path.length(), filePath.length() - path.length());
ret = AddFileToPackage(asciiFile.c_str(), zipFile.c_str()); result = AddFileToPackage(filePath.c_str(), zipPath.c_str());
if (!ret) if (!result)
{ {
std::wstring error = L"Error adding file:\n"; std::wstring error = L"Error adding file:\n";
error += path; error += path;
error += base; error += base;
error += fd.cFileName; error += fd.cFileName;
error += L"\n\nClick OK to close Packager."; error += L"\n\nClick OK to close Packager.";
MessageBox(NULL, error.c_str(), L"Rainmeter Skin Packager", MB_ERROR); MessageBox(c_Dialog->GetWindow(), error.c_str(), L"Rainmeter Skin Packager", MB_OK | MB_ICONERROR);
break; break;
} }
} }
@ -475,20 +472,19 @@ bool CDialogPackage::AddFolderToPackage(const std::wstring& path, std::wstring b
while (FindNextFile(hFind, &fd)); while (FindNextFile(hFind, &fd));
FindClose(hFind); FindClose(hFind);
if (recursive && ret) if (result)
{ {
base += L'\\';
std::list<std::wstring>::const_iterator iter = folders.begin(); std::list<std::wstring>::const_iterator iter = folders.begin();
for ( ; iter != folders.end(); ++iter) for ( ; iter != folders.end(); ++iter)
{ {
std::wstring newBase = base + (*iter); std::wstring newBase = base + (*iter);
newBase += L'\\'; newBase += L'\\';
ret = AddFolderToPackage(path, newBase, zipPrefix, recursive); result = AddFolderToPackage(path, newBase, zipPrefix);
if (!ret) break; if (!result) break;
} }
} }
return ret; return result;
} }
std::wstring CDialogPackage::SelectFolder(HWND parent, const std::wstring& existingPath) std::wstring CDialogPackage::SelectFolder(HWND parent, const std::wstring& existingPath)

View File

@ -81,8 +81,8 @@ private:
bool CreatePackage(); bool CreatePackage();
static unsigned __stdcall PackagerThreadProc(void* pParam); static unsigned __stdcall PackagerThreadProc(void* pParam);
bool AddFileToPackage(const char* realPath, const char* zipPath); bool AddFileToPackage(const WCHAR* realPath, const WCHAR* zipPath);
bool AddFolderToPackage(const std::wstring& path, std::wstring base, const char* zipPrefix, bool recursive); bool AddFolderToPackage(const std::wstring& path, std::wstring base, const WCHAR* zipPrefix);
static std::wstring SelectFolder(HWND parent, const std::wstring& existingPath); static std::wstring SelectFolder(HWND parent, const std::wstring& existingPath);
static INT_PTR CALLBACK SelectFolderDlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); static INT_PTR CALLBACK SelectFolderDlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);