Skip to content
This repository has been archived by the owner on Apr 28, 2020. It is now read-only.

Allow loading mods from version-specified directory i.e. mods/rift/1.13/ #65

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ minecraft {

replace "@VERSION@", project.version
replaceIn "org/dimdev/riftloader/Main.java"

replace "@MCVERSION@", project.minecraft.version
replaceIn "org/dimdev/riftloader/RiftLoader.java"
}

mixin {
Expand Down
50 changes: 39 additions & 11 deletions src/main/java/org/dimdev/riftloader/RiftLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class RiftLoader {
private static final Logger log = LogManager.getLogger("RiftLoader");

public final File modsDir = new File(Launch.minecraftHome, "mods");
public final File modsRiftDir = new File(modsDir, "rift");
public final File modsVersionSpecifiedDir = new File(modsRiftDir, "@MCVERSION@");
public final File configDir = new File(Launch.minecraftHome, "config");
private Side side;
private boolean loaded;
Expand All @@ -49,19 +51,24 @@ public void load(boolean isClient) {

side = isClient ? Side.CLIENT : Side.SERVER;

findMods(modsDir);
// load mods from classpath
findClasspathMods();
// load mods from mods/rift/"version"/
findJarMods(modsVersionSpecifiedDir);
// load mods from folder mods/rift/, create the folder if not exists
findJarMods(modsRiftDir, true);
// load mods from mods/, create the folder if not exists
findJarMods(modsDir, true);
sortMods();
initMods();
initAccessTransformer();
}

/**
* Looks for Rift mods (jars containing a 'riftmod.json' at their root) in
* the 'modsDir' directory (creating it if it doesn't exist) and loads them
* into the 'modInfoMap'.
**/
private void findMods(File modsDir) {
// Load classpath mods
* Load mods from classpath
*/
private void findClasspathMods() {
int modCount = modInfoMap.size();
log.info("Searching mods on classpath");
try {
Enumeration<URL> urls = ClassLoader.getSystemResources("riftmod.json");
Expand Down Expand Up @@ -94,11 +101,31 @@ private void findMods(File modsDir) {
throw new RuntimeException(e);
}

// Load jar mods
modCount = modInfoMap.size() - modCount;
log.info("Loaded " + modCount + " mods");
}

/**
* Looks for Rift mods (jars containing a 'riftmod.json' at their root) in
* the 'modsDir' directory (creating it if it doesn't exist) and loads them
* into the 'modInfoMap'.
**/
private void findJarMods(File modsDir) {
this.findJarMods(modsDir, false);
}

private void findJarMods(File modsDir, boolean createDir) {
if (createDir) {
// create dir if needed
modsDir.mkdirs();
} else if (!modsVersionSpecifiedDir.isDirectory()) {
// skip loading if folder not exists
return;
}
int modCount = modInfoMap.size();
log.info("Searching for mods in " + modsDir);
modsDir.mkdirs();
for (File file : modsDir.listFiles()) {
if (!file.getName().endsWith(".jar")) continue;
if (!file.getName().toLowerCase().endsWith(".jar")) continue;

try (JarFile jar = new JarFile(file)) {
// Check if the file contains a 'riftmod.json'
Expand Down Expand Up @@ -128,7 +155,8 @@ private void findMods(File modsDir) {
}
}

log.info("Loaded " + modInfoMap.size() + " mods");
modCount = modInfoMap.size() - modCount;
log.info("Loaded " + modCount + " mods");
}

private void loadModFromJson(InputStream in, File source) {
Expand Down