Search in sources :

Example 1 with AsyncGuiPoolWorker

use of com.pixbits.lib.concurrent.AsyncGuiPoolWorker in project rom-manager by Jakz.

the class Scanner method scanForRoms.

public void scanForRoms(boolean total) throws IOException {
    ignoredPaths.clear();
    foundFiles.clear();
    clones.clear();
    if (total) {
        logger.i(LogTarget.romset(set), "Scanning for roms");
        set.resetStatus();
    } else {
        logger.i(LogTarget.romset(set), "Scanning for new roms");
        set.romStream().filter(r -> r.isPresent()).map(r -> r.handle().path()).forEach(ignoredPaths::add);
    }
    ignoredPaths.addAll(settings.getIgnoredPaths());
    Path folder = settings.romsPath;
    if (!canProceedWithScan())
        return;
    FolderScanner folderScanner = new FolderScanner(ignoredPaths, true);
    final Set<Path> pathsToScan = folderScanner.scan(folder);
    List<VerifierEntry> foundEntries = Collections.synchronizedList(new ArrayList<>());
    /* scanning task */
    {
        Operation<Path, List<VerifierEntry>> operation = path -> {
            logger.d(LogTarget.file(path), "> Scanning %s", path.getFileName().toString());
            try {
                return scanner.scanFile(path);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            } catch (RuntimeException e) {
                logger.e(LogTarget.file(path), "Exception while scanning %s", path.toString());
                e.printStackTrace();
                if (!SevenZip.isInitializedSuccessfully()) {
                    Throwable ee = SevenZip.getLastInitializationException();
                    ee.printStackTrace();
                }
                return null;
            }
        };
        BiConsumer<Long, Float> guiProgress = (i, f) -> {
            Main.progress.update(f, "Scanning " + i + " of " + pathsToScan.size() + "...");
        };
        Consumer<List<VerifierEntry>> callback = results -> {
            foundEntries.addAll(results);
        };
        Runnable onComplete = () -> {
            SwingUtilities.invokeLater(() -> {
                Main.progress.finished();
                List<VerifierEntry> transformedEntries = foundEntries.stream().collect(Collectors.toList());
                HandleSet handleSet = new HandleSet(transformedEntries);
                HandleSet.Stats stats = handleSet.stats();
                logger.i(LogTarget.romset(set), "Found %d potential matches (%d binary, %d inside archives, %d nested inside %d archives).", stats.totalHandles, stats.binaryCount, stats.archivedCount, stats.nestedArchiveInnerCount, stats.nestedArchiveCount);
                transformedEntries.forEach(h -> logger.d(LogTarget.romset(set), "> %s", h.toString()));
                Runnable verifyTask = verifyTask(transformedEntries, total);
                verifyTask.run();
            });
        };
        AsyncGuiPoolWorker<Path, List<VerifierEntry>> worker = new AsyncGuiPoolWorker<>(operation, guiProgress, 1);
        SwingUtilities.invokeLater(() -> {
            Main.progress.show(Main.mainFrame, "Scanning " + pathsToScan.size() + " files", () -> worker.cancel());
        });
        worker.compute(pathsToScan, callback, onComplete);
    }
}
Also used : IntStream(java.util.stream.IntStream) Game(com.github.jakz.romlib.data.game.Game) Dialogs(jack.rm.gui.Dialogs) MyGameSetFeatures(jack.rm.data.romset.MyGameSetFeatures) ScannerPlugin(jack.rm.plugins.types.ScannerPlugin) Function(java.util.function.Function) TreeSet(java.util.TreeSet) Handle(com.pixbits.lib.io.archive.handles.Handle) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) GameSet(com.github.jakz.romlib.data.set.GameSet) SwingUtilities(javax.swing.SwingUtilities) HandleSet(com.pixbits.lib.io.archive.HandleSet) StreamException(com.pixbits.lib.functional.StreamException) Log(com.pixbits.lib.log.Log) BiConsumer(java.util.function.BiConsumer) Rom(com.github.jakz.romlib.data.game.Rom) Path(java.nio.file.Path) Files(java.nio.file.Files) LogSource(jack.rm.log.LogSource) LogTarget(jack.rm.log.LogTarget) Operation(com.pixbits.lib.concurrent.Operation) PluginRealType(jack.rm.plugins.PluginRealType) Set(java.util.Set) FormatSupportPlugin(jack.rm.plugins.types.FormatSupportPlugin) IOException(java.io.IOException) GameStatus(com.github.jakz.romlib.data.game.GameStatus) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) FolderScanner(com.pixbits.lib.io.FolderScanner) List(java.util.List) Logger(com.pixbits.lib.log.Logger) VerifierPlugin(jack.rm.plugins.types.VerifierPlugin) SevenZip(net.sf.sevenzipjbinding.SevenZip) Collections(java.util.Collections) VerifierEntry(com.pixbits.lib.io.archive.VerifierEntry) Main(jack.rm.Main) AsyncGuiPoolWorker(com.pixbits.lib.concurrent.AsyncGuiPoolWorker) Settings(jack.rm.data.romset.Settings) Path(java.nio.file.Path) Operation(com.pixbits.lib.concurrent.Operation) IOException(java.io.IOException) AsyncGuiPoolWorker(com.pixbits.lib.concurrent.AsyncGuiPoolWorker) VerifierEntry(com.pixbits.lib.io.archive.VerifierEntry) BiConsumer(java.util.function.BiConsumer) Consumer(java.util.function.Consumer) FolderScanner(com.pixbits.lib.io.FolderScanner) BiConsumer(java.util.function.BiConsumer) HandleSet(com.pixbits.lib.io.archive.HandleSet)

Aggregations

Game (com.github.jakz.romlib.data.game.Game)1 GameStatus (com.github.jakz.romlib.data.game.GameStatus)1 Rom (com.github.jakz.romlib.data.game.Rom)1 GameSet (com.github.jakz.romlib.data.set.GameSet)1 AsyncGuiPoolWorker (com.pixbits.lib.concurrent.AsyncGuiPoolWorker)1 Operation (com.pixbits.lib.concurrent.Operation)1 StreamException (com.pixbits.lib.functional.StreamException)1 FolderScanner (com.pixbits.lib.io.FolderScanner)1 HandleSet (com.pixbits.lib.io.archive.HandleSet)1 VerifierEntry (com.pixbits.lib.io.archive.VerifierEntry)1 Handle (com.pixbits.lib.io.archive.handles.Handle)1 Log (com.pixbits.lib.log.Log)1 Logger (com.pixbits.lib.log.Logger)1 Main (jack.rm.Main)1 MyGameSetFeatures (jack.rm.data.romset.MyGameSetFeatures)1 Settings (jack.rm.data.romset.Settings)1 Dialogs (jack.rm.gui.Dialogs)1 LogSource (jack.rm.log.LogSource)1 LogTarget (jack.rm.log.LogTarget)1 PluginRealType (jack.rm.plugins.PluginRealType)1