async void skydriveList_SelectionChanged(object sender, SelectionChangedEventArgs e) { ImportFileItem item = this.skydriveList.SelectedItem as ImportFileItem; if (item == null) { return; } ROMDatabase db = ROMDatabase.Current; if (item.Type == SkyDriveItemType.ROM) { if (item.Stream != null) { await SkyDriveImportPage.ImportROM(item, this); } } else if (item.Type == SkyDriveItemType.Savestate || item.Type == SkyDriveItemType.SRAM) { //check to make sure there is a rom with matching name ROMDBEntry entry = null; if (item.Type == SkyDriveItemType.Savestate) //save state { entry = db.GetROMFromSavestateName(item.Name); } else if (item.Type == SkyDriveItemType.SRAM) //sram { entry = db.GetROMFromSRAMName(item.Name); } if (entry == null) //no matching file name { MessageBox.Show(AppResources.NoMatchingNameText, AppResources.ErrorCaption, MessageBoxButton.OK); return; } //check to make sure format is right if (item.Type == SkyDriveItemType.Savestate) //save state { string slot = item.Name.Substring(item.Name.Length - 5, 1); int parsedSlot = 0; if (!int.TryParse(slot, out parsedSlot)) { MessageBox.Show(AppResources.ImportSavestateInvalidFormat, AppResources.ErrorCaption, MessageBoxButton.OK); return; } } if (item.Stream != null) { await SkyDriveImportPage.ImportSave(item, this); } } }
public static async Task ImportROM(ImportFileItem item, DependencyObject page) { var indicator = SystemTray.GetProgressIndicator(page); indicator.IsIndeterminate = true; indicator.Text = String.Format(AppResources.ImportingProgressText, item.Name); try { StorageFolder folder = ApplicationData.Current.LocalFolder; StorageFolder romFolder = await folder.CreateFolderAsync("roms", CreationCollisionOption.OpenIfExists); ROMDatabase db = ROMDatabase.Current; var romEntry = db.GetROM(item.Name); bool fileExisted = false; if (romEntry != null) { fileExisted = true; //MessageBox.Show(String.Format(AppResources.ROMAlreadyExistingError, item.Name), AppResources.ErrorCaption, MessageBoxButton.OK); //return; } if (item.Stream != null) { byte[] tmpBuf = new byte[32]; StorageFile destinationFile = await romFolder.CreateFileAsync(item.Name, CreationCollisionOption.ReplaceExisting); using (IRandomAccessStream destStream = await destinationFile.OpenAsync(FileAccessMode.ReadWrite)) using (DataWriter writer = new DataWriter(destStream)) { if (item.Stream.CanSeek) { item.Stream.Seek(0, SeekOrigin.Begin); } while (item.Stream.Read(tmpBuf, 0, tmpBuf.Length) != 0) { writer.WriteBytes(tmpBuf); } await writer.StoreAsync(); await writer.FlushAsync(); writer.DetachStream(); } item.Downloading = false; if (!fileExisted) { var entry = FileHandler.InsertNewDBEntry(destinationFile.Name); await FileHandler.FindExistingSavestatesForNewROM(entry); db.CommitChanges(); } //update voice command list await MainPage.UpdateGameListForVoiceCommand(); MessageBox.Show(String.Format(AppResources.DownloadCompleteText, item.Name)); } else { MessageBox.Show(String.Format(AppResources.DownloadErrorText, item.Name, "Import error"), AppResources.ErrorCaption, MessageBoxButton.OK); } } catch (Exception ex) { MessageBox.Show(ex.Message, AppResources.ErrorCaption, MessageBoxButton.OK); } #if GBC indicator.Text = AppResources.ApplicationTitle2; #else indicator.Text = AppResources.ApplicationTitle; #endif indicator.IsIndeterminate = false; }
public static async Task ImportSave(ImportFileItem item, DependencyObject page) { var indicator = SystemTray.GetProgressIndicator(page); indicator.IsIndeterminate = true; indicator.Text = String.Format(AppResources.ImportingProgressText, item.Name); try { StorageFolder folder = ApplicationData.Current.LocalFolder; StorageFolder romFolder = await folder.CreateFolderAsync(FileHandler.ROM_DIRECTORY, CreationCollisionOption.OpenIfExists); StorageFolder saveFolder = await romFolder.CreateFolderAsync(FileHandler.SAVE_DIRECTORY, CreationCollisionOption.OpenIfExists); ROMDatabase db = ROMDatabase.Current; if (item.Stream != null) { byte[] tmpBuf = new byte[32]; StorageFile destinationFile = null; ROMDBEntry entry = null; if (item.Type == SkyDriveItemType.SRAM) { entry = db.GetROMFromSRAMName(item.Name); if (entry != null) { entry.SuspendAutoLoadLastState = true; destinationFile = await saveFolder.CreateFileAsync(Path.GetFileNameWithoutExtension(entry.FileName) + ".srm", CreationCollisionOption.ReplaceExisting); } else { destinationFile = await saveFolder.CreateFileAsync(item.Name, CreationCollisionOption.ReplaceExisting); } } else if (item.Type == SkyDriveItemType.Savestate) { entry = db.GetROMFromSavestateName(item.Name); if (entry != null) { destinationFile = await saveFolder.CreateFileAsync(Path.GetFileNameWithoutExtension(entry.FileName) + item.Name.Substring(item.Name.Length - 4), CreationCollisionOption.ReplaceExisting); } else { destinationFile = await saveFolder.CreateFileAsync(item.Name, CreationCollisionOption.ReplaceExisting); } } using (IRandomAccessStream destStream = await destinationFile.OpenAsync(FileAccessMode.ReadWrite)) using (DataWriter writer = new DataWriter(destStream)) { if (item.Stream.CanSeek) { item.Stream.Seek(0, SeekOrigin.Begin); } while (item.Stream.Read(tmpBuf, 0, tmpBuf.Length) != 0) { writer.WriteBytes(tmpBuf); } await writer.StoreAsync(); await writer.FlushAsync(); writer.DetachStream(); } item.Downloading = false; if (item.Type == SkyDriveItemType.Savestate) { String number = item.Name.Substring(item.Name.Length - 1, 1); int slot = int.Parse(number); if (entry != null) //NULL = do nothing { SavestateEntry saveentry = db.SavestateEntryExisting(entry.FileName, slot); if (saveentry != null) { //delete entry db.RemoveSavestateFromDB(saveentry); } SavestateEntry ssEntry = new SavestateEntry() { ROM = entry, Savetime = DateTime.Now, Slot = slot, FileName = item.Name }; db.Add(ssEntry); db.CommitChanges(); } } MessageBox.Show(String.Format(AppResources.DownloadCompleteText, item.Name)); } else { MessageBox.Show(String.Format(AppResources.DownloadErrorText, item.Name, "Import error"), AppResources.ErrorCaption, MessageBoxButton.OK); } } catch (Exception ex) { MessageBox.Show(ex.Message, AppResources.ErrorCaption, MessageBoxButton.OK); } #if GBC indicator.Text = AppResources.ApplicationTitle2; #else indicator.Text = AppResources.ApplicationTitle; #endif indicator.IsIndeterminate = false; }
private async Task <List <ImportFileItem> > GetFilesInArchive(SkyDriveItemType parentType, IStorageFile file) { List <ImportFileItem> listItems = new List <ImportFileItem>(); if (file != null) { IRandomAccessStream accessStream = await file.OpenReadAsync(); Stream s = accessStream.AsStreamForRead((int)accessStream.Size); //get list of file IArchive archive = null; if (parentType == SkyDriveItemType.Rar) { archive = SharpCompress.Archive.Rar.RarArchive.Open(s); } else if (parentType == SkyDriveItemType.Zip) { archive = SharpCompress.Archive.Zip.ZipArchive.Open(s); } else if (parentType == SkyDriveItemType.SevenZip) { archive = SharpCompress.Archive.SevenZip.SevenZipArchive.Open(s); } foreach (var entry in archive.Entries) { if (!entry.IsDirectory) { Stream data = new MemoryStream(); entry.WriteTo(data); data.Position = 0; String name = entry.FilePath; SkyDriveItemType type = SkyDriveItemType.File; int dotIndex = -1; if ((dotIndex = name.LastIndexOf('.')) != -1) { String substrName = name.Substring(dotIndex).ToLower(); type = SkyDriveImportPage.GetSkyDriveItemType(substrName); } if (type == SkyDriveItemType.File) { data.Close(); continue; } ImportFileItem listItem = new ImportFileItem() { Name = name, Type = type, Stream = data }; listItems.Add(listItem); } } //close the zip stream since we have the stream of each item inside it already s.Close(); s = null; } return(listItems); }