2020-03-26 02:26:31 +01:00
|
|
|
package de.mc8051.arma3launcher.objects;
|
|
|
|
|
2020-03-27 03:04:15 +01:00
|
|
|
import de.mc8051.arma3launcher.ArmA3Launcher;
|
2020-03-27 14:40:44 +01:00
|
|
|
import de.mc8051.arma3launcher.utils.FileUtils;
|
2020-03-30 02:46:25 +02:00
|
|
|
import de.mc8051.arma3launcher.utils.URLUtils;
|
2020-04-01 18:55:10 +02:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2020-03-26 02:26:31 +01:00
|
|
|
|
|
|
|
import java.io.File;
|
2020-03-27 03:04:15 +01:00
|
|
|
import java.io.IOException;
|
2020-03-27 14:40:44 +01:00
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2020-03-26 02:26:31 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by gurkengewuerz.de on 25.03.2020.
|
|
|
|
*/
|
|
|
|
public class ModFile implements AbstractMod {
|
|
|
|
|
2020-04-01 18:55:10 +02:00
|
|
|
private static final Logger logger = LogManager.getLogger(ModFile.class);
|
|
|
|
|
2020-03-26 02:26:31 +01:00
|
|
|
private File f;
|
|
|
|
private long size;
|
|
|
|
private String folder;
|
|
|
|
private String filename;
|
|
|
|
private String modfileString;
|
2020-03-27 03:04:15 +01:00
|
|
|
private String sha1sum;
|
|
|
|
private String parent;
|
|
|
|
private String localGeneratedSHA1sum = "";
|
2020-04-16 19:49:31 +02:00
|
|
|
private long lastModified = -1;
|
|
|
|
private long localLastModified = -1;
|
2020-03-26 02:26:31 +01:00
|
|
|
|
2020-04-16 19:49:31 +02:00
|
|
|
public ModFile(File f, String modfile, String parent, long size, String sha1sum, long lastModified) {
|
2020-03-26 02:26:31 +01:00
|
|
|
// File: Abosolut Path
|
|
|
|
// modfile: addons/config/something.pbo
|
|
|
|
// size: size as in metafile on server
|
|
|
|
this.f = f;
|
|
|
|
this.size = size;
|
2020-03-27 14:40:44 +01:00
|
|
|
this.filename = Paths.get(f.getPath()).getFileName().toString();
|
2020-03-26 02:26:31 +01:00
|
|
|
this.modfileString = modfile;
|
2020-03-27 03:04:15 +01:00
|
|
|
this.sha1sum = sha1sum.toLowerCase();
|
|
|
|
this.parent = parent;
|
2020-04-16 19:49:31 +02:00
|
|
|
this.lastModified = lastModified;
|
2020-03-27 03:04:15 +01:00
|
|
|
}
|
|
|
|
|
2020-04-16 19:49:31 +02:00
|
|
|
public ModFile(File f, String modfile, long size, String sha1sum, long lastModified) {
|
|
|
|
this(f, modfile, null, size, sha1sum, lastModified);
|
2020-03-26 02:26:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public long getSize() {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ArrayList<String> getPath() {
|
|
|
|
ArrayList<String> list = new ArrayList<>();
|
2020-03-27 03:04:15 +01:00
|
|
|
File relativePath = new File("./" + modfileString);
|
|
|
|
|
2020-03-26 02:26:31 +01:00
|
|
|
do {
|
|
|
|
list.add(relativePath.getName());
|
|
|
|
relativePath = relativePath.getParentFile();
|
|
|
|
} while (relativePath.getParentFile() != null);
|
|
|
|
Collections.reverse(list);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getLocalSize() {
|
2020-03-27 03:04:15 +01:00
|
|
|
if (!f.exists() || !f.isFile()) return -1;
|
2020-03-26 02:26:31 +01:00
|
|
|
return f.length();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean exists() {
|
2020-03-27 03:04:15 +01:00
|
|
|
if (!f.exists() || !f.isFile()) return false;
|
2020-03-26 02:26:31 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getName() {
|
2020-03-27 14:40:44 +01:00
|
|
|
return filename;
|
2020-03-26 02:26:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getModfileString() {
|
|
|
|
return modfileString;
|
|
|
|
}
|
|
|
|
|
2020-03-28 05:17:50 +01:00
|
|
|
public String getModPath() {
|
|
|
|
return (parent == null ? "" : parent + "/") + modfileString;
|
|
|
|
}
|
|
|
|
|
2020-03-26 02:26:31 +01:00
|
|
|
public File getLocaleFile() {
|
|
|
|
return f;
|
|
|
|
}
|
2020-03-27 03:04:15 +01:00
|
|
|
|
|
|
|
public String getSHA1Sum() {
|
|
|
|
return sha1sum;
|
|
|
|
}
|
|
|
|
|
2020-04-16 19:49:31 +02:00
|
|
|
public long getLocalLastModified() {
|
|
|
|
if (localLastModified <= 0 && exists()) {
|
|
|
|
localLastModified = (int) (f.lastModified() / 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
return localLastModified;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getLastModified() {
|
|
|
|
return lastModified;
|
|
|
|
}
|
|
|
|
|
2020-03-27 03:04:15 +01:00
|
|
|
public String getLocalGeneratedSHA1Sum() {
|
|
|
|
try {
|
|
|
|
if (localGeneratedSHA1sum.isEmpty() && exists()) {
|
2020-03-27 14:40:44 +01:00
|
|
|
localGeneratedSHA1sum = FileUtils.sha1Hex(f);
|
2020-03-27 03:04:15 +01:00
|
|
|
}
|
2020-03-27 14:40:44 +01:00
|
|
|
} catch (IOException | NoSuchAlgorithmException e) {
|
2020-04-01 18:55:10 +02:00
|
|
|
logger.error(e);
|
2020-03-27 03:04:15 +01:00
|
|
|
}
|
|
|
|
return localGeneratedSHA1sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getRemoteFile() {
|
|
|
|
String s = ArmA3Launcher.config.getString("sync.url");
|
|
|
|
if (parent == null || parent.isEmpty()) {
|
2020-03-30 02:46:25 +02:00
|
|
|
return s + "/" + URLUtils.encodeToURL(getName());
|
2020-03-27 03:04:15 +01:00
|
|
|
}
|
|
|
|
|
2020-03-30 02:46:25 +02:00
|
|
|
s += "/" + URLUtils.encodeToURL(parent);
|
2020-03-27 03:04:15 +01:00
|
|
|
for (String seg : getPath()) {
|
2020-03-30 02:46:25 +02:00
|
|
|
s += "/" + URLUtils.encodeToURL(seg);
|
2020-03-27 03:04:15 +01:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getParent() {
|
|
|
|
return parent;
|
|
|
|
}
|
2020-03-26 02:26:31 +01:00
|
|
|
}
|