Search in sources :

Example 1 with DownloadWorker

use of jack.rm.files.DownloadWorker in project rom-manager by Jakz.

the class DatUpdater method updateDat.

public static void updateDat(GameSet set, Consumer<Boolean> callback) throws IOException {
    Path destDat = set.datPath();
    AtomicReference<Path> tmpDownloadPath = new AtomicReference<Path>(Files.createTempFile(null, null));
    AtomicReference<Path> tmpZippedPath = new AtomicReference<Path>();
    AtomicLong crc = new AtomicLong(-1);
    AtomicLong size = new AtomicLong(-1);
    /* if dat file exists we first compute the CRC and size of the current file to compare with the one that is going to be downloaded */
    if (set.canBeLoaded()) {
        crc.set(FileUtils.calculateCRCFast(destDat));
        size.set(Files.size(destDat));
    }
    final Consumer<Boolean> consolidationStep = r -> {
        try {
            Path src = tmpDownloadPath.get();
            if (crc.get() != -1) {
                long ncrc = FileUtils.calculateCRCFast(src);
                long nsize = Files.size(src);
                if (ncrc == crc.get() && nsize == size.get()) {
                    Dialogs.showWarning("Dat already up-to-date", "Your DAT version is already up to date!", Main.gsettingsView);
                    return;
                }
            }
            Files.createDirectories(destDat.getParent());
            Files.move(src, destDat, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            e.printStackTrace();
        }
    };
    final Consumer<Boolean> cleanupStep = r -> {
        try {
            if (tmpDownloadPath.get() != null)
                Files.deleteIfExists(tmpDownloadPath.get());
            if (tmpZippedPath.get() != null)
                Files.deleteIfExists(tmpZippedPath.get());
        } catch (IOException e) {
            e.printStackTrace();
            callback.accept(false);
        }
    };
    Consumer<Boolean> extractionStep = r -> {
        boolean isZipped = false;
        try {
            ZipFile zfile = new ZipFile(tmpDownloadPath.get().toFile());
            isZipped = true;
            zfile.close();
        } catch (IOException e) {
        }
        try {
            if (isZipped) {
                tmpZippedPath.set(tmpDownloadPath.get());
                tmpDownloadPath.set(Files.createTempFile(null, null));
                logger.i(LogTarget.romset(set), "Extracting DAT for " + set.ident() + " to " + tmpDownloadPath);
                ZipExtractWorker<?> worker = new ZipExtractWorker<OperationDetails>(tmpZippedPath.get(), tmpDownloadPath.get(), new OperationDetails() {

                    public String getTitle() {
                        return "Uncompressing";
                    }

                    public String getProgressText() {
                        return "Progress..";
                    }
                }, consolidationStep.andThen(cleanupStep).andThen(callback), Main.gsettingsView);
                worker.execute();
            } else
                consolidationStep.andThen(cleanupStep).andThen(callback).accept(true);
        } catch (IOException e) {
            e.printStackTrace();
            callback.accept(false);
        }
    };
    logger.i(LogTarget.romset(set), "Downloading DAT for " + set.ident() + " to " + tmpDownloadPath);
    final Provider.Source source = set.info().getProvider().getSource();
    DownloadWorker<?> worker = new DownloadWorker<OperationDetails>(source.getURL(), tmpDownloadPath.get(), OperationDetails.of("Downloading", "Progress.."), extractionStep, Main.progress, Main.gsettingsView, source.getPostArguments());
    worker.execute();
}
Also used : Path(java.nio.file.Path) Files(java.nio.file.Files) LogSource(jack.rm.log.LogSource) LogTarget(jack.rm.log.LogTarget) Dialogs(jack.rm.gui.Dialogs) IOException(java.io.IOException) DownloadWorker(jack.rm.files.DownloadWorker) AtomicReference(java.util.concurrent.atomic.AtomicReference) StandardCopyOption(java.nio.file.StandardCopyOption) OperationDetails(com.pixbits.lib.concurrent.OperationDetails) Consumer(java.util.function.Consumer) GameSet(com.github.jakz.romlib.data.set.GameSet) AtomicLong(java.util.concurrent.atomic.AtomicLong) FileUtils(com.pixbits.lib.io.FileUtils) ZipExtractWorker(jack.rm.files.ZipExtractWorker) Logger(com.pixbits.lib.log.Logger) Provider(com.github.jakz.romlib.data.set.Provider) Log(com.pixbits.lib.log.Log) ZipFile(java.util.zip.ZipFile) Path(java.nio.file.Path) Main(jack.rm.Main) DownloadWorker(jack.rm.files.DownloadWorker) ZipExtractWorker(jack.rm.files.ZipExtractWorker) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) Provider(com.github.jakz.romlib.data.set.Provider) OperationDetails(com.pixbits.lib.concurrent.OperationDetails) AtomicLong(java.util.concurrent.atomic.AtomicLong) ZipFile(java.util.zip.ZipFile)

Aggregations

GameSet (com.github.jakz.romlib.data.set.GameSet)1 Provider (com.github.jakz.romlib.data.set.Provider)1 OperationDetails (com.pixbits.lib.concurrent.OperationDetails)1 FileUtils (com.pixbits.lib.io.FileUtils)1 Log (com.pixbits.lib.log.Log)1 Logger (com.pixbits.lib.log.Logger)1 Main (jack.rm.Main)1 DownloadWorker (jack.rm.files.DownloadWorker)1 ZipExtractWorker (jack.rm.files.ZipExtractWorker)1 Dialogs (jack.rm.gui.Dialogs)1 LogSource (jack.rm.log.LogSource)1 LogTarget (jack.rm.log.LogTarget)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 StandardCopyOption (java.nio.file.StandardCopyOption)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Consumer (java.util.function.Consumer)1 ZipFile (java.util.zip.ZipFile)1