-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathIMixinPlugin.java
More file actions
219 lines (193 loc) · 6.99 KB
/
IMixinPlugin.java
File metadata and controls
219 lines (193 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* 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;
import com.falsepattern.lib.util.FileUtil;
import lombok.val;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.ApiStatus;
import org.spongepowered.asm.lib.tree.ClassNode;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
import net.minecraft.launchwrapper.Launch;
import java.io.File;
import java.net.URL;
import java.nio.file.Path;
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;
import java.util.stream.Collectors;
import static java.nio.file.Files.walk;
public interface IMixinPlugin extends IMixinConfigPlugin {
Path MODS_DIRECTORY_PATH = FileUtil.getMinecraftHome().toPath().resolve("mods");
static Logger createLogger(String modName) {
return LogManager.getLogger(modName + " Mixin Loader");
}
/**
* @deprecated since 1.4.0
*/
@Deprecated
static File findJarOf(final ITargetedMod mod) {
File result = null;
try (val stream = walk(MODS_DIRECTORY_PATH)) {
result = stream.filter(mod::isMatchingJar).map(Path::toFile).findFirst().orElse(null);
} catch (Exception e) {
e.printStackTrace();
}
if (result == null) {
File found = null;
for (URL url : Launch.classLoader.getURLs()) {
try {
val file = new File(url.toURI());
Path path = file.toPath();
if (mod.isMatchingJar(path)) {
found = file;
break;
}
} catch (Exception ignored) {
}
}
result = found;
}
return result;
}
/**
* @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() : Collections.singleton(jar);
}
val results = new HashSet<File>();
try (val stream = walk(MODS_DIRECTORY_PATH)) {
results.addAll(stream.filter(mod::isMatchingJar).map(Path::toFile).collect(Collectors.toSet()));
} catch (Exception e) {
e.printStackTrace();
}
for (URL url : Launch.classLoader.getURLs()) {
try {
val file = new File(url.toURI());
Path path = file.toPath();
if (mod.isMatchingJar(path)) {
results.add(file);
break;
}
} catch (Exception ignored) {
}
}
return results;
}
Logger getLogger();
IMixin[] getMixinEnumValues();
ITargetedMod[] getTargetedModEnumValues();
/**
* @since 1.4.0
*/
default boolean useNewFindJar() {
return false;
}
@Override
@ApiStatus.Internal
default void onLoad(String mixinPackage) {
}
@Override
@ApiStatus.Internal
default String getRefMapperConfig() {
return null;
}
@Override
@ApiStatus.Internal
default boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
return true;
}
@Override
@ApiStatus.Internal
default void acceptTargets(Set<String> myTargets, Set<String> otherTargets) {
}
@Override
@ApiStatus.Internal
default List<String> getMixins() {
val isDevelopmentEnvironment = (boolean) Launch.blackboard.get("fml.deobfuscatedEnvironment");
val targetedMods = getTargetedModEnumValues();
val loadedMods = Arrays.stream(targetedMods)
//Java 21 compat
.filter(new Predicate<ITargetedMod>() {
@Override
public boolean test(ITargetedMod mod) {
boolean loadJar = IMixinPlugin.this.loadJarOf(mod);
return (mod.isLoadInDevelopment() && isDevelopmentEnvironment)
|| loadJar;
}
})
.collect(Collectors.toList());
for (val mod : targetedMods) {
if (loadedMods.contains(mod)) {
getLogger().info("Found " + mod.getModName() + "! Integrating now...");
} else {
getLogger().info("Could not find " + mod.getModName() + "! Skipping integration....");
}
}
List<String> mixins = new ArrayList<>();
for (val mixin : getMixinEnumValues()) {
if (mixin.shouldLoad(loadedMods)) {
String mixinClass = mixin.getSide().name().toLowerCase() + "." + mixin.getMixin();
mixins.add(mixinClass);
getLogger().info("Loading mixin: " + mixinClass);
}
}
return mixins;
}
@ApiStatus.Internal
default boolean loadJarOf(final ITargetedMod mod) {
try {
val jars = findJarsOf(this, mod);
if (jars.isEmpty()) {
getLogger().info("Jar not found for {}", mod);
return false;
}
for (val jar: jars) {
getLogger().info("Attempting to add {} to the URL Class Path", jar);
try {
MinecraftURLClassPath.addJar(jar);
} catch (Throwable e) {
e.printStackTrace();
}
}
} catch (Throwable e) {
e.printStackTrace();
}
return true;
}
@Override
@ApiStatus.Internal
default void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {
}
@Override
@ApiStatus.Internal
default void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {
}
}