2021-11-02 16:44:26 +01:00
|
|
|
package io.gitlab.jfronny.inceptum.model.inceptum;
|
|
|
|
|
2021-11-04 14:07:34 +01:00
|
|
|
import io.gitlab.jfronny.inceptum.Inceptum;
|
|
|
|
import io.gitlab.jfronny.inceptum.model.curseforge.CurseforgeFingerprint;
|
|
|
|
import io.gitlab.jfronny.inceptum.model.inceptum.source.CurseforgeModSource;
|
2021-11-02 16:44:26 +01:00
|
|
|
import io.gitlab.jfronny.inceptum.model.inceptum.source.ModSource;
|
2021-11-04 14:07:34 +01:00
|
|
|
import io.gitlab.jfronny.inceptum.model.inceptum.source.ModrinthModSource;
|
|
|
|
import io.gitlab.jfronny.inceptum.util.HashUtils;
|
|
|
|
import io.gitlab.jfronny.inceptum.util.api.CurseforgeApi;
|
|
|
|
import io.gitlab.jfronny.inceptum.util.api.ModrinthApi;
|
2021-11-02 16:44:26 +01:00
|
|
|
|
2021-11-04 14:07:34 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.util.*;
|
2021-11-02 16:44:26 +01:00
|
|
|
|
|
|
|
public class ModDescription {
|
2021-11-04 14:07:34 +01:00
|
|
|
public Map<ModSource, Optional<ModSource>> sources; //key: source, value: update
|
2021-11-02 16:44:26 +01:00
|
|
|
public String sha1;
|
2021-11-02 19:52:35 +01:00
|
|
|
public String murmur2;
|
2021-11-02 16:44:26 +01:00
|
|
|
public List<String> dependents; // by file name
|
|
|
|
public List<String> dependencies; // by file name
|
2021-11-04 14:07:34 +01:00
|
|
|
|
|
|
|
public void hydrateUpdates(String gameVersion) {
|
|
|
|
for (ModSource source : sources.keySet().toArray(ModSource[]::new)) {
|
|
|
|
addSource(source, gameVersion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ModDescription of(Path mod, String gameVersion) {
|
|
|
|
String sha1 = null;
|
|
|
|
String murmur2 = null;
|
|
|
|
try {
|
|
|
|
byte[] data = Files.readAllBytes(mod);
|
|
|
|
sha1 = HashUtils.sha1(data);
|
|
|
|
murmur2 = HashUtils.murmur2(data);
|
|
|
|
} catch (IOException e) {
|
|
|
|
Inceptum.LOGGER.error("Could not read file hash", e);
|
|
|
|
}
|
|
|
|
return of(sha1, murmur2, Optional.empty(), gameVersion);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ModDescription of(String sha1, String murmur2, Optional<ModSource> knownSource, String gameVersion) {
|
|
|
|
ModDescription res = new ModDescription();
|
|
|
|
res.sources = new LinkedHashMap<>();
|
|
|
|
res.sha1 = sha1;
|
|
|
|
res.murmur2 = murmur2;
|
|
|
|
res.dependents = new ArrayList<>();
|
|
|
|
res.dependencies = new ArrayList<>();
|
|
|
|
knownSource.ifPresent(modSource -> res.addSource(modSource, gameVersion));
|
|
|
|
if (knownSource.isEmpty() || !(knownSource.get() instanceof ModrinthModSource)) {
|
|
|
|
try {
|
|
|
|
res.addSource(new ModrinthModSource(ModrinthApi.getVersionByHash(sha1).id), gameVersion);
|
|
|
|
}
|
|
|
|
catch (IOException e) {
|
|
|
|
// not found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (knownSource.isEmpty() || !(knownSource.get() instanceof CurseforgeModSource)) {
|
|
|
|
try {
|
|
|
|
CurseforgeFingerprint cf = CurseforgeApi.checkFingerprint(murmur2);
|
|
|
|
if (!cf.exactMatches.isEmpty()) {
|
|
|
|
CurseforgeFingerprint.Mod f = cf.exactMatches.get(0);
|
|
|
|
res.addSource(new CurseforgeModSource(f.id, f.file.id), gameVersion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IOException e) {
|
|
|
|
// not found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addSource(ModSource source, String gameVersion) {
|
|
|
|
try {
|
|
|
|
sources.put(source, source.getUpdate(gameVersion));
|
|
|
|
} catch (IOException e) {
|
|
|
|
Inceptum.LOGGER.error("Could not check " + source.getName() + " for updates", e);
|
|
|
|
}
|
|
|
|
}
|
2021-11-02 16:44:26 +01:00
|
|
|
}
|