|
1 | 1 | package de.blazemcworld.jsscripts; |
2 | 2 |
|
3 | | -import net.minecraft.client.MinecraftClient; |
4 | 3 | import net.minecraft.text.Text; |
5 | 4 | import net.minecraft.util.Formatting; |
6 | 5 | import net.minecraft.util.Pair; |
|
11 | 10 | import org.objectweb.asm.tree.ClassNode; |
12 | 11 | import org.objectweb.asm.tree.FieldNode; |
13 | 12 | import org.objectweb.asm.tree.MethodNode; |
14 | | -import org.reflections.Reflections; |
15 | | -import org.reflections.scanners.Scanners; |
16 | | -import org.reflections.util.ConfigurationBuilder; |
17 | 13 |
|
18 | 14 | import java.io.File; |
19 | 15 | import java.io.InputStream; |
20 | | -import java.nio.file.Files; |
21 | | -import java.nio.file.Path; |
| 16 | +import java.net.URI; |
| 17 | +import java.net.URL; |
| 18 | +import java.nio.file.*; |
22 | 19 | import java.util.*; |
23 | 20 | import java.util.stream.Collectors; |
| 21 | +import java.util.stream.Stream; |
24 | 22 |
|
25 | 23 | public class TypingGen { |
26 | 24 |
|
27 | | - public static void genAllTypes() { |
| 25 | + public static void genTypesIn(String targets) { |
28 | 26 | try { |
29 | 27 | Path out = JsScripts.MC.runDirectory.toPath() |
30 | 28 | .resolve("JsScripts").resolve("types"); |
31 | 29 |
|
32 | | - if (out.toFile().exists()) { |
33 | | - JsScripts.displayChat(Text.literal("Deleting previous typings...").formatted(Formatting.AQUA)); |
34 | | - deleteRecursively(out.toFile()); |
35 | | - JsScripts.displayChat(Text.literal("Done!").formatted(Formatting.AQUA)); |
36 | | - } |
37 | | - |
38 | 30 | JsScripts.displayChat(Text.literal("Scanning available classes...").formatted(Formatting.AQUA)); |
39 | | - Reflections r = new Reflections(new ConfigurationBuilder() |
40 | | - .forPackages("com", "net", "java", "org", "io", "de") |
41 | | - .addClassLoaders(MinecraftClient.class.getClassLoader(), TypingGen.class.getClassLoader()) |
42 | | - .addScanners(Scanners.MethodsReturn, Scanners.SubTypes, Scanners.MethodsParameter)); |
| 31 | + |
| 32 | + ClassLoader cl = JsScripts.class.getClassLoader(); |
43 | 33 |
|
44 | 34 | Set<String> all = new HashSet<>(); |
45 | 35 |
|
46 | | - all.addAll(r.getStore().get("SubTypes").keySet()); |
47 | | - all.addAll(r.getStore().get("MethodsReturn").keySet()); |
48 | | - all.addAll(r.getStore().get("MethodsParameter").keySet()); |
| 36 | + for (String target : targets.split(" ")) { |
| 37 | + if (!target.endsWith("*")) { |
| 38 | + all.add(Mappings.remapClass("named", Mappings.current(), target)); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + Set<String> checkedPackages = new HashSet<>(); |
| 43 | + |
| 44 | + List<FileSystem> fsList = new ArrayList<>(); |
| 45 | + |
| 46 | + for (Class<?> c : Injector.listLoadedClasses()) { |
| 47 | + String p = c.getPackageName(); |
| 48 | + while (true) { |
| 49 | + if (checkedPackages.contains(p)) break; |
| 50 | + checkedPackages.add(p); |
| 51 | + |
| 52 | + Enumeration<URL> enu = cl.getResources(p.replace('.', '/')); |
| 53 | + |
| 54 | + while (enu.hasMoreElements()) { |
| 55 | + URI u = enu.nextElement().toURI(); |
| 56 | + |
| 57 | + try { |
| 58 | + FileSystems.getFileSystem(u); |
| 59 | + } catch (FileSystemNotFoundException err) { |
| 60 | + HashMap<String, Boolean> options = new HashMap<>(); |
| 61 | + options.put("create", true); |
| 62 | + fsList.add(FileSystems.newFileSystem(u, options)); |
| 63 | + } |
| 64 | + |
| 65 | + try (Stream<Path> javaClasses = Files.walk(Path.of(u))) { |
| 66 | + for (Path cp : javaClasses.collect(Collectors.toSet())) { |
| 67 | + String str = cp.toString(); |
| 68 | + if (str.endsWith(".class")) { |
| 69 | + all.add(str.substring(1).replaceAll("/", ".").substring(0, str.length() - 7)); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if (!p.contains(".")) { |
| 76 | + break; |
| 77 | + } |
| 78 | + p = p.substring(0, p.indexOf(".")); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + for (FileSystem fs : fsList) { |
| 83 | + fs.close(); |
| 84 | + } |
| 85 | + |
| 86 | + FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/")); |
| 87 | + |
| 88 | + Stream<Path> javaClasses = Files.walk(fs.getPath("/")); |
| 89 | + for (Path path : javaClasses.collect(Collectors.toSet())) { |
| 90 | + List<String> parts = new ArrayList<>(Arrays.asList(path.toString().split("/"))); |
| 91 | + |
| 92 | + if (parts.size() > 3) { |
| 93 | + parts = parts.subList(3, parts.size()); |
| 94 | + if (parts.get(parts.size() - 1).endsWith(".class")) { |
| 95 | + String str = String.join(".", parts); |
| 96 | + all.add(str.substring(0, str.length() - 6)); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + javaClasses.close(); |
49 | 101 |
|
50 | 102 | JsScripts.displayChat(Text.literal("Found " + all.size() + " classes!").formatted(Formatting.AQUA)); |
51 | 103 |
|
| 104 | + all = all.stream().filter(name -> { |
| 105 | + if (name.startsWith("jdk")) return false; |
| 106 | + if (!name.contains(".")) return false; |
| 107 | + |
| 108 | + for (String target : targets.split(" ")) { |
| 109 | + if (target.endsWith("*") && name.startsWith(target.substring(0, target.length() - 1))) { |
| 110 | + return true; |
| 111 | + } |
| 112 | + if (target.equals(name)) { |
| 113 | + return true; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return false; |
| 118 | + }).collect(Collectors.toSet()); |
| 119 | + |
| 120 | + JsScripts.displayChat(Text.literal("Of which " + all.size() + " classes match the filter.").formatted(Formatting.AQUA)); |
| 121 | + |
52 | 122 | JsScripts.displayChat(Text.literal("Starting generation...").formatted(Formatting.AQUA)); |
53 | 123 |
|
54 | 124 | long nextProgress = System.currentTimeMillis() + 3000; |
55 | 125 | long start = System.currentTimeMillis(); |
56 | 126 | int progress = 0; |
57 | | - |
58 | | - ClassLoader cl = JsScripts.class.getClassLoader(); |
| 127 | + int errors = 0; |
59 | 128 |
|
60 | 129 | for (String currentName : all) { |
61 | 130 | if (nextProgress < System.currentTimeMillis()) { |
62 | 131 | nextProgress = System.currentTimeMillis() + 3000; |
63 | 132 | String est = "%.1fs".formatted((float) (all.size() - progress) / ((float) progress / (System.currentTimeMillis() - start)) / 1000f); |
64 | 133 | JsScripts.displayChat(Text.literal("Generating... " + progress + " of " + all.size() + " completed, est: " + est).formatted(Formatting.AQUA)); |
65 | 134 | } |
66 | | - if (currentName.startsWith("jdk.") || !currentName.contains(".") || currentName.endsWith("[]")) { |
67 | | - progress++; |
68 | | - continue; |
69 | | - } |
70 | 135 | try { |
71 | 136 | genTypesFor(currentName, out, cl); |
72 | 137 | } catch (Exception e) { |
73 | 138 | e.printStackTrace(); |
| 139 | + errors++; |
74 | 140 | } |
75 | 141 | progress++; |
76 | 142 | } |
77 | 143 |
|
78 | 144 | JsScripts.displayChat(Text.literal("Done!").formatted(Formatting.AQUA)); |
| 145 | + |
| 146 | + if (errors > 0) { |
| 147 | + JsScripts.displayChat(Text.literal("Got an error for " + errors + " classes.").formatted(Formatting.AQUA)); |
| 148 | + } |
79 | 149 | } catch (Exception err) { |
80 | 150 | JsScripts.displayChat(Text.literal("Error generating type declarations").formatted(Formatting.RED)); |
81 | 151 | err.printStackTrace(); |
82 | 152 | } |
83 | 153 | } |
84 | 154 |
|
85 | | - public static void genTypesFor(String className) throws Exception { |
86 | | - Path out = JsScripts.MC.runDirectory.toPath() |
87 | | - .resolve("JsScripts").resolve("types"); |
88 | | - ClassLoader cl = JsScripts.class.getClassLoader(); |
89 | | - className = Mappings.remapClass("named",Mappings.current(), className); |
90 | | - genTypesFor(className, out, cl); |
91 | | - } |
92 | | - |
93 | 155 | private static void genTypesFor(String currentName, Path outDir, ClassLoader cl) throws Exception { |
94 | 156 | InputStream stream = cl.getResourceAsStream(currentName.replace('.', '/') + ".class"); |
95 | 157 | if (stream == null) { |
@@ -243,15 +305,6 @@ private static Pair<List<String>, String> parseMethodDescriptor(String descripto |
243 | 305 | return new Pair<>(params, out); |
244 | 306 | } |
245 | 307 |
|
246 | | - private static void deleteRecursively(File f) { |
247 | | - if (f.isDirectory()) { |
248 | | - for (File c : f.listFiles()) { |
249 | | - deleteRecursively(c); |
250 | | - } |
251 | | - } |
252 | | - f.delete(); |
253 | | - } |
254 | | - |
255 | 308 | private static String typeImport(String clazz, HashMap<String, String> imports) { |
256 | 309 | if (clazz.startsWith("L") && clazz.endsWith(";")) { |
257 | 310 | clazz = clazz.substring(1, clazz.length() - 1); |
|
0 commit comments