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);
|
2023-05-05 18:49:56 +02:00
|
|
|
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 {
|
2023-05-05 18:49:56 +02:00
|
|
|
ModrinthVersion.File file = current.files().get(0);
|
|
|
|
Path path = getJarPath();
|
2022-01-18 18:43:39 +01:00
|
|
|
try {
|
2023-05-05 18:49:56 +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);
|
|
|
|
}
|
2023-05-05 18:49:56 +02: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<>();
|
2023-05-05 18:49:56 +02:00
|
|
|
for (ModrinthVersion.Dependency dependency : current.dependencies()) {
|
2022-01-24 19:21:23 +01:00
|
|
|
//TODO show optional dependencies
|
2023-05-05 18:49:56 +02:00
|
|
|
if (dependency.dependency_type() == ModrinthVersion.Dependency.DependencyType.required)
|
|
|
|
deps.add(new ModrinthModSource(dependency.version_id()));
|
2022-01-24 19:21:23 +01:00
|
|
|
}
|
|
|
|
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), () -> {
|
2023-07-24 14:29:06 +02:00
|
|
|
ModrinthVersion next = ModrinthApi.getLatestVersions(getModId(), gameVersion).get(current.version_type());
|
2022-01-24 19:21:23 +01:00
|
|
|
if (next == null) return Optional.empty();
|
2023-05-05 18:49:56 +02:00
|
|
|
if (next.version_number().equals(current.version_number())) return Optional.empty();
|
|
|
|
return Optional.of(new ModrinthModSource(next.id()));
|
2022-01-24 19:21:23 +01:00
|
|
|
});
|
2021-11-02 17:25:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getVersion() {
|
2023-05-05 18:49:56 +02:00
|
|
|
return current.version_number();
|
2021-11-02 17:25:34 +01:00
|
|
|
}
|
|
|
|
|
2021-11-04 14:07:34 +01:00
|
|
|
@Override
|
|
|
|
public String getName() {
|
2023-05-05 18:49:56 +02:00
|
|
|
return "modrinth/" + getShortName() + '/' + current.version_number();
|
2021-12-28 13:13:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getShortName() {
|
2023-05-05 18:49:56 +02:00
|
|
|
return (mod.slug() == null ? mod.id() : mod.slug());
|
2021-12-25 22:09:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getFileName() {
|
2023-05-05 18:49:56 +02:00
|
|
|
return current.files().get(0).filename();
|
2021-11-04 14:07:34 +01:00
|
|
|
}
|
|
|
|
|
2023-07-24 14:29:06 +02:00
|
|
|
@Override
|
|
|
|
public String getDescription() {
|
|
|
|
return mod.body();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getSummary() {
|
|
|
|
return mod.description();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
return obj instanceof ModSource ms && equals(ms);
|
|
|
|
}
|
|
|
|
|
2021-11-02 19:52:35 +01:00
|
|
|
@Override
|
|
|
|
public boolean equals(ModSource other) {
|
2023-05-05 18:49:56 +02:00
|
|
|
return other instanceof ModrinthModSource ms && ms.getModId().equals(getModId()) && ms.versionId.equals(versionId);
|
2021-11-02 19:52:35 +01:00
|
|
|
}
|
|
|
|
|
2023-07-24 14:29:06 +02:00
|
|
|
@Override
|
|
|
|
public boolean projectMatches(ModSource other) {
|
|
|
|
return other instanceof ModrinthModSource ms && ms.getModId().equals(getModId());
|
|
|
|
}
|
|
|
|
|
2021-11-02 17:25:34 +01:00
|
|
|
public String getVersionId() {
|
|
|
|
return versionId;
|
|
|
|
}
|
2021-11-04 14:07:34 +01:00
|
|
|
|
|
|
|
public String getModId() {
|
2023-05-05 18:49:56 +02: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 {
|
2023-05-05 18:49:56 +02:00
|
|
|
ModrinthVersion.File orig = current.files().get(0);
|
2022-11-18 21:58:40 +01:00
|
|
|
return new ModrinthModpackManifest.File(
|
2023-05-05 18:49:56 +02:00
|
|
|
"mods/" + orig.filename(),
|
2022-11-18 21:58:40 +01:00
|
|
|
new ModrinthHashes(
|
2023-05-05 18:49:56 +02:00
|
|
|
orig.hashes().sha1(),
|
|
|
|
orig.hashes().sha512()
|
2022-11-18 21:58:40 +01:00
|
|
|
),
|
|
|
|
null, // env
|
2023-05-05 18:49:56 +02:00
|
|
|
List.of(orig.url()),
|
|
|
|
Files.size(getJarPath())
|
2022-11-18 21:58:40 +01:00
|
|
|
);
|
2022-09-18 18:56:44 +02:00
|
|
|
}
|
2021-11-02 16:44:26 +01:00
|
|
|
}
|