/// <summary>
        /// Extract game information from a PC archive file
        /// </summary>
        /// <param name="archivePath"></param>
        /// <param name="archive"></param>
        /// <returns></returns>
        public static GameOnPc ExtractPcGameDataFromArchive(string archivePath, IArchive archive)
        {
            var      gdiReader = new GdiReader();
            BaseGame game      = gdiReader.ExtractGameDataFromArchive(archivePath, archive);

            if (game.GameName == "GDMENU")
            {
                return(null);
            }

            var gameOnPc = ConvertBaseGameToGameOnPc(game);

            gameOnPc.IsCompressed = true;

            return(gameOnPc);
        }
        /// <summary>
        /// Extract game information from a folder
        /// </summary>
        /// <param name="folderPath"></param>
        /// <returns></returns>
        private static BaseGame ExtractGameData(string folderPath)
        {
            BaseGame game = null;

            string imagePath = FileManager.GetImageFilesPathInFolder(folderPath).FirstOrDefault();

            if (!string.IsNullOrEmpty(imagePath) && imagePath.EndsWith(".gdi"))
            {
                var gdiReader = new GdiReader();
                game = gdiReader.ExtractGameData(imagePath);
            }
            else if (!string.IsNullOrEmpty(imagePath) && imagePath.EndsWith(".cdi"))
            {
                using (var fs = File.OpenRead(imagePath))
                {
                    var cdiReader = new CdiReader();
                    game = cdiReader.ExtractGameData(imagePath);
                }
            }
            else
            {
                game = new BaseGame
                {
                    FullPath      = folderPath,
                    Path          = folderPath.Split(Path.DirectorySeparatorChar).Last(),
                    Size          = FileManager.GetDirectorySize(folderPath),
                    FormattedSize = FileManager.GetDirectoryFormattedSize(folderPath)
                };
            }

            if (game.GameName == "GDMENU")
            {
                return(null);
            }

            return(game);
        }
示例#3
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);
            }
        }