parser refactor
All checks were successful
studiorailgun/highlevel-netcode-gen/pipeline/head This commit looks good
All checks were successful
studiorailgun/highlevel-netcode-gen/pipeline/head This commit looks good
This commit is contained in:
parent
9239aee487
commit
53e47ba2d8
@ -1,5 +1,6 @@
|
||||
package electrosphere.main.core.btree;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import electrosphere.main.core.syncfield.SynchronizedField;
|
||||
@ -27,7 +28,7 @@ public class BehaviorTree {
|
||||
boolean isServer;
|
||||
|
||||
//The synchronized fields within this tree
|
||||
List<SynchronizedField> synchronizedFields;
|
||||
List<SynchronizedField> synchronizedFields = new LinkedList<SynchronizedField>();
|
||||
|
||||
//The file this type appears in
|
||||
TargetFile targetFile;
|
||||
@ -47,7 +48,6 @@ public class BehaviorTree {
|
||||
String className,
|
||||
String correspondingTreeName,
|
||||
boolean isServer,
|
||||
List<SynchronizedField> synchronizedFields,
|
||||
TargetFile targetFile
|
||||
){
|
||||
this.id = id;
|
||||
@ -55,7 +55,6 @@ public class BehaviorTree {
|
||||
this.className = className;
|
||||
this.correspondingTreeName = correspondingTreeName;
|
||||
this.isServer = isServer;
|
||||
this.synchronizedFields = synchronizedFields;
|
||||
this.targetFile = targetFile;
|
||||
}
|
||||
|
||||
@ -107,6 +106,14 @@ public class BehaviorTree {
|
||||
return synchronizedFields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a synchronized field to this behavior tree
|
||||
* @param field The synchronized field
|
||||
*/
|
||||
public void addSynchronizedField(SynchronizedField field){
|
||||
this.synchronizedFields.add(field);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the target file associated with this behavior tree
|
||||
* @return The target file
|
||||
|
||||
@ -1,18 +1,8 @@
|
||||
package electrosphere.main.project;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.jboss.forge.roaster.model.source.AnnotationSource;
|
||||
import org.jboss.forge.roaster.model.source.EnumConstantSource;
|
||||
import org.jboss.forge.roaster.model.source.FieldSource;
|
||||
import org.jboss.forge.roaster.model.source.JavaClassSource;
|
||||
import org.jboss.forge.roaster.model.source.JavaEnumSource;
|
||||
import org.jboss.forge.roaster.model.source.JavaSource;
|
||||
|
||||
import electrosphere.main.Main;
|
||||
import electrosphere.main.client.ClientSynchronizationManager;
|
||||
@ -21,6 +11,7 @@ import electrosphere.main.core.enums.BTreeIdEnum;
|
||||
import electrosphere.main.core.enums.FieldIdEnum;
|
||||
import electrosphere.main.core.syncfield.SynchronizedField;
|
||||
import electrosphere.main.core.syncfield.SynchronizedType;
|
||||
import electrosphere.main.project.parsers.MainParser;
|
||||
import electrosphere.main.targets.TargetFile;
|
||||
import electrosphere.main.util.ClassSourceUtils;
|
||||
|
||||
@ -31,32 +22,14 @@ public class ProjectStructure {
|
||||
|
||||
//The root folder of the project
|
||||
File rootFolder;
|
||||
|
||||
|
||||
//The list of files to parse
|
||||
List<TargetFile> targetFiles;
|
||||
|
||||
//The list of behavior trees to synchronize
|
||||
List<BehaviorTree> behaviorTrees = new LinkedList<BehaviorTree>();
|
||||
|
||||
//All fields that are to be synchronized
|
||||
List<SynchronizedField> synchronizedFields = new LinkedList<SynchronizedField>();
|
||||
|
||||
//All types (enums) that can be synchronized
|
||||
List<SynchronizedType> synchronizedTypes = new LinkedList<SynchronizedType>();
|
||||
|
||||
//the client synchronization manager
|
||||
TargetFile clientSynchronizationManager;
|
||||
|
||||
//the file that has the enum of all behavior tree ids
|
||||
TargetFile behaviorTreeIdEnums;
|
||||
int behaviorTreeIdIterator = 0; //used to generate unique ids for new trees
|
||||
Map<String,Integer> existingTreeNameToIdMap; //the id map associated with the behaviorTreeIdEnums file
|
||||
|
||||
//the file that contains the enum of all synced fields to their ids
|
||||
TargetFile fieldIdEnums;
|
||||
int fieldIdIterator = 0; //used to generate unique ids for new fields
|
||||
Map<String,Integer> existingFieldNameToIdMap; //the map of existing field names to their ids
|
||||
/**
|
||||
* The main parser
|
||||
*/
|
||||
MainParser mainParser = new MainParser();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
@ -73,87 +46,7 @@ public class ProjectStructure {
|
||||
* Parses the target files in this project into rich data to generate off of
|
||||
*/
|
||||
public void parseRichStructure(){
|
||||
int typeIterator = 0;
|
||||
for(TargetFile target : targetFiles){
|
||||
AnnotationSource<JavaClassSource> mainAnnotation = target.getSource().getAnnotation("SynchronizedBehaviorTree");
|
||||
|
||||
//parse btrees
|
||||
if(mainAnnotation != null){
|
||||
String bTreeName = mainAnnotation.getStringValue("name");
|
||||
String bTreeCorrespondingName = mainAnnotation.getStringValue("correspondingTree");
|
||||
boolean isServer = Boolean.parseBoolean(mainAnnotation.getStringValue("isServer"));
|
||||
|
||||
|
||||
//parse sync'd fields
|
||||
List<SynchronizedField> syncedFields = new LinkedList<SynchronizedField>();
|
||||
for(FieldSource<JavaClassSource> fieldSource : target.getSource().getFields()){
|
||||
AnnotationSource<JavaClassSource> syncAnnotation = fieldSource.getAnnotation("SyncedField");
|
||||
if(syncAnnotation != null){
|
||||
String fieldName = fieldSource.getName();
|
||||
String typeName = fieldSource.getType().getName();
|
||||
int fieldId = getFieldId(bTreeName,fieldName);
|
||||
SynchronizedField field = new SynchronizedField(fieldId,fieldName,typeName,target);
|
||||
this.synchronizedFields.add(field);
|
||||
syncedFields.add(field);
|
||||
}
|
||||
}
|
||||
|
||||
//parse sync'd enum types
|
||||
for(JavaSource<?> fieldSource : target.getSource().getNestedTypes()){
|
||||
if(fieldSource instanceof JavaEnumSource){
|
||||
JavaEnumSource enumSource = (JavaEnumSource)fieldSource;
|
||||
AnnotationSource<JavaEnumSource> annotation = enumSource.getAnnotation("SynchronizableEnum");
|
||||
if(annotation != null){
|
||||
List<String> values = new LinkedList<String>();
|
||||
for(EnumConstantSource constants: enumSource.getEnumConstants()){
|
||||
values.add(constants.getName());
|
||||
}
|
||||
SynchronizedType type = new SynchronizedType(typeIterator, enumSource.getName(), values,target);
|
||||
synchronizedTypes.add(type);
|
||||
typeIterator++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BehaviorTree newBehaviorTree = new BehaviorTree(
|
||||
getBTreeId(bTreeName),
|
||||
bTreeName,
|
||||
target.getSource().getName(),
|
||||
bTreeCorrespondingName,
|
||||
isServer,
|
||||
syncedFields,
|
||||
target
|
||||
);
|
||||
behaviorTrees.add(newBehaviorTree);
|
||||
|
||||
//push behavior tree to each field
|
||||
for(SynchronizedField field : syncedFields){
|
||||
field.setParent(newBehaviorTree);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
//specific cases
|
||||
|
||||
//client synchronization manager
|
||||
if(target.getName().contains("ClientSynchronizationManager")){
|
||||
this.clientSynchronizationManager = target;
|
||||
}
|
||||
|
||||
//currently defined behavior tree ids
|
||||
//this is guaranteed to be parsed before the regular behavior trees that are discovered in the recursive file parser
|
||||
if(target.getName().contains("BehaviorTreeIdEnums")){
|
||||
this.behaviorTreeIdEnums = target;
|
||||
pushExistingTreeIds();
|
||||
}
|
||||
|
||||
//class storing all current field's ids
|
||||
if(target.getName().contains("FieldIdEnums")){
|
||||
this.fieldIdEnums = target;
|
||||
pushExistingFieldIds();
|
||||
}
|
||||
}
|
||||
|
||||
this.mainParser.parse(targetFiles);
|
||||
}
|
||||
|
||||
|
||||
@ -162,7 +55,7 @@ public class ProjectStructure {
|
||||
*/
|
||||
public void generate(){
|
||||
//generate in-class functions for btrees
|
||||
for(BehaviorTree tree : behaviorTrees){
|
||||
for(BehaviorTree tree : this.getBehaviorTrees()){
|
||||
if(Main.LOG){
|
||||
System.out.println("Generating for " + tree.getClassName());
|
||||
}
|
||||
@ -217,14 +110,14 @@ public class ProjectStructure {
|
||||
ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.entity.EntityDataStrings");
|
||||
}
|
||||
//generate enums for all synchronized types
|
||||
for(SynchronizedType type : synchronizedTypes){
|
||||
for(SynchronizedType type : this.mainParser.getSynchronizedTypes()){
|
||||
ClassSourceUtils.addOrReplaceMethod(this, type.getTargetFile(), type.getToShortConversionMethodName(), type.getToShortConversionMethodContent());
|
||||
ClassSourceUtils.addOrReplaceMethod(this, type.getTargetFile(), type.getFromShortConversionMethodName(), type.getFromShortConversionMethodContent());
|
||||
}
|
||||
BTreeIdEnum.generate(this);
|
||||
FieldIdEnum.generate(this);
|
||||
//client sync manager
|
||||
ClientSynchronizationManager.update(this, clientSynchronizationManager);
|
||||
ClientSynchronizationManager.update(this, this.mainParser.getClientSynchronizationManager());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -254,12 +147,7 @@ public class ProjectStructure {
|
||||
* @return The synchronized type if it exists, null otherwise
|
||||
*/
|
||||
public SynchronizedType getType(String typeName){
|
||||
for(SynchronizedType type : this.synchronizedTypes){
|
||||
if(type.getName().equals(typeName)){
|
||||
return type;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return mainParser.getType(typeName);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -268,86 +156,7 @@ public class ProjectStructure {
|
||||
* @return The tree if it exists, null otherwise
|
||||
*/
|
||||
public BehaviorTree getTree(String typeName){
|
||||
for(BehaviorTree tree : this.behaviorTrees){
|
||||
if(tree.getName().equals(typeName)){
|
||||
return tree;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the behavior tree objects with the behavior tree ids that are already defined
|
||||
*/
|
||||
private void pushExistingTreeIds(){
|
||||
existingTreeNameToIdMap = new HashMap<String,Integer>();
|
||||
for(FieldSource<JavaClassSource> fieldSource : this.behaviorTreeIdEnums.getSource().getFields()){
|
||||
int value = Integer.parseInt(fieldSource.getLiteralInitializer());
|
||||
String fieldName = fieldSource.getName();
|
||||
String capitalizedBTreeName = fieldName.split("_")[1];
|
||||
existingTreeNameToIdMap.put(capitalizedBTreeName,value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the field objects with the field ids that are already defined
|
||||
*/
|
||||
private void pushExistingFieldIds(){
|
||||
existingFieldNameToIdMap = new HashMap<String,Integer>();
|
||||
if(this.fieldIdEnums != null){
|
||||
for(FieldSource<JavaClassSource> fieldSource : this.fieldIdEnums.getSource().getFields()){
|
||||
int value = Integer.parseInt(fieldSource.getLiteralInitializer());
|
||||
String fieldName = fieldSource.getName();
|
||||
String capitalizedFieldName = fieldName.split("_")[1] + "_" + fieldName.split("_")[3];
|
||||
existingFieldNameToIdMap.put(capitalizedFieldName,value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the id for a given btree
|
||||
* @param bTreeName the name of the btree
|
||||
* @return the id
|
||||
*/
|
||||
private int getBTreeId(String bTreeName){
|
||||
String capitalizedName = bTreeName.toUpperCase();
|
||||
if(existingTreeNameToIdMap.containsKey(capitalizedName)){
|
||||
return existingTreeNameToIdMap.get(capitalizedName);
|
||||
} else {
|
||||
Collection<Integer> existingIds = existingTreeNameToIdMap.values();
|
||||
//increment ids until we hit an available one
|
||||
while(existingIds.contains(behaviorTreeIdIterator)){
|
||||
behaviorTreeIdIterator++;
|
||||
}
|
||||
existingTreeNameToIdMap.put(capitalizedName,behaviorTreeIdIterator);
|
||||
return behaviorTreeIdIterator;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the id for a given field
|
||||
* @param bTreeName the name of the btree that the field is inside of
|
||||
* @param fieldName the name of the field
|
||||
* @return the id
|
||||
*/
|
||||
private int getFieldId(String bTreeName, String fieldName){
|
||||
//init the map if it doesn't already exist (ie the file doesn't exist)
|
||||
if(existingFieldNameToIdMap == null){
|
||||
existingFieldNameToIdMap = new HashMap<String,Integer>();
|
||||
}
|
||||
//get the id
|
||||
String qualifiedName = bTreeName.toUpperCase() + "_" + fieldName.toUpperCase();
|
||||
if(existingFieldNameToIdMap.containsKey(qualifiedName)){
|
||||
return existingFieldNameToIdMap.get(qualifiedName);
|
||||
} else {
|
||||
Collection<Integer> existingIds = existingFieldNameToIdMap.values();
|
||||
//increment ids until we hit an available one
|
||||
while(existingIds.contains(fieldIdIterator)){
|
||||
fieldIdIterator++;
|
||||
}
|
||||
existingFieldNameToIdMap.put(qualifiedName,fieldIdIterator);
|
||||
return fieldIdIterator;
|
||||
}
|
||||
return mainParser.getTree(typeName);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -355,7 +164,7 @@ public class ProjectStructure {
|
||||
* @return The list of synchronized fields
|
||||
*/
|
||||
public List<SynchronizedField> getSynchronizedFields(){
|
||||
return this.synchronizedFields;
|
||||
return mainParser.getSynchronizedFields();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -363,7 +172,7 @@ public class ProjectStructure {
|
||||
* @return The list of all behavior trees
|
||||
*/
|
||||
public List<BehaviorTree> getBehaviorTrees(){
|
||||
return this.behaviorTrees;
|
||||
return mainParser.getBehaviorTrees();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,111 @@
|
||||
package electrosphere.main.project.parsers;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.jboss.forge.roaster.model.source.AnnotationSource;
|
||||
import org.jboss.forge.roaster.model.source.FieldSource;
|
||||
import org.jboss.forge.roaster.model.source.JavaClassSource;
|
||||
|
||||
import electrosphere.main.core.btree.BehaviorTree;
|
||||
import electrosphere.main.targets.TargetFile;
|
||||
|
||||
/**
|
||||
* Parses behavior trees from the project structure
|
||||
*/
|
||||
public class BTreeParser {
|
||||
|
||||
//The list of behavior trees to synchronize
|
||||
List<BehaviorTree> behaviorTrees = new LinkedList<BehaviorTree>();
|
||||
|
||||
//the file that has the enum of all behavior tree ids
|
||||
TargetFile behaviorTreeIdEnums;
|
||||
int behaviorTreeIdIterator = 0; //used to generate unique ids for new trees
|
||||
Map<String,Integer> existingTreeNameToIdMap = new HashMap<String,Integer>(); //the id map associated with the behaviorTreeIdEnums file
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Gets the list of all parsed behavior trees
|
||||
* @return The list of all parsed behavior trees
|
||||
*/
|
||||
public List<BehaviorTree> getBehaviorTrees(){
|
||||
return this.behaviorTrees;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a target file
|
||||
* @param target The target file
|
||||
* @return The behavior tree that was parsed or null if no behavior tree was parsed
|
||||
*/
|
||||
public BehaviorTree parse(TargetFile target){
|
||||
BehaviorTree rVal = null;
|
||||
|
||||
AnnotationSource<JavaClassSource> mainAnnotation = target.getSource().getAnnotation("SynchronizedBehaviorTree");
|
||||
//parse btrees
|
||||
if(mainAnnotation != null){
|
||||
String bTreeName = mainAnnotation.getStringValue("name");
|
||||
String bTreeCorrespondingName = mainAnnotation.getStringValue("correspondingTree");
|
||||
boolean isServer = Boolean.parseBoolean(mainAnnotation.getStringValue("isServer"));
|
||||
|
||||
rVal = new BehaviorTree(
|
||||
getBTreeId(bTreeName),
|
||||
bTreeName,
|
||||
target.getSource().getName(),
|
||||
bTreeCorrespondingName,
|
||||
isServer,
|
||||
target
|
||||
);
|
||||
behaviorTrees.add(rVal);
|
||||
}
|
||||
|
||||
//
|
||||
//specific cases
|
||||
|
||||
//currently defined behavior tree ids
|
||||
//this is guaranteed to be parsed before the regular behavior trees that are discovered in the recursive file parser
|
||||
if(target.getName().contains("BehaviorTreeIdEnums")){
|
||||
this.behaviorTreeIdEnums = target;
|
||||
pushExistingTreeIds();
|
||||
}
|
||||
|
||||
return rVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the behavior tree objects with the behavior tree ids that are already defined
|
||||
*/
|
||||
private void pushExistingTreeIds(){
|
||||
existingTreeNameToIdMap = new HashMap<String,Integer>();
|
||||
for(FieldSource<JavaClassSource> fieldSource : this.behaviorTreeIdEnums.getSource().getFields()){
|
||||
int value = Integer.parseInt(fieldSource.getLiteralInitializer());
|
||||
String fieldName = fieldSource.getName();
|
||||
String capitalizedBTreeName = fieldName.split("_")[1];
|
||||
existingTreeNameToIdMap.put(capitalizedBTreeName,value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the id for a given btree
|
||||
* @param bTreeName the name of the btree
|
||||
* @return the id
|
||||
*/
|
||||
private int getBTreeId(String bTreeName){
|
||||
String capitalizedName = bTreeName.toUpperCase();
|
||||
if(existingTreeNameToIdMap.containsKey(capitalizedName)){
|
||||
return existingTreeNameToIdMap.get(capitalizedName);
|
||||
} else {
|
||||
Collection<Integer> existingIds = existingTreeNameToIdMap.values();
|
||||
//increment ids until we hit an available one
|
||||
while(existingIds.contains(behaviorTreeIdIterator)){
|
||||
behaviorTreeIdIterator++;
|
||||
}
|
||||
existingTreeNameToIdMap.put(capitalizedName,behaviorTreeIdIterator);
|
||||
return behaviorTreeIdIterator;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,66 @@
|
||||
package electrosphere.main.project.parsers;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.jboss.forge.roaster.model.source.AnnotationSource;
|
||||
import org.jboss.forge.roaster.model.source.EnumConstantSource;
|
||||
import org.jboss.forge.roaster.model.source.JavaClassSource;
|
||||
import org.jboss.forge.roaster.model.source.JavaEnumSource;
|
||||
import org.jboss.forge.roaster.model.source.JavaSource;
|
||||
|
||||
import electrosphere.main.core.syncfield.SynchronizedType;
|
||||
import electrosphere.main.targets.TargetFile;
|
||||
|
||||
/**
|
||||
* Parses synchronized enums from a target file
|
||||
*/
|
||||
public class EnumParser {
|
||||
|
||||
//All types (enums) that can be synchronized
|
||||
List<SynchronizedType> synchronizedTypes = new LinkedList<SynchronizedType>();
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Parses a target file
|
||||
* @param target The target file
|
||||
* @return The list of types parsed from this target file
|
||||
*/
|
||||
public List<SynchronizedType> parse(TargetFile target, int typeIterator){
|
||||
List<SynchronizedType> rVal = new LinkedList<SynchronizedType>();
|
||||
|
||||
AnnotationSource<JavaClassSource> mainAnnotation = target.getSource().getAnnotation("SynchronizedBehaviorTree");
|
||||
//parse btrees
|
||||
if(mainAnnotation != null){
|
||||
//parse sync'd enum types
|
||||
for(JavaSource<?> fieldSource : target.getSource().getNestedTypes()){
|
||||
if(fieldSource instanceof JavaEnumSource){
|
||||
JavaEnumSource enumSource = (JavaEnumSource)fieldSource;
|
||||
AnnotationSource<JavaEnumSource> annotation = enumSource.getAnnotation("SynchronizableEnum");
|
||||
if(annotation != null){
|
||||
List<String> values = new LinkedList<String>();
|
||||
for(EnumConstantSource constants: enumSource.getEnumConstants()){
|
||||
values.add(constants.getName());
|
||||
}
|
||||
SynchronizedType type = new SynchronizedType(typeIterator, enumSource.getName(), values,target);
|
||||
synchronizedTypes.add(type);
|
||||
rVal.add(type);
|
||||
typeIterator++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return rVal;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the list of synchronized types
|
||||
* @return The list of synchronized types
|
||||
*/
|
||||
public List<SynchronizedType> getSynchronizedTypes(){
|
||||
return this.synchronizedTypes;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,115 @@
|
||||
package electrosphere.main.project.parsers;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.jboss.forge.roaster.model.source.AnnotationSource;
|
||||
import org.jboss.forge.roaster.model.source.FieldSource;
|
||||
import org.jboss.forge.roaster.model.source.JavaClassSource;
|
||||
|
||||
import electrosphere.main.core.syncfield.SynchronizedField;
|
||||
import electrosphere.main.targets.TargetFile;
|
||||
|
||||
/**
|
||||
* Parses synchronized fields from a target file
|
||||
*/
|
||||
public class FieldParser {
|
||||
|
||||
//All fields that are to be synchronized
|
||||
List<SynchronizedField> synchronizedFields = new LinkedList<SynchronizedField>();
|
||||
|
||||
//the file that contains the enum of all synced fields to their ids
|
||||
TargetFile fieldIdEnums;
|
||||
int fieldIdIterator = 0; //used to generate unique ids for new fields
|
||||
Map<String,Integer> existingFieldNameToIdMap = new HashMap<String,Integer>(); //the map of existing field names to their ids
|
||||
|
||||
|
||||
/**
|
||||
* Parses a given target file
|
||||
* @param target The target file
|
||||
* @return The list of fields parsed from this file
|
||||
*/
|
||||
public List<SynchronizedField> parse(TargetFile target){
|
||||
List<SynchronizedField> rVal = new LinkedList<SynchronizedField>();
|
||||
|
||||
|
||||
AnnotationSource<JavaClassSource> mainAnnotation = target.getSource().getAnnotation("SynchronizedBehaviorTree");
|
||||
//parse btrees
|
||||
if(mainAnnotation != null){
|
||||
String bTreeName = mainAnnotation.getStringValue("name");
|
||||
//parse sync'd fields
|
||||
for(FieldSource<JavaClassSource> fieldSource : target.getSource().getFields()){
|
||||
AnnotationSource<JavaClassSource> syncAnnotation = fieldSource.getAnnotation("SyncedField");
|
||||
if(syncAnnotation != null){
|
||||
String fieldName = fieldSource.getName();
|
||||
String typeName = fieldSource.getType().getName();
|
||||
int fieldId = getFieldId(bTreeName,fieldName);
|
||||
SynchronizedField field = new SynchronizedField(fieldId,fieldName,typeName,target);
|
||||
this.synchronizedFields.add(field);
|
||||
rVal.add(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//class storing all current field's ids
|
||||
if(target.getName().contains("FieldIdEnums")){
|
||||
this.fieldIdEnums = target;
|
||||
pushExistingFieldIds();
|
||||
}
|
||||
|
||||
return rVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of synchronized fields
|
||||
* @return The list of synchronized fields
|
||||
*/
|
||||
public List<SynchronizedField> getSynchronizedFields(){
|
||||
return this.synchronizedFields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the field objects with the field ids that are already defined
|
||||
*/
|
||||
private void pushExistingFieldIds(){
|
||||
existingFieldNameToIdMap = new HashMap<String,Integer>();
|
||||
if(this.fieldIdEnums != null){
|
||||
for(FieldSource<JavaClassSource> fieldSource : this.fieldIdEnums.getSource().getFields()){
|
||||
int value = Integer.parseInt(fieldSource.getLiteralInitializer());
|
||||
String fieldName = fieldSource.getName();
|
||||
String capitalizedFieldName = fieldName.split("_")[1] + "_" + fieldName.split("_")[3];
|
||||
existingFieldNameToIdMap.put(capitalizedFieldName,value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the id for a given field
|
||||
* @param bTreeName the name of the btree that the field is inside of
|
||||
* @param fieldName the name of the field
|
||||
* @return the id
|
||||
*/
|
||||
private int getFieldId(String bTreeName, String fieldName){
|
||||
//init the map if it doesn't already exist (ie the file doesn't exist)
|
||||
if(existingFieldNameToIdMap == null){
|
||||
existingFieldNameToIdMap = new HashMap<String,Integer>();
|
||||
}
|
||||
//get the id
|
||||
String qualifiedName = bTreeName.toUpperCase() + "_" + fieldName.toUpperCase();
|
||||
if(existingFieldNameToIdMap.containsKey(qualifiedName)){
|
||||
return existingFieldNameToIdMap.get(qualifiedName);
|
||||
} else {
|
||||
Collection<Integer> existingIds = existingFieldNameToIdMap.values();
|
||||
//increment ids until we hit an available one
|
||||
while(existingIds.contains(fieldIdIterator)){
|
||||
fieldIdIterator++;
|
||||
}
|
||||
existingFieldNameToIdMap.put(qualifiedName,fieldIdIterator);
|
||||
return fieldIdIterator;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
140
src/main/java/electrosphere/main/project/parsers/MainParser.java
Normal file
140
src/main/java/electrosphere/main/project/parsers/MainParser.java
Normal file
@ -0,0 +1,140 @@
|
||||
package electrosphere.main.project.parsers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.jboss.forge.roaster.model.source.AnnotationSource;
|
||||
import org.jboss.forge.roaster.model.source.JavaClassSource;
|
||||
|
||||
import electrosphere.main.core.btree.BehaviorTree;
|
||||
import electrosphere.main.core.syncfield.SynchronizedField;
|
||||
import electrosphere.main.core.syncfield.SynchronizedType;
|
||||
import electrosphere.main.targets.TargetFile;
|
||||
|
||||
/**
|
||||
* The top level parser object
|
||||
*/
|
||||
public class MainParser {
|
||||
|
||||
/**
|
||||
* The behavior tree parser
|
||||
*/
|
||||
BTreeParser bTreeParser = new BTreeParser();
|
||||
|
||||
/**
|
||||
* The enum parser
|
||||
*/
|
||||
EnumParser enumParser = new EnumParser();
|
||||
|
||||
/**
|
||||
* The field parser
|
||||
*/
|
||||
FieldParser fieldParser = new FieldParser();
|
||||
|
||||
//the client synchronization manager
|
||||
TargetFile clientSynchronizationManager;
|
||||
|
||||
/**
|
||||
* Parses all the target files
|
||||
* @param targetFiles The list of target files
|
||||
*/
|
||||
public void parse(List<TargetFile> targetFiles){
|
||||
int typeIterator = 0;
|
||||
for(TargetFile target : targetFiles){
|
||||
AnnotationSource<JavaClassSource> mainAnnotation = target.getSource().getAnnotation("SynchronizedBehaviorTree");
|
||||
|
||||
//parse btrees
|
||||
if(mainAnnotation != null){
|
||||
BehaviorTree bTree = this.bTreeParser.parse(target);
|
||||
List<SynchronizedType> types = this.enumParser.parse(target, typeIterator);
|
||||
List<SynchronizedField> fields = this.fieldParser.parse(target);
|
||||
|
||||
linkData(bTree, fields, types);
|
||||
}
|
||||
|
||||
//
|
||||
//specific cases
|
||||
|
||||
//client synchronization manager
|
||||
if(target.getName().contains("ClientSynchronizationManager")){
|
||||
this.clientSynchronizationManager = target;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Links all the values gathered from a behavior tree file
|
||||
* @param tree The tree
|
||||
* @param fields The list of fields
|
||||
* @param types The list of types
|
||||
*/
|
||||
private void linkData(BehaviorTree tree, List<SynchronizedField> fields, List<SynchronizedType> types){
|
||||
|
||||
//link fields
|
||||
for(SynchronizedField field : fields){
|
||||
field.setParent(tree);
|
||||
tree.addSynchronizedField(field);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets a synchronized type from a name
|
||||
* @param typeName The name of the type
|
||||
* @return The synchronized type if it exists, null otherwise
|
||||
*/
|
||||
public SynchronizedType getType(String typeName){
|
||||
for(SynchronizedType type : this.enumParser.getSynchronizedTypes()){
|
||||
if(type.getName().equals(typeName)){
|
||||
return type;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a behavior tree from its name
|
||||
* @param typeName The name of the tree
|
||||
* @return The tree if it exists, null otherwise
|
||||
*/
|
||||
public BehaviorTree getTree(String typeName){
|
||||
for(BehaviorTree tree : this.bTreeParser.getBehaviorTrees()){
|
||||
if(tree.getName().equals(typeName)){
|
||||
return tree;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of synchronized fields
|
||||
* @return The list of synchronized fields
|
||||
*/
|
||||
public List<SynchronizedField> getSynchronizedFields(){
|
||||
return this.fieldParser.getSynchronizedFields();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of all behavior trees
|
||||
* @return The list of all behavior trees
|
||||
*/
|
||||
public List<BehaviorTree> getBehaviorTrees(){
|
||||
return this.bTreeParser.getBehaviorTrees();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of all synchronized types
|
||||
* @return The list of all synchronized types
|
||||
*/
|
||||
public List<SynchronizedType> getSynchronizedTypes(){
|
||||
return this.enumParser.getSynchronizedTypes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the client synchronization manager target file
|
||||
* @return The target file
|
||||
*/
|
||||
public TargetFile getClientSynchronizationManager(){
|
||||
return this.clientSynchronizationManager;
|
||||
}
|
||||
|
||||
}
|
||||
26
src/main/java/electrosphere/main/source/VirtualClass.java
Normal file
26
src/main/java/electrosphere/main/source/VirtualClass.java
Normal file
@ -0,0 +1,26 @@
|
||||
package electrosphere.main.source;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Represents the contents of a class
|
||||
*/
|
||||
public abstract class VirtualClass {
|
||||
|
||||
/**
|
||||
* All the imports in this virtual class
|
||||
*/
|
||||
List<String> imports;
|
||||
|
||||
/**
|
||||
* All the fields in this class
|
||||
*/
|
||||
List<VirtualField> fields;
|
||||
|
||||
/**
|
||||
* All the methods in this class
|
||||
*/
|
||||
List<VirtualMethod> methods;
|
||||
|
||||
|
||||
}
|
||||
14
src/main/java/electrosphere/main/source/VirtualField.java
Normal file
14
src/main/java/electrosphere/main/source/VirtualField.java
Normal file
@ -0,0 +1,14 @@
|
||||
package electrosphere.main.source;
|
||||
|
||||
/**
|
||||
* Represents a field in a class
|
||||
*/
|
||||
public interface VirtualField {
|
||||
|
||||
/**
|
||||
* Gets the name of the field
|
||||
* @return The name of the field
|
||||
*/
|
||||
public String getName();
|
||||
|
||||
}
|
||||
21
src/main/java/electrosphere/main/source/VirtualMethod.java
Normal file
21
src/main/java/electrosphere/main/source/VirtualMethod.java
Normal file
@ -0,0 +1,21 @@
|
||||
package electrosphere.main.source;
|
||||
|
||||
/**
|
||||
* Represents a method in a class
|
||||
*/
|
||||
public interface VirtualMethod {
|
||||
|
||||
/**
|
||||
* Gets the name of the method
|
||||
* @return The name of the method
|
||||
*/
|
||||
public String getName();
|
||||
|
||||
|
||||
/**
|
||||
* Gets the content of this method
|
||||
* @return The content
|
||||
*/
|
||||
public String getContent();
|
||||
|
||||
}
|
||||
@ -1,43 +0,0 @@
|
||||
package electrosphere.main.targets;
|
||||
|
||||
import org.jboss.forge.roaster.model.source.AnnotationSource;
|
||||
import org.jboss.forge.roaster.model.source.FieldSource;
|
||||
import org.jboss.forge.roaster.model.source.JavaClassSource;
|
||||
|
||||
public class NetcodeGenTarget {
|
||||
|
||||
int id;
|
||||
String name;
|
||||
String typeName;
|
||||
FieldSource<JavaClassSource> field;
|
||||
AnnotationSource<JavaClassSource> annotation;
|
||||
|
||||
public NetcodeGenTarget(int id, String name, String typeName, FieldSource<JavaClassSource> field, AnnotationSource<JavaClassSource> annotation){
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.typeName = typeName;
|
||||
this.field = field;
|
||||
this.annotation = annotation;
|
||||
}
|
||||
|
||||
public int getId(){
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName(){
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getTypeName(){
|
||||
return typeName;
|
||||
}
|
||||
|
||||
public FieldSource<JavaClassSource> getField(){
|
||||
return field;
|
||||
}
|
||||
|
||||
public AnnotationSource<JavaClassSource> getAnnotation(){
|
||||
return annotation;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user