コード例 #1
0
        public async Task AddGame(GameOnPc game, short destinationFolderIndex)
        {
            string format            = GetGdemuFolderNameFromIndex(destinationFolderIndex);
            string destinationFolder = Path.GetFullPath(DrivePath + destinationFolderIndex.ToString(format));

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

                var oldGdiPath = Directory.EnumerateFiles(game.FullPath).Single(f => Path.GetExtension(f) == ".gdi");

                //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", oldGdiPath, 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 = GameManager.GetGdiFromFile(gdiPath);
                File.Delete(gdiPath);
                newGdi.SaveTo(Path.Combine(destinationFolder, "disc.gdi"), true);
                newGdi.RenameTrackFiles(destinationFolder);
            }
            else
            {
                await FileManager.CopyDirectoryContentToAnother(game.FullPath, destinationFolder, true);

                var gdiPath = Directory.EnumerateFiles(destinationFolder).Single(f => Path.GetExtension(f) == ".gdi");
                var newGdi  = GameManager.GetGdiFromFile(gdiPath);
                File.Delete(gdiPath);
                newGdi.SaveTo(Path.Combine(destinationFolder, "disc.gdi"), true);
                newGdi.RenameTrackFiles(destinationFolder);
            }
        }