Ejemplo n.º 1
0
        private string ExtractArchive(GameOnPc game)
        {
            string oldGdiPath;
            var    archive   = ArchiveFactory.Open(game.FullPath);
            var    gpiEntry  = archive.Entries.FirstOrDefault(e => e.Key.EndsWith(".gdi") || e.Key.EndsWith(".cdi"));
            var    separator = "/";
            var    pathParts = gpiEntry.Key.Split(separator);
            List <IArchiveEntry> entriesToExtract = new List <IArchiveEntry>();

            if (pathParts.Count() > 1)
            {
                string rootPath = gpiEntry.Key.Replace(pathParts.Last(), string.Empty);
                entriesToExtract.AddRange(ArchiveManager.RetreiveFilesFromArchiveStartingWith(archive, rootPath));
            }
            else
            {
                entriesToExtract.AddRange(archive.Entries.Where(e => !e.Key.Contains(separator) && !e.IsDirectory));
            }

            if (!Directory.Exists(tempPath))
            {
                Directory.CreateDirectory(tempPath);
            }

            foreach (var entry in entriesToExtract)
            {
                var fileName = entry.Key.Split(@"/").Last().Split(@"\").Last();
                File.Create(tempPath + fileName).Close();
                entry.WriteToFile(tempPath + fileName);
            }

            oldGdiPath = Directory.EnumerateFiles(tempPath).Single(f => Path.GetExtension(f) == ".gdi");

            return(oldGdiPath);
        }
Ejemplo n.º 2
0
        private GameOnPc RetrieveGameInFolder(string folderPath)
        {
            GameOnPc game       = null;
            var      imageFiles = FileManager.GetImageFilesPathInFolder(folderPath);

            if (imageFiles.Count() > 1)
            {
                WriteError($"You have more than one GDI/CDI file in the folder {folderPath}. Please make sure you only have one GDI/CDI per folder.");
                CopyProgressBar.Value++;
                CopyProgressBar.Refresh();
                return(null);
            }

            if (imageFiles.Any())
            {
                try
                {
                    game = GameManager.ExtractPcGameData(folderPath);
                }
                catch (Exception error)
                {
                    WriteError(error.Message);
                    CopyProgressBar.Value++;
                    CopyProgressBar.Refresh();
                    return(null);
                }
            }

            CopyProgressBar.Value++;
            CopyProgressBar.Refresh();

            return(game);
        }
 public static void LinkGameOnPcToGameOnSd(GameOnPc gameOnPc, GameOnSd gameOnSd)
 {
     gameOnPc.IsInSdCard      = true;
     gameOnPc.MustBeOnSd      = true;
     gameOnPc.SdFolder        = gameOnSd.Path;
     gameOnPc.SdSize          = FileManager.GetDirectorySize(gameOnSd.FullPath);
     gameOnPc.SdFormattedSize = FileManager.GetDirectoryFormattedSize(gameOnSd.FullPath);
 }
Ejemplo n.º 4
0
        private GameOnPc RetrieveGameInArchive(string compressedFilePath)
        {
            GameOnPc game = null;

            WriteInfo($"Checking archive {compressedFilePath}...");
            IArchive archive;

            try
            {
                archive = ArchiveFactory.Open(new FileInfo(compressedFilePath));
            }
            catch (Exception ex)
            {
                WriteError($"Could not open archive {compressedFilePath}. Error: {ex.Message}");
                CopyProgressBar.Value += 10;
                CopyProgressBar.Refresh();
                return(null);
            }

            if (ArchiveManager.RetreiveUniqueFileFromArchiveEndingWith(archive, ".gdi") == null)
            {
                WriteWarning($"Could not find GDI in archive {compressedFilePath} (CDI are ignored in archives)");
                CopyProgressBar.Value += 10;
                CopyProgressBar.Refresh();
                return(null);
            }
            else
            {
                try
                {
                    if (archive.Type == SharpCompress.Common.ArchiveType.SevenZip && !viewModel.MustScanSevenZip)
                    {
                        WriteWarning($"Archive {compressedFilePath} ignored as it's a 7z file and the option isn't ticked.");
                        CopyProgressBar.Value += 10;
                        CopyProgressBar.Refresh();
                        return(null);
                    }

                    game = GameManager.ExtractPcGameDataFromArchive(compressedFilePath, archive);
                }
                catch (Exception ex)
                {
                    WriteError(ex.Message);
                    CopyProgressBar.Value += 10;
                    CopyProgressBar.Refresh();
                    return(null);
                }
            }
            CopyProgressBar.Value += 10;
            CopyProgressBar.Refresh();

            return(game);
        }
Ejemplo n.º 5
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);
            }
        }
 public static void UnLinkGameOnPcToGameOnSd(GameOnPc gameOnPc)
 {
     gameOnPc.IsInSdCard = false;
     gameOnPc.MustBeOnSd = false;
 }
Ejemplo n.º 7
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);
            }
        }