From b663af52f6ab62c3dbb63e0fecf9bab4d839892b Mon Sep 17 00:00:00 2001 From: Gurkengewuerz Date: Mon, 30 Mar 2020 17:19:07 +0200 Subject: [PATCH] added "UseWorkshop" function Try to find the correct pbo from the steam "!Workshop" folder in order to reduce downloads --- .../de/mc8051/arma3launcher/LauncherGUI.java | 14 ++++++- .../arma3launcher/repo/FileChecker.java | 4 +- .../arma3launcher/repo/sync/Syncer.java | 35 ++++++++++++++++ .../arma3launcher/repo/sync/WorkshopUtil.java | 40 +++++++++++++++++++ 4 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 gui/src/main/java/de/mc8051/arma3launcher/repo/sync/WorkshopUtil.java diff --git a/gui/src/main/java/de/mc8051/arma3launcher/LauncherGUI.java b/gui/src/main/java/de/mc8051/arma3launcher/LauncherGUI.java index 71dfc9f..ca5ebe1 100644 --- a/gui/src/main/java/de/mc8051/arma3launcher/LauncherGUI.java +++ b/gui/src/main/java/de/mc8051/arma3launcher/LauncherGUI.java @@ -1060,6 +1060,14 @@ public class LauncherGUI implements Observer { case FINNISHED: refreshRepoButton.setEnabled(true); updateRepoTree(); + + final Parameter checkModsetParameter = Parameters.CHECK_MODSET.toBooolParameter(); + if(checkModsetParameter.getValue() != null && checkModsetParameter.getValue()) { + if(!fileChecker.isChecked()) { + SwingUtilities.invokeLater(() -> fileCheck(false)); + Logger.getLogger(getClass().getName()).log(Level.INFO, "Started file check on launch"); + } + } break; case RUNNING: @@ -1122,7 +1130,8 @@ public class LauncherGUI implements Observer { lastSynclist = null; } else if (s.equals("syncStopped")) { - new Thread(() -> fileChecker.check(true)).start(); + final Parameter workshopParameter = Parameters.USE_WORKSHOP.toBooolParameter(); + new Thread(() -> fileChecker.check(!(workshopParameter.getValue() != null && workshopParameter.getValue()))).start(); SwingUtilities.invokeLater(() -> { syncDownloadButton.setEnabled(false); syncDownloadAbortButton.setEnabled(false); @@ -1133,7 +1142,8 @@ public class LauncherGUI implements Observer { TaskBarUtils.getInstance().off(); }); } else if (s.equals("syncComplete")) { - new Thread(() -> fileChecker.check(true)).start(); + final Parameter workshopParameter = Parameters.USE_WORKSHOP.toBooolParameter(); + new Thread(() -> fileChecker.check(!(workshopParameter.getValue() != null && workshopParameter.getValue()))).start(); SwingUtilities.invokeLater(() -> { syncDownloadButton.setEnabled(false); syncDownloadAbortButton.setEnabled(false); diff --git a/gui/src/main/java/de/mc8051/arma3launcher/repo/FileChecker.java b/gui/src/main/java/de/mc8051/arma3launcher/repo/FileChecker.java index e2fbee0..d56e139 100644 --- a/gui/src/main/java/de/mc8051/arma3launcher/repo/FileChecker.java +++ b/gui/src/main/java/de/mc8051/arma3launcher/repo/FileChecker.java @@ -138,7 +138,9 @@ public class FileChecker implements Observable { if (modPath == null) modPath = ""; try { - List filePathList = Files.find(Paths.get(modPath), + final Path path = Paths.get(modPath); + if(!path.toFile().exists() || !path.toFile().isDirectory()) return; + List filePathList = Files.find(path, Integer.MAX_VALUE, (filePath, fileAttr) -> fileAttr.isRegularFile()) .collect(Collectors.toList()); diff --git a/gui/src/main/java/de/mc8051/arma3launcher/repo/sync/Syncer.java b/gui/src/main/java/de/mc8051/arma3launcher/repo/sync/Syncer.java index b1607f0..6bbc0e6 100644 --- a/gui/src/main/java/de/mc8051/arma3launcher/repo/sync/Syncer.java +++ b/gui/src/main/java/de/mc8051/arma3launcher/repo/sync/Syncer.java @@ -5,6 +5,8 @@ import co.bitshfted.xapps.zsync.ZsyncException; import co.bitshfted.xapps.zsync.http.ContentRange; import de.mc8051.arma3launcher.ArmA3Launcher; import de.mc8051.arma3launcher.LauncherGUI; +import de.mc8051.arma3launcher.Parameter; +import de.mc8051.arma3launcher.Parameters; import de.mc8051.arma3launcher.interfaces.Observable; import de.mc8051.arma3launcher.interfaces.Observer; import de.mc8051.arma3launcher.objects.AbstractMod; @@ -13,15 +15,21 @@ import de.mc8051.arma3launcher.utils.Humanize; import de.mc8051.arma3launcher.utils.TaskBarUtils; import javax.swing.*; +import java.io.File; import java.io.IOException; import java.net.URI; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.stream.Collectors; /** * Created by gurkengewuerz.de on 25.03.2020. @@ -58,6 +66,8 @@ public class Syncer implements Observable, SyncListener { private Zsync zsync; private LauncherGUI gui; + private Map workshopFiles = new HashMap<>(); + public Syncer(LauncherGUI gui) { zsync = new Zsync(); this.gui = gui; @@ -88,6 +98,9 @@ public class Syncer implements Observable, SyncListener { TaskBarUtils.getInstance().normal(); }); + final Parameter workshopParameter = Parameters.USE_WORKSHOP.toBooolParameter(); + if(workshopParameter.getValue() != null && workshopParameter.getValue()) workshopFiles = WorkshopUtil.workshopFiles(); + boolean lastPause = false; while (running) { if (stopped) { @@ -131,6 +144,28 @@ public class Syncer implements Observable, SyncListener { } if (mf != null) { + final Path mfPath = mf.getLocaleFile().toPath(); + if(!workshopFiles.isEmpty()) { + try { + final String modfilePatj = mf.getModfileString().replace("/", File.separator).toLowerCase(); + Map.Entry workshopFile = workshopFiles.entrySet() + .stream().filter(e -> e.getKey().toAbsolutePath().toString().toLowerCase().endsWith(modfilePatj)).findFirst().get(); + if(workshopFile.getValue() == mf.getSize()) { + Files.copy(workshopFile.getKey(), mfPath, StandardCopyOption.REPLACE_EXISTING); + Logger.getLogger(getClass().getName()).log(Level.INFO, "Found workshop file and copied: " + mfPath); + success++; + finnishCurrent(); + continue; + } + } catch (NoSuchElementException | IOException ignored) {} + } + if(workshopFiles.containsKey(mfPath)) { + final Long workshopFileSize = workshopFiles.get(mfPath); + if(mf.getSize() == workshopFileSize) { + Logger.getLogger(getClass().getName()).log(Level.INFO, mfPath + ""); + } + } + Zsync.Options o = new Zsync.Options(); o.setOutputFile(Paths.get(mf.getLocaleFile().getAbsolutePath())); o.setUseragent(ArmA3Launcher.USER_AGENT); diff --git a/gui/src/main/java/de/mc8051/arma3launcher/repo/sync/WorkshopUtil.java b/gui/src/main/java/de/mc8051/arma3launcher/repo/sync/WorkshopUtil.java new file mode 100644 index 0000000..c4a6a56 --- /dev/null +++ b/gui/src/main/java/de/mc8051/arma3launcher/repo/sync/WorkshopUtil.java @@ -0,0 +1,40 @@ +package de.mc8051.arma3launcher.repo.sync; + +import de.mc8051.arma3launcher.Parameters; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * Created by gurkengewuerz.de on 30.03.2020. + */ +public class WorkshopUtil { + + public static Map workshopFiles() { + Map fileMap = new HashMap<>(); + + final String armaPath = Parameters.ARMA_PATH.toStringParameter().getValue(); + if(armaPath == null) return fileMap; + + final Path workshopPath = Paths.get(armaPath, "!Workshop"); + + if(!workshopPath.toFile().exists()) return fileMap; + if(!workshopPath.toFile().isDirectory()) return fileMap; + + try { + fileMap = Files.find(workshopPath, + Integer.MAX_VALUE, + (filePath, fileAttr) -> fileAttr.isRegularFile()) + .filter((p) -> p.toFile().getName().endsWith(".pbo")) + .collect(Collectors.toMap(path -> path, path -> path.toFile().length())); + return fileMap; + } catch (IOException ex) { + return fileMap; + } + } +}