From 5ce511bee6e499dd037cfa98bc715cafaf203cf2 Mon Sep 17 00:00:00 2001 From: Skye Date: Fri, 19 Jul 2024 15:52:41 +0900 Subject: [PATCH] feat: use kaleido --- build.gradle | 1 + common/build.gradle | 1 + common/src/main/java/link/e4mc/Agnos.java | 7 +++++++ common/src/main/java/link/e4mc/Config.java | 16 +++++++++++++++ .../main/java/link/e4mc/QuiclimeSession.java | 20 +++++++++++++++---- fabric/build.gradle | 1 + .../main/java/link/e4mc/fabric/AgnosImpl.java | 5 ++++- forge/build.gradle | 4 ++++ .../main/java/link/e4mc/forge/AgnosImpl.java | 5 ++++- gradle.properties | 2 +- 10 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 common/src/main/java/link/e4mc/Config.java diff --git a/build.gradle b/build.gradle index bdf3a9f..33625c3 100644 --- a/build.gradle +++ b/build.gradle @@ -42,6 +42,7 @@ allprojects { // See https://docs.gradle.org/current/userguide/declaring_repositories.html // for more information about repositories. maven { url 'https://jitpack.io' } + maven { url 'https://repo.sleeping.town/' } } tasks.withType(JavaCompile) { diff --git a/common/build.gradle b/common/build.gradle index 1b83d53..3deb03c 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -13,6 +13,7 @@ dependencies { implementation("com.github.vgskye.netty-incubator-codec-quic:netty-incubator-codec-classes-quic:${rootProject.quic_version}") { exclude(group: "io.netty") } + implementation 'folk.sisby:kaleido-config:0.3.1+1.3.1' } publishing { diff --git a/common/src/main/java/link/e4mc/Agnos.java b/common/src/main/java/link/e4mc/Agnos.java index a0d7fab..c3815e5 100644 --- a/common/src/main/java/link/e4mc/Agnos.java +++ b/common/src/main/java/link/e4mc/Agnos.java @@ -2,9 +2,16 @@ import dev.architectury.injectables.annotations.ExpectPlatform; +import java.nio.file.Path; + public class Agnos { @ExpectPlatform public static boolean isClient() { return false; } + + @ExpectPlatform + public static Path configDir() { + return null; + } } diff --git a/common/src/main/java/link/e4mc/Config.java b/common/src/main/java/link/e4mc/Config.java new file mode 100644 index 0000000..3854c82 --- /dev/null +++ b/common/src/main/java/link/e4mc/Config.java @@ -0,0 +1,16 @@ +package link.e4mc; + +import folk.sisby.kaleido.api.ReflectiveConfig; +import folk.sisby.kaleido.lib.quiltconfig.api.annotations.Comment; +import folk.sisby.kaleido.lib.quiltconfig.api.values.TrackedValue; + +public class Config extends ReflectiveConfig { + public static final Config INSTANCE = Config.createToml(Agnos.configDir(), "e4mc", "e4mc", Config.class); + + @Comment("Whether to use the broker to get the best relay based on location or use a hard-coded relay.") + public final TrackedValue useBroker = this.value(true); + public final TrackedValue brokerUrl = this.value("https://broker.e4mc.link/getBestRelay"); + + public final TrackedValue relayHost = this.value("test.e4mc.link"); + public final TrackedValue relayPort = this.value(25575); +} \ No newline at end of file diff --git a/common/src/main/java/link/e4mc/QuiclimeSession.java b/common/src/main/java/link/e4mc/QuiclimeSession.java index 62845dd..19edb96 100644 --- a/common/src/main/java/link/e4mc/QuiclimeSession.java +++ b/common/src/main/java/link/e4mc/QuiclimeSession.java @@ -236,11 +236,11 @@ public void startAsync() { thread.start(); } - public void start() { - try { + private static BrokerResponse getRelay() throws Exception { + if (Config.INSTANCE.useBroker.value()) { var httpClient = HttpClient.newHttpClient(); var request = HttpRequest - .newBuilder(new URI("https://broker.e4mc.link/getBestRelay")) + .newBuilder(new URI(Config.INSTANCE.brokerUrl.value())) .header("Accept", "application/json") .build(); LOGGER.info("req: {}", request); @@ -249,7 +249,19 @@ public void start() { if (response.statusCode() != 200) { throw new RuntimeException(); } - var relayInfo = gson.fromJson(response.body(), BrokerResponse.class); + return gson.fromJson(response.body(), BrokerResponse.class); + } else { + var resp = new BrokerResponse(); + resp.id = "custom"; + resp.host = Config.INSTANCE.relayHost.value(); + resp.port = Config.INSTANCE.relayPort.value(); + return resp; + } + } + + public void start() { + try { + var relayInfo = getRelay(); LOGGER.info("using relay {}", relayInfo.id); QuicSslContext context = QuicSslContextBuilder .forClient() diff --git a/fabric/build.gradle b/fabric/build.gradle index 619f548..f9aa301 100644 --- a/fabric/build.gradle +++ b/fabric/build.gradle @@ -29,6 +29,7 @@ dependencies { shadowCommon(implementation("com.github.vgskye.netty-incubator-codec-quic:netty-incubator-codec-classes-quic:${rootProject.quic_version}") { exclude(group: "io.netty") }) + include 'folk.sisby:kaleido-config:0.3.1+1.3.1' } processResources { diff --git a/fabric/src/main/java/link/e4mc/fabric/AgnosImpl.java b/fabric/src/main/java/link/e4mc/fabric/AgnosImpl.java index dba9c28..c201c80 100644 --- a/fabric/src/main/java/link/e4mc/fabric/AgnosImpl.java +++ b/fabric/src/main/java/link/e4mc/fabric/AgnosImpl.java @@ -1,6 +1,5 @@ package link.e4mc.fabric; -import link.e4mc.Agnos; import net.fabricmc.api.EnvType; import net.fabricmc.loader.api.FabricLoader; @@ -10,4 +9,8 @@ public class AgnosImpl { public static boolean isClient() { return FabricLoader.getInstance().getEnvironmentType().equals(EnvType.CLIENT); } + + public static Path configDir() { + return FabricLoader.getInstance().getConfigDir(); + } } diff --git a/forge/build.gradle b/forge/build.gradle index 477dde7..e8b2703 100644 --- a/forge/build.gradle +++ b/forge/build.gradle @@ -36,6 +36,7 @@ dependencies { shadowCommon(implementation("com.github.vgskye.netty-incubator-codec-quic:netty-incubator-codec-classes-quic:${rootProject.quic_version}") { exclude(group: "io.netty") }) + shadowCommon('folk.sisby:kaleido-config:0.3.1+1.3.1') } processResources { @@ -52,6 +53,9 @@ shadowJar { configurations = [project.configurations.shadowCommon] archiveClassifier = "dev-shadow" + + relocate 'com.electronwill.nightconfig', 'link.e4mc.shadow.nightconfig' + relocate 'folk.sisby.kaleido', 'link.e4mc.shadow.kaleido' } remapJar { diff --git a/forge/src/main/java/link/e4mc/forge/AgnosImpl.java b/forge/src/main/java/link/e4mc/forge/AgnosImpl.java index 03e0d82..a55ecd2 100644 --- a/forge/src/main/java/link/e4mc/forge/AgnosImpl.java +++ b/forge/src/main/java/link/e4mc/forge/AgnosImpl.java @@ -1,6 +1,5 @@ package link.e4mc.forge; -import link.e4mc.Agnos; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.fml.loading.FMLLoader; import net.minecraftforge.fml.loading.FMLPaths; @@ -11,4 +10,8 @@ public class AgnosImpl { public static boolean isClient() { return FMLLoader.getDist().equals(Dist.CLIENT); } + + public static Path configDir() { + return FMLPaths.CONFIGDIR.get(); + } } diff --git a/gradle.properties b/gradle.properties index a8db28f..c418818 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ minecraft_version=1.18.2 enabled_platforms=fabric,forge archives_base_name=e4mc_minecraft -mod_version=5.0.0 +mod_version=5.1.0 maven_group=link.e4mc architectury_version=4.11.93