This repository was archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMixinGenExtension.java
More file actions
319 lines (260 loc) · 10.8 KB
/
MixinGenExtension.java
File metadata and controls
319 lines (260 loc) · 10.8 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package io.github.opencubicchunks.gradle;
import static org.apache.tools.ant.util.StringUtils.removePrefix;
import static org.apache.tools.ant.util.StringUtils.removeSuffix;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import com.google.gson.stream.JsonWriter;
import org.gradle.api.Action;
import org.gradle.api.file.SourceDirectorySet;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.tasks.SourceSet;
// Note: this intentionally only contains the parts that I actually use
@SuppressWarnings("unused") public class MixinGenExtension {
private final Map<String, Action<MixinConfig>> configs = new HashMap<>();
private String filePattern = "mixin.%s.json";
private String defaultRefmap = "mixin.refmap.json";
private String defaultPackagePrefix = "mixin.refmap.json";
private String defaultCompatibilityLevel;
private String defaultMinVersion;
MixinGenExtension() {
}
public void setFilePattern(String pattern) {
this.filePattern = pattern;
}
public String getFilePattern() {
return this.filePattern;
}
public String getDefaultRefmap() {
return defaultRefmap;
}
public void setDefaultRefmap(String defaultRefmap) {
this.defaultRefmap = defaultRefmap;
}
public String getDefaultPackagePrefix() {
return defaultPackagePrefix;
}
public void setDefaultPackagePrefix(String defaultPackagePrefix) {
this.defaultPackagePrefix = defaultPackagePrefix;
}
public String getDefaultCompatibilityLevel() {
return defaultCompatibilityLevel;
}
public void setDefaultCompatibilityLevel(String level) {
this.defaultCompatibilityLevel = level;
}
public String getDefaultMinVersion() {
return defaultMinVersion;
}
public void setDefaultMinVersion(String defaultMinVersion) {
this.defaultMinVersion = defaultMinVersion;
}
public void config(String name, Action<MixinConfig> configure) {
configs.put(name, configure);
}
public static class MixinConfig {
private Boolean required;
private String packageName;
private String refmap;
private String compatibilityLevel;
private String minVersion;
private String configurationPlugin;
private Integer mixinPriority;
private Integer injectorsDefaultRequire;
private Boolean conformVisibility;
private String sourceSet;
public String getSourceSet() {
return sourceSet;
}
public void setSourceSet(String sourceSet) {
this.sourceSet = sourceSet;
}
public Boolean getRequired() {
return required;
}
public void setRequired(boolean required) {
this.required = required;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public String getRefmap() {
return refmap;
}
public void setRefmap(String refmap) {
this.refmap = refmap;
}
public String getCompatibilityLevel() {
return compatibilityLevel;
}
public void setCompatibilityLevel(String compatibilityLevel) {
this.compatibilityLevel = compatibilityLevel;
}
public String getMinVersion() {
return minVersion;
}
public void setMinVersion(String minVersion) {
this.minVersion = minVersion;
}
public String getConfigurationPlugin() {
return configurationPlugin;
}
public void setConfigurationPlugin(String configurationPlugin) {
this.configurationPlugin = configurationPlugin;
}
public Integer getInjectorsDefaultRequire() {
return injectorsDefaultRequire;
}
public void setInjectorsDefaultRequire(int injectorsDefaultRequire) {
this.injectorsDefaultRequire = injectorsDefaultRequire;
}
public Boolean getConformVisibility() {
return conformVisibility;
}
public void setConformVisibility(boolean conformVisibility) {
this.conformVisibility = conformVisibility;
}
public Integer getMixinPriority() {
return mixinPriority;
}
public void setMixinPriority(Integer mixinPriority) {
this.mixinPriority = mixinPriority;
}
}
void generateFiles(JavaPluginConvention convention) throws IOException {
for (String name : configs.keySet()) {
MixinConfig config = new MixinConfig();
Action<MixinConfig> configure = configs.get(name);
if (defaultPackagePrefix != null) {
config.packageName = defaultPackagePrefix + "." + name;
}
if (defaultRefmap != null) {
config.refmap = defaultRefmap;
}
if (defaultCompatibilityLevel != null) {
config.compatibilityLevel = defaultCompatibilityLevel;
}
if (defaultMinVersion != null) {
config.minVersion = defaultMinVersion;
}
configure.execute(config);
SourceSet main = convention.getSourceSets().getByName(config.sourceSet == null ? "main" : config.sourceSet);
Set<File> resourcesSet = main.getResources().getSrcDirs();
Path resources = resourcesSet.iterator().next().getCanonicalFile().toPath();
String fileName = String.format(filePattern, name);
try (JsonWriter writer = new JsonWriter(Files.newBufferedWriter(resources.resolve(fileName)))) {
writer.setIndent(" ");
writer.beginObject();
if (config.required != null) {
writer.name("required").value(config.required);
}
if (config.packageName != null) {
writer.name("package").value(config.packageName);
}
if (config.refmap != null) {
writer.name("refmap").value(config.refmap);
}
if (config.configurationPlugin != null) {
writer.name("plugin").value(config.configurationPlugin);
}
if (config.compatibilityLevel != null) {
writer.name("compatibilityLevel").value(config.compatibilityLevel);
}
if (config.minVersion != null) {
writer.name("minVersion").value(config.minVersion);
}
if (config.mixinPriority != null) {
writer.name("mixinPriority").value(config.mixinPriority);
}
if (config.injectorsDefaultRequire != null) {
writer.name("injectors").beginObject();
writer.name("defaultRequire").value(config.injectorsDefaultRequire);
writer.endObject();
}
if (config.conformVisibility != null) {
writer.name("overwrites").beginObject();
writer.name("conformVisibility").value(config.conformVisibility);
writer.endObject();
}
writeMixins(convention, name, config, writer);
writer.endObject();
}
}
}
private void writeMixins(JavaPluginConvention convention, String name, MixinConfig config, JsonWriter writer) throws IOException {
SourceSet main = convention.getSourceSets().getByName(config.sourceSet == null ? "main": config.sourceSet);
Set<Path> classes = getMixinClasses(config, main.getAllJava());
Set<Path> commonSet = new HashSet<>();
Set<Path> clientSet = new HashSet<>();
Set<Path> serverSet = new HashSet<>();
Path prefixPath = Paths.get(config.packageName.replace('.', '/'));
for (Path mixinClass : classes) {
Path relative = prefixPath.relativize(mixinClass);
if (relative.startsWith("client")) {
clientSet.add(relative);
} else if (relative.startsWith("server")) {
serverSet.add(relative);
} else if (relative.startsWith("common")) {
commonSet.add(relative);
}
}
Function<Path, String> transform = path ->
removeSuffix(removePrefix(path.toString().replace(File.separatorChar, '.'), name + "."), ".java");
List<String> common = commonSet.stream().map(transform).sorted(Comparator.comparing(a -> a.toLowerCase(Locale.ROOT))).collect(Collectors.toList());
List<String> client = clientSet.stream().map(transform).sorted(Comparator.comparing(a -> a.toLowerCase(Locale.ROOT))).collect(Collectors.toList());
List<String> server = serverSet.stream().map(transform).sorted(Comparator.comparing(a -> a.toLowerCase(Locale.ROOT))).collect(Collectors.toList());
writer.name("mixins").beginArray();
for (String path : common) {
writer.value(path);
}
writer.endArray();
writer.name("client").beginArray();
for (String path : client) {
writer.value(path);
}
writer.endArray();
writer.name("server").beginArray();
for (String path : server) {
writer.value(path);
}
writer.endArray();
}
private Set<Path> getMixinClasses(MixinConfig config, SourceDirectorySet allJava) throws IOException {
System.out.println("GetMixin Classes for " + config.packageName + " in " + allJava.getSrcDirs());
Set<Path> srcPaths = new HashSet<>();
for (File file : allJava.getSrcDirs()) {
Path toPath = file.getCanonicalFile().toPath();
System.out.println("GetMixin " + toPath);
srcPaths.add(toPath);
}
Set<Path> classes = new HashSet<>();
for (File it : allJava) {
Path javaClass = it.getCanonicalFile().toPath();
System.out.println("Class: " + javaClass);
for (Path srcPath : srcPaths) {
if (javaClass.startsWith(srcPath)) {
Path relative = srcPath.relativize(javaClass);
if (relative.toString().replace(File.separatorChar, '.').startsWith(config.packageName)
&& !relative.toString().endsWith("package-info.java")) {
classes.add(relative);
}
}
}
}
return classes;
}
}