Barely functional IO

This commit is contained in:
Anuken
2020-02-13 19:26:36 -05:00
parent ad248e2e20
commit e3621f44da
68 changed files with 323 additions and 497 deletions

View File

@ -73,10 +73,16 @@ public class Annotations{
/** Indicates an entity definition. */
@Retention(RetentionPolicy.SOURCE)
public @interface EntityDef{
/** List of component interfaces */
Class[] value();
/** Whether the class is final */
boolean isFinal() default true;
/** If true, entities are recycled. */
boolean pooled() default false;
/** Whether to serialize (makes the serialize method return this value) */
boolean serialize() default true;
/** Whether to generate IO code */
boolean genio() default true;
}
/** Indicates an internal interface for entity components. */

View File

@ -45,6 +45,12 @@ public abstract class BaseProcessor extends AbstractProcessor{
|| type.equals("long") || type.equals("float") || type.equals("double") || type.equals("char");
}
public static boolean instanceOf(String type, String other){
TypeElement a = elementu.getTypeElement(type);
TypeElement b = elementu.getTypeElement(other);
return a != null && b != null && typeu.isSubtype(a.asType(), b.asType());
}
public static String getDefault(String value){
switch(value){
case "float":

View File

@ -5,10 +5,9 @@ import com.squareup.javapoet.*;
import com.squareup.javapoet.MethodSpec.*;
import mindustry.annotations.*;
import javax.lang.model.element.*;
import static mindustry.annotations.BaseProcessor.instanceOf;
public class EntityIO{
final TypeElement contentElem = BaseProcessor.elementu.getTypeElement("mindustry.ctype.Content");
final MethodSpec.Builder builder;
final boolean write;
@ -18,13 +17,12 @@ public class EntityIO{
}
void io(TypeName type, String field) throws Exception{
TypeElement element = BaseProcessor.elementu.getTypeElement(type.toString());
if(type.isPrimitive()){
s(type.toString(), field);
}else if(type.toString().equals("java.lang.String")){
s("UTF", field);
}else if(element != null && BaseProcessor.typeu.isSubtype(element.asType(), contentElem.asType())){
}else if(instanceOf(type.toString(), "mindustry.ctype.Content")){
if(write){
s("short", field + ".id");
}else{

View File

@ -327,8 +327,10 @@ public class EntityProcess extends BaseProcessor{
//SPECIAL CASE: I/O code
//note that serialization is generated even for non-serializing entities for manual usage
if(first.name().equals("read") || first.name().equals("write")){
if((first.name().equals("read") || first.name().equals("write")) && ann.genio()){
EntityIO writer = new EntityIO(mbuilder, first.name().equals("write"));
//subclasses *have* to call this method
mbuilder.addAnnotation(CallSuper.class);
//write or read each non-transient field
for(FieldSpec spec : builder.fieldSpecs){
if(!spec.hasModifier(Modifier.TRANSIENT) && !spec.hasModifier(Modifier.STATIC) && !spec.hasModifier(Modifier.FINAL)){