示例#1
0
        public async Task AddGame(GameOnPc game, short destinationFolderIndex)
        {
            string format            = GetGdemuFolderNameFromIndex(destinationFolderIndex);
            string destinationFolder = Path.GetFullPath(DrivePath + destinationFolderIndex.ToString(format));
            string oldImagePath      = Directory.EnumerateFiles(game.FullPath).SingleOrDefault(f => Path.GetExtension(f) == ".gdi" || Path.GetExtension(f) == ".cdi");

            try
            {
                if (game.IsCompressed)
                {
                    oldImagePath = ExtractArchive(game);
                }

                if (game.MustShrink)
                {
                    if (Directory.Exists(destinationFolder))
                    {
                        FileManager.RemoveAllFilesInDirectory(destinationFolder);
                    }
                    else
                    {
                        Directory.CreateDirectory(destinationFolder);
                    }

                    //var commandResult = await Command
                    //    .Run(@".\gditools\dist\gditools_messily_tweaked.exe", oldGdiPath, destinationFolder)
                    //    .Task;

                    var cts = new CancellationTokenSource();
                    cts.CancelAfter(TimeSpan.FromMinutes(2));
                    Command command = null;
                    try
                    {
                        command = Command.Run(@".\gditools\dist\gditools_messily_tweaked.exe", oldImagePath, destinationFolder);
                        await command.Task.WaitOrCancel(cts.Token);
                    }
                    catch (OperationCanceledException ex)

                    {
                        if (command != null)
                        {
                            command.Kill();
                        }

                        throw new OperationCanceledException($"Timeout while shrinking {game.GameName}. You might need to copy it without shrinking.");
                    }

                    //if (!commandResult.Success)
                    //{
                    //    // There is always an error even if it's working, need find out why
                    //    //throw new System.Exception("There was an error while shriking the GDI: " + commandResult.StandardError);
                    //}

                    var gdiPath = Directory.EnumerateFiles(destinationFolder).SingleOrDefault(f => Path.GetExtension(f) == ".gdi");
                    if (gdiPath == null)
                    {
                        throw new OperationCanceledException($"Could not shrink {game.GameName}. You might need to copy it without shrinking.");
                    }
                    var newGdi = GdiReader.GetGdiFromFile(gdiPath);
                    File.Delete(gdiPath);
                    newGdi.SaveTo(Path.Combine(destinationFolder, "disc.gdi"), true);
                    newGdi.RenameTrackFiles(destinationFolder);
                }
                else
                {
                    if (game.IsGdi)
                    {
                        if (!Directory.Exists(destinationFolder))
                        {
                            Directory.CreateDirectory(destinationFolder);
                        }
                        else
                        {
                            FileManager.RemoveAllFilesInDirectory(destinationFolder);
                        }

                        foreach (var track in game.GdiInfo.Tracks)
                        {
                            using (FileStream SourceStream = File.Open(game.FullPath + @"\" + track.FileName, FileMode.Open))
                            {
                                using (FileStream DestinationStream = File.Create(Path.Combine(destinationFolder, track.FileName)))
                                {
                                    await SourceStream.CopyToAsync(DestinationStream);
                                }
                            }
                        }

                        string gdiPath = Directory.EnumerateFiles(game.FullPath).FirstOrDefault(f => System.IO.Path.GetExtension(f) == ".gdi");
                        var    newGdi  = GdiReader.GetGdiFromFile(gdiPath);
                        newGdi.SaveTo(Path.Combine(destinationFolder, "disc.gdi"), true);
                        newGdi.RenameTrackFiles(destinationFolder);
                    }
                    else // CDI
                    {
                        using (FileStream SourceStream = File.Open(oldImagePath, FileMode.Open))
                        {
                            if (!Directory.Exists(destinationFolder))
                            {
                                Directory.CreateDirectory(destinationFolder);
                            }

                            using (FileStream DestinationStream = File.Create(Path.Combine(destinationFolder, "disc.cdi")))
                            {
                                await SourceStream.CopyToAsync(DestinationStream);
                            }
                        }
                    }
                }
            }
            catch
            {
                // If there is a problem, we roll back and remove the folder on the SD card.
                if (Directory.Exists(destinationFolder))
                {
                    Directory.Delete(destinationFolder);
                }

                throw;
            }
            finally
            {
                FileManager.RemoveAllFilesInDirectory(tempPath);
            }
        }