impl WSC2

This commit is contained in:
Daniella / Tove 2024-10-14 12:30:08 +02:00
parent 81341d0709
commit 853b2baaf6
Signed by: TudbuT
GPG key ID: B3CF345217F202D3
2 changed files with 77 additions and 3 deletions

View file

@ -13,7 +13,7 @@ import de.com.baseband.client.mixin.MixinProxy;
import de.com.baseband.client.registry.Configuration;
import de.com.baseband.client.registry.Updater;
import de.com.baseband.client.util.interact.Chat;
import de.com.baseband.client.util.net.WebServiceClient;
import de.com.baseband.client.util.net.WebServiceClient2;
import de.com.baseband.client.util.render.NotificationRender;
import de.com.baseband.prod.LoadHandler;
import de.tudbut.tools.Lock;
@ -36,6 +36,7 @@ public class BaseBand {
public static final EventBus EVENT_BUS = new EventBus(Throwable::printStackTrace);
public static final RemoteEventBus REMOTE_EVENT_BUS = new RemoteEventBus();
public static final StaticEventHandler STATIC_EVENT_HANDLER = new StaticEventHandler();
public static final WebServiceClient2 wsc2 = new WebServiceClient2();
public static boolean enabled = true;
public static boolean finishedDisabling = false;
public static Minecraft mc;
@ -80,7 +81,6 @@ public class BaseBand {
Lock lock = new Lock();
while (enabled) {
lock.lock(1000);
WebServiceClient.connect();
for (Updater updater : Configuration.updaters) {
updater.poll();
}
@ -96,6 +96,11 @@ public class BaseBand {
LOGGER.info("Unloaded.");
}, "Async Config Updater").start();
LOGGER.info("Starting up WSC2...");
wsc2.start();
wsc2.lock.waitHere();
LOGGER.info("Initialized.");
}

View file

@ -1,6 +1,75 @@
package de.com.baseband.client.util.net;
public class WebServiceClient2 {
import de.com.baseband.client.BaseBand;
import de.com.baseband.client.util.data.GitHash;
import de.com.baseband.prod.LoadHandler;
import de.tudbut.parsing.JSON;
import de.tudbut.parsing.TCN;
import de.tudbut.tools.Lock;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class WebServiceClient2 extends Thread {
// TODO make
// -> https://downloads.baseband.com.de
BufferedReader connection;
public Lock lock = new Lock();
boolean outdated = false;
public WebServiceClient2() {
try {
connection = new BufferedReader(new InputStreamReader(new URL("https://downloads.baseband.com.de/listen").openStream()));
lock.lock();
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
BaseBand.LOGGER.fatal("Cannot connect to WSC2.");
BaseBand.shutdown();
}
}
public void run() {
while (true) {
try {
while (true) {
String line = connection.readLine();
TCN event = JSON.read(line);
switch (event.getString("event-kind")) {
case "init":
BaseBand.notify("Connected to WSC2.");
lock.unlock();
break;
case "refresh:1.12.2":
TCN branch = event.getSub("data").getSub(LoadHandler.data.getString("branch"));
if (branch != null) {
if (!branch.getString("commit").equals(GitHash.GIT_HASH) && !outdated) {
outdated = true;
BaseBand.notifyAll("§d§lBaseBand has updated. To update, restart Minecraft.", 40000);
}
}
break;
case "refresh:ednieva":
break;
default:
BaseBand.notify("Incompatible WSC2 event received: " + event.getString("event-kind") + ". Please report this.");
break;
}
}
} catch (Exception e) {
connection = null;
BaseBand.LOGGER.info("WSC2 disconnected.");
lock.lock(10000);
lock.waitHere();
try {
connection = new BufferedReader(new InputStreamReader(new URL("https://downloads.baseband.com.de/listen").openStream()));
} catch (IOException ex) {}
}
}
}
}