|
| 1 | +package com.prunoideae.probejs.compiler; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.prunoideae.probejs.ProbePaths; |
| 5 | +import com.prunoideae.probejs.formatter.formatter.FormatterClass; |
| 6 | +import com.prunoideae.probejs.formatter.formatter.FormatterNamespace; |
| 7 | +import com.prunoideae.probejs.formatter.formatter.IFormatter; |
| 8 | +import com.prunoideae.probejs.info.type.TypeInfoClass; |
| 9 | +import dev.latvian.mods.kubejs.RegistryObjectBuilderTypes; |
| 10 | + |
| 11 | +import java.io.BufferedWriter; |
| 12 | +import java.io.IOException; |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.util.*; |
| 15 | +import java.util.stream.Collectors; |
| 16 | + |
| 17 | +public class RegistryCompiler { |
| 18 | + public static Set<Class<?>> getRegistryClasses() { |
| 19 | + Set<Class<?>> result = new HashSet<>(); |
| 20 | + result.add(RegistryObjectBuilderTypes.class); |
| 21 | + result.add(RegistryObjectBuilderTypes.RegistryEventJS.class); |
| 22 | + RegistryObjectBuilderTypes.MAP.values().forEach(v -> v.types.values().forEach(v1 -> result.add(v1.builderClass()))); |
| 23 | + return result; |
| 24 | + } |
| 25 | + |
| 26 | + public static void compileEventRegistries(BufferedWriter writer) throws IOException { |
| 27 | + Gson stringG = new Gson(); |
| 28 | + for (var types : RegistryObjectBuilderTypes.MAP.values()) { |
| 29 | + String fullName = types.registryKey.location().getNamespace() + "." + types.registryKey.location().getPath().replace('/', '.') + ".registry"; |
| 30 | + String registryName = FormatterRegistry.getFormattedRegistryName(types); |
| 31 | + writer.write("declare function onEvent(name: %s, handler: (event: Registry.%s) => void);\n".formatted(stringG.toJson(fullName), registryName)); |
| 32 | + if (types.registryKey.location().getNamespace().equals("minecraft")) { |
| 33 | + String shortName = types.registryKey.location().getPath().replace('/', '.') + ".registry"; |
| 34 | + writer.write("declare function onEvent(name: %s, handler: (event: Registry.%s) => void);\n".formatted(stringG.toJson(shortName), registryName)); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public static void compileRegistries() throws IOException { |
| 40 | + BufferedWriter writer = Files.newBufferedWriter(ProbePaths.GENERATED.resolve("registries.d.ts")); |
| 41 | + writer.write("/// <reference path=\"./globals.d.ts\" />\n"); |
| 42 | + IFormatter namespace = new FormatterNamespace("Registry", RegistryObjectBuilderTypes.MAP.values().stream().map(FormatterRegistry::new).collect(Collectors.toList())); |
| 43 | + writer.write(String.join("\n", namespace.format(0, 4))); |
| 44 | + writer.flush(); |
| 45 | + } |
| 46 | + |
| 47 | + public static void getBuilderTypes() { |
| 48 | + RegistryObjectBuilderTypes.MAP.forEach((k, v) -> { |
| 49 | + System.out.println(k.registry()); |
| 50 | + v.types.forEach((k1, v1) -> { |
| 51 | + System.out.println(" " + k1); |
| 52 | + System.out.println(" " + v1.builderClass()); |
| 53 | + }); |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + private static class FormatterRegistry implements IFormatter { |
| 58 | + RegistryObjectBuilderTypes<?> types; |
| 59 | + String name; |
| 60 | + |
| 61 | + private static String getFormattedRegistryName(RegistryObjectBuilderTypes<?> types) { |
| 62 | + return Arrays.stream(types.registryKey.location().getPath().split("/")).map(str -> str.substring(0, 1).toUpperCase() + str.substring(1)).collect(Collectors.joining("")); |
| 63 | + } |
| 64 | + |
| 65 | + private FormatterRegistry(RegistryObjectBuilderTypes<?> types) { |
| 66 | + this.types = types; |
| 67 | + this.name = getFormattedRegistryName(types); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public List<String> format(Integer indent, Integer stepIndent) { |
| 72 | + List<String> formatted = new ArrayList<>(); |
| 73 | + int stepped = indent + stepIndent; |
| 74 | + Gson stringG = new Gson(); |
| 75 | + formatted.add(" ".repeat(indent) + "class %s extends %s {".formatted(name, FormatterClass.formatTypeParameterized(new TypeInfoClass(RegistryObjectBuilderTypes.RegistryEventJS.class)))); |
| 76 | + for (RegistryObjectBuilderTypes.BuilderType<?> builder : types.types.values()) { |
| 77 | + formatted.add(" ".repeat(stepped) + "create(id: string, type: %s): %s;".formatted(stringG.toJson(builder.type()), FormatterClass.formatTypeParameterized(new TypeInfoClass(builder.builderClass())))); |
| 78 | + } |
| 79 | + formatted.add(" ".repeat(indent) + "}"); |
| 80 | + return formatted; |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments