Skip to content

Commit

Permalink
feat: add version ranges for compat modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamalam360 committed Jan 8, 2023
1 parent 5c39d2e commit 74c414f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.VersionParsingException;
import net.fabricmc.loader.api.metadata.version.VersionPredicate;

/**
* <p>Helper class to initialize 'compatibility modules' for a mod. A compatibility module is
Expand All @@ -50,6 +52,18 @@
* </pre>
* <p>JamLib only loads these classes if the other mod is present, so as long as you are
* careful, you shouldn't run into classloading issues.</p>
*
* <p>You can also optionally define a version(-range), which is parsed using {@link VersionPredicate#parse(String)}:</p>
* <pre>
* {@code
* "custom": {
* "jamlib:compatibility_modules": {
* "emi[>=0.3.0]": "io.github.jamalam360.test.EmiCompatibility",
* "emi[<0.3.0]": "io.github.jamalam360.test.OldEmiCompatibility",
* }
* }
* }
* </pre>
*/
public class JamLibCompatibilityModuleHandler {

Expand All @@ -72,8 +86,22 @@ public static void initialize(String modId) {
}

mod.get().getMetadata().getCustomValue("jamlib:compatibility_modules").getAsObject().forEach(((e -> {
if (FabricLoader.getInstance().isModLoaded(e.getKey())) {
JamLib.LOGGER.info("Initializing", modId, "compatibility module for", e.getKey());
String compatId = e.getKey();
VersionPredicate versionPredicate = null;

if (compatId.contains("[") && compatId.endsWith("]")) {
String version = compatId.substring(compatId.indexOf("[") + 1, compatId.length() - 1);
compatId = compatId.substring(0, compatId.indexOf("["));

try {
versionPredicate = VersionPredicate.parse(version);
} catch (VersionParsingException ex) {
JamLib.LOGGER.warn("Encountered invalid version range", version, "from compatibility modules of mod", modId);
}
}

if (FabricLoader.getInstance().isModLoaded(compatId) && (versionPredicate == null || versionPredicate.test(FabricLoader.getInstance().getModContainer(compatId).get().getMetadata().getVersion()))) {
JamLib.LOGGER.info("Initializing", modId, "compatibility module for", compatId);

try {
Class<?> clazz = Class.forName(e.getValue().getAsString());
Expand Down
4 changes: 3 additions & 1 deletion src/testmod/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
},
"custom": {
"jamlib:compatibility_modules": {
"fabric": "io.github.jamalam360.jamlib.test.TestCompatibilityModule"
"fabric": "io.github.jamalam360.jamlib.test.TestCompatibilityModule",
"fabricloader[<0.12.5]": "io.github.jamalam360.jamlib.test.TestCompatibilityModule",
"emi": "io.github.jamalam360.jamlib.test.TestCompatibilityModule"
}
}
}

0 comments on commit 74c414f

Please sign in to comment.