Skip to content

Commit

Permalink
New Configuration for ModAssetsProcess
Browse files Browse the repository at this point in the history
  • Loading branch information
Zelaux committed Sep 18, 2024
1 parent 437f4df commit 22ab978
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
4 changes: 4 additions & 0 deletions annotations/src/main/java/mmc/annotations/ModAnnotations.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ public class ModAnnotations extends Annotations{
*/
@Retention(RetentionPolicy.SOURCE)
public @interface ModAssetsAnnotation{
/**
* Separator detween 'modId-spritName'
* */
String separator() default "-";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import com.squareup.javapoet.*;
import mindustry.*;
import mindustry.annotations.*;
import mindustry.annotations.Annotations.StyleDefaults;
import mindustry.mod.Mods.*;
import mmc.annotations.ModAnnotations.ModAssetsAnnotation;
import mmc.annotations.SupportedAnnotationTypes;
import mmc.annotations.*;

Expand All @@ -16,17 +18,17 @@
import javax.lang.model.element.*;
import java.util.*;

@SupportedAnnotationTypes(mmc.annotations.ModAnnotations.ModAssetsAnnotation.class)
public class ModAssetsProcess extends ModBaseProcessor{
static String capitalize(String s){
@SupportedAnnotationTypes(ModAssetsAnnotation.class)
public class ModAssetsProcess extends ModBaseProcessor {
static String capitalize(String s) {
StringBuilder result = new StringBuilder(s.length());

for(int i = 0; i < s.length(); i++){
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(c != '_' && c != '-'){
if(i > 0 && (s.charAt(i - 1) == '_' || s.charAt(i - 1) == '-')){
if (c != '_' && c != '-') {
if (i > 0 && (s.charAt(i - 1) == '_' || s.charAt(i - 1) == '-')) {
result.append(Character.toUpperCase(c));
}else{
} else {
result.append(c);
}
}
Expand All @@ -36,31 +38,33 @@ static String capitalize(String s){
}

@Override
public void process(RoundEnvironment env) throws Exception{
public void process(RoundEnvironment env) throws Exception {
boolean root = rootPackageName.equals("mmc");
if(root) return;
if (root) return;
String path = getAssetsPath();
processSounds(classPrefix() + "Sounds", path + "/sounds", "arc.audio.Sound");
processSounds(classPrefix() + "Musics", path + "/music", "arc.audio.Music");
processUI(env.getElementsAnnotatedWith(Annotations.StyleDefaults.class));
processUI(env.getElementsAnnotatedWith(StyleDefaults.class), types(ModAssetsAnnotation.class)
.get(0).annotation(ModAssetsAnnotation.class).separator()
);
}

private String getAssetsPath(){
private String getAssetsPath() {
return rootDirectory + "/" + annotationsSettings(AnnotationSettingsEnum.assetsPath, "core/assets");
}

void processUI(Set<? extends Element> elements) throws Exception{
void processUI(Set<? extends Element> elements, String separator) throws Exception {
TypeSpec.Builder type = TypeSpec.classBuilder(classPrefix() + "Tex").addModifiers(Modifier.PUBLIC);
// TypeSpec.Builder ictype = TypeSpec.classBuilder("Icon").addModifiers(Modifier.PUBLIC);
// TypeSpec.Builder ichtype = TypeSpec.classBuilder("Iconc").addModifiers(Modifier.PUBLIC);
MethodSpec.Builder loadStyles = MethodSpec.methodBuilder("loadStyles").addModifiers(Modifier.PUBLIC, Modifier.STATIC);
MethodSpec.Builder load = MethodSpec.methodBuilder("load").addModifiers(Modifier.PUBLIC, Modifier.STATIC);
load.addStatement("$T __loadedMod__=$T.mods.getMod($S)", LoadedMod.class, Vars.class,modInfo().name);
load.addStatement("$T __loadedMod__=$T.mods.getMod($S)", LoadedMod.class, Vars.class, modInfo().name);
String assetsRawPath = annotationsSettings(AnnotationSettingsEnum.assetsRawPath, rootDirectory + "/core/assets-raw");
if(assetsRawPath.equals("null")) assetsRawPath = getAssetsPath();
if (assetsRawPath.equals("null")) assetsRawPath = getAssetsPath();
String[] resourcesArray = {assetsRawPath + "/sprites/ui"/*, assetsRawPath + "/sprites/cui"*/};
Cons<Fi> walker = p -> {
if(!p.extEquals("png")) return;
if (!p.extEquals("png")) return;

String filename = p.name();
filename = filename.substring(0, filename.indexOf("."));
Expand All @@ -70,27 +74,27 @@ void processUI(Set<? extends Element> elements) throws Exception{

String varname = capitalize(sfilen);

if(SourceVersion.isKeyword(varname)) varname += "s";
if (SourceVersion.isKeyword(varname)) varname += "s";

type.addField(ClassName.bestGuess(dtype), varname, Modifier.STATIC, Modifier.PUBLIC);
load.addStatement(varname + " = (" + dtype + ")arc.Core.atlas.drawable(__loadedMod__.name+\"_\"+$S)", sfilen);
load.addStatement(varname + " = (" + dtype + ")arc.Core.atlas.drawable(__loadedMod__.name+$S+$S)", separator, sfilen);
};


for(Element elem : elements){
for (Element elem : elements) {
Seq.with(elem.getEnclosedElements()).each(e -> e.getKind() == ElementKind.FIELD, field -> {
String fname = field.getSimpleName().toString();
if(fname.startsWith("default")){
if (fname.startsWith("default")) {
loadStyles.addStatement("arc.Core.scene.addStyle(" + field.asType().toString() + ".class, " + field.getEnclosingElement().toString() + "." + fname + ")");
}
});
}
for(String resources : resourcesArray){
for (String resources : resourcesArray) {
Fi folder = rootDirectory.child(resources);
if(folder.exists()){
if (folder.exists()) {
folder.walk(walker);
// warn("Found ui folder "+ folder.file().getAbsolutePath());
}else{
} else {
// warn("Cannot find ui folder "+ folder.file().getAbsolutePath());
}
}
Expand All @@ -100,25 +104,25 @@ void processUI(Set<? extends Element> elements) throws Exception{
JavaFile.builder(packageName, type.build()).build().writeTo(BaseProcessor.filer);
}

void processSounds(String classname, String path, String rtype) throws Exception{
void processSounds(String classname, String path, String rtype) throws Exception {
System.out.println("classname: " + classname + ", path: " + path);
TypeSpec.Builder type = TypeSpec.classBuilder(classname).addModifiers(Modifier.PUBLIC);
MethodSpec.Builder loadBegin = MethodSpec.methodBuilder("load").addModifiers(Modifier.PUBLIC, Modifier.STATIC).addException(ClassName.get(Exception.class));
MethodSpec.Builder loadNowBegin = MethodSpec.methodBuilder("loadNow").addModifiers(Modifier.PUBLIC, Modifier.STATIC).addException(ClassName.get(Exception.class));

HashSet<String> names = new HashSet<>();

loadNowBegin.addStatement("$T __loadedMod__=$T.mods.getMod($S)", LoadedMod.class, Vars.class,modInfo().name);
loadNowBegin.addStatement("$T __loadedMod__=$T.mods.getMod($S)", LoadedMod.class, Vars.class, modInfo().name);
Fi.get(path).walk(p -> {
// System.out.println("p: "+p);
String name = p.nameWithoutExtension();
if(names.contains(name)){
if (names.contains(name)) {
BaseProcessor.err("Duplicate file name: " + p + "!");
}else{
} else {
names.add(name);
}

if(SourceVersion.isKeyword(name)) name += "s";
if (SourceVersion.isKeyword(name)) name += "s";

String filepath = path.substring(path.lastIndexOf("/") + 1) + p.path().substring(p.path().lastIndexOf(path) + path.length());

Expand All @@ -127,13 +131,13 @@ void processSounds(String classname, String path, String rtype) throws Exception
// loadBegin.addStatement(Strings.format("@=new @(ModVars.modVars.modAssets.get(\"@\",\"@\"));", name, rtype, path.substring(path.lastIndexOf("/")+1) ,p.path().substring(p.path().lastIndexOf(path) + path.length()+1)));
// loadBegin.addStatement( Strings.format("@=new @(ModVars.modVars.modInfo.root.child(@));",name,rtype,filename));
loadBegin.addStatement("arc.Core.assets.load(" + filename + ", " + rtype + ".class).loaded = a -> " + name + " = (" + rtype + ")a", filepath, filepath.replace(".ogg", ".mp3"));
loadNowBegin.addStatement(name + " = new " + rtype + "(__loadedMod__.root.child("+ Strings.join("\").child(\"",filename.split("[/\\\\]")) +"))");
loadNowBegin.addStatement(name + " = new " + rtype + "(__loadedMod__.root.child(" + Strings.join("\").child(\"", filename.split("[/\\\\]")) + "))");
// loadNowBegin.addStatement("arc.Core.assets.load(" + filename + ", " + rtype + ".class).loaded = a -> " + name + " = (" + rtype + ")a", filepath, filepath.replace(".ogg", ".mp3"));

type.addField(FieldSpec.builder(ClassName.bestGuess(rtype), name, Modifier.STATIC, Modifier.PUBLIC).initializer("new arc.audio." + rtype.substring(rtype.lastIndexOf(".") + 1) + "()").build());
});

if(classname.equals(classPrefix() + "Sounds")){
if (classname.equals(classPrefix() + "Sounds")) {
type.addField(FieldSpec.builder(ClassName.bestGuess(rtype), "none", Modifier.STATIC, Modifier.PUBLIC).initializer("new arc.audio." + rtype.substring(rtype.lastIndexOf(".") + 1) + "()").build());
}

Expand Down

0 comments on commit 22ab978

Please sign in to comment.