Skip to content

Commit e80f565

Browse files
committed
V2 mixin DSL
1 parent c0c50b8 commit e80f565

File tree

6 files changed

+342
-0
lines changed

6 files changed

+342
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* This file is part of FalsePatternLib.
3+
*
4+
* Copyright (C) 2022-2025 FalsePattern
5+
* All Rights Reserved
6+
*
7+
* The above copyright notice and this permission notice shall be included
8+
* in all copies or substantial portions of the Software.
9+
*
10+
* FalsePatternLib is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Lesser General Public License as published by
12+
* the Free Software Foundation, only version 3 of the License.
13+
*
14+
* FalsePatternLib is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public License
20+
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
21+
*/
22+
23+
package com.falsepattern.lib.mixin.v2;
24+
25+
import com.gtnewhorizon.gtnhmixins.builders.IMixins;
26+
import lombok.val;
27+
import org.apache.logging.log4j.LogManager;
28+
import org.apache.logging.log4j.Logger;
29+
import org.jetbrains.annotations.Nullable;
30+
import org.spongepowered.asm.lib.tree.ClassNode;
31+
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
32+
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
33+
34+
import java.util.List;
35+
import java.util.Set;
36+
37+
public interface IMixinPlugin<E extends Enum<E> & IMixins> extends IMixinConfigPlugin {
38+
static Logger createLogger(String modName) {
39+
return LogManager.getLogger(modName + " Mixin Loader");
40+
}
41+
42+
Logger getLogger();
43+
44+
Class<E> getMixinEnum();
45+
46+
@Override
47+
default void onLoad(String mixinPackage) {}
48+
49+
@Override
50+
default @Nullable String getRefMapperConfig() {
51+
return null;
52+
}
53+
54+
@Override
55+
default boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
56+
return true;
57+
}
58+
59+
@Override
60+
default void acceptTargets(Set<String> myTargets, Set<String> otherTargets) {}
61+
62+
@Override
63+
default List<String> getMixins() {
64+
val logger = getLogger();
65+
logger.info("Scanning mixins...");
66+
try {
67+
return IMixins.getMixins(getMixinEnum());
68+
} finally {
69+
logger.info("Mixins scanned.");
70+
}
71+
}
72+
73+
@Override
74+
default void preApply(String s, ClassNode classNode, String s1, IMixinInfo iMixinInfo) {}
75+
76+
@Override
77+
default void postApply(String s, ClassNode classNode, String s1, IMixinInfo iMixinInfo) {}
78+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* This file is part of FalsePatternLib.
3+
*
4+
* Copyright (C) 2022-2025 FalsePattern
5+
* All Rights Reserved
6+
*
7+
* The above copyright notice and this permission notice shall be included
8+
* in all copies or substantial portions of the Software.
9+
*
10+
* FalsePatternLib is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Lesser General Public License as published by
12+
* the Free Software Foundation, only version 3 of the License.
13+
*
14+
* FalsePatternLib is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public License
20+
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
21+
*/
22+
23+
package com.falsepattern.lib.mixin.v2;
24+
25+
import com.gtnewhorizon.gtnhmixins.builders.IBaseTransformer;
26+
import com.gtnewhorizon.gtnhmixins.builders.ITargetMod;
27+
import com.gtnewhorizon.gtnhmixins.builders.MixinBuilder;
28+
import lombok.val;
29+
import org.jetbrains.annotations.NotNull;
30+
import org.jetbrains.annotations.Nullable;
31+
32+
import java.util.function.BooleanSupplier;
33+
34+
public class MixinHelper {
35+
public static @NotNull MixinBuilder builder(@NotNull SidedMixins @NotNull ... mixins) {
36+
return builderImpl(null, null, mixins);
37+
}
38+
39+
public static @NotNull MixinBuilder builder(@Nullable BooleanSupplier cond, @NotNull SidedMixins @NotNull ... mixins) {
40+
return builderImpl(cond, null, mixins);
41+
}
42+
43+
public static @NotNull MixinBuilder builder(@NotNull TaggedMod mod, @NotNull SidedMixins @NotNull ... mixins) {
44+
return builderImpl(null, mods(mod), mixins);
45+
}
46+
47+
public static @NotNull MixinBuilder builder(@NotNull TaggedMod @Nullable [] mods, @NotNull SidedMixins @NotNull ... mixins) {
48+
return builderImpl(null, mods, mixins);
49+
}
50+
51+
public static @NotNull MixinBuilder builder(@NotNull BooleanSupplier cond, @NotNull TaggedMod mod, @NotNull SidedMixins @NotNull ... mixins) {
52+
return builderImpl(cond, mods(mod), mixins);
53+
}
54+
55+
public static @NotNull MixinBuilder builder(@NotNull BooleanSupplier cond, @NotNull TaggedMod @NotNull [] mods, @NotNull SidedMixins @NotNull ... mixins) {
56+
return builderImpl(cond, mods, mixins);
57+
}
58+
59+
private static @NotNull MixinBuilder builderImpl(@Nullable BooleanSupplier cond, @NotNull TaggedMod @Nullable [] requiredMods, @NotNull SidedMixins @NotNull ... mixins) {
60+
val builder = new MixinBuilder();
61+
if (requiredMods != null) {
62+
for (val mod : requiredMods) {
63+
if (mod.require) {
64+
builder.addRequiredMod(mod.mod);
65+
} else {
66+
builder.addExcludedMod(mod.mod);
67+
}
68+
}
69+
}
70+
for (val mixin : mixins) {
71+
builder.addSidedMixins(mixin.side, mixin.mixins);
72+
}
73+
if (cond != null) {
74+
builder.setApplyIf(cond::getAsBoolean);
75+
}
76+
return builder;
77+
}
78+
79+
public static @NotNull SidedMixins common(@NotNull String @NotNull ... mixins) {
80+
for (int i = 0; i < mixins.length; i++) {
81+
mixins[i] = "common." + mixins[i];
82+
}
83+
return new SidedMixins(IBaseTransformer.Side.COMMON, mixins);
84+
}
85+
86+
public static @NotNull SidedMixins client(@NotNull String @NotNull ... mixins) {
87+
for (int i = 0; i < mixins.length; i++) {
88+
mixins[i] = "client." + mixins[i];
89+
}
90+
return new SidedMixins(IBaseTransformer.Side.CLIENT, mixins);
91+
}
92+
93+
public static @NotNull SidedMixins server(@NotNull String @NotNull ... mixins) {
94+
for (int i = 0; i < mixins.length; i++) {
95+
mixins[i] = "server." + mixins[i];
96+
}
97+
return new SidedMixins(IBaseTransformer.Side.SERVER, mixins);
98+
}
99+
100+
public static @NotNull TaggedMod require(@NotNull ITargetMod mod) {
101+
return new TaggedMod(true, mod);
102+
}
103+
104+
public static @NotNull TaggedMod avoid(@NotNull ITargetMod mod) {
105+
return new TaggedMod(false, mod);
106+
}
107+
108+
public static @NotNull TaggedMod @NotNull [] mods(@NotNull TaggedMod @NotNull ... t) {
109+
return t;
110+
}
111+
112+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* This file is part of FalsePatternLib.
3+
*
4+
* Copyright (C) 2022-2025 FalsePattern
5+
* All Rights Reserved
6+
*
7+
* The above copyright notice and this permission notice shall be included
8+
* in all copies or substantial portions of the Software.
9+
*
10+
* FalsePatternLib is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Lesser General Public License as published by
12+
* the Free Software Foundation, only version 3 of the License.
13+
*
14+
* FalsePatternLib is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public License
20+
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
21+
*/
22+
23+
package com.falsepattern.lib.mixin.v2;
24+
25+
import com.gtnewhorizon.gtnhmixins.builders.IBaseTransformer;
26+
import lombok.RequiredArgsConstructor;
27+
import org.jetbrains.annotations.NotNull;
28+
29+
@RequiredArgsConstructor
30+
public class SidedMixins {
31+
public final IBaseTransformer.@NotNull Side side;
32+
public final @NotNull String @NotNull [] mixins;
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* This file is part of FalsePatternLib.
3+
*
4+
* Copyright (C) 2022-2025 FalsePattern
5+
* All Rights Reserved
6+
*
7+
* The above copyright notice and this permission notice shall be included
8+
* in all copies or substantial portions of the Software.
9+
*
10+
* FalsePatternLib is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Lesser General Public License as published by
12+
* the Free Software Foundation, only version 3 of the License.
13+
*
14+
* FalsePatternLib is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public License
20+
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
21+
*/
22+
23+
package com.falsepattern.lib.mixin.v2;
24+
25+
import com.gtnewhorizon.gtnhmixins.builders.ITargetMod;
26+
import lombok.RequiredArgsConstructor;
27+
import org.jetbrains.annotations.NotNull;
28+
29+
@RequiredArgsConstructor
30+
public class TaggedMod {
31+
public final boolean require;
32+
public final @NotNull ITargetMod mod;
33+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* This file is part of FalsePatternLib.
3+
*
4+
* Copyright (C) 2022-2025 FalsePattern
5+
* All Rights Reserved
6+
*
7+
* The above copyright notice and this permission notice shall be included
8+
* in all copies or substantial portions of the Software.
9+
*
10+
* FalsePatternLib is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Lesser General Public License as published by
12+
* the Free Software Foundation, only version 3 of the License.
13+
*
14+
* FalsePatternLib is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public License
20+
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
21+
*/
22+
23+
package com.falsepattern.lib.mixin.v2;
24+
25+
import com.gtnewhorizon.gtnhmixins.builders.ITargetMod;
26+
import com.gtnewhorizon.gtnhmixins.builders.TargetModBuilder;
27+
import lombok.Getter;
28+
import org.intellij.lang.annotations.Language;
29+
import org.jetbrains.annotations.NotNull;
30+
import org.jetbrains.annotations.Nullable;
31+
32+
import java.util.function.Consumer;
33+
34+
public class TargetMod implements ITargetMod {
35+
@Getter
36+
private final TargetModBuilder builder;
37+
38+
private final String name;
39+
40+
public TargetMod(@NotNull String name, @Language(value = "JAVA", prefix = "import ", suffix = ";") @NotNull String className) {
41+
this(name, className, null);
42+
}
43+
44+
public TargetMod(@NotNull String name, @Language(value = "JAVA", prefix = "import ", suffix = ";") @NotNull String className, @Nullable Consumer<TargetModBuilder> cfg) {
45+
this.name = name;
46+
builder = new TargetModBuilder();
47+
builder.setTargetClass(className);
48+
if (cfg != null) {
49+
cfg.accept(builder);
50+
}
51+
}
52+
53+
@Override
54+
public String toString() {
55+
return name;
56+
}
57+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* This file is part of FalsePatternLib.
3+
*
4+
* Copyright (C) 2022-2025 FalsePattern
5+
* All Rights Reserved
6+
*
7+
* The above copyright notice and this permission notice shall be included
8+
* in all copies or substantial portions of the Software.
9+
*
10+
* FalsePatternLib is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Lesser General Public License as published by
12+
* the Free Software Foundation, only version 3 of the License.
13+
*
14+
* FalsePatternLib is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public License
20+
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
21+
*/
22+
23+
/**
24+
* @since 1.9.0
25+
*/
26+
@ApiStatus.Experimental
27+
package com.falsepattern.lib.mixin.v2;
28+
29+
import org.jetbrains.annotations.ApiStatus;

0 commit comments

Comments
 (0)