Merge pull request 'add manifest versioning' (#9) from owlsys/add-manifest-version into main

Reviewed-on: https://git-esnesnon.ecorous.org/esnesnon/nonsense-loader/pulls/9
Reviewed-by: Ecorous <ecorous@outlook.com>
This commit is contained in:
Ecorous 2024-05-26 08:05:29 -04:00
commit 44378d47cc
5 changed files with 55 additions and 24 deletions

View file

@ -1,4 +1,7 @@
[frog]
format_version = "1.0.0"
[frog.mod]
id = "example_mod"
name = "Example Mod"
version = "1.0.0"

View file

@ -7,13 +7,14 @@ import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;
import com.electronwill.nightconfig.core.CommentedConfig;
import com.electronwill.nightconfig.core.UnmodifiableConfig;
import com.electronwill.nightconfig.core.file.FileNotFoundAction;
import com.electronwill.nightconfig.toml.TomlParser;
import lombok.AllArgsConstructor;
import org.ecorous.esnesnon.nonsense.loader.api.mod.*;
import org.ecorous.esnesnon.nonsense.loader.impl.SemVerParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -43,31 +44,55 @@ public class ModPropertiesReader {
}
private static ModProperties readProperties(UnmodifiableConfig config) {
String id = config.get("frog.id");
String name = config.get("frog.name");
String version = config.get("frog.version");
String license = config.get("frog.license");
String manifestVer = config.get("frog.format_version");
List<UnmodifiableConfig> creditsList = config.get("frog.credits");
Map<String, Collection<String>> credits = new HashMap<>();
if (creditsList != null) {
creditsList.forEach(c -> credits.put(c.get("name"), c.get("roles")));
}
Collection<ModDependencies.Entry> depends = config.get("frog.dependencies.depends");
Collection<ModDependencies.Entry> breaks = config.get("frog.dependencies.breaks");
Collection<ModDependencies.Entry> suggests = config.get("frog.dependencies.suggests");
UnmodifiableConfig extensionsConfig = config.get("frog.extensions");
Map<String, Object> extensions = new HashMap<>();
if (extensionsConfig != null) {
extensionsConfig.entrySet().forEach(entry -> extensions.put(entry.getKey(), entry.getValue()));
}
try {
return new ModPropertiesImpl(id, name, SemVerImpl.parse(version), License.fromId(license), ModCredits.of(credits), new ModDependencies(depends, breaks, suggests), ModExtensions.of(extensions));
} catch (SemVerParseException e) {
return FormatVersion.get(manifestVer).parse(config);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@AllArgsConstructor
private enum FormatVersion {
V1_0_0("1.0.0", config -> {
String id = config.get("frog.mod.id");
String name = config.get("frog.mod.name");
String version = config.get("frog.mod.version");
String license = config.get("frog.mod.license");
List<UnmodifiableConfig> creditsList = config.get("frog.mod.credits");
Map<String, Collection<String>> credits = new HashMap<>();
if (creditsList != null) {
creditsList.forEach(c -> credits.put(c.get("name"), c.get("roles")));
}
Collection<ModDependencies.Entry> depends = config.get("frog.dependencies.depends");
Collection<ModDependencies.Entry> breaks = config.get("frog.dependencies.breaks");
Collection<ModDependencies.Entry> suggests = config.get("frog.dependencies.suggests");
UnmodifiableConfig extensionsConfig = config.get("frog.extensions");
Map<String, Object> extensions = new HashMap<>();
if (extensionsConfig != null) {
extensionsConfig.entrySet().forEach(entry -> extensions.put(entry.getKey(), entry.getValue()));
}
return new ModPropertiesImpl(id, name, SemVerImpl.parse(version), License.fromId(license), ModCredits.of(credits), new ModDependencies(depends, breaks, suggests), ModExtensions.of(extensions));
});
private final String version;
private final Parser parser;
private static final Map<String, Parser> versions = Arrays.stream(values()).collect(Collectors.toMap(v -> v.version, v -> v.parser));
public static Parser get(String version){
return versions.get(version);
}
@FunctionalInterface
public interface Parser {
ModProperties parse(UnmodifiableConfig config) throws IOException;
}
}
}

View file

@ -18,7 +18,7 @@ public interface NonsensePlugin extends Runnable {
return false;
}
default void init(LoaderImpl loader) {
default void init(LoaderImpl loader) throws Exception {
}
Collection<ModProperties> getMods();

View file

@ -44,7 +44,7 @@ public class Minecraft implements NonsensePlugin {
}
@Override
public void init(LoaderImpl loader) {
public void init(LoaderImpl loader) throws Exception {
Path remappedGamePath = loader.getGameDir().resolve(".nonsense/remappedJars").resolve(version).resolve("game-" + version + "-remapped.jar");
if (!Files.exists(remappedGamePath)){

View file

@ -1,4 +1,7 @@
[frog]
format_version = "1.0.0"
[frog.mod]
id = "frogloader"
name = "Nonsense Loader"
version = "${version}"