Annotation processor for sound asset generation

This commit is contained in:
Anuken 2019-07-30 12:05:59 -04:00
parent bc77fd619d
commit a00a208ff8
4 changed files with 73 additions and 2 deletions

View File

@ -0,0 +1,69 @@
package io.anuke.annotations;
import com.squareup.javapoet.*;
import javax.annotation.processing.*;
import javax.lang.model.*;
import javax.lang.model.element.*;
import javax.tools.Diagnostic.*;
import java.nio.file.*;
import java.util.*;
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class AssetsAnnotationProcessor extends AbstractProcessor{
/** Name of the base package to put all the generated classes. */
private static final String packageName = "io.anuke.mindustry.gen";
private int round;
@Override
public synchronized void init(ProcessingEnvironment processingEnv){
super.init(processingEnv);
//put all relevant utils into utils class
Utils.typeUtils = processingEnv.getTypeUtils();
Utils.elementUtils = processingEnv.getElementUtils();
Utils.filer = processingEnv.getFiler();
Utils.messager = processingEnv.getMessager();
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv){
if(round++ != 0) return false; //only process 1 round
try{
TypeSpec.Builder type = TypeSpec.classBuilder("Sounds").addModifiers(Modifier.PUBLIC);
HashSet<String> names = new HashSet<>();
Files.list(Paths.get("core/assets/sounds/")).forEach(p -> {
String name = p.getFileName().toString();
name = name.substring(0, name.indexOf("."));
if(names.contains(name)){
Utils.messager.printMessage(Kind.ERROR, "Duplicate sound file name: " + p.toString() + "!");
}else{
names.add(name);
}
});
Files.list(Paths.get("core/assets/sounds/")).forEach(p -> {
String fname = p.getFileName().toString();
String name = p.getFileName().toString();
name = name.substring(0, name.indexOf("."));
if(SourceVersion.isKeyword(name)){
name = name + "s";
}
type.addField(FieldSpec.builder(ClassName.bestGuess("io.anuke.arc.audio.Sound"), name, Modifier.STATIC, Modifier.PUBLIC, Modifier.FINAL)
.initializer(CodeBlock.builder().add("io.anuke.arc.Core.audio.newSound(io.anuke.arc.Core.files.internal($S))", "sounds/" + fname).build()).build());
});
JavaFile.builder(packageName, type.build()).build().writeTo(Utils.filer);
return true;
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Override
public Set<String> getSupportedAnnotationTypes() {
return Collections.singleton("*");
}
}

View File

@ -1,4 +1,5 @@
io.anuke.annotations.RemoteMethodAnnotationProcessor
io.anuke.annotations.SerializeAnnotationProcessor
io.anuke.annotations.StructAnnotationProcessor
io.anuke.annotations.CallSuperAnnotationProcessor
io.anuke.annotations.CallSuperAnnotationProcessor
io.anuke.annotations.AssetsAnnotationProcessor

Binary file not shown.

View File

@ -14,6 +14,7 @@ import io.anuke.mindustry.entities.bullet.*;
import io.anuke.mindustry.entities.traits.*;
import io.anuke.mindustry.entities.type.*;
import io.anuke.mindustry.gen.*;
import io.anuke.mindustry.gen.Sounds;
import io.anuke.mindustry.net.Net;
public class Weapon{
@ -53,7 +54,7 @@ public class Weapon{
/** whether shooter rotation is ignored when shooting. */
public boolean ignoreRotation = false;
public Sound shootSound = Core.audio.newSound(Core.files.internal("sounds/shoot.ogg"));
public Sound shootSound = Sounds.die;
public TextureRegion region;