Skip to content

Commit

Permalink
add more robust mixin file discovery API
Browse files Browse the repository at this point in the history
  • Loading branch information
FalsePattern committed Jul 19, 2024
1 parent c373de4 commit da80279
Showing 1 changed file with 49 additions and 12 deletions.
61 changes: 49 additions & 12 deletions src/main/java/com/falsepattern/lib/mixin/IMixinPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
package com.falsepattern.lib.mixin;

import com.falsepattern.lib.DeprecationDetails;
import com.falsepattern.lib.StableAPI;
import com.falsepattern.lib.util.FileUtil;
import lombok.val;
Expand All @@ -40,6 +41,8 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
Expand All @@ -57,6 +60,8 @@ static Logger createLogger(String modName) {
return LogManager.getLogger(modName + " Mixin Loader");
}

@Deprecated
@DeprecationDetails(deprecatedSince = "1.4.0")
@StableAPI.Expose
static File findJarOf(final ITargetedMod mod) {
File result = null;
Expand All @@ -83,6 +88,32 @@ static File findJarOf(final ITargetedMod mod) {
return result;
}

@StableAPI.Expose(since = "1.4.0")
static Set<File> findJarsOf(IMixinPlugin self, final ITargetedMod mod) {
if (!self.useNewFindJar()) {
val jar = findJarOf(mod);
return jar == null ? Collections.emptySet() : Set.of(jar);
}
val results = new HashSet<File>();
try (val stream = walk(MODS_DIRECTORY_PATH)) {
results.addAll(stream.filter(mod::isMatchingJar).map(Path::toFile).toList());
} catch (Exception e) {
e.printStackTrace();
}
for (URL url : Launch.classLoader.getURLs()) {
try {
String file = url.getFile();
Path path = Paths.get(file);
if (mod.isMatchingJar(path)) {
results.add(path.toFile());
break;
}
} catch (Exception ignored) {
}
}
return results;
}

@StableAPI.Expose
Logger getLogger();

Expand All @@ -92,6 +123,11 @@ static File findJarOf(final ITargetedMod mod) {
@StableAPI.Expose
ITargetedMod[] getTargetedModEnumValues();

@StableAPI.Expose(since = "1.4.0")
default boolean useNewFindJar() {
return false;
}

@Override
@StableAPI.Expose(since = "__INTERNAL__")
default void onLoad(String mixinPackage) {
Expand Down Expand Up @@ -126,8 +162,9 @@ default List<String> getMixins() {
.filter(new Predicate<ITargetedMod>() {
@Override
public boolean test(ITargetedMod mod) {
boolean loadJar = IMixinPlugin.this.loadJarOf(mod);
return (mod.isLoadInDevelopment() && isDevelopmentEnvironment)
|| IMixinPlugin.this.loadJarOf(mod);
|| loadJar;
}
})
.collect(Collectors.toList());
Expand All @@ -153,24 +190,24 @@ public boolean test(ITargetedMod mod) {

@StableAPI.Expose(since = "__INTERNAL__")
default boolean loadJarOf(final ITargetedMod mod) {
boolean success = false;
try {
File jar = findJarOf(mod);
if (jar == null) {
getLogger().info("Jar not found for " + mod);
val jars = findJarsOf(this, mod);
if (jars.isEmpty()) {
getLogger().info("Jar not found for {}", mod);
return false;
}
getLogger().info("Attempting to add " + jar + " to the URL Class Path");
success = true;
if (!jar.exists()) {
success = false;
throw new FileNotFoundException(jar.toString());
for (val jar: jars) {
getLogger().info("Attempting to add {} to the URL Class Path", jar);
try {
MinecraftURLClassPath.addJar(jar);
} catch (Throwable e) {
e.printStackTrace();
}
}
MinecraftURLClassPath.addJar(jar);
} catch (Throwable e) {
e.printStackTrace();
}
return success;
return true;
}

@StableAPI.Expose(since = "__INTERNAL__")
Expand Down

0 comments on commit da80279

Please sign in to comment.