2022-07-31 20:41:56 +02:00
|
|
|
/*
|
2022-07-30 15:21:11 +02:00
|
|
|
* Copyright (C) 2022 FalsePattern
|
|
|
|
|
* All Rights Reserved
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice, this permission notice and the word "SNEED"
|
|
|
|
|
* 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/>.
|
|
|
|
|
*/
|
2022-04-17 17:26:30 +02:00
|
|
|
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;
|
|
|
|
|
|
2022-04-17 17:26:30 +02:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.stream.Collectors;
|
2022-06-15 18:39:15 +02:00
|
|
|
|
2022-04-17 17:26:30 +02:00
|
|
|
import static java.nio.file.Files.walk;
|
|
|
|
|
|
2022-06-12 11:58:13 +02:00
|
|
|
@StableAPI(since = "0.8.0")
|
2022-04-17 17:26:30 +02:00
|
|
|
public interface IMixinPlugin extends IMixinConfigPlugin {
|
2022-08-06 13:12:55 +02:00
|
|
|
@StableAPI.Expose
|
2022-06-15 18:39:15 +02:00
|
|
|
Path MODS_DIRECTORY_PATH = FileUtil.getMinecraftHome().toPath().resolve("mods");
|
2022-04-17 17:26:30 +02:00
|
|
|
|
2022-08-06 13:12:55 +02:00
|
|
|
@StableAPI.Expose
|
2022-04-17 20:16:59 +02:00
|
|
|
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) {
|
|
|
|
|
try (val stream = walk(MODS_DIRECTORY_PATH)) {
|
|
|
|
|
return stream.filter(mod::isMatchingJar).map(Path::toFile).findFirst().orElse(null);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-06 13:12:55 +02:00
|
|
|
@StableAPI.Expose
|
|
|
|
|
Logger getLogger();
|
|
|
|
|
|
|
|
|
|
@StableAPI.Expose
|
|
|
|
|
IMixin[] getMixinEnumValues();
|
|
|
|
|
|
|
|
|
|
@StableAPI.Expose
|
|
|
|
|
ITargetedMod[] getTargetedModEnumValues();
|
|
|
|
|
|
2022-04-17 17:26:30 +02:00
|
|
|
@Override
|
2022-08-20 20:08:26 +02:00
|
|
|
@StableAPI.Expose(since = "__INTERNAL__")
|
2022-04-17 17:26:30 +02:00
|
|
|
default void onLoad(String mixinPackage) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-08-20 20:08:26 +02:00
|
|
|
@StableAPI.Expose(since = "__INTERNAL__")
|
2022-04-17 17:26:30 +02:00
|
|
|
default String getRefMapperConfig() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-08-20 20:08:26 +02:00
|
|
|
@StableAPI.Expose(since = "__INTERNAL__")
|
2022-04-17 17:26:30 +02:00
|
|
|
default boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-08-20 20:08:26 +02:00
|
|
|
@StableAPI.Expose(since = "__INTERNAL__")
|
2022-04-17 17:26:30 +02:00
|
|
|
default void acceptTargets(Set<String> myTargets, Set<String> otherTargets) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-08-20 20:08:26 +02:00
|
|
|
@StableAPI.Expose(since = "__INTERNAL__")
|
2022-04-17 17:26:30 +02:00
|
|
|
default List<String> getMixins() {
|
|
|
|
|
val isDevelopmentEnvironment = (boolean) Launch.blackboard.get("fml.deobfuscatedEnvironment");
|
2022-04-17 17:44:01 +02:00
|
|
|
val targetedMods = getTargetedModEnumValues();
|
2022-04-17 17:26:30 +02:00
|
|
|
val loadedMods = Arrays.stream(targetedMods)
|
2022-06-08 16:01:06 +02:00
|
|
|
.filter(mod -> (mod.isLoadInDevelopment() && isDevelopmentEnvironment) || loadJarOf(mod))
|
2022-04-17 17:26:30 +02:00
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
for (val mod : targetedMods) {
|
2022-06-08 16:01:06 +02:00
|
|
|
if (loadedMods.contains(mod)) {
|
2022-04-17 17:26:30 +02:00
|
|
|
getLogger().info("Found " + mod.getModName() + "! Integrating now...");
|
2022-06-08 16:01:06 +02:00
|
|
|
} else {
|
2022-04-17 17:26:30 +02:00
|
|
|
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()) {
|
2022-04-17 17:26:30 +02:00
|
|
|
if (mixin.shouldLoad(loadedMods)) {
|
2022-04-17 20:00:43 +02:00
|
|
|
String mixinClass = mixin.getSide().name().toLowerCase() + "." + mixin.getMixin();
|
2022-04-17 17:26:30 +02:00
|
|
|
mixins.add(mixinClass);
|
|
|
|
|
getLogger().info("Loading mixin: " + mixinClass);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return mixins;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-20 20:08:26 +02:00
|
|
|
@StableAPI.Expose(since = "__INTERNAL__")
|
2022-04-17 17:26:30 +02:00
|
|
|
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) {
|
2022-04-17 17:26:30 +02:00
|
|
|
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()) {
|
2022-04-17 17:26:30 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
}
|
2022-04-17 17:26:30 +02:00
|
|
|
}
|