Skip to content

Commit

Permalink
Fix quilt support for mod update checker
Browse files Browse the repository at this point in the history
  • Loading branch information
Prospector committed Feb 17, 2023
1 parent 500630e commit 4b1b53f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/terraformersmc/modmenu/ModMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class ModMenu implements ClientModInitializer {
private static List<Supplier<Map<String, ConfigScreenFactory<?>>>> dynamicScreenFactories = new ArrayList<>();

private static int cachedDisplayedModCount = -1;
public static boolean runningQuilt = false;

public static Screen getConfigScreen(String modid, Screen menuScreen) {
if (ModMenuConfig.HIDDEN_CONFIGS.getValue().contains(modid)) {
Expand All @@ -71,6 +72,7 @@ public static Screen getConfigScreen(String modid, Screen menuScreen) {
@Override
public void onInitializeClient() {
ModMenuConfigManager.initializeConfig();
runningQuilt = FabricLoader.getInstance().isModLoaded("quilt_loader");
Map<String, ConfigScreenFactory<?>> factories = new HashMap<>();
Set<String> modpackMods = new HashSet<>();
FabricLoader.getInstance().getEntrypointContainers("modmenu", ModMenuApi.class).forEach(entrypoint -> {
Expand All @@ -91,7 +93,7 @@ public void onInitializeClient() {
// Fill mods map
for (ModContainer modContainer : FabricLoader.getInstance().getAllMods()) {
if (!ModMenuConfig.HIDDEN_MODS.getValue().contains(modContainer.getMetadata().getId())) {
if (FabricLoader.getInstance().isModLoaded("quilt_loader")) {
if (ModMenu.runningQuilt) {
QuiltMod mod = new QuiltMod(modContainer, modpackMods);
MODS.put(mod.getId(), mod);
ModrinthUtil.checkForUpdates(mod);
Expand Down
30 changes: 18 additions & 12 deletions src/main/java/com/terraformersmc/modmenu/util/ModrinthUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class ModrinthUtil {
.header("User-Agent", userAgent)
.uri(URI.create("https://api.modrinth.com/v2/project/%s/version?loaders=%s&game_versions=%s".formatted(
projectId,
URLEncoder.encode("[\"%s\"]".formatted(FabricLoader.getInstance().isModLoaded("quilt_loader") ? "quilt" : "fabric"), StandardCharsets.UTF_8),
URLEncoder.encode("[\"%s\"]".formatted(ModMenu.runningQuilt ? "quilt" : "fabric"), StandardCharsets.UTF_8),
URLEncoder.encode("[\"%s\"]".formatted(SharedConstants.getGameVersion().getName()), StandardCharsets.UTF_8)
)))
.build();
Expand All @@ -80,17 +80,23 @@ public class ModrinthUtil {
} else if (latestRsp.statusCode() == 200) {
LOGGER.debug("Getting latest version from Modrinth.");
var versions = JsonParser.parseString(latestRsp.body()).getAsJsonArray();
var latestObj = versions.get(0).getAsJsonObject();
var latestVersion = latestObj.get("version_number").getAsString();
var latestId = latestObj.get("id").getAsString();
var latestHash = latestObj.get("files").getAsJsonArray().asList()
.stream().filter(file -> file.getAsJsonObject().get("primary").getAsBoolean()).findFirst()
.get().getAsJsonObject().get("hashes").getAsJsonObject().get("sha512").getAsString();
if (!Objects.equals(latestHash, localHash)) {
// hashes different, there's an update.
LOGGER.info("Update available for '{}@{}', ({} -> {})", mod.getId(), mod.getVersion(), modrinthVersion, latestVersion);
mod.setModrinthData(new ModrinthData(projectId, latestId, latestVersion));
ModMenu.modUpdateAvailable = true;
String currentLoader = ModMenu.runningQuilt ? "Quilt" : "Fabric";
LOGGER.debug("Versions of {} for {}: {}", mod.getId(), currentLoader, versions);
if (versions.isEmpty()) {
LOGGER.debug("No versions of {} for {} found on Modrinth.", mod.getId(), currentLoader);
} else {
var latestObj = versions.get(0).getAsJsonObject();
var latestVersion = latestObj.get("version_number").getAsString();
var latestId = latestObj.get("id").getAsString();
var latestHash = latestObj.get("files").getAsJsonArray().asList()
.stream().filter(file -> file.getAsJsonObject().get("primary").getAsBoolean()).findFirst()
.get().getAsJsonObject().get("hashes").getAsJsonObject().get("sha512").getAsString();
if (!Objects.equals(latestHash, localHash)) {
// hashes different, there's an update.
LOGGER.info("Update available for '{}@{}', ({} -> {})", mod.getId(), mod.getVersion(), modrinthVersion, latestVersion);
mod.setModrinthData(new ModrinthData(projectId, latestId, latestVersion));
ModMenu.modUpdateAvailable = true;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.Lists;
import com.google.common.hash.Hashing;
import com.google.common.io.Files;
import com.terraformersmc.modmenu.util.ModrinthUtil;
import com.terraformersmc.modmenu.util.mod.fabric.FabricMod;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -12,6 +13,7 @@
import org.quiltmc.loader.api.QuiltLoader;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -66,13 +68,15 @@ public QuiltMod(net.fabricmc.loader.api.ModContainer fabricModContainer, Set<Str
public @Nullable String getSha512Hash() throws IOException {
var fabricResult = super.getSha512Hash();
if (fabricResult == null) {
if (container.getSourceType().equals(ModContainer.BasicSourceType.NORMAL_QUILT)) {
ModrinthUtil.LOGGER.debug("Checking {}", getId());
if (container.getSourceType().equals(ModContainer.BasicSourceType.NORMAL_QUILT) || container.getSourceType().equals(ModContainer.BasicSourceType.NORMAL_FABRIC)) {
var path = container.getSourcePaths().stream()
.filter(p -> p.stream().anyMatch(p2 -> p2.toString().toLowerCase(Locale.ROOT).endsWith(".jar"))).findFirst().orElse(Collections.emptyList())
.stream().filter(p -> p.toString().toLowerCase(Locale.ROOT).endsWith(".jar")).findFirst();
if (path.isPresent()) {
if (path.isPresent() && path.get().getFileSystem() == FileSystems.getDefault()) {
var file = path.get().toFile();
if (file.isFile()) {
ModrinthUtil.LOGGER.debug(" Found {} hash", getId());
return Files.asByteSource(file).hash(Hashing.sha512()).toString();
}
}
Expand Down

0 comments on commit 4b1b53f

Please sign in to comment.