Fix some things

This commit is contained in:
Johannes Frohnmeyer 2021-12-28 13:13:48 +01:00
parent bce9a323f1
commit ca2ad0306a
Signed by: Johannes
GPG Key ID: E76429612C2929F4
8 changed files with 73 additions and 36 deletions

View File

@ -65,7 +65,12 @@ public final class CurseforgeModSource implements ModSource {
@Override
public String getName() {
return "curseforge/" + (mod.slug == null ? mod.id : mod.slug) + '/' + current.id;
return "curseforge/" + getShortName() + '/' + current.id;
}
@Override
public String getShortName() {
return mod.slug == null ? mod.id.toString() : mod.slug;
}
@Override

View File

@ -42,6 +42,11 @@ public record DirectModSource(String fileName, String url, Set<ModSource> depend
return "direct/" + (url.contains(":") ? url.split(":")[1] : url).replaceAll("^/+", "");
}
@Override
public String getShortName() {
return fileName;
}
@Override
public String getFileName() {
return fileName;

View File

@ -13,6 +13,7 @@ public interface ModSource {
Optional<ModSource> getUpdate(String gameVersion) throws IOException;
String getVersion();
String getName();
String getShortName();
String getFileName();
boolean equals(ModSource other);
default Path getJarPath() {

View File

@ -76,7 +76,12 @@ public final class ModrinthModSource implements ModSource {
@Override
public String getName() {
return "modrinth/" + (mod.slug == null ? mod.id : mod.slug) + '/' + current.version_number;
return "modrinth/" + getShortName() + '/' + current.version_number;
}
@Override
public String getShortName() {
return (mod.slug == null ? mod.id : mod.slug);
}
@Override

View File

@ -32,9 +32,9 @@ public class HttpUtils {
private Method method;
public Request(Method method, String url) {
this.url = url;
this.url = url.replace(" ", "%20");
try {
this.builder = HttpRequest.newBuilder().uri(new URI(url)).header("User-Agent", "Meteor Client");
this.builder = HttpRequest.newBuilder().uri(new URI(this.url)).header("User-Agent", "Meteor Client");
this.method = method;
} catch (URISyntaxException e) {
Inceptum.LOGGER.error("Could not create request", e);

View File

@ -40,6 +40,9 @@ public class ModsDirScanner implements Closeable {
Set<IWModDescription> mods = new TreeSet<>();
if (Files.isDirectory(modsDir)) {
for (Path path : Utils.ls(modsDir)) {
String fn = path.getFileName().toString();
if (fn.endsWith(".imod") && Files.exists(path.getParent().resolve(fn.substring(0, fn.length() - 5))))
continue;
mods.add(get(path));
}
}
@ -47,12 +50,11 @@ public class ModsDirScanner implements Closeable {
}
public IWModDescription get(Path path) {
if (descriptions.containsKey(path))
return descriptions.get(path);
else {
if (!descriptions.containsKey(path)) {
// not yet scanned
return new IWModDescription(path); //TODO figure out why this causes blocking
descriptions.put(path, new IWModDescription(path));
}
return descriptions.get(path);
}
private void scanTaskInternal() {
@ -63,29 +65,40 @@ public class ModsDirScanner implements Closeable {
continue;
}
for (Path mods : Utils.ls(modsDir)) {
if (!Files.exists(mods)) continue;
if (disposed) return;
if (Files.isDirectory(mods)) {
descriptions.put(mods, new IWModDescription(mods));
} else {
if (mods.toString().endsWith(".jar") || mods.toString().endsWith(".jar.disabled")) {
Path imod = mods.getParent().resolve(mods.getFileName() + ".imod");
final var imod = new Object() {
Path i = mods.getParent().resolve(mods.getFileName() + ".imod");
ModDescription md;
};
// mod description
if (!Files.exists(imod)) {
Utils.writeObject(imod, ModDescription.of(mods));
if (!Files.exists(imod.i)) {
Utils.writeObject(imod.i, ModDescription.of(mods));
}
ModDescription md = Utils.loadObject(imod, ModDescription.class);
evaluateSources(md, mods, imod);
descriptions.put(imod, new IWModDescription(mods, Optional.of(md), getFmj(mods, md), Optional.of(imod)));
imod.md = Utils.loadObject(imod.i, ModDescription.class);
evaluateSources(imod.md, mods, imod.i, newImod -> {
imod.i = newImod;
imod.md = Utils.loadObject(imod.i, ModDescription.class);
});
descriptions.put(imod.i, new IWModDescription(mods, Optional.of(imod.md), getFmj(mods, imod.md), Optional.of(imod.i)));
}
else if (mods.toString().endsWith(".imod")) {
else if (mods.toString().endsWith(".imod") || mods.toString().endsWith(".imod.disabled")) {
//TODO ensure this is not called while downloading a pack
//String fn = mods.getFileName().toString();
//if (!Files.exists(mods.getParent().resolve(fn.substring(0, fn.length() - 5))))
// Files.delete(mods);
String fn = mods.getFileName().toString();
Path modFile = mods.getParent().resolve(fn.substring(0, fn.length() - 5));
ModDescription md = Utils.loadObject(mods, ModDescription.class);
evaluateSources(md, modFile, mods);
descriptions.put(mods, new IWModDescription(mods, Optional.of(md), getFmj(modFile, md), Optional.of(mods)));
final var imod = new Object() {
Path i = mods;
ModDescription md = Utils.loadObject(mods, ModDescription.class);
};
evaluateSources(imod.md, modFile, imod.i, newImod -> {
imod.i = newImod;
imod.md = Utils.loadObject(imod.i, ModDescription.class);
});
descriptions.put(imod.i, new IWModDescription(imod.i, Optional.of(imod.md), getFmj(modFile, imod.md), Optional.of(imod.i)));
}
else {
descriptions.put(mods, new IWModDescription(mods));
@ -99,23 +112,27 @@ public class ModsDirScanner implements Closeable {
}
}
private void evaluateSources(ModDescription md, Path modFile, Path imodFile) throws IOException {
private <TEx extends Throwable> void evaluateSources(ModDescription md, Path modFile, Path imodFile, ThrowingConsumer<Path, TEx> imodModified) throws IOException, TEx {
boolean modified = false;
if (md.initialize(getGameVersion())) {
Utils.writeObject(imodFile, md);
modified = true;
}
boolean hasSource = false;
ModSource selectedSource = null;
for (ModSource source : md.sources.keySet()) {
//Optional<ModSource> ms = source.getUpdate(instance.getMinecraftVersion());
//TODO properly cache
source.getUpdate(getGameVersion());
if (!Files.exists(source.getJarPath())) source.download();
hasSource = true;
//if (ms.isEmpty()) continue;
//if (update.isEmpty()) update = ms;
selectedSource = source;
}
if (hasSource && Files.exists(modFile)) {
if (selectedSource != null && Files.exists(modFile)) {
Files.delete(modFile);
Path newImod = imodFile.getParent().resolve(selectedSource.getShortName() + ".imod");
Files.move(imodFile, newImod);
imodFile = newImod;
modified = true;
}
if (modified) imodModified.consume(imodFile);
}
private Optional<FabricModJson> getFmj(Path modJarDefault, ModDescription md) throws IOException, URISyntaxException {
@ -137,7 +154,7 @@ public class ModsDirScanner implements Closeable {
disposed = true;
}
public static record IWModDescription(Path path, Optional<ModDescription> mod, Optional<FabricModJson> fmj, Optional<Path> imod) implements Comparable<IWModDescription> {
public record IWModDescription(Path path, Optional<ModDescription> mod, Optional<FabricModJson> fmj, Optional<Path> imod) implements Comparable<IWModDescription> {
public String getName() {
if (fmj.isEmpty()) return path.getFileName().toString();
String base;

View File

@ -26,7 +26,6 @@ import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;
public class InstanceLauncher {
public static void launch(Path instancePath, InstanceMeta instance, LaunchType launchType, boolean restart, AuthInfo authInfo) throws LaunchException, IOException {
@ -67,7 +66,10 @@ public class InstanceLauncher {
StringBuilder fabricAddMods = new StringBuilder("-Dfabric.addMods=");
Path mods = instancePath.resolve("mods");
if (Files.exists(mods)) {
for (Path imod : Utils.ls(mods, ((Predicate<Path>) path -> path.getFileName().toString().endsWith(".imod")))) {
for (Path imod : Utils.ls(mods, path -> {
String fn = path.getFileName().toString();
return fn.endsWith(".imod") && !fn.endsWith(".disabled.imod");
})) {
String fn = imod.getFileName().toString();
if (Files.exists(imod.getParent().resolve(fn.substring(0, fn.length() - 5))))
continue;

View File

@ -16,7 +16,6 @@ import io.gitlab.jfronny.inceptum.windows.control.InstanceManageControls;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@ -113,13 +112,16 @@ public class InstanceEditWindow extends Window {
ImGui.separator();
try {
for (ModsDirScanner.IWModDescription mod : mds.getMods()) {
boolean wasEnabled = !mod.path().toString().endsWith(".disabled");
final String disabledSuffix = ".disabled";
final String imodDisabledSuffix = ".disabled.imod";
String fName = mod.path().getFileName().toString();
boolean wasEnabled = !fName.endsWith(disabledSuffix) && !fName.endsWith(imodDisabledSuffix);
if (ImGui.checkbox("##" + mod.getName(), wasEnabled)) {
String fName = mod.path().getFileName().toString();
final String disabledSuffix = ".disabled";
if (fName.endsWith(disabledSuffix))
fName = fName.substring(0, fName.length() - disabledSuffix.length());
if (wasEnabled) fName += disabledSuffix;
if (fName.endsWith(imodDisabledSuffix))
fName = fName.substring(0, fName.length() - imodDisabledSuffix.length()) + ".imod";
if (wasEnabled) fName += fName.endsWith(".imod") ? imodDisabledSuffix : disabledSuffix;
Path newSel = mod.path().getParent().resolve(fName);
try {
Files.move(mod.path(), newSel);