|
|
|
|
@@ -38,10 +38,12 @@ public class EntityProcess extends BaseProcessor{
|
|
|
|
|
ObjectMap<Selement, Seq<Stype>> defComponents = new ObjectMap<>();
|
|
|
|
|
ObjectMap<String, String> varInitializers = new ObjectMap<>();
|
|
|
|
|
ObjectMap<String, String> methodBlocks = new ObjectMap<>();
|
|
|
|
|
ObjectMap<Stype, ObjectSet<Stype>> baseClassDeps = new ObjectMap<>();
|
|
|
|
|
ObjectSet<String> imports = new ObjectSet<>();
|
|
|
|
|
Seq<Selement> allGroups = new Seq<>();
|
|
|
|
|
Seq<Selement> allDefs = new Seq<>();
|
|
|
|
|
Seq<Stype> allInterfaces = new Seq<>();
|
|
|
|
|
Seq<TypeSpec.Builder> baseClasses = new Seq<>();
|
|
|
|
|
ClassSerializer serializer;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
@@ -151,12 +153,53 @@ public class EntityProcess extends BaseProcessor{
|
|
|
|
|
|
|
|
|
|
write(inter);
|
|
|
|
|
|
|
|
|
|
//generate base class if necessary
|
|
|
|
|
//SPECIAL CASE: components with EntityDefs don't get a base class! the generated class becomes the base class itself
|
|
|
|
|
if(component.annotation(Component.class).base()){
|
|
|
|
|
|
|
|
|
|
Seq<Stype> deps = depends.copy().and(component);
|
|
|
|
|
baseClassDeps.get(component, ObjectSet::new).addAll(deps);
|
|
|
|
|
|
|
|
|
|
//do not generate base classes when the component will generate one itself
|
|
|
|
|
if(!component.has(EntityDef.class)){
|
|
|
|
|
TypeSpec.Builder base = TypeSpec.classBuilder(baseName(component)).addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT);
|
|
|
|
|
|
|
|
|
|
//go through all the fields.
|
|
|
|
|
for(Stype type : deps){
|
|
|
|
|
//add public fields
|
|
|
|
|
for(Svar field : type.fields().select(e -> !e.is(Modifier.STATIC) && !e.is(Modifier.PRIVATE) && !e.has(Import.class) && !e.has(ReadOnly.class))){
|
|
|
|
|
FieldSpec.Builder builder = FieldSpec.builder(field.tname(),field.name(), Modifier.PUBLIC);
|
|
|
|
|
|
|
|
|
|
//keep transience
|
|
|
|
|
if(field.is(Modifier.TRANSIENT)) builder.addModifiers(Modifier.TRANSIENT);
|
|
|
|
|
//keep all annotations
|
|
|
|
|
builder.addAnnotations(field.annotations().map(AnnotationSpec::get));
|
|
|
|
|
|
|
|
|
|
//add initializer if it exists
|
|
|
|
|
if(varInitializers.containsKey(field.descString())){
|
|
|
|
|
builder.initializer(varInitializers.get(field.descString()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
base.addField(builder.build());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//add interfaces
|
|
|
|
|
for(Stype type : depends){
|
|
|
|
|
base.addSuperinterface(tname(packageName, interfaceName(type)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//add to queue to be written later
|
|
|
|
|
baseClasses.add(base);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//LOGGING
|
|
|
|
|
|
|
|
|
|
Log.debug("&gGenerating interface for " + component.name());
|
|
|
|
|
|
|
|
|
|
for(TypeName tn : inter.superinterfaces){
|
|
|
|
|
Log.debug("&g> &lbextends @", simpleName(tn.toString()));
|
|
|
|
|
Log.debug("&g> &lbimplements @", simpleName(tn.toString()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//log methods generated
|
|
|
|
|
@@ -173,7 +216,12 @@ public class EntityProcess extends BaseProcessor{
|
|
|
|
|
//this needs to be done before the entity interfaces are generated, as the entity classes need to know which groups to add themselves to
|
|
|
|
|
for(Selement<?> group : allGroups){
|
|
|
|
|
GroupDef an = group.annotation(GroupDef.class);
|
|
|
|
|
Seq<Stype> types = types(an, GroupDef::value).map(this::interfaceToComp);
|
|
|
|
|
Seq<Stype> types = types(an, GroupDef::value).map(stype -> {
|
|
|
|
|
Stype result = interfaceToComp(stype);
|
|
|
|
|
if(result == null) throw new IllegalArgumentException("Interface " + stype + " does not have an associated component!");
|
|
|
|
|
return result;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
boolean collides = an.collide();
|
|
|
|
|
groupDefs.add(new GroupDefinition(group.name().startsWith("g") ? group.name().substring(1) : group.name(),
|
|
|
|
|
ClassName.bestGuess(packageName + "." + interfaceName(types.first())), types, an.spatial(), an.mapping(), collides));
|
|
|
|
|
@@ -211,29 +259,45 @@ public class EntityProcess extends BaseProcessor{
|
|
|
|
|
TypeSpec.Builder builder = TypeSpec.classBuilder(name).addModifiers(Modifier.PUBLIC);
|
|
|
|
|
if(isFinal) builder.addModifiers(Modifier.FINAL);
|
|
|
|
|
|
|
|
|
|
//all component classes (not interfaces)
|
|
|
|
|
Seq<Stype> components = allComponents(type);
|
|
|
|
|
Seq<GroupDefinition> groups = groupDefs.select(g -> (!g.components.isEmpty() && !g.components.contains(s -> !components.contains(s))) || g.manualInclusions.contains(type));
|
|
|
|
|
ObjectMap<String, Seq<Smethod>> methods = new ObjectMap<>();
|
|
|
|
|
ObjectMap<FieldSpec, Svar> specVariables = new ObjectMap<>();
|
|
|
|
|
ObjectSet<String> usedFields = new ObjectSet<>();
|
|
|
|
|
|
|
|
|
|
//make sure there's less than 2 base classes
|
|
|
|
|
Seq<Stype> baseClasses = components.select(s -> s.annotation(Component.class).base());
|
|
|
|
|
if(baseClasses.size > 2){
|
|
|
|
|
err("No entity may have more than 2 base classes. Base classes: " + baseClasses, type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//get base class type name for extension
|
|
|
|
|
Stype baseClassType = baseClasses.any() ? baseClasses.first() : null;
|
|
|
|
|
@Nullable TypeName baseClass = baseClasses.any() ? tname(packageName + "." + baseName(baseClassType)) : null;
|
|
|
|
|
//whether the main class is the base itself
|
|
|
|
|
boolean typeIsBase = baseClassType != null && type.has(Component.class) && type.annotation(Component.class).base();
|
|
|
|
|
|
|
|
|
|
//add serialize() boolean
|
|
|
|
|
builder.addMethod(MethodSpec.methodBuilder("serialize").addModifiers(Modifier.PUBLIC, Modifier.FINAL).returns(boolean.class).addStatement("return " + ann.serialize()).build());
|
|
|
|
|
|
|
|
|
|
//all SyncField fields
|
|
|
|
|
Seq<Svar> syncedFields = new Seq<>();
|
|
|
|
|
Seq<Svar> allFields = new Seq<>();
|
|
|
|
|
Seq<FieldSpec> allFieldSpecs = new Seq<>();
|
|
|
|
|
|
|
|
|
|
boolean isSync = components.contains(s -> s.name().contains("Sync"));
|
|
|
|
|
|
|
|
|
|
//add all components
|
|
|
|
|
for(Stype comp : components){
|
|
|
|
|
//whether this component's fields are defined in the base class
|
|
|
|
|
boolean isShadowed = baseClass != null && !typeIsBase && baseClassDeps.get(baseClassType).contains(comp);
|
|
|
|
|
|
|
|
|
|
//write fields to the class; ignoring transient/imported ones
|
|
|
|
|
Seq<Svar> fields = comp.fields().select(f -> !f.has(Import.class));
|
|
|
|
|
for(Svar f : fields){
|
|
|
|
|
if(!usedFields.add(f.name())){
|
|
|
|
|
err("Field '" + f.name() + "' of component '" + comp.name() + "' re-defines a field in entity '" + type.name() + "'");
|
|
|
|
|
err("Field '" + f.name() + "' of component '" + comp.name() + "' redefines a field in entity '" + type.name() + "'");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -255,9 +319,19 @@ public class EntityProcess extends BaseProcessor{
|
|
|
|
|
|
|
|
|
|
fbuilder.addModifiers(f.has(ReadOnly.class) ? Modifier.PROTECTED : Modifier.PUBLIC);
|
|
|
|
|
fbuilder.addAnnotations(f.annotations().map(AnnotationSpec::get));
|
|
|
|
|
builder.addField(fbuilder.build());
|
|
|
|
|
specVariables.put(builder.fieldSpecs.get(builder.fieldSpecs.size() - 1), f);
|
|
|
|
|
FieldSpec spec = fbuilder.build();
|
|
|
|
|
|
|
|
|
|
//whether this field would be added to the superclass
|
|
|
|
|
boolean isVisible = !f.is(Modifier.STATIC) && !f.is(Modifier.PRIVATE) && !f.has(ReadOnly.class);
|
|
|
|
|
|
|
|
|
|
//add the field only if it isn't visible or it wasn't implemented by the base class
|
|
|
|
|
if(!isShadowed || !isVisible){
|
|
|
|
|
builder.addField(spec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
specVariables.put(spec, f);
|
|
|
|
|
|
|
|
|
|
allFieldSpecs.add(spec);
|
|
|
|
|
allFields.add(f);
|
|
|
|
|
|
|
|
|
|
//add extra sync fields
|
|
|
|
|
@@ -294,7 +368,7 @@ public class EntityProcess extends BaseProcessor{
|
|
|
|
|
.addModifiers(Modifier.PUBLIC)
|
|
|
|
|
.addStatement("return $S + $L", name + "#", "id").build());
|
|
|
|
|
|
|
|
|
|
EntityIO io = new EntityIO(type.name(), builder, serializer, rootDirectory.child("annotations/src/main/resources/revisions").child(name));
|
|
|
|
|
EntityIO io = new EntityIO(type.name(), builder, allFieldSpecs, serializer, rootDirectory.child("annotations/src/main/resources/revisions").child(name));
|
|
|
|
|
//entities with no sync comp and no serialization gen no code
|
|
|
|
|
boolean hasIO = ann.genio() && (components.contains(s -> s.name().contains("Sync")) || ann.serialize());
|
|
|
|
|
|
|
|
|
|
@@ -435,7 +509,7 @@ public class EntityProcess extends BaseProcessor{
|
|
|
|
|
builder.addSuperinterface(Poolable.class);
|
|
|
|
|
//implement reset()
|
|
|
|
|
MethodSpec.Builder resetBuilder = MethodSpec.methodBuilder("reset").addModifiers(Modifier.PUBLIC);
|
|
|
|
|
for(FieldSpec spec : builder.fieldSpecs){
|
|
|
|
|
for(FieldSpec spec : allFieldSpecs){
|
|
|
|
|
@Nullable Svar variable = specVariables.get(spec);
|
|
|
|
|
if(variable != null && variable.isAny(Modifier.STATIC, Modifier.FINAL)) continue;
|
|
|
|
|
String desc = variable.descString();
|
|
|
|
|
@@ -462,7 +536,7 @@ public class EntityProcess extends BaseProcessor{
|
|
|
|
|
.returns(tname(packageName + "." + name))
|
|
|
|
|
.addStatement(ann.pooled() ? "return Pools.obtain($L.class, " +name +"::new)" : "return new $L()", name).build());
|
|
|
|
|
|
|
|
|
|
definitions.add(new EntityDefinition(packageName + "." + name, builder, type, components, groups));
|
|
|
|
|
definitions.add(new EntityDefinition(packageName + "." + name, builder, type, baseClass, components, groups, allFieldSpecs));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//generate groups
|
|
|
|
|
@@ -522,9 +596,9 @@ public class EntityProcess extends BaseProcessor{
|
|
|
|
|
int maxID = max == null ? 0 : max + 1;
|
|
|
|
|
|
|
|
|
|
//assign IDs
|
|
|
|
|
definitions.sort(Structs.comparing(t -> t.base.toString()));
|
|
|
|
|
definitions.sort(Structs.comparing(t -> t.naming.toString()));
|
|
|
|
|
for(EntityDefinition def : definitions){
|
|
|
|
|
String name = def.base.fullName();
|
|
|
|
|
String name = def.naming.fullName();
|
|
|
|
|
if(map.containsKey(name)){
|
|
|
|
|
def.classID = map.getInt(name);
|
|
|
|
|
}else{
|
|
|
|
|
@@ -557,7 +631,7 @@ public class EntityProcess extends BaseProcessor{
|
|
|
|
|
for(EntityDefinition def : definitions){
|
|
|
|
|
//store mapping
|
|
|
|
|
idStore.addStatement("idMap[$L] = $L::new", def.classID, def.name);
|
|
|
|
|
extraNames.get(def.base).each(extra -> {
|
|
|
|
|
extraNames.get(def.naming).each(extra -> {
|
|
|
|
|
idStore.addStatement("nameMap.put($S, $L::new)", extra, def.name);
|
|
|
|
|
if(!Strings.camelToKebab(extra).equals(extra)){
|
|
|
|
|
idStore.addStatement("nameMap.put($S, $L::new)", Strings.camelToKebab(extra), def.name);
|
|
|
|
|
@@ -576,11 +650,21 @@ public class EntityProcess extends BaseProcessor{
|
|
|
|
|
}else{
|
|
|
|
|
//round 3: generate actual classes and implement interfaces
|
|
|
|
|
|
|
|
|
|
//write base classes
|
|
|
|
|
for(TypeSpec.Builder b : baseClasses){
|
|
|
|
|
write(b, imports.asArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//implement each definition
|
|
|
|
|
for(EntityDefinition def : definitions){
|
|
|
|
|
|
|
|
|
|
ObjectSet<String> methodNames = def.components.flatMap(type -> type.methods().map(Smethod::simpleString)).<String>as().asSet();
|
|
|
|
|
|
|
|
|
|
//add base class extension if it exists
|
|
|
|
|
if(def.extend != null){
|
|
|
|
|
def.builder.superclass(def.extend);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//get interface for each component
|
|
|
|
|
for(Stype comp : def.components){
|
|
|
|
|
|
|
|
|
|
@@ -596,7 +680,7 @@ public class EntityProcess extends BaseProcessor{
|
|
|
|
|
//generate getter/setter for each method
|
|
|
|
|
for(Smethod method : inter.methods()){
|
|
|
|
|
String var = method.name();
|
|
|
|
|
FieldSpec field = Seq.with(def.builder.fieldSpecs).find(f -> f.name.equals(var));
|
|
|
|
|
FieldSpec field = Seq.with(def.fieldSpecs).find(f -> f.name.equals(var));
|
|
|
|
|
//make sure it's a real variable AND that the component doesn't already implement it somewhere with custom logic
|
|
|
|
|
if(field == null || methodNames.contains(method.simpleString())) continue;
|
|
|
|
|
|
|
|
|
|
@@ -681,12 +765,27 @@ public class EntityProcess extends BaseProcessor{
|
|
|
|
|
/** @return interface for a component type */
|
|
|
|
|
String interfaceName(Stype comp){
|
|
|
|
|
String suffix = "Comp";
|
|
|
|
|
if(!comp.name().endsWith(suffix)){
|
|
|
|
|
err("All components must have names that end with 'Comp'", comp.e);
|
|
|
|
|
}
|
|
|
|
|
if(!comp.name().endsWith(suffix)) err("All components must have names that end with 'Comp'", comp.e);
|
|
|
|
|
|
|
|
|
|
//example: BlockComp -> IBlock
|
|
|
|
|
return comp.name().substring(0, comp.name().length() - suffix.length()) + "c";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return base class name for a component type */
|
|
|
|
|
String baseName(Stype comp){
|
|
|
|
|
String suffix = "Comp";
|
|
|
|
|
if(!comp.name().endsWith(suffix)) err("All components must have names that end with 'Comp'", comp.e);
|
|
|
|
|
boolean isConcrete = comp.has(EntityDef.class); //concrete base implementations have no "Base" suffix
|
|
|
|
|
|
|
|
|
|
return comp.name().substring(0, comp.name().length() - suffix.length()) + (isConcrete ? "" : "Base");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Nullable Stype interfaceToComp(Stype type){
|
|
|
|
|
//example: IBlock -> BlockComp
|
|
|
|
|
String name = type.name().substring(0, type.name().length() - 1) + "Comp";
|
|
|
|
|
return componentNames.get(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return all components that a entity def has */
|
|
|
|
|
Seq<Stype> allComponents(Selement<?> type){
|
|
|
|
|
if(!defComponents.containsKey(type)){
|
|
|
|
|
@@ -746,19 +845,10 @@ public class EntityProcess extends BaseProcessor{
|
|
|
|
|
return interfaceToComp(type) != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Nullable Stype interfaceToComp(Stype type){
|
|
|
|
|
String name = type.name().substring(0, type.name().length() - 1) + "Comp";
|
|
|
|
|
return componentNames.get(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String createName(Selement<?> elem){
|
|
|
|
|
Seq<Stype> comps = types(elem.annotation(EntityDef.class), EntityDef::value).map(this::interfaceToComp);;
|
|
|
|
|
comps.sortComparing(Selement::name);
|
|
|
|
|
return comps.toString("", s -> s.name().replace("Comp", "")) + "Entity";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boolean isComponent(Stype type){
|
|
|
|
|
return type.annotation(Component.class) != null;
|
|
|
|
|
return comps.toString("", s -> s.name().replace("Comp", ""));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<T extends Annotation> Seq<Stype> types(T t, Cons<T> consumer){
|
|
|
|
|
@@ -795,17 +885,21 @@ public class EntityProcess extends BaseProcessor{
|
|
|
|
|
class EntityDefinition{
|
|
|
|
|
final Seq<GroupDefinition> groups;
|
|
|
|
|
final Seq<Stype> components;
|
|
|
|
|
final Seq<FieldSpec> fieldSpecs;
|
|
|
|
|
final TypeSpec.Builder builder;
|
|
|
|
|
final Selement base;
|
|
|
|
|
final Selement naming;
|
|
|
|
|
final String name;
|
|
|
|
|
final @Nullable TypeName extend;
|
|
|
|
|
int classID;
|
|
|
|
|
|
|
|
|
|
public EntityDefinition(String name, Builder builder, Selement base, Seq<Stype> components, Seq<GroupDefinition> groups){
|
|
|
|
|
public EntityDefinition(String name, Builder builder, Selement naming, TypeName extend, Seq<Stype> components, Seq<GroupDefinition> groups, Seq<FieldSpec> fieldSpec){
|
|
|
|
|
this.builder = builder;
|
|
|
|
|
this.name = name;
|
|
|
|
|
this.base = base;
|
|
|
|
|
this.naming = naming;
|
|
|
|
|
this.groups = groups;
|
|
|
|
|
this.components = components;
|
|
|
|
|
this.extend = extend;
|
|
|
|
|
this.fieldSpecs = fieldSpec;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@@ -813,7 +907,7 @@ public class EntityProcess extends BaseProcessor{
|
|
|
|
|
return "Definition{" +
|
|
|
|
|
"groups=" + groups +
|
|
|
|
|
"components=" + components +
|
|
|
|
|
", base=" + base +
|
|
|
|
|
", base=" + naming +
|
|
|
|
|
'}';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|