Files
FalsePatternLib/src/main/java/com/falsepattern/lib/mixin/IMixinPlugin.java
T

185 lines
6.2 KiB
Java
Raw Normal View History

2022-07-31 20:41:56 +02:00
/*
2023-07-16 12:36:11 +02:00
* Copyright (C) 2022-2023 FalsePattern
2022-07-30 15:21:11 +02:00
* All Rights Reserved
*
2023-07-16 12:36:11 +02:00
* The above copyright notice and this permission notice
2022-07-30 15:21:11 +02:00
* shall be included in all copies or substantial portions of the Software.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.lib.mixin;
2022-06-12 11:58:13 +02:00
import com.falsepattern.lib.StableAPI;
2022-07-31 20:41:56 +02:00
import com.falsepattern.lib.util.FileUtil;
import lombok.val;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2022-09-01 19:19:53 +02:00
import org.spongepowered.asm.lib.tree.ClassNode;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
2022-07-31 20:41:56 +02:00
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
import net.minecraft.launchwrapper.Launch;
import java.io.File;
import java.io.FileNotFoundException;
2023-02-11 11:22:59 +01:00
import java.net.URL;
import java.nio.file.Path;
2023-02-11 11:22:59 +01:00
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
2023-11-19 18:16:08 +01:00
import java.util.function.Predicate;
import java.util.stream.Collectors;
import static java.nio.file.Files.walk;
2022-06-12 11:58:13 +02:00
@StableAPI(since = "0.8.0")
public interface IMixinPlugin extends IMixinConfigPlugin {
2022-08-06 13:12:55 +02:00
@StableAPI.Expose
Path MODS_DIRECTORY_PATH = FileUtil.getMinecraftHome().toPath().resolve("mods");
2022-08-06 13:12:55 +02:00
@StableAPI.Expose
static Logger createLogger(String modName) {
return LogManager.getLogger(modName + " Mixin Loader");
}
2022-08-06 13:12:55 +02:00
@StableAPI.Expose
2022-07-31 20:41:56 +02:00
static File findJarOf(final ITargetedMod mod) {
2023-02-11 11:22:59 +01:00
File result = null;
2022-07-31 20:41:56 +02:00
try (val stream = walk(MODS_DIRECTORY_PATH)) {
2023-07-16 13:09:05 +02:00
result = stream.filter(mod::isMatchingJar).map(Path::toFile).findFirst().orElse(null);
2023-02-11 11:22:59 +01:00
} catch (Exception e) {
2022-07-31 20:41:56 +02:00
e.printStackTrace();
}
2023-02-11 11:22:59 +01:00
if (result == null) {
2023-04-02 15:20:11 +02:00
File found = null;
for (URL url : Launch.classLoader.getURLs()) {
try {
String file = url.getFile();
Path path = Paths.get(file);
if (mod.isMatchingJar(path)) {
found = path.toFile();
break;
}
2023-07-16 13:09:05 +02:00
} catch (Exception ignored) {
}
2023-02-11 11:22:59 +01:00
}
2023-04-02 15:20:11 +02:00
result = found;
2023-02-11 11:22:59 +01:00
}
return result;
2022-07-31 20:41:56 +02:00
}
2022-08-06 13:12:55 +02:00
@StableAPI.Expose
Logger getLogger();
@StableAPI.Expose
IMixin[] getMixinEnumValues();
@StableAPI.Expose
ITargetedMod[] getTargetedModEnumValues();
@Override
2022-08-20 20:08:26 +02:00
@StableAPI.Expose(since = "__INTERNAL__")
default void onLoad(String mixinPackage) {
}
@Override
2022-08-20 20:08:26 +02:00
@StableAPI.Expose(since = "__INTERNAL__")
default String getRefMapperConfig() {
return null;
}
@Override
2022-08-20 20:08:26 +02:00
@StableAPI.Expose(since = "__INTERNAL__")
default boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
return true;
}
@Override
2022-08-20 20:08:26 +02:00
@StableAPI.Expose(since = "__INTERNAL__")
default void acceptTargets(Set<String> myTargets, Set<String> otherTargets) {
}
@Override
2022-08-20 20:08:26 +02:00
@StableAPI.Expose(since = "__INTERNAL__")
default List<String> getMixins() {
val isDevelopmentEnvironment = (boolean) Launch.blackboard.get("fml.deobfuscatedEnvironment");
2022-04-17 17:44:01 +02:00
val targetedMods = getTargetedModEnumValues();
val loadedMods = Arrays.stream(targetedMods)
2023-11-19 18:16:08 +01:00
//Java 21 compat
.filter(new Predicate<ITargetedMod>() {
@Override
public boolean test(ITargetedMod mod) {
return (mod.isLoadInDevelopment() && isDevelopmentEnvironment)
|| IMixinPlugin.this.loadJarOf(mod);
}
})
.collect(Collectors.toList());
for (val mod : targetedMods) {
2022-06-08 16:01:06 +02:00
if (loadedMods.contains(mod)) {
getLogger().info("Found " + mod.getModName() + "! Integrating now...");
2022-06-08 16:01:06 +02:00
} else {
getLogger().info("Could not find " + mod.getModName() + "! Skipping integration....");
}
}
List<String> mixins = new ArrayList<>();
2022-04-17 17:44:01 +02:00
for (val mixin : getMixinEnumValues()) {
if (mixin.shouldLoad(loadedMods)) {
2022-04-17 20:00:43 +02:00
String mixinClass = mixin.getSide().name().toLowerCase() + "." + mixin.getMixin();
mixins.add(mixinClass);
getLogger().info("Loading mixin: " + mixinClass);
}
}
return mixins;
}
2022-08-20 20:08:26 +02:00
@StableAPI.Expose(since = "__INTERNAL__")
default boolean loadJarOf(final ITargetedMod mod) {
boolean success = false;
try {
File jar = findJarOf(mod);
2022-06-08 16:01:06 +02:00
if (jar == null) {
getLogger().info("Jar not found for " + mod);
return false;
}
getLogger().info("Attempting to add " + jar + " to the URL Class Path");
success = true;
2022-06-08 16:01:06 +02:00
if (!jar.exists()) {
success = false;
throw new FileNotFoundException(jar.toString());
}
MinecraftURLClassPath.addJar(jar);
} catch (Throwable e) {
e.printStackTrace();
}
return success;
}
2022-08-20 20:08:26 +02:00
@StableAPI.Expose(since = "__INTERNAL__")
2022-09-01 19:19:53 +02:00
@Override
2022-08-21 11:09:33 +02:00
default void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {
2022-06-08 16:01:06 +02:00
}
2022-08-20 20:08:26 +02:00
@StableAPI.Expose(since = "__INTERNAL__")
2022-09-01 19:19:53 +02:00
@Override
2022-08-21 11:09:33 +02:00
default void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {
2022-06-08 16:01:06 +02:00
}
}