diff --git a/generator/src/main/java/io/github/lonamiwebs/overgram/Overgram.java b/generator/src/main/java/io/github/lonamiwebs/overgram/Overgram.java index 593664b..dfb470a 100644 --- a/generator/src/main/java/io/github/lonamiwebs/overgram/Overgram.java +++ b/generator/src/main/java/io/github/lonamiwebs/overgram/Overgram.java @@ -12,13 +12,15 @@ import java.util.Map; public class Overgram { public static void main(final String... args) throws IOException { + int layer = 0; final Map> objects = new HashMap<>(); for (int i = 1; i < args.length; ++i) { - TLReader.readTlObjects(objects, new File(args[i])); + layer = Math.max(layer, TLReader.readTlObjects(objects, new File(args[i]))); } final File parent = new File(args[0]); Generator.generateJava( + layer, objects.get("types"), objects.get("functions"), new File(parent, "Abstract.java"), diff --git a/generator/src/main/java/io/github/lonamiwebs/overgram/generator/Generator.java b/generator/src/main/java/io/github/lonamiwebs/overgram/generator/Generator.java index 3ed08f1..c6b1f6d 100644 --- a/generator/src/main/java/io/github/lonamiwebs/overgram/generator/Generator.java +++ b/generator/src/main/java/io/github/lonamiwebs/overgram/generator/Generator.java @@ -12,11 +12,11 @@ public class Generator { private static final String VARIABLE_SUFFIX = "$"; public static void generateJava( - final List types, final List functions, + final int layer, final List types, final List functions, final File abstractsFile, final File typesFile, final File functionsFile) throws IOException { makeParents(abstractsFile, typesFile, functionsFile); try (final Writer writer = new BufferedWriter(new FileWriter(abstractsFile))) { - writeAbstract(writer, types); + writeAbstract(writer, layer, types); } try (final Writer writer = new BufferedWriter(new FileWriter(typesFile))) { writeCode(writer, "Types", "%s", types, false); @@ -26,10 +26,11 @@ public class Generator { } } - private static void writeAbstract(final Writer writer, final List objects) throws IOException { + private static void writeAbstract(final Writer writer, final int layer, final List objects) throws IOException { writer.write("package io.github.lonamiwebs.overgram.tl;\n"); writer.write("@SuppressWarnings(\"unused\")\n"); writer.write("public class Abstract {\n"); + writer.write("public static final int LAYER = " + layer + ";\n"); for (final Map.Entry> nsNames : stringsByNamespace(uniqueTypes(objects)).entrySet()) { if (!nsNames.getKey().isEmpty()) { writer.write("public static class "); diff --git a/generator/src/main/java/io/github/lonamiwebs/overgram/parser/TLReader.java b/generator/src/main/java/io/github/lonamiwebs/overgram/parser/TLReader.java index 483e6f2..1524cbd 100644 --- a/generator/src/main/java/io/github/lonamiwebs/overgram/parser/TLReader.java +++ b/generator/src/main/java/io/github/lonamiwebs/overgram/parser/TLReader.java @@ -12,16 +12,43 @@ import java.util.Map; public class TLReader { - public static void readTlObjects(final Map> result, final File file) { - for (final Map.Entry> entry : readTl(file).entrySet()) { + public static int readTlObjects(final Map> result, final File file) { + final int[] layer = new int[1]; + for (final Map.Entry> entry : readTl(file, layer).entrySet()) { final List objects = result.computeIfAbsent(entry.getKey(), k -> new ArrayList<>()); for (final String string : entry.getValue()) { objects.add(TLObject.fromString(string)); } } + return layer[0]; } - private static Map> readTl(final File file) { + private static void scanCommentForLayer(final CharacterIterator iter, final int[] layer) { + final String target = "/ LAYER "; + int i = 0; + char c = '\0'; + while (iter.hasNext() && (c = iter.next()) != '\n') { + if (c != target.charAt(i)) { + break; + } + + ++i; + if (i == target.length()) { + final StringBuilder builder = new StringBuilder(); + while (iter.hasNext() && Character.isDigit(c = iter.next())) { + builder.append(c); + } + layer[0] = Integer.parseInt(builder.toString()); + break; + } + } + + while (iter.hasNext() && c != '\n') { + c = iter.next(); + } + } + + private static Map> readTl(final File file, final int[] layer) { final Map> result = new HashMap<>(); String section = "types"; @@ -32,9 +59,7 @@ public class TLReader { switch (c) { // Assume '/' only occurs in "// comments...\n" case '/': - do { - c = iter.next(); - } while (iter.hasNext() && c != '\n'); + scanCommentForLayer(iter, layer); break; // Assume '-' only occurs in "---sections---\n"