V2 mixin DSL

This commit is contained in:
FalsePattern committed 2025-09-08 00:50:13 +02:00
1 parent c0c50b841e
commit e80f56523f
6 files changed
+342

No files matched your search

@@ -0,0 +1,78 @@
/*
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2025 FalsePattern
* All Rights Reserved
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* FalsePatternLib 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, only version 3 of the License.
*
* FalsePatternLib 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.lib.mixin.v2;
import com.gtnewhorizon.gtnhmixins.builders.IMixins;
import lombok.val;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.lib.tree.ClassNode;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
import java.util.List;
import java.util.Set;
public interface IMixinPlugin<E extends Enum<E> & IMixins> extends IMixinConfigPlugin {
static Logger createLogger(String modName) {
return LogManager.getLogger(modName + " Mixin Loader");
}
Logger getLogger();
Class<E> getMixinEnum();
@Override
default void onLoad(String mixinPackage) {}
@Override
default @Nullable String getRefMapperConfig() {
return null;
}
@Override
default boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
return true;
}
@Override
default void acceptTargets(Set<String> myTargets, Set<String> otherTargets) {}
@Override
default List<String> getMixins() {
val logger = getLogger();
logger.info("Scanning mixins...");
try {
return IMixins.getMixins(getMixinEnum());
} finally {
logger.info("Mixins scanned.");
}
}
@Override
default void preApply(String s, ClassNode classNode, String s1, IMixinInfo iMixinInfo) {}
@Override
default void postApply(String s, ClassNode classNode, String s1, IMixinInfo iMixinInfo) {}
}
@@ -0,0 +1,112 @@
/*
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2025 FalsePattern
* All Rights Reserved
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* FalsePatternLib 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, only version 3 of the License.
*
* FalsePatternLib 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.lib.mixin.v2;
import com.gtnewhorizon.gtnhmixins.builders.IBaseTransformer;
import com.gtnewhorizon.gtnhmixins.builders.ITargetMod;
import com.gtnewhorizon.gtnhmixins.builders.MixinBuilder;
import lombok.val;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.BooleanSupplier;
public class MixinHelper {
public static @NotNull MixinBuilder builder(@NotNull SidedMixins @NotNull ... mixins) {
return builderImpl(null, null, mixins);
}
public static @NotNull MixinBuilder builder(@Nullable BooleanSupplier cond, @NotNull SidedMixins @NotNull ... mixins) {
return builderImpl(cond, null, mixins);
}
public static @NotNull MixinBuilder builder(@NotNull TaggedMod mod, @NotNull SidedMixins @NotNull ... mixins) {
return builderImpl(null, mods(mod), mixins);
}
public static @NotNull MixinBuilder builder(@NotNull TaggedMod @Nullable [] mods, @NotNull SidedMixins @NotNull ... mixins) {
return builderImpl(null, mods, mixins);
}
public static @NotNull MixinBuilder builder(@NotNull BooleanSupplier cond, @NotNull TaggedMod mod, @NotNull SidedMixins @NotNull ... mixins) {
return builderImpl(cond, mods(mod), mixins);
}
public static @NotNull MixinBuilder builder(@NotNull BooleanSupplier cond, @NotNull TaggedMod @NotNull [] mods, @NotNull SidedMixins @NotNull ... mixins) {
return builderImpl(cond, mods, mixins);
}
private static @NotNull MixinBuilder builderImpl(@Nullable BooleanSupplier cond, @NotNull TaggedMod @Nullable [] requiredMods, @NotNull SidedMixins @NotNull ... mixins) {
val builder = new MixinBuilder();
if (requiredMods != null) {
for (val mod : requiredMods) {
if (mod.require) {
builder.addRequiredMod(mod.mod);
} else {
builder.addExcludedMod(mod.mod);
}
}
}
for (val mixin : mixins) {
builder.addSidedMixins(mixin.side, mixin.mixins);
}
if (cond != null) {
builder.setApplyIf(cond::getAsBoolean);
}
return builder;
}
public static @NotNull SidedMixins common(@NotNull String @NotNull ... mixins) {
for (int i = 0; i < mixins.length; i++) {
mixins[i] = "common." + mixins[i];
}
return new SidedMixins(IBaseTransformer.Side.COMMON, mixins);
}
public static @NotNull SidedMixins client(@NotNull String @NotNull ... mixins) {
for (int i = 0; i < mixins.length; i++) {
mixins[i] = "client." + mixins[i];
}
return new SidedMixins(IBaseTransformer.Side.CLIENT, mixins);
}
public static @NotNull SidedMixins server(@NotNull String @NotNull ... mixins) {
for (int i = 0; i < mixins.length; i++) {
mixins[i] = "server." + mixins[i];
}
return new SidedMixins(IBaseTransformer.Side.SERVER, mixins);
}
public static @NotNull TaggedMod require(@NotNull ITargetMod mod) {
return new TaggedMod(true, mod);
}
public static @NotNull TaggedMod avoid(@NotNull ITargetMod mod) {
return new TaggedMod(false, mod);
}
public static @NotNull TaggedMod @NotNull [] mods(@NotNull TaggedMod @NotNull ... t) {
return t;
}
}
@@ -0,0 +1,33 @@
/*
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2025 FalsePattern
* All Rights Reserved
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* FalsePatternLib 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, only version 3 of the License.
*
* FalsePatternLib 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.lib.mixin.v2;
import com.gtnewhorizon.gtnhmixins.builders.IBaseTransformer;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
@RequiredArgsConstructor
public class SidedMixins {
public final IBaseTransformer.@NotNull Side side;
public final @NotNull String @NotNull [] mixins;
}
@@ -0,0 +1,33 @@
/*
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2025 FalsePattern
* All Rights Reserved
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* FalsePatternLib 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, only version 3 of the License.
*
* FalsePatternLib 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.lib.mixin.v2;
import com.gtnewhorizon.gtnhmixins.builders.ITargetMod;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
@RequiredArgsConstructor
public class TaggedMod {
public final boolean require;
public final @NotNull ITargetMod mod;
}
@@ -0,0 +1,57 @@
/*
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2025 FalsePattern
* All Rights Reserved
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* FalsePatternLib 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, only version 3 of the License.
*
* FalsePatternLib 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.lib.mixin.v2;
import com.gtnewhorizon.gtnhmixins.builders.ITargetMod;
import com.gtnewhorizon.gtnhmixins.builders.TargetModBuilder;
import lombok.Getter;
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.Consumer;
public class TargetMod implements ITargetMod {
@Getter
private final TargetModBuilder builder;
private final String name;
public TargetMod(@NotNull String name, @Language(value = "JAVA", prefix = "import ", suffix = ";") @NotNull String className) {
this(name, className, null);
}
public TargetMod(@NotNull String name, @Language(value = "JAVA", prefix = "import ", suffix = ";") @NotNull String className, @Nullable Consumer<TargetModBuilder> cfg) {
this.name = name;
builder = new TargetModBuilder();
builder.setTargetClass(className);
if (cfg != null) {
cfg.accept(builder);
}
}
@Override
public String toString() {
return name;
}
}
@@ -0,0 +1,29 @@
/*
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2025 FalsePattern
* All Rights Reserved
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* FalsePatternLib 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, only version 3 of the License.
*
* FalsePatternLib 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* @since 1.9.0
*/
@ApiStatus.Experimental
package com.falsepattern.lib.mixin.v2;
import org.jetbrains.annotations.ApiStatus;