Skip to content
This repository was archived by the owner on Nov 17, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jdk:
install: true
script:
- mvn clean package
- java -jar ./target/protogen-1.0-SNAPSHOT-shaded.jar all
- java -jar ./target/protogen-1.0-SNAPSHOT-shaded.jar --type all

cache:
directories:
Expand Down
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>net.sf.jopt-simple</groupId>
<artifactId>jopt-simple</artifactId>
<version>5.0.4</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>7.1</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-tree</artifactId>
<version>7.1</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-commons</artifactId>
<version>7.1</version>
</dependency>
</dependencies>

<build>
Expand Down
95 changes: 60 additions & 35 deletions src/main/java/org/feathercore/protogen/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@

package org.feathercore.protogen;

import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import joptsimple.util.EnumConverter;
import org.feathercore.protogen.burger.BurgerReader;
import org.feathercore.protogen.generate.MaterialGenerator;
import org.feathercore.protogen.generate.PacketGenerator;
import org.feathercore.protogen.generate.ParticleGenerator;
import org.feathercore.protogen.generate.SoundGenerator;
import org.feathercore.protogen.util.FileUtil;
import org.feathercore.protogen.version.MinecraftVersion;
import org.feathercore.protogen.wiki.WikiPacketInfo;
import org.feathercore.protogen.wiki.WikiReader;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -34,71 +40,90 @@
* @author xtrafrancyz
*/
public class Bootstrap {
private static final File VALID_DIR = new File("gen");
private static final File BROKEN_DIR = new File("gen-broken");
private static final Path VALID_DIR;
private static final Path BROKEN_DIR;

private static final CachedDataSource<BurgerReader> BURGER = new CachedDataSource<>(BurgerReader::new);
private static final CachedDataSource<WikiReader> WIKI = new CachedDataSource<>(WikiReader::new);
private static final MinecraftVersion DEFAULT_VERSION = MinecraftVersion._1_13_2;
private static final CachedDataSource<BurgerReader> BURGER = new CachedDataSource<>();
private static final CachedDataSource<WikiReader> WIKI = new CachedDataSource<>();

private static final Map<String, GenRunnable> GENERATORS = new HashMap<String, GenRunnable>() {{
put("materials", Bootstrap::genMaterials);
put("sounds", Bootstrap::genSounds);
put("particles", Bootstrap::genParticles);
put("packets", Bootstrap::genPackets);
private static final Map<GeneratorType, GenRunnable> GENERATORS = new HashMap<GeneratorType, GenRunnable>() {{
put(GeneratorType.MATERIALS, Bootstrap::genMaterials);
put(GeneratorType.SOUNDS, Bootstrap::genSounds);
put(GeneratorType.PARTICLES, Bootstrap::genParticles);
put(GeneratorType.PACKETS, Bootstrap::genPackets);
put(GeneratorType.ALL, () -> {
Bootstrap.genMaterials();
Bootstrap.genSounds();
Bootstrap.genParticles();
Bootstrap.genPackets();
});
}};

public static void main(String[] args) throws Exception {
if (args.length == 0) {
System.out.println("Choose what to generate:");
System.out.println(" " + String.join(", ", GENERATORS.keySet()));
System.out.println(" or");
System.out.println(" all");
return;
}
OptionParser parser = new OptionParser();
OptionSpec<GeneratorType> specType = parser.accepts("type")
.withRequiredArg()
.ofType(GeneratorType.class)
.withValuesConvertedBy(new EnumConverter<GeneratorType>(GeneratorType.class) {})
.required();
OptionSpec<MinecraftVersion> specVersion = parser.accepts("version")
.withRequiredArg()
.ofType(MinecraftVersion.class)
.withValuesConvertedBy(new EnumConverter<MinecraftVersion>(MinecraftVersion.class) {
@Override
public MinecraftVersion convert(String value) {
return super.convert("_".concat(value.replace('.', '_')));
}
})
.defaultsTo(DEFAULT_VERSION);
OptionSet options = parser.parse(args);
GeneratorType type = options.valueOf(specType);
MinecraftVersion version = options.valueOf(specVersion);

System.out.println("Generator: " + type + ", version: " + version);

FileUtil.deleteRecursive(VALID_DIR);
FileUtil.deleteRecursive(BROKEN_DIR);

if (args.length == 1 && args[0].equalsIgnoreCase("all")) {
for (GenRunnable gen : GENERATORS.values()) {
gen.run();
}
} else {
for (final String arg : args) {
GenRunnable gen = GENERATORS.get(arg.toLowerCase());
if (gen != null) {
gen.run();
}
}
}
BURGER.setHandle(() -> new BurgerReader(version));
// TODO workaround please
WIKI.setHandle(() -> new WikiReader(version.getLink()));
GENERATORS.get(type).run();
}

private static void genMaterials() throws Exception {
String generated = new MaterialGenerator(BURGER.get()).generate();
FileUtil.writeFile(new File(VALID_DIR, MaterialGenerator.CLASS_NAME + ".java"), generated);
FileUtil.writeFile(VALID_DIR.resolve(MaterialGenerator.CLASS_NAME + ".java"), generated);
}

private static void genParticles() throws Exception {
//String generated = new ParticleGenerator(new WikiReader(new File("proto.html")).getParticles()).generate();
String generated = new ParticleGenerator(WIKI.get().getParticles()).generate();
FileUtil.writeFile(new File(VALID_DIR, ParticleGenerator.CLASS_NAME + ".java"), generated);
FileUtil.writeFile(VALID_DIR.resolve(ParticleGenerator.CLASS_NAME + ".java"), generated);
}

private static void genSounds() throws Exception {
String generated = new SoundGenerator(BURGER.get().getSounds()).generate();
FileUtil.writeFile(new File(VALID_DIR, SoundGenerator.CLASS_NAME + ".java"), generated);
FileUtil.writeFile(VALID_DIR.resolve(SoundGenerator.CLASS_NAME + ".java"), generated);
}

private static void genPackets() throws Exception {
List<WikiPacketInfo> packets = WIKI.get().getPackets();
for (WikiPacketInfo info : packets) {
String generated = new PacketGenerator(info).generate();
File protoDir = new File(info.isBroken() ? BROKEN_DIR : VALID_DIR, info.getProtocol().name().toLowerCase());
File finalDir = new File(protoDir, info.getSender().name().toLowerCase());
FileUtil.writeFile(new File(finalDir, info.getStandardClassName() + ".java"), generated);
Path protoDir = (info.isBroken() ? BROKEN_DIR : VALID_DIR).resolve(info.getProtocol().name().toLowerCase());
Path finalDir = protoDir.resolve(info.getSender().name().toLowerCase());
FileUtil.writeFile(finalDir.resolve(info.getStandardClassName() + ".java"), generated);
}
}

static {
Path workingDir = Paths.get(System.getProperty("user.dir"));
VALID_DIR = workingDir.resolve("gen");
BROKEN_DIR = workingDir.resolve("gen-broken");
}

private interface GenRunnable {
void run() throws Exception;
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/feathercore/protogen/CachedDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ public class CachedDataSource<T> {
private Creator<T> creator;
private T cache;

public CachedDataSource(Creator<T> creator) {
this.creator = creator;
}

public T get() throws Exception {
if (cache == null) {
cache = creator.create();
}
return cache;
}

public void setHandle(Creator<T> creator) {
this.creator = creator;
}

public interface Creator<T> {
T create() throws Exception;
}
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/org/feathercore/protogen/GeneratorType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2019 Feather Core
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.feathercore.protogen;

public enum GeneratorType {

MATERIALS,
SOUNDS,
PARTICLES,
PACKETS,
ALL
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
* @author xtrafrancyz
*/
public class BurgerItem {
//private static boolean warned;

private String textId;
private String enumName;
private int id;
private int maxStackSize;

private boolean solid;

public BurgerItem(JsonObject json) {
this.textId = json.get("text_id").getAsString();
this.id = json.get("numeric_id").getAsInt();
Expand All @@ -49,4 +52,8 @@ public int getId() {
public int getMaxStackSize() {
return maxStackSize;
}

public boolean isSolid() {
return solid;
}
}
19 changes: 8 additions & 11 deletions src/main/java/org/feathercore/protogen/burger/BurgerReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.feathercore.protogen.util.NetworkUtil;
import org.feathercore.protogen.version.MinecraftVersion;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.*;

/**
Expand All @@ -34,21 +32,18 @@
* @author xtrafrancyz
*/
public class BurgerReader {
public static final String STANDARD_URL = "https://pokechu22.github.io/Burger/1.13.2.json";

public static final String URL = "https://pokechu22.github.io/Burger/%s.json";

private List<BurgerSound> sounds;
private List<BurgerItem> items;
private Map<String, BurgerBlock> blocks;

public BurgerReader() throws IOException {
parse(NetworkUtil.get(STANDARD_URL));
}

public BurgerReader(File file) throws IOException {
parse(new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8));
public BurgerReader(MinecraftVersion version) throws IOException {
parse(NetworkUtil.get(String.format(URL, version.getName())), version);
}

private void parse(String str) {
private void parse(String str, MinecraftVersion version) {
JsonObject root = new JsonParser()
.parse(str)
.getAsJsonArray()
Expand All @@ -62,6 +57,8 @@ private void parse(String str) {
JsonObject sound = entry.getValue().getAsJsonObject();
this.sounds.add(new BurgerSound(entry.getKey(), sound.get("id").getAsInt()));
}
JsonObject classes = root.getAsJsonObject("classes");
version.setBlockClass(classes.getAsJsonPrimitive("block.register").getAsString());

// Items
this.items = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ protected void generateClass() {
sb.append(T1).append("@Getter(AccessLevel.NONE) int id;").append(LF);
sb.append(T1).append("int maxStackSize;").append(LF);
sb.append(T1).append("boolean block;").append(LF);
sb.append(T2).append("boolean solid;").append(LF);

sb.append(LF);

Expand Down Expand Up @@ -90,7 +91,8 @@ private void appendMaterials() {
sb.append(T1).append(item.getEnumName())
.append("(").append(item.getId()).append(", ")
.append(item.getMaxStackSize()).append(", ")
.append(blocks.containsKey(item.getTextId()))
.append(blocks.containsKey(item.getTextId())).append(", ")
.append(item.isSolid())
.append(")");

if (i == this.items.size() - 1) {
Expand Down
50 changes: 35 additions & 15 deletions src/main/java/org/feathercore/protogen/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,51 @@

package org.feathercore.protogen.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

/**
* @author xtrafrancyz
*/
@SuppressWarnings({"ConstantConditions", "ResultOfMethodCallIgnored"})
public class FileUtil {
public static void writeFile(File file, String content) {
file.getParentFile().mkdirs();
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(content.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
public final class FileUtil {

private FileUtil() { }

public static void writeFile(Path file, String content) {
try {
Path parent = file.getParent();
if(parent != null && !Files.isDirectory(parent)) {
Files.createDirectories(parent);
}
Files.write(file, content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
} catch (IOException ex) {
ex.printStackTrace();
}
}

public static void deleteRecursive(File file) {
if (file.isDirectory()) {
for (File f : file.listFiles()) {
deleteRecursive(f);
public static void deleteRecursive(Path dir) {
if (Files.isDirectory(dir)) {
try {
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
FileVisitResult result = super.postVisitDirectory(dir, exc);
Files.delete(dir);
return result;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return super.visitFile(file, attrs);
}
});
} catch (IOException ex) {
ex.printStackTrace();
}
}
file.delete();
}
}
Loading