Do not init ViaVersion on main thread, platform dump

This commit is contained in:
creeper123123321 2019-02-07 16:45:45 -02:00
parent bb2a937415
commit e8848372c2
No known key found for this signature in database
GPG Key ID: 0AC57D54786721D1
3 changed files with 98 additions and 106 deletions

View File

@ -22,9 +22,12 @@
* SOFTWARE.
*/
package com.github.creeper123123321.viafabric.platform;
package com.github.creeper123123321.viafabric;
import com.github.creeper123123321.viafabric.commands.VRCommandHandler;
import com.github.creeper123123321.viafabric.platform.VRInjector;
import com.github.creeper123123321.viafabric.platform.VRLoader;
import com.github.creeper123123321.viafabric.platform.VRPlatform;
import com.github.creeper123123321.viafabric.protocol.protocol1_7_6_10to1_7_1_5.Protocol1_7_6_10to1_7_1_5;
import com.github.creeper123123321.viafabric.protocol.protocol1_8to1_7_6_10.Protocol1_8TO1_7_6_10;
import us.myles.ViaVersion.ViaManager;
@ -36,13 +39,15 @@ import java.util.Collections;
public class VRViaVersionInitializer {
public static void init() {
Via.init(ViaManager.builder()
.injector(new VRInjector())
.loader(new VRLoader())
.commandHandler(new VRCommandHandler())
.platform(new VRPlatform()).build());
Via.getManager().init();
ProtocolRegistry.registerProtocol(new Protocol1_7_6_10to1_7_1_5(), Collections.singletonList(ProtocolVersion.v1_7_6.getId()), ProtocolVersion.v1_7_1.getId());
ProtocolRegistry.registerProtocol(new Protocol1_8TO1_7_6_10(), Collections.singletonList(ProtocolVersion.v1_8.getId()), ProtocolVersion.v1_7_6.getId());
ViaFabric.EVENT_LOOP.submit(() -> {
Via.init(ViaManager.builder()
.injector(new VRInjector())
.loader(new VRLoader())
.commandHandler(new VRCommandHandler())
.platform(new VRPlatform()).build());
Via.getManager().init();
ProtocolRegistry.registerProtocol(new Protocol1_7_6_10to1_7_1_5(), Collections.singletonList(ProtocolVersion.v1_7_6.getId()), ProtocolVersion.v1_7_1.getId());
ProtocolRegistry.registerProtocol(new Protocol1_8TO1_7_6_10(), Collections.singletonList(ProtocolVersion.v1_8.getId()), ProtocolVersion.v1_7_6.getId());
});
}
}

View File

@ -38,7 +38,6 @@ import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
@ -77,114 +76,82 @@ public class ViaFabric implements ClientModInitializer {
@Override
public void onInitializeClient() {
int timeDivisor = 1000 * 60 * 60 * 24;
long cachedTime = System.currentTimeMillis() / timeDivisor;
File viaVersionJar = FabricLoader.INSTANCE.getConfigDirectory().toPath().resolve("ViaFabric").resolve("viaversion.jar").toFile();
if (!(viaVersionJar.exists() && viaVersionJar.lastModified() / timeDivisor == cachedTime)) {
String localMd5 = null;
try {
if (viaVersionJar.exists()) {
try (InputStream is = Files.newInputStream(viaVersionJar.toPath())) {
localMd5 = DigestUtils.md5Hex(is);
}
}
URL versionsUrl = new URL("https://repo.viaversion.com/us/myles/viaversion/?" + cachedTime);
JLOGGER.info("Checking for ViaVersion updates " + versionsUrl);
HttpURLConnection con = (HttpURLConnection) versionsUrl.openConnection();
con.setRequestProperty("User-Agent", "ViaFabric/" + ViaFabric.getVersion());
String rawOutput = CharStreams.toString(new InputStreamReader(con.getInputStream()));
Pattern urlPattern = Pattern.compile("<A href='([^']*)/'>");
Matcher matcher = urlPattern.matcher(rawOutput);
List<String> versions = new ArrayList<>();
while (matcher.find()) {
versions.add(matcher.group(1));
}
String bestViaVersion = null;
String mcVersion = MinecraftClient.getInstance().getGame().getVersion().getName();
if (mcVersion.contains("w") || mcVersion.contains("-")) {
bestViaVersion = versions.stream()
.filter(it -> it.endsWith(mcVersion))
.max(Comparator.comparing(Version::new))
.orElse(null);
}
if (bestViaVersion == null) {
bestViaVersion = versions.stream()
.filter(it -> it.endsWith("-SNAPSHOT") || it.endsWith("-DEV") || !it.contains("-"))
.max(Comparator.comparing(Version::new))
.orElse(null);
}
HttpURLConnection md5Con = (HttpURLConnection) new URL(
"https://repo.viaversion.com/us/myles/viaversion/" + bestViaVersion
+ "/viaversion-" + bestViaVersion + ".jar.md5").openConnection();
md5Con.setRequestProperty("User-Agent", "ViaFabric/" + ViaFabric.getVersion());
String remoteMd5 = CharStreams.toString(new InputStreamReader(md5Con.getInputStream()));
if (!remoteMd5.equals(localMd5)) {
URL url = new URL("https://repo.viaversion.com/us/myles/viaversion/" + bestViaVersion
+ "/viaversion-" + bestViaVersion + ".jar");
ViaFabric.JLOGGER.info("Downloading " + url);
HttpURLConnection jarCon = (HttpURLConnection) url.openConnection();
jarCon.setRequestProperty("User-Agent", "ViaFabric/" + ViaFabric.getVersion());
FileUtils.copyInputStreamToFile(jarCon.getInputStream(), viaVersionJar);
} else {
JLOGGER.info("No updates found");
viaVersionJar.setLastModified(System.currentTimeMillis());
}
} catch (IOException e) {
e.printStackTrace();
}
try {
checkForUpdates(viaVersionJar, "viaversion", "us/myles", "");
} catch (Exception e) {
e.printStackTrace();
}
File viaRewindJar = FabricLoader.INSTANCE.getConfigDirectory().toPath().resolve("ViaFabric").resolve("viarewind.jar").toFile();
if (!(viaRewindJar.exists() && viaRewindJar.lastModified() / timeDivisor == cachedTime)) {
String localMd5 = null;
try {
if (viaRewindJar.exists()) {
try (InputStream is = Files.newInputStream(viaRewindJar.toPath())) {
localMd5 = DigestUtils.md5Hex(is);
}
}
URL versionsUrl = new URL("https://repo.viaversion.com/de/gerrygames/viarewind-all/?" + cachedTime);
JLOGGER.info("Checking for ViaRewind updates " + versionsUrl);
HttpURLConnection con = (HttpURLConnection) versionsUrl.openConnection();
con.setRequestProperty("User-Agent", "ViaFabric/" + ViaFabric.getVersion());
String rawOutput = CharStreams.toString(new InputStreamReader(con.getInputStream()));
Pattern urlPattern = Pattern.compile("<A href='([^']*)/'>");
Matcher matcher = urlPattern.matcher(rawOutput);
List<String> versions = new ArrayList<>();
while (matcher.find()) {
versions.add(matcher.group(1));
}
String bestViaRewind = versions.stream().max(Comparator.comparing(Version::new)).orElse(null);
HttpURLConnection md5Con = (HttpURLConnection) new URL(
"https://repo.viaversion.com/de/gerrygames/viarewind-all/" + bestViaRewind
+ "/viarewind-all-" + bestViaRewind + ".jar.md5").openConnection();
md5Con.setRequestProperty("User-Agent", "ViaFabric/" + ViaFabric.getVersion());
String remoteMd5 = CharStreams.toString(new InputStreamReader(md5Con.getInputStream()));
if (!remoteMd5.equals(localMd5)) {
URL url = new URL("https://repo.viaversion.com/de/gerrygames/viarewind-all/" + bestViaRewind
+ "/viarewind-all-" + bestViaRewind + ".jar");
ViaFabric.JLOGGER.info("Downloading " + url);
HttpURLConnection jarCon = (HttpURLConnection) url.openConnection();
jarCon.setRequestProperty("User-Agent", "ViaFabric/" + ViaFabric.getVersion());
FileUtils.copyInputStreamToFile(jarCon.getInputStream(), viaRewindJar);
} else {
JLOGGER.info("No updates found");
viaRewindJar.setLastModified(System.currentTimeMillis());
}
} catch (IOException e) {
e.printStackTrace();
}
try {
checkForUpdates(viaRewindJar, "viarewind-all", "de/gerrygames", "ViaRewind");
} catch (Exception e) {
e.printStackTrace();
}
try {
Method addUrl = ViaFabric.class.getClassLoader().getClass().getMethod("addURL", URL.class);
addUrl.setAccessible(true);
addUrl.invoke(ViaFabric.class.getClassLoader(), viaVersionJar.toURI().toURL());
addUrl.invoke(ViaFabric.class.getClassLoader(), viaRewindJar.toURI().toURL());
Class.forName("com.github.creeper123123321.viafabric.platform.VRViaVersionInitializer")
Class.forName("com.github.creeper123123321.viafabric.VRViaVersionInitializer")
.getMethod("init")
.invoke(null);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | MalformedURLException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
private void checkForUpdates(File jar, String artifactName, String groupIdPath, String depName) throws Exception {
int timeDivisor = 1000 * 60 * 60 * 24;
long cachedTime = System.currentTimeMillis() / timeDivisor;
if (!(jar.exists() && jar.lastModified() / timeDivisor == cachedTime)) {
String localMd5 = null;
if (jar.exists()) {
try (InputStream is = Files.newInputStream(jar.toPath())) {
localMd5 = DigestUtils.md5Hex(is);
}
}
URL versionsUrl = new URL("https://repo.viaversion.com/" + groupIdPath + "/" + artifactName + "/?" + cachedTime);
JLOGGER.info("Checking for " + depName + " updates " + versionsUrl);
HttpURLConnection con = (HttpURLConnection) versionsUrl.openConnection();
con.setRequestProperty("User-Agent", "ViaFabric/" + ViaFabric.getVersion());
String rawOutput = CharStreams.toString(new InputStreamReader(con.getInputStream()));
Pattern urlPattern = Pattern.compile("<A href='([^']*)/'>");
Matcher matcher = urlPattern.matcher(rawOutput);
List<String> versions = new ArrayList<>();
while (matcher.find()) {
versions.add(matcher.group(1));
}
String mcVersion = MinecraftClient.getInstance().getGame().getVersion().getName();
String bestViaVersion = null;
if (mcVersion.contains("w") || mcVersion.contains("-")) {
bestViaVersion = versions.stream()
.filter(it -> it.endsWith(mcVersion))
.max(Comparator.comparing(Version::new))
.orElse(null);
}
if (bestViaVersion == null) {
bestViaVersion = versions.stream()
.filter(it -> it.endsWith("-SNAPSHOT") || it.endsWith("-DEV") || !it.contains("-"))
.max(Comparator.comparing(Version::new))
.orElse(null);
}
HttpURLConnection md5Con = (HttpURLConnection) new URL(
"https://repo.viaversion.com/" + groupIdPath + "/" + artifactName + "/" + bestViaVersion
+ "/" + artifactName + "-" + bestViaVersion + ".jar.md5").openConnection();
md5Con.setRequestProperty("User-Agent", "ViaFabric/" + ViaFabric.getVersion());
String remoteMd5 = CharStreams.toString(new InputStreamReader(md5Con.getInputStream()));
if (!remoteMd5.equals(localMd5)) {
URL url = new URL("https://repo.viaversion.com/" + groupIdPath + "/" + artifactName + "/" + bestViaVersion
+ "/" + artifactName + "-" + bestViaVersion + ".jar");
ViaFabric.JLOGGER.info("Downloading " + url);
HttpURLConnection jarCon = (HttpURLConnection) url.openConnection();
jarCon.setRequestProperty("User-Agent", "ViaFabric/" + ViaFabric.getVersion());
FileUtils.copyInputStreamToFile(jarCon.getInputStream(), jar);
} else {
JLOGGER.info("No updates found");
jar.setLastModified(System.currentTimeMillis());
}
}
}
}

View File

@ -28,6 +28,7 @@ import com.github.creeper123123321.viafabric.ViaFabric;
import com.github.creeper123123321.viafabric.protocol.Interceptor;
import com.github.creeper123123321.viafabric.util.FutureTaskId;
import net.fabricmc.loader.FabricLoader;
import net.fabricmc.loader.ModContainer;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.ViaAPI;
@ -38,17 +39,22 @@ import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.platform.TaskId;
import us.myles.ViaVersion.api.platform.ViaPlatform;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.dump.PluginInfo;
import us.myles.ViaVersion.exception.CancelException;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter;
import us.myles.ViaVersion.sponge.VersionInfo;
import us.myles.ViaVersion.util.GsonUtil;
import us.myles.viaversion.libs.gson.JsonObject;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import java.util.stream.Collectors;
public class VRPlatform implements ViaPlatform {
private VRViaConfig config = new VRViaConfig(new File(FabricLoader.INSTANCE.getConfigDirectory(), "ViaFabric"));
@ -200,7 +206,21 @@ public class VRPlatform implements ViaPlatform {
@Override
public JsonObject getDump() {
return new JsonObject();
JsonObject platformSpecific = new JsonObject();
List<PluginInfo> mods = new ArrayList<>();
for (ModContainer mod : FabricLoader.INSTANCE.getModContainers()) {
mods.add(new PluginInfo(true,
mod.getInfo().getName(),
mod.getInfo().getVersionString(),
String.join(", ", mod.getInfo().getInitializers()),
mod.getInfo().getAuthors().stream()
.map(info -> info.getName() + " <" + info.getEmail() + "> " + "[" + info.getWebsite() + "]")
.collect(Collectors.toList())
));
}
platformSpecific.add("mods", GsonUtil.getGson().toJsonTree(mods));
return platformSpecific;
}
@Override