2020-03-26 02:26:31 +01:00
|
|
|
package de.mc8051.arma3launcher.repo;
|
|
|
|
|
|
|
|
import de.mc8051.arma3launcher.ArmA3Launcher;
|
|
|
|
import de.mc8051.arma3launcher.interfaces.Observable;
|
|
|
|
import de.mc8051.arma3launcher.interfaces.Observer;
|
|
|
|
import de.mc8051.arma3launcher.objects.AbstractMod;
|
|
|
|
import de.mc8051.arma3launcher.objects.Mod;
|
|
|
|
import de.mc8051.arma3launcher.objects.ModFile;
|
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.lang.reflect.Array;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by gurkengewuerz.de on 26.03.2020.
|
|
|
|
*/
|
|
|
|
public class FileChecker implements Observable {
|
|
|
|
|
|
|
|
private List<Observer> observerList = new ArrayList<>();
|
|
|
|
private JProgressBar pb;
|
|
|
|
private boolean stop = false;
|
|
|
|
|
2020-03-28 05:17:50 +01:00
|
|
|
private boolean checked = false;
|
|
|
|
|
2020-03-27 03:04:15 +01:00
|
|
|
private ArrayList<Path> deleted = new ArrayList<>();
|
|
|
|
private HashMap<String, ArrayList<ModFile>> changed = new HashMap<>();
|
2020-03-26 02:26:31 +01:00
|
|
|
int changedCount = 0;
|
2020-03-27 03:04:15 +01:00
|
|
|
private HashMap<String, ArrayList<ModFile>> added = new HashMap<>();
|
2020-03-26 02:26:31 +01:00
|
|
|
int addedCount = 0;
|
|
|
|
|
|
|
|
long size = 0;
|
|
|
|
|
|
|
|
public FileChecker(JProgressBar pb) {
|
|
|
|
this.pb = pb;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void check() {
|
|
|
|
deleted.clear();
|
|
|
|
changed.clear();
|
|
|
|
changedCount = 0;
|
|
|
|
added.clear();
|
|
|
|
addedCount = 0;
|
|
|
|
size = 0;
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
SwingUtilities.invokeLater(() -> {
|
|
|
|
pb.setMaximum(RepositoryManger.MOD_LIST_SIZE);
|
|
|
|
pb.setValue(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
for (AbstractMod abstractMod : RepositoryManger.MOD_LIST) {
|
|
|
|
if(stop) {
|
|
|
|
stop = false;
|
|
|
|
notifyObservers("fileCheckerStopped");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(abstractMod instanceof Mod) {
|
|
|
|
Mod m = (Mod) abstractMod;
|
|
|
|
|
|
|
|
for (ModFile mf : m.getFiles()) {
|
2020-03-27 03:04:15 +01:00
|
|
|
checkFile(m.getName(), mf);
|
|
|
|
|
2020-03-26 02:26:31 +01:00
|
|
|
i++;
|
|
|
|
int finalI = i;
|
|
|
|
SwingUtilities.invokeLater(() -> {
|
|
|
|
pb.setValue(finalI);
|
|
|
|
});
|
|
|
|
|
|
|
|
if(stop) {
|
|
|
|
stop = false;
|
|
|
|
notifyObservers("fileCheckerStopped");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (abstractMod instanceof ModFile) {
|
|
|
|
ModFile mf = (ModFile) abstractMod;
|
|
|
|
checkFile(mf.getName(), mf);
|
|
|
|
i++;
|
|
|
|
int finalI1 = i;
|
|
|
|
SwingUtilities.invokeLater(() -> {
|
|
|
|
pb.setValue(finalI1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkDeleted();
|
|
|
|
notifyObservers("fileChecker");
|
2020-03-28 05:17:50 +01:00
|
|
|
checked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isChecked() {
|
|
|
|
return checked;
|
2020-03-26 02:26:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void stop() {
|
|
|
|
stop = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkFile(String mod, ModFile mf) {
|
2020-03-27 03:04:15 +01:00
|
|
|
ArrayList<ModFile> temp = new ArrayList<>();
|
|
|
|
|
2020-03-26 02:26:31 +01:00
|
|
|
if(!mf.exists()) {
|
2020-03-27 03:04:15 +01:00
|
|
|
if(added.containsKey(mod)) temp =added.get(mod);
|
|
|
|
temp.add(mf);
|
|
|
|
added.put(mod, temp);
|
2020-03-26 02:26:31 +01:00
|
|
|
addedCount++;
|
|
|
|
size += mf.getSize();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-27 03:04:15 +01:00
|
|
|
if(mf.getLocalSize() != mf.getSize() || !mf.getSHA1Sum().equalsIgnoreCase(mf.getLocalGeneratedSHA1Sum())) {
|
|
|
|
if(changed.containsKey(mod)) temp =changed.get(mod);
|
|
|
|
temp.add(mf);
|
|
|
|
changed.put(mod, temp);
|
2020-03-26 02:26:31 +01:00
|
|
|
changedCount++;
|
|
|
|
size += mf.getSize();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkDeleted() {
|
|
|
|
String modPath = ArmA3Launcher.user_config.get("client", "modPath");
|
|
|
|
if(modPath == null) modPath = "";
|
|
|
|
|
|
|
|
try {
|
|
|
|
List<Path> filePathList = Files.find(Paths.get(modPath),
|
|
|
|
Integer.MAX_VALUE,
|
|
|
|
(filePath, fileAttr) -> fileAttr.isRegularFile())
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
|
|
for (Path localPath : filePathList) {
|
|
|
|
ModFile deleteable = null;
|
|
|
|
|
|
|
|
outerloop:
|
|
|
|
for (AbstractMod abstractMod : RepositoryManger.MOD_LIST) {
|
|
|
|
if(abstractMod instanceof Mod) {
|
|
|
|
Mod m = (Mod) abstractMod;
|
|
|
|
|
|
|
|
for (ModFile mf : m.getFiles()) {
|
|
|
|
if (mf.getLocaleFile().getPath().equals(localPath.toString())) {
|
|
|
|
deleteable = mf;
|
|
|
|
break outerloop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (abstractMod instanceof ModFile) {
|
|
|
|
ModFile mf = (ModFile) abstractMod;
|
|
|
|
if (mf.getLocaleFile().getPath().equals(localPath.toString())) {
|
|
|
|
deleteable = mf;
|
|
|
|
break outerloop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (deleteable == null) {
|
|
|
|
deleted.add(localPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public ArrayList<Path> getDeleted() {
|
|
|
|
return deleted;
|
|
|
|
}
|
|
|
|
|
|
|
|
public HashMap<String, ArrayList<ModFile>> getChanged() {
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
public HashMap<String, ArrayList<ModFile>> getAdded() {
|
|
|
|
return added;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getDeletedCount() {
|
|
|
|
return deleted.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getChangedCount() {
|
|
|
|
return changedCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getAddedCount() {
|
|
|
|
return addedCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getSize() {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void addObserver(Observer observer) {
|
|
|
|
observerList.add(observer);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeObserver(Observer observer) {
|
|
|
|
observerList.remove(observer);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void notifyObservers(String obj) {
|
|
|
|
for (Observer obs : observerList) obs.update(obj);
|
|
|
|
}
|
|
|
|
}
|