Skip to content

Commit

Permalink
Start work on update checker impl
Browse files Browse the repository at this point in the history
  • Loading branch information
IThundxr committed Sep 30, 2024
1 parent a7d818d commit a275aa6
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 6 deletions.
2 changes: 2 additions & 0 deletions common/src/main/java/com/railwayteam/railways/Railways.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.railwayteam.railways.multiloader.Loader;
import com.railwayteam.railways.registry.CRCommands;
import com.railwayteam.railways.registry.CRPackets;
import com.railwayteam.railways.util.RailwaysUpdateCheck;
import com.railwayteam.railways.util.Utils;
import com.simibubi.create.Create;
import com.simibubi.create.foundation.data.CreateRegistrate;
Expand Down Expand Up @@ -91,6 +92,7 @@ private static void migrateConfig(Path path, Function<String, String> converter)

public static void init() {
LOGGER.info("{} v{} initializing! Commit hash: {} Create version: {} on platform: {}", NAME, RailwaysBuildInfo.VERSION, RailwaysBuildInfo.GIT_COMMIT, Create.VERSION, Loader.getFormatted());
RailwaysUpdateCheck.execute();

Path configDir = Utils.configDir();
Path clientConfigDir = configDir.resolve(MODID + "-client.toml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ public void assertForDataGen() {
assert (!requiredForDataGen || isLoaded);
}

public static boolean isModLoaded(String id) {
return isModLoaded(id, null);
}

@ExpectPlatform
public static boolean isModLoaded(String id, @Nullable String fabricId) {
throw new AssertionError();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.railwayteam.railways.multiloader;

import com.railwayteam.railways.compat.Mods;
import com.railwayteam.railways.util.TextUtils;
import dev.architectury.injectables.annotations.ExpectPlatform;
import org.jetbrains.annotations.ApiStatus.Internal;
Expand All @@ -26,7 +27,7 @@
import java.util.function.Supplier;

public enum Loader {
FORGE, FABRIC;
FORGE, NEOFORGE, FABRIC, QUILT;

public static final Loader CURRENT = getCurrent();

Expand All @@ -40,7 +41,15 @@ public void runIfCurrent(Supplier<Runnable> run) {
}

public static String getFormatted() {
return TextUtils.titleCaseConversion(Loader.CURRENT.name().toLowerCase(Locale.ROOT));
return TextUtils.titleCaseConversion(getActual().name().toLowerCase(Locale.ROOT));
}

// Returns the actual loader, ex: quilt on quilt instead of fabric for quilt
public static Loader getActual() {
//noinspection ConstantValue
if (FABRIC.isCurrent() && Mods.isModLoaded("quilt_loader"))
return QUILT;
return CURRENT;
}

@Internal
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Steam 'n' Rails
* Copyright (c) 2024 The Railways Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.railwayteam.railways.util;

import com.railwayteam.railways.Railways;
import com.railwayteam.railways.RailwaysBuildInfo;
import com.railwayteam.railways.multiloader.Loader;
import net.minecraft.SharedConstants;
import net.minecraft.Util;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.time.Duration;

public class RailwaysUpdateCheck {
public static void execute() {
// TODO switch to nonCriticalIoPool() in 1.21.1
Util.ioPool().submit(() -> {
String uri = String.format(
"https://update.api.ithundxr.dev/update-check?mod_id=%s&mod_version=%s&mc_version=%s&loader=%s",
Railways.MODID,
RailwaysBuildInfo.VERSION,
SharedConstants.getCurrentVersion().getName(),
Loader.getActual()
);

HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri(URI.create(uri))
.build();

try {
HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10L))
.followRedirects(HttpClient.Redirect.ALWAYS)
.build()
.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
} catch (IOException | InterruptedException ignored) {}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,14 @@
import com.railwayteam.railways.registry.forge.CRParticleTypesParticleEntryImpl;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands.CommandSelection;
import net.minecraftforge.common.util.MavenVersionStringHelper;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.forgespi.language.IModInfo;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;

Expand Down

0 comments on commit a275aa6

Please sign in to comment.