properly implement download speed checking
All checks were successful
/ Build BaseBand DSM & Broadway (push) Successful in 2m11s
All checks were successful
/ Build BaseBand DSM & Broadway (push) Successful in 2m11s
This commit is contained in:
parent
e2ff3c5a14
commit
ea62001d41
4 changed files with 38 additions and 53 deletions
|
@ -11,7 +11,9 @@ import de.tudbut.io.TypedInputStream;
|
|||
import de.tudbut.io.TypedOutputStream;
|
||||
import de.tudbut.net.http.HTTPUtils;
|
||||
import de.tudbut.parsing.TCN;
|
||||
import de.tudbut.security.AccessKiller;
|
||||
import de.tudbut.tools.Hasher;
|
||||
import de.tudbut.tools.SimpleLock;
|
||||
import de.tudbut.tools.Tools;
|
||||
import de.tudbut.tools.encryption.Key;
|
||||
import de.tudbut.tools.encryption.RawKey;
|
||||
|
@ -29,6 +31,7 @@ import java.net.Socket;
|
|||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
@ -59,8 +62,14 @@ public class Loader implements Util {
|
|||
private static CustomClassLoader classLoader;
|
||||
|
||||
public static void run() {
|
||||
AccessKiller.killReflectionFor(
|
||||
Tweaker.class,
|
||||
Loader.class,
|
||||
CustomClassLoader.class,
|
||||
CustomClassLoader.CustomMixinServer.class);
|
||||
|
||||
try(Socket client = new Socket("baseband.com.de", 40000)) {
|
||||
client.setSoTimeout(15000);
|
||||
client.setSoTimeout(5000);
|
||||
client.getOutputStream().write(0);
|
||||
TypedInputStream inputStream = new TypedInputStream(client.getInputStream());
|
||||
TypedOutputStream outputStream = new TypedOutputStream(client.getOutputStream());
|
||||
|
@ -92,12 +101,37 @@ public class Loader implements Util {
|
|||
TCN clientData = TCN.readMap(Tools.stringToMap(key.decryptString(inputStream.readString())));
|
||||
|
||||
HashMap<String, byte[]> data = new HashMap<>();
|
||||
SimpleLock downloadUpdated = new SimpleLock();
|
||||
SimpleLock continueDownload = new SimpleLock();
|
||||
AtomicInteger bytes = new AtomicInteger();
|
||||
new Thread(() -> {
|
||||
long lastUpdate = System.currentTimeMillis();
|
||||
while (classLoader == null) {
|
||||
downloadUpdated.waitHere(2000);
|
||||
if(downloadUpdated.isLocked()) {
|
||||
LOGGER.warn("No new BaseBand chunk has been downloaded in 2 seconds. Consider restarting your game.");
|
||||
}
|
||||
else if(System.currentTimeMillis() - lastUpdate >= 500) {
|
||||
LOGGER.info("Downloading at {}KB/s...", (int) ((bytes.get() / 1024f) * (System.currentTimeMillis() - lastUpdate) / 1000f));
|
||||
lastUpdate = System.currentTimeMillis();
|
||||
bytes.set(0);
|
||||
}
|
||||
downloadUpdated.lock();
|
||||
continueDownload.unlock();
|
||||
}
|
||||
}, "Download guard").start();
|
||||
RawKey rk = new RawKey(key.toBytes());
|
||||
int n = inputStream.readInt();
|
||||
for (int i = 0; i < n; i++) {
|
||||
data.put(rk.decryptString(inputStream.readString()), rk.decryptBytes(inputStream.readByteArray()));
|
||||
String name = inputStream.readString();
|
||||
byte[] classBytes = inputStream.readByteArray();
|
||||
data.put(rk.decryptString(name), rk.decryptBytes(classBytes));
|
||||
bytes.getAndAdd(classBytes.length);
|
||||
continueDownload.lock();
|
||||
downloadUpdated.unlock();
|
||||
continueDownload.waitHere();
|
||||
}
|
||||
LOGGER.info("BaseBand downloaded: {} classes.", data.size());
|
||||
LOGGER.info("BaseBand downloaded: {} chunks.", data.size());
|
||||
classLoader = new CustomClassLoader(data);
|
||||
classLoader.inject();
|
||||
classLoader.informClient(clientData);
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
|
||||
package de.com.baseband.launcher;
|
||||
|
||||
import de.com.baseband.launcher.util.Util;
|
||||
import net.minecraft.launchwrapper.ITweaker;
|
||||
import net.minecraft.launchwrapper.LaunchClassLoader;
|
||||
import net.minecraftforge.common.ForgeVersion;
|
||||
|
@ -22,7 +21,7 @@ import java.util.Map;
|
|||
@SuppressWarnings("unused")
|
||||
@IFMLLoadingPlugin.Name("baseband")
|
||||
@IFMLLoadingPlugin.MCVersion(ForgeVersion.mcVersion)
|
||||
public class Tweaker implements ITweaker, IFMLLoadingPlugin, Util {
|
||||
public class Tweaker implements ITweaker, IFMLLoadingPlugin {
|
||||
|
||||
public static void loaded(Class<?> baseBandClass) {
|
||||
Loader.loaded(baseBandClass);
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package de.com.baseband.launcher.classloader;
|
||||
|
||||
import de.com.baseband.launcher.url.ByteURLHandler;
|
||||
import de.com.baseband.launcher.url.ResourceConnection;
|
||||
import de.com.baseband.launcher.url.URLWrapper;
|
||||
import de.com.baseband.launcher.util.Util;
|
||||
import de.tudbut.parsing.TCN;
|
||||
|
@ -43,13 +41,6 @@ public class CustomClassLoader extends ClassLoader implements Util {
|
|||
}
|
||||
|
||||
public CustomClassLoader(Map<String, byte[]> data) {
|
||||
AccessKiller.killReflectionFor(
|
||||
CustomClassLoader.class,
|
||||
CustomMixinServer.class,
|
||||
URLWrapper.class,
|
||||
ByteURLHandler.class,
|
||||
ResourceConnection.class);
|
||||
|
||||
names.addAll(data.keySet());
|
||||
this.binaryKeeper = new DataKeeper<>(
|
||||
new PermissionOR(new ClassLoaderRestriction(this), new CallClassRestriction(CustomClassLoader.class, CustomMixinServer.class)),
|
||||
|
|
|
@ -1,39 +0,0 @@
|
|||
package de.com.baseband.launcher.util;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
public class SecondCounter {
|
||||
|
||||
private static class TimeStampedByte {
|
||||
long timestamp;
|
||||
int bytes;
|
||||
|
||||
TimeStampedByte(long timestamp, int bytes) {
|
||||
this.timestamp = timestamp;
|
||||
this.bytes = bytes;
|
||||
}
|
||||
}
|
||||
|
||||
private final Queue<TimeStampedByte> count = new LinkedList<>();
|
||||
|
||||
public void increment(int bytes) {
|
||||
count.add(new TimeStampedByte(System.currentTimeMillis() + 1000L, bytes));
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
long time = System.currentTimeMillis();
|
||||
try {
|
||||
while (!count.isEmpty() && count.peek().timestamp < time) {
|
||||
count.remove();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// empty catch block
|
||||
}
|
||||
int byteCount = 0;
|
||||
for (TimeStampedByte tsb : count) {
|
||||
byteCount += tsb.bytes;
|
||||
}
|
||||
return byteCount;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue