// SmpackageExportDlg.cpp : implementation file // #include "stdafx.h" #include "smpackage.h" #include "SmpackageExportDlg.h" #include "RageUtil.h" #include "ZipArchive\ZipArchive.h" #include "EnterName.h" #include "EnterComment.h" #include "smpackageUtil.h" #include "EditInsallations.h" #include "IniFile.h" #include #include #include using namespace std; #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CSmpackageExportDlg dialog CSmpackageExportDlg::CSmpackageExportDlg(CWnd* pParent /*=NULL*/) : CDialog(CSmpackageExportDlg::IDD, pParent) { //{{AFX_DATA_INIT(CSmpackageExportDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT } void CSmpackageExportDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSmpackageExportDlg) DDX_Control(pDX, IDC_COMBO_DIR, m_comboDir); DDX_Control(pDX, IDC_BUTTON_EXPORT_AS_INDIVIDUAL, m_buttonExportAsIndividual); DDX_Control(pDX, IDC_BUTTON_EXPORT_AS_ONE, m_buttonExportAsOne); DDX_Control(pDX, IDC_TREE, m_tree); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CSmpackageExportDlg, CDialog) //{{AFX_MSG_MAP(CSmpackageExportDlg) ON_BN_CLICKED(IDC_BUTTON_EXPORT_AS_ONE, OnButtonExportAsOne) ON_BN_CLICKED(IDC_BUTTON_EXPORT_AS_INDIVIDUAL, OnButtonExportAsIndividual) ON_BN_CLICKED(IDC_BUTTON_PLAY, OnButtonPlay) ON_BN_CLICKED(IDC_BUTTON_EDIT, OnButtonEdit) ON_CBN_SELCHANGE(IDC_COMBO_DIR, OnSelchangeComboDir) ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSmpackageExportDlg message handlers BOOL CSmpackageExportDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here // RefreshInstallationList(); RefreshTree(); return TRUE; // return TRUE unless you set the focus to a control } CString ReplaceInvalidFileNameChars( CString sOldFileName ) { CString sNewFileName = sOldFileName; const char charsToReplace[] = { ' ', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '+', '=', '[', ']', '{', '}', '|', ':', '\"', '\\', '<', '>', ',', '?', '/' }; for( int i=0; i &asPathToFilesOut ) { vector asDirectoriesToExplore; // HACK: // Must use backslashes in the path, or else WinZip and WinRAR don't see the files. // Not sure if this is ZipArchive's fault. if( IsADirectory(sDirOrFile) && sDirOrFile.Right(1) != "\\" ) { sDirOrFile += "\\"; sDirOrFile += "*.*"; } if( IsAFile(sDirOrFile) ) { asPathToFilesOut.push_back( sDirOrFile ); return; } GetDirListing( sDirOrFile, asPathToFilesOut, false, true ); GetDirListing( sDirOrFile, asDirectoriesToExplore, true, true ); while( asDirectoriesToExplore.size() > 0 ) { GetDirListing( asDirectoriesToExplore[0] + "\\*.*", asPathToFilesOut, false, true ); GetDirListing( asDirectoriesToExplore[0] + "\\*.*", asDirectoriesToExplore, true, true ); asDirectoriesToExplore.erase( asDirectoriesToExplore.begin() ); } } CString GetDesktopPath() { static TCHAR strNull[2] = _T(""); static TCHAR strPath[MAX_PATH]; DWORD dwType; DWORD dwSize = MAX_PATH; HKEY hKey; // Open the appropriate registry key LONG lResult = RegOpenKeyEx( HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"), 0, KEY_READ, &hKey ); if( ERROR_SUCCESS != lResult ) return strNull; lResult = RegQueryValueEx( hKey, _T("Desktop"), NULL, &dwType, (BYTE*)strPath, &dwSize ); RegCloseKey( hKey ); if( ERROR_SUCCESS != lResult ) return strNull; return strPath; } bool ExportPackage( CString sPackageName, const CStringArray& asDirectoriesToExport, CString sComment ) { CZipArchive zip; // // Create the package zip file // const CString sPackagePath = GetDesktopPath() + "\\" + sPackageName; try { zip.Open( sPackagePath, CZipArchive::zipCreate ); } catch( CException* e ) { e->ReportError(); zip.Close(); e->Delete(); return false; } zip.SetGlobalComment( sComment ); /* Find files to add to zip. */ unsigned i; vector asFilePaths; for( i=0; i Directories; for( i=0; i::const_iterator it; int num = 0; for( it = Directories.begin(); it != Directories.end(); ++it ) ini.SetValue( "Packages", ssprintf("%i", num++), *it ); ini.SetValueI( "Packages", "NumPackages", num ); CString buf; ini.WriteBuf(buf); CZipMemFile control; control.Write( buf.GetBuffer(0), buf.GetLength() ); control.Seek( 0, CZipAbstractFile::begin ); zip.AddNewFile( control, "smzip.ctl" ); } // // Add files to zip // for( unsigned j=0; jDelete(); return false; } } zip.Close(); return true; } bool CSmpackageExportDlg::MakeComment( CString &comment ) { bool DontAskForComment; if( GetPref("DontAskForComment", DontAskForComment) && DontAskForComment ) { comment = ""; return true; } EnterComment commentDlg; int nResponse = commentDlg.DoModal(); if( nResponse != IDOK ) return false; // cancelled comment = commentDlg.m_sEnteredComment; if( commentDlg.m_bDontAsk ) SetPref( "DontAskForComment", true ); return true; } void CSmpackageExportDlg::OnButtonExportAsOne() { CStringArray asPaths; GetCheckedPaths( asPaths ); if( asPaths.size() == 0 ) { AfxMessageBox( "No items are checked" ); return; } else if( asPaths.size() == 1 ) { OnButtonExportAsIndividual(); return; } // Generate a package name CString sPackageName; EnterName nameDlg; int nResponse = nameDlg.DoModal(); if( nResponse != IDOK ) return; // cancelled sPackageName = nameDlg.m_sEnteredName; sPackageName = ReplaceInvalidFileNameChars( sPackageName+".smzip" ); // Generate a comment CString sComment; if( !MakeComment(sComment) ) return; // cancelled if( ExportPackage( sPackageName, asPaths, sComment ) ) AfxMessageBox( ssprintf("Successfully exported package '%s' to your Desktop.",sPackageName) ); } void CSmpackageExportDlg::OnButtonExportAsIndividual() { CStringArray asPaths; GetCheckedPaths( asPaths ); if( asPaths.size() == 0 ) { AfxMessageBox( "No items are checked" ); return; } // Generate a comment CString sComment; if( !MakeComment(sComment) ) return; // cancelled bool bAllSucceeded = true; CStringArray asExportedPackages; CStringArray asFailedPackages; for( unsigned i=0; i1?"s":"", join("', '",asExportedPackages) ); else sMessage = ssprintf(" The packages %s failed to export.", join(", ",asFailedPackages) ); AfxMessageBox( sMessage ); } void CSmpackageExportDlg::OnButtonPlay() { // TODO: Add your control notification handler code here LaunchGame(); exit(0); } void CSmpackageExportDlg::GetTreeItems( CArray& aItemsOut ) { CArray aRootsToExplore; // add all top-level roots HTREEITEM item = m_tree.GetRootItem(); while( item != NULL ) { aRootsToExplore.Add( item ); item = m_tree.GetNextSiblingItem( item ); } while( aRootsToExplore.GetSize() > 0 ) { HTREEITEM item = aRootsToExplore[0]; aRootsToExplore.RemoveAt( 0 ); aItemsOut.Add( item ); HTREEITEM child = m_tree.GetChildItem( item ); while( child != NULL ) { aRootsToExplore.Add( child ); child = m_tree.GetNextSiblingItem( child ); } } } void CSmpackageExportDlg::GetCheckedTreeItems( CArray& aCheckedItemsOut ) { CArray aItems; GetTreeItems( aItems ); for( int i=0; i aItems; GetCheckedTreeItems( aItems ); for( int i=0; i aItems; GetTreeItems( aItems ); for( int i=0; i