remove some dependencies and write own utils

This commit is contained in:
Niklas 2020-03-27 14:40:44 +01:00
parent ed51a990a9
commit 9c0b6bcf83
13 changed files with 211 additions and 65 deletions

41
pom.xml
View File

@ -26,16 +26,6 @@
<artifactId>config</artifactId> <artifactId>config</artifactId>
<version>1.4.0</version> <version>1.4.0</version>
</dependency> </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.14</version>
</dependency>
<dependency> <dependency>
<groupId>com.formdev</groupId> <groupId>com.formdev</groupId>
<artifactId>flatlaf</artifactId> <artifactId>flatlaf</artifactId>
@ -51,31 +41,38 @@
<artifactId>ini4j</artifactId> <artifactId>ini4j</artifactId>
<version>0.5.4</version> <version>0.5.4</version>
</dependency> </dependency>
<dependency>
<groupId>com.github.mfornos</groupId>
<artifactId>humanize-slim</artifactId>
<version>1.2.2</version>
</dependency>
<dependency> <dependency>
<groupId>com.github.Gurkengewuerz</groupId> <groupId>com.github.Gurkengewuerz</groupId>
<artifactId>zsyncer</artifactId> <artifactId>zsyncer</artifactId>
<version>locale_fix-2f7565d392-1</version> <version>locale_fix-2f7565d392-1</version>
</dependency> </dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.4.1</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/project.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
<plugins> <plugins>
<plugin> <plugin>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version> <version>3.5.1</version>
<configuration> <configuration>
<source>1.8</source> <source>12</source>
<target>1.8</target> <target>12</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>

View File

@ -13,6 +13,7 @@ import java.awt.event.WindowEvent;
import java.io.File; import java.io.File;
import java.util.Arrays; import java.util.Arrays;
import java.util.Locale; import java.util.Locale;
import java.util.Properties;
import java.util.Timer; import java.util.Timer;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -35,7 +36,11 @@ public class ArmA3Launcher {
config = ConfigFactory.load("arma3launcher"); config = ConfigFactory.load("arma3launcher");
CLIENT_NAME = config.getString("name"); CLIENT_NAME = config.getString("name");
VERSION = config.getString("version");
final Properties properties = new Properties();
properties.load(ArmA3Launcher.class.getClassLoader().getResourceAsStream("project.properties"));
VERSION = properties.getProperty("version");
APPLICATION_PATH = getAppData() + CLIENT_NAME; APPLICATION_PATH = getAppData() + CLIENT_NAME;

View File

@ -8,7 +8,7 @@
<properties/> <properties/>
<border type="none"/> <border type="none"/>
<children> <children>
<splitpane id="6353c"> <splitpane id="6353c" binding="splitView">
<constraints> <constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"> <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="200" height="200"/> <preferred-size width="200" height="200"/>
@ -16,7 +16,7 @@
</constraints> </constraints>
<properties> <properties>
<continuousLayout value="false"/> <continuousLayout value="false"/>
<dividerLocation value="160"/> <dividerLocation value="-1"/>
<dividerSize value="10"/> <dividerSize value="10"/>
<enabled value="false"/> <enabled value="false"/>
<orientation value="1"/> <orientation value="1"/>

View File

@ -18,8 +18,8 @@ import de.mc8051.arma3launcher.repo.SyncList;
import de.mc8051.arma3launcher.repo.Syncer; import de.mc8051.arma3launcher.repo.Syncer;
import de.mc8051.arma3launcher.steam.SteamTimer; import de.mc8051.arma3launcher.steam.SteamTimer;
import de.mc8051.arma3launcher.utils.Callback; import de.mc8051.arma3launcher.utils.Callback;
import de.mc8051.arma3launcher.utils.Humanize;
import de.mc8051.arma3launcher.utils.LangUtils; import de.mc8051.arma3launcher.utils.LangUtils;
import humanize.Humanize;
import javax.swing.*; import javax.swing.*;
import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionEvent;
@ -134,6 +134,7 @@ public class LauncherGUI implements Observer {
private JLabel syncFileCountLabel; private JLabel syncFileCountLabel;
public JLabel syncDownloadedLabel; public JLabel syncDownloadedLabel;
public JLabel syncDownloadSpeedLabel; public JLabel syncDownloadSpeedLabel;
private JSplitPane splitView;
private JCheckBoxTree repoTree; private JCheckBoxTree repoTree;
private FileChecker fileChecker; private FileChecker fileChecker;
@ -317,6 +318,7 @@ public class LauncherGUI implements Observer {
syncDownloadButton.addActionListener(new ActionListener() { syncDownloadButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if(lastSynclist == null) return;
new Thread(() -> syncer.sync(lastSynclist.clone())).start(); new Thread(() -> syncer.sync(lastSynclist.clone())).start();
} }
}); });
@ -365,6 +367,7 @@ public class LauncherGUI implements Observer {
armaStatus.setText(LangUtils.getInstance().getString("closed")); armaStatus.setText(LangUtils.getInstance().getString("closed"));
armaStatus.setForeground(Color.red); armaStatus.setForeground(Color.red);
} }
splitView.setDividerLocation(-1);
} }
public void techCheck() { public void techCheck() {
@ -629,6 +632,7 @@ public class LauncherGUI implements Observer {
// Select Mod if in modset.Mods // Select Mod if in modset.Mods
// Custom Checkbox Render // Custom Checkbox Render
// Wenn modset.type == Server alle Checkboxen deaktivieren! // Wenn modset.type == Server alle Checkboxen deaktivieren!
// Show hint that server modsets cant be edited
} }
public void updateRepoTree() { public void updateRepoTree() {

View File

@ -1,15 +1,15 @@
package de.mc8051.arma3launcher.objects; package de.mc8051.arma3launcher.objects;
import de.mc8051.arma3launcher.ArmA3Launcher; import de.mc8051.arma3launcher.ArmA3Launcher;
import org.apache.commons.codec.digest.DigestUtils; import de.mc8051.arma3launcher.utils.FileUtils;
import org.apache.commons.io.FilenameUtils;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.logging.Level; import java.util.logging.Level;
@ -24,7 +24,6 @@ public class ModFile implements AbstractMod {
private long size; private long size;
private String folder; private String folder;
private String filename; private String filename;
private String extension;
private String modfileString; private String modfileString;
private String sha1sum; private String sha1sum;
private String parent; private String parent;
@ -36,9 +35,7 @@ public class ModFile implements AbstractMod {
// size: size as in metafile on server // size: size as in metafile on server
this.f = f; this.f = f;
this.size = size; this.size = size;
this.folder = FilenameUtils.getPath(modfile); this.filename = Paths.get(f.getPath()).getFileName().toString();
this.filename = FilenameUtils.getBaseName(modfile);
this.extension = FilenameUtils.getExtension(modfile);
this.modfileString = modfile; this.modfileString = modfile;
this.sha1sum = sha1sum.toLowerCase(); this.sha1sum = sha1sum.toLowerCase();
this.parent = parent; this.parent = parent;
@ -52,18 +49,6 @@ public class ModFile implements AbstractMod {
return size; return size;
} }
public String getReletaivePath() {
return folder;
}
public String getFilename() {
return filename;
}
public String getExtension() {
return extension;
}
public ArrayList<String> getPath() { public ArrayList<String> getPath() {
ArrayList<String> list = new ArrayList<>(); ArrayList<String> list = new ArrayList<>();
File relativePath = new File("./" + modfileString); File relativePath = new File("./" + modfileString);
@ -87,7 +72,7 @@ public class ModFile implements AbstractMod {
} }
public String getName() { public String getName() {
return filename + (extension.equals("") ? "" : "." + extension); return filename;
} }
public String getModfileString() { public String getModfileString() {
@ -105,9 +90,9 @@ public class ModFile implements AbstractMod {
public String getLocalGeneratedSHA1Sum() { public String getLocalGeneratedSHA1Sum() {
try { try {
if (localGeneratedSHA1sum.isEmpty() && exists()) { if (localGeneratedSHA1sum.isEmpty() && exists()) {
localGeneratedSHA1sum = DigestUtils.sha1Hex(new FileInputStream(f.getAbsolutePath())).toLowerCase(); localGeneratedSHA1sum = FileUtils.sha1Hex(f);
} }
} catch (IOException e) { } catch (IOException | NoSuchAlgorithmException e) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e); Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
} }
return localGeneratedSHA1sum; return localGeneratedSHA1sum;

View File

@ -9,14 +9,17 @@ import de.mc8051.arma3launcher.objects.ModFile;
import de.mc8051.arma3launcher.objects.Modset; import de.mc8051.arma3launcher.objects.Modset;
import de.mc8051.arma3launcher.objects.Server; import de.mc8051.arma3launcher.objects.Server;
import de.mc8051.arma3launcher.utils.Callback; import de.mc8051.arma3launcher.utils.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
@ -24,6 +27,8 @@ import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import static java.time.temporal.ChronoUnit.SECONDS;
/** /**
* Created by gurkengewuerz.de on 24.03.2020. * Created by gurkengewuerz.de on 24.03.2020.
*/ */
@ -36,28 +41,37 @@ public class RepositoryManger implements Observable {
private static HashMap<Type, DownloadStatus> statusMap = new HashMap<>(); private static HashMap<Type, DownloadStatus> statusMap = new HashMap<>();
private List<Observer> observerList = new ArrayList<>(); private List<Observer> observerList = new ArrayList<>();
private OkHttpClient client = new OkHttpClient();
private RepositoryManger() { private RepositoryManger() {
statusMap.put(Type.METADATA, DownloadStatus.FINNISHED); statusMap.put(Type.METADATA, DownloadStatus.FINNISHED);
statusMap.put(Type.MODSET, DownloadStatus.FINNISHED); statusMap.put(Type.MODSET, DownloadStatus.FINNISHED);
} }
private void getAsync(String url, Callback.HttpCallback callback) { private void getAsync(String urlS, Callback.HttpCallback callback) {
new Thread(() -> { new Thread(() -> {
try { try {
Request request = new Request.Builder() URI url = new URI(urlS);
.url(url)
HttpRequest request = HttpRequest.newBuilder()
.uri(url)
.GET()
.headers("Content-Type", "text/plain;charset=UTF-8")
.timeout(Duration.of(3, SECONDS))
.build(); .build();
Response r = client.newCall(request).execute(); HttpResponse<String> response = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.ALWAYS)
.build().send(request, HttpResponse.BodyHandlers.ofString());
Response r = new Response(response);
if (!r.isSuccessful()) { if (!r.isSuccessful()) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Cant open " + r.request().url().toString() + " code " + r.code()); Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Cant open " + r.request().uri() + " code " + r.getStatusCode());
return; return;
} }
callback.response(r); callback.response(r);
} catch (IOException e) { } catch (IOException | URISyntaxException | InterruptedException e) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e); Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
callback.response(null); callback.response(null);
} }
@ -77,7 +91,7 @@ public class RepositoryManger implements Observable {
} }
try { try {
JSONObject jsonObject = new JSONObject(r.body().string()); JSONObject jsonObject = new JSONObject(r.getBody());
if (jsonObject.has("modsets")) { if (jsonObject.has("modsets")) {
Modset.MODSET_LIST.clear(); Modset.MODSET_LIST.clear();
@ -104,7 +118,7 @@ public class RepositoryManger implements Observable {
statusMap.replace(Type.METADATA, DownloadStatus.FINNISHED); statusMap.replace(Type.METADATA, DownloadStatus.FINNISHED);
RepositoryManger.getInstance().notifyObservers(Type.METADATA.toString()); RepositoryManger.getInstance().notifyObservers(Type.METADATA.toString());
} catch (IOException | NullPointerException e) { } catch (NullPointerException e) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e); Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
} }
} }
@ -126,7 +140,7 @@ public class RepositoryManger implements Observable {
try { try {
RepositoryManger.MOD_LIST.clear(); RepositoryManger.MOD_LIST.clear();
RepositoryManger.MOD_LIST_SIZE = 0; RepositoryManger.MOD_LIST_SIZE = 0;
JSONObject jsonObject = new JSONObject(r.body().string()); JSONObject jsonObject = new JSONObject(r.getBody());
String modPath = ArmA3Launcher.user_config.get("client", "modPath"); String modPath = ArmA3Launcher.user_config.get("client", "modPath");
if(modPath == null) modPath = ""; if(modPath == null) modPath = "";
@ -170,7 +184,7 @@ public class RepositoryManger implements Observable {
statusMap.replace(Type.MODSET, DownloadStatus.FINNISHED); statusMap.replace(Type.MODSET, DownloadStatus.FINNISHED);
RepositoryManger.getInstance().notifyObservers(Type.MODSET.toString()); RepositoryManger.getInstance().notifyObservers(Type.MODSET.toString());
} catch (IOException | NullPointerException e) { } catch (NullPointerException e) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e); Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
} }
} }

View File

@ -0,0 +1,34 @@
package de.mc8051.arma3launcher.repo;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
/**
* Created by gurkengewuerz.de on 27.03.2020.
*/
public class Response {
private int statusCode;
private HttpResponse<String> response;
public Response(HttpResponse<String> response) {
this.response = response;
statusCode = response.statusCode();
}
public boolean isSuccessful() {
return statusCode >= 200 && statusCode <= 299;
}
public HttpRequest request() {
return response.request();
}
public int getStatusCode() {
return statusCode;
}
public String getBody() {
return response.body();
}
}

View File

@ -9,7 +9,7 @@ import de.mc8051.arma3launcher.interfaces.Observable;
import de.mc8051.arma3launcher.interfaces.Observer; import de.mc8051.arma3launcher.interfaces.Observer;
import de.mc8051.arma3launcher.objects.AbstractMod; import de.mc8051.arma3launcher.objects.AbstractMod;
import de.mc8051.arma3launcher.objects.ModFile; import de.mc8051.arma3launcher.objects.ModFile;
import humanize.Humanize; import de.mc8051.arma3launcher.utils.Humanize;
import javax.swing.*; import javax.swing.*;
import java.io.IOException; import java.io.IOException;

View File

@ -1,6 +1,6 @@
package de.mc8051.arma3launcher.utils; package de.mc8051.arma3launcher.utils;
import okhttp3.Response; import de.mc8051.arma3launcher.repo.Response;
import java.io.File; import java.io.File;

View File

@ -0,0 +1,68 @@
package de.mc8051.arma3launcher.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* Created by gurkengewuerz.de on 27.03.2020.
*/
public class FileUtils {
public static final Charset DEFAULT_CHARSET;
private static final char[] DIGITS_LOWER;
private static final char[] DIGITS_UPPER;
public static String sha1Hex(File f) throws IOException, NoSuchAlgorithmException {
return encodeHexString(sha1(f));
}
public static byte[] sha1(File file) throws IOException, NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
InputStream fis = new FileInputStream(file);
int n = 0;
byte[] buffer = new byte[8192];
while (n != -1) {
n = fis.read(buffer);
if (n > 0) {
digest.update(buffer, 0, n);
}
}
return digest.digest();
}
public static String encodeHexString(byte[] data) {
return new String(encodeHex(data));
}
public static char[] encodeHex(byte[] data) {
return encodeHex(data, true);
}
public static char[] encodeHex(byte[] data, boolean toLowerCase) {
return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
}
protected static char[] encodeHex(byte[] data, char[] toDigits) {
int l = data.length;
char[] out = new char[l << 1];
int i = 0;
for(int var5 = 0; i < l; ++i) {
out[var5++] = toDigits[(240 & data[i]) >>> 4];
out[var5++] = toDigits[15 & data[i]];
}
return out;
}
static {
DEFAULT_CHARSET = StandardCharsets.UTF_8;
DIGITS_LOWER = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
DIGITS_UPPER = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
}
}

View File

@ -0,0 +1,38 @@
package de.mc8051.arma3launcher.utils;
import java.text.DecimalFormat;
/**
* Created by gurkengewuerz.de on 27.03.2020.
*/
public class Humanize {
private static final long K = 1024;
private static final long M = K * K;
private static final long G = M * K;
private static final long T = G * K;
public static String binaryPrefix(final long value){
final long[] dividers = new long[] { T, G, M, K, 1 };
final String[] units = new String[] { "TB", "GB", "MB", "KB", "B" };
if(value < 1)
throw new IllegalArgumentException("Invalid file size: " + value);
String result = null;
for(int i = 0; i < dividers.length; i++){
final long divider = dividers[i];
if(value >= divider){
result = format(value, divider, units[i]);
break;
}
}
return result;
}
private static String format(final long value,
final long divider,
final String unit){
final double result =
divider > 1 ? (double) value / (double) divider : (double) value;
return new DecimalFormat("#,##0.#").format(result) + " " + unit;
}
}

View File

@ -1,5 +1,4 @@
{ {
"version": "0.1.1",
"name": "TheTown Client", "name": "TheTown Client",
"title": "Welcome! :P", "title": "Welcome! :P",
"subtitle": "${name} v${version}", "subtitle": "${name} v${version}",

View File

@ -0,0 +1,2 @@
version=${project.version}
artifactId=${project.artifactId}