2022-09-04 21:21:24 +02:00
|
|
|
package io.gitlab.jfronny.inceptum.launcher.system.source;
|
2021-11-02 16:44:26 +01:00
|
|
|
|
2022-04-28 23:13:37 +02:00
|
|
|
import io.gitlab.jfronny.commons.HashUtils;
|
2022-09-04 21:21:24 +02:00
|
|
|
import io.gitlab.jfronny.commons.cache.MemoryOperationResultCache;
|
2022-04-28 21:07:03 +02:00
|
|
|
import io.gitlab.jfronny.commons.tuple.Tuple;
|
2022-09-04 21:21:24 +02:00
|
|
|
import io.gitlab.jfronny.inceptum.common.Net;
|
|
|
|
import io.gitlab.jfronny.inceptum.common.Utils;
|
|
|
|
import io.gitlab.jfronny.inceptum.launcher.api.ModrinthApi;
|
2022-11-04 17:02:24 +01:00
|
|
|
import io.gitlab.jfronny.inceptum.launcher.model.modrinth.*;
|
2021-11-02 16:44:26 +01:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2022-01-18 18:43:39 +01:00
|
|
|
import java.net.URISyntaxException;
|
2021-11-02 19:52:35 +01:00
|
|
|
import java.nio.file.Files;
|
2021-11-02 16:44:26 +01:00
|
|
|
import java.nio.file.Path;
|
2022-09-04 21:21:24 +02:00
|
|
|
import java.util.*;
|
2021-11-02 16:44:26 +01:00
|
|
|
|
2021-11-02 17:25:34 +01:00
|
|
|
public final class ModrinthModSource implements ModSource {
|
2022-09-04 21:21:24 +02:00
|
|
|
private static final MemoryOperationResultCache<Tuple<String, String>, Optional<ModSource>> UPDATE_CACHE = new MemoryOperationResultCache<>(Utils.CACHE_SIZE);
|
|
|
|
private static final MemoryOperationResultCache<String, Set<ModSource>> DEPENDENCIES_CACHE = new MemoryOperationResultCache<>(Utils.CACHE_SIZE);
|
2022-01-24 19:21:23 +01:00
|
|
|
|
2021-11-02 17:25:34 +01:00
|
|
|
private final String versionId;
|
|
|
|
private final ModrinthVersion current;
|
2022-02-28 10:32:20 +01:00
|
|
|
private final ModrinthProject mod;
|
2021-11-02 17:25:34 +01:00
|
|
|
|
|
|
|
public ModrinthModSource(String versionId) throws IOException {
|
|
|
|
this.versionId = versionId;
|
2021-11-04 14:07:34 +01:00
|
|
|
this.current = ModrinthApi.getVersion(versionId);
|
|
|
|
this.mod = ModrinthApi.getMod(getModId());
|
2021-11-02 17:25:34 +01:00
|
|
|
}
|
|
|
|
|
2021-11-02 16:44:26 +01:00
|
|
|
@Override
|
2021-12-25 22:09:51 +01:00
|
|
|
public ModDownload download() throws IOException {
|
2021-11-02 17:25:34 +01:00
|
|
|
ModrinthVersion.File file = current.files.get(0);
|
2021-12-25 22:09:51 +01:00
|
|
|
Path path = getJarPath();
|
2022-01-18 18:43:39 +01:00
|
|
|
try {
|
2022-09-04 21:21:24 +02:00
|
|
|
Net.downloadFile(file.url, file.hashes.sha1, path);
|
2022-01-18 18:43:39 +01:00
|
|
|
} catch (URISyntaxException e) {
|
|
|
|
throw new IOException("Could not download file", e);
|
|
|
|
}
|
2021-11-02 19:52:35 +01:00
|
|
|
return new ModDownload(file.hashes.sha1, HashUtils.murmur2(Files.readAllBytes(path)), path);
|
2021-11-02 16:44:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-06-05 16:45:22 +02:00
|
|
|
public Set<ModSource> getDependencies(String gameVersion) throws IOException {
|
2022-01-24 19:21:23 +01:00
|
|
|
return DEPENDENCIES_CACHE.get(versionId, () -> {
|
|
|
|
Set<ModSource> deps = new HashSet<>();
|
|
|
|
for (ModrinthVersion.Dependency dependency : current.dependencies) {
|
|
|
|
//TODO show optional dependencies
|
|
|
|
if (dependency.dependency_type == ModrinthVersion.Dependency.DependencyType.required)
|
|
|
|
deps.add(new ModrinthModSource(dependency.version_id));
|
|
|
|
}
|
|
|
|
return Set.copyOf(deps);
|
|
|
|
});
|
2021-11-02 16:44:26 +01:00
|
|
|
}
|
2021-11-02 17:25:34 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Optional<ModSource> getUpdate(String gameVersion) throws IOException {
|
2022-01-24 19:21:23 +01:00
|
|
|
return UPDATE_CACHE.get(Tuple.of(versionId, gameVersion), () -> {
|
|
|
|
ModrinthVersion stable = null;
|
|
|
|
ModrinthVersion beta = null;
|
|
|
|
ModrinthVersion latest = null;
|
|
|
|
for (ModrinthVersion version : ModrinthApi.getVersions(getModId())) {
|
|
|
|
if (version.game_versions.contains(gameVersion) && version.loaders.contains("fabric")) {
|
|
|
|
latest = version;
|
|
|
|
if (version.version_type == ModrinthVersion.VersionType.beta || version.version_type == ModrinthVersion.VersionType.release)
|
|
|
|
beta = version;
|
|
|
|
if (version.version_type == ModrinthVersion.VersionType.release)
|
|
|
|
stable = version;
|
|
|
|
}
|
2021-11-02 17:25:34 +01:00
|
|
|
}
|
2022-01-24 19:21:23 +01:00
|
|
|
ModrinthVersion next = switch (current.version_type) {
|
|
|
|
case alpha -> latest;
|
|
|
|
case beta -> beta;
|
|
|
|
case release -> stable;
|
|
|
|
};
|
|
|
|
if (next == null) return Optional.empty();
|
|
|
|
if (next.version_number.equals(current.version_number)) return Optional.empty();
|
|
|
|
return Optional.of(new ModrinthModSource(next.id));
|
|
|
|
});
|
2021-11-02 17:25:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getVersion() {
|
|
|
|
return current.version_number;
|
|
|
|
}
|
|
|
|
|
2021-11-04 14:07:34 +01:00
|
|
|
@Override
|
|
|
|
public String getName() {
|
2021-12-28 13:13:48 +01:00
|
|
|
return "modrinth/" + getShortName() + '/' + current.version_number;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getShortName() {
|
|
|
|
return (mod.slug == null ? mod.id : mod.slug);
|
2021-12-25 22:09:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getFileName() {
|
|
|
|
return current.files.get(0).filename;
|
2021-11-04 14:07:34 +01:00
|
|
|
}
|
|
|
|
|
2021-11-02 19:52:35 +01:00
|
|
|
@Override
|
|
|
|
public boolean equals(ModSource other) {
|
2022-01-02 18:26:49 +01:00
|
|
|
return other instanceof ModrinthModSource ms && ms.getModId().equals(getModId()) && ms.versionId.equals(versionId);
|
2021-11-02 19:52:35 +01:00
|
|
|
}
|
|
|
|
|
2021-11-02 17:25:34 +01:00
|
|
|
public String getVersionId() {
|
|
|
|
return versionId;
|
|
|
|
}
|
2021-11-04 14:07:34 +01:00
|
|
|
|
|
|
|
public String getModId() {
|
2022-02-28 10:32:20 +01:00
|
|
|
return current.project_id;
|
2021-11-04 14:07:34 +01:00
|
|
|
}
|
2022-09-18 18:56:44 +02:00
|
|
|
|
|
|
|
public ModrinthModpackManifest.File toManifest() throws IOException {
|
|
|
|
ModrinthVersion.File orig = current.files.get(0);
|
|
|
|
ModrinthModpackManifest.File f = new ModrinthModpackManifest.File();
|
|
|
|
f.path = "mods/" + orig.filename;
|
|
|
|
f.hashes = new ModrinthHashes();
|
|
|
|
f.hashes.sha1 = orig.hashes.sha1;
|
|
|
|
f.hashes.sha512 = orig.hashes.sha512;
|
|
|
|
f.downloads = List.of(orig.url);
|
|
|
|
f.fileSize = Files.size(getJarPath());
|
|
|
|
return f;
|
|
|
|
}
|
2021-11-02 16:44:26 +01:00
|
|
|
}
|