Search in sources :

Example 1 with FolderScanner

use of com.pixbits.lib.io.FolderScanner 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)

Example 2 with FolderScanner

use of com.pixbits.lib.io.FolderScanner in project rom-manager by Jakz.

the class MoveUnknownFilesPlugin method execute.

@Override
public void execute(GameSet set) {
    try {
        counter = 0;
        if (!Files.exists(path) || !Files.isDirectory(path))
            Files.createDirectory(path);
        Set<Path> existing = set.romStream().filter(r -> r.isPresent()).map(r -> r.handle().path()).collect(Collectors.toSet());
        Settings settings = getGameSetSettings();
        Set<Path> total = new FolderScanner(FileSystems.getDefault().getPathMatcher("glob:*.*"), settings.getIgnoredPaths(), true).scan(settings.romsPath);
        total.removeAll(existing);
        total.stream().filter(f -> !f.getParent().equals(path)).forEach(f -> {
            Path dest = path.resolve(f.getFileName());
            int i = 1;
            while (Files.exists(dest)) dest = path.resolve(f.getFileName().toString() + (i++));
            try {
                Files.move(f, dest);
                ++counter;
            } catch (IOException e) {
                e.printStackTrace();
            /* TODO: log */
            }
        });
        message("Moved " + counter + " unknown files");
    } catch (IOException e) {
        e.printStackTrace();
    // TODO: log
    }
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) CleanupPlugin(jack.rm.plugins.types.CleanupPlugin) Files(java.nio.file.Files) ExposedParameter(com.pixbits.lib.plugin.ExposedParameter) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) GameSet(com.github.jakz.romlib.data.set.GameSet) FolderScanner(com.pixbits.lib.io.FolderScanner) Path(java.nio.file.Path) PluginWithIgnorePaths(jack.rm.plugins.PluginWithIgnorePaths) FileSystems(java.nio.file.FileSystems) Settings(jack.rm.data.romset.Settings) FolderScanner(com.pixbits.lib.io.FolderScanner) IOException(java.io.IOException) Settings(jack.rm.data.romset.Settings)

Aggregations

GameSet (com.github.jakz.romlib.data.set.GameSet)2 FolderScanner (com.pixbits.lib.io.FolderScanner)2 Settings (jack.rm.data.romset.Settings)2 IOException (java.io.IOException)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 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 AsyncGuiPoolWorker (com.pixbits.lib.concurrent.AsyncGuiPoolWorker)1 Operation (com.pixbits.lib.concurrent.Operation)1 StreamException (com.pixbits.lib.functional.StreamException)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