From 8e1559c764ce8ecae5dbd635342ec33b43bd4235 Mon Sep 17 00:00:00 2001 From: austin Date: Wed, 31 Jul 2024 16:08:15 -0400 Subject: [PATCH] refactoring generating methods for behavior tree --- .../main/core/VirtualProject.java | 19 ++++ .../main/core/btree/BehaviorTree.java | 27 ------ .../main/core/btree/methods/ClientAttach.java | 54 +++++++++++ .../main/core/btree/methods/ClientDetach.java | 53 +++++++++++ .../core/btree/methods/ClientFieldGetter.java | 61 +++++++++++++ .../core/btree/methods/ClientFieldSetter.java | 61 +++++++++++++ .../main/core/btree/methods/Constructor.java | 53 +++++++++++ .../main/core/btree/methods/Fetch.java | 53 +++++++++++ .../btree/methods/FromTypeConversion.java | 59 ++++++++++++ .../main/core/btree/methods/ServerAttach.java | 54 +++++++++++ .../main/core/btree/methods/ServerDetach.java | 54 +++++++++++ .../core/btree/methods/ServerFieldGetter.java | 59 ++++++++++++ .../core/btree/methods/ServerFieldSetter.java | 91 +++++++++++++++++++ .../core/btree/methods/ToTypeConversion.java | 62 +++++++++++++ .../core/syncfield/SynchronizedField.java | 61 ++----------- .../main/core/syncfield/SynchronizedType.java | 8 ++ .../main/project/ProjectStructure.java | 45 ++++----- .../main/project/parsers/BTreeParser.java | 22 +++++ .../main/project/parsers/MainParser.java | 19 +++- .../main/source/VirtualClass.java | 30 +++++- .../main/source/VirtualMethod.java | 17 +++- 21 files changed, 851 insertions(+), 111 deletions(-) create mode 100644 src/main/java/electrosphere/main/core/btree/methods/ClientAttach.java create mode 100644 src/main/java/electrosphere/main/core/btree/methods/ClientDetach.java create mode 100644 src/main/java/electrosphere/main/core/btree/methods/ClientFieldGetter.java create mode 100644 src/main/java/electrosphere/main/core/btree/methods/ClientFieldSetter.java create mode 100644 src/main/java/electrosphere/main/core/btree/methods/Constructor.java create mode 100644 src/main/java/electrosphere/main/core/btree/methods/Fetch.java create mode 100644 src/main/java/electrosphere/main/core/btree/methods/FromTypeConversion.java create mode 100644 src/main/java/electrosphere/main/core/btree/methods/ServerAttach.java create mode 100644 src/main/java/electrosphere/main/core/btree/methods/ServerDetach.java create mode 100644 src/main/java/electrosphere/main/core/btree/methods/ServerFieldGetter.java create mode 100644 src/main/java/electrosphere/main/core/btree/methods/ServerFieldSetter.java create mode 100644 src/main/java/electrosphere/main/core/btree/methods/ToTypeConversion.java diff --git a/src/main/java/electrosphere/main/core/VirtualProject.java b/src/main/java/electrosphere/main/core/VirtualProject.java index 2e6ed5b..0f2404a 100644 --- a/src/main/java/electrosphere/main/core/VirtualProject.java +++ b/src/main/java/electrosphere/main/core/VirtualProject.java @@ -6,6 +6,7 @@ import java.util.List; import electrosphere.main.client.ClientSynchronizationManager; import electrosphere.main.core.statics.btreeenum.BTreeIdEnum; import electrosphere.main.core.statics.fieldenum.FieldIdEnum; +import electrosphere.main.project.ProjectStructure; import electrosphere.main.source.VirtualClass; import electrosphere.main.targets.TargetFile; @@ -70,4 +71,22 @@ public class VirtualProject { return this.clientSynchronizationManager; } + /** + * Adds a virtual class to the project + * @param virtualClass The virtual class + */ + public void addClass(VirtualClass virtualClass){ + this.classes.add(virtualClass); + } + + /** + * Generates the new file contents for all files in the virtual project + * @param structure The project structure + */ + public void generate(ProjectStructure structure){ + for(VirtualClass virtualClass : this.classes){ + virtualClass.generate(structure); + } + } + } diff --git a/src/main/java/electrosphere/main/core/btree/BehaviorTree.java b/src/main/java/electrosphere/main/core/btree/BehaviorTree.java index e077f9b..90301e2 100644 --- a/src/main/java/electrosphere/main/core/btree/BehaviorTree.java +++ b/src/main/java/electrosphere/main/core/btree/BehaviorTree.java @@ -132,20 +132,6 @@ public class BehaviorTree extends VirtualClass { return rVal; } - /** - * Gets the content for the attach method of this behavior tree - * @param isServer true if this is a server behavior tree, false otherwise - * @return The attach method content - */ - public String getAttachMethodContent(){ - String templateSource = "/server/AttachBTree.java"; - if(!isServer){ - templateSource = "/client/AttachBTree.java"; - } - String rVal = TemplateInjectionUtils.getFragmentWithReplacement(templateSource, this.name.toUpperCase(), "TREE_" + this.name.toUpperCase(), className); - return rVal; - } - /** * Gets the detatch method's name * @return The detatch method's name @@ -156,19 +142,6 @@ public class BehaviorTree extends VirtualClass { return rVal; } - /** - * Gets the content for the detatch method - * @return The content - */ - public String getDetachMethodContent(){ - String templateSource = "/server/DetachBTree.java"; - if(!isServer){ - templateSource = "/client/DetachBTree.java"; - } - String rVal = TemplateInjectionUtils.getFragmentWithReplacement(templateSource, this.name.toUpperCase()); - return rVal; - } - /** * Gets the fetch method's name * @return The name diff --git a/src/main/java/electrosphere/main/core/btree/methods/ClientAttach.java b/src/main/java/electrosphere/main/core/btree/methods/ClientAttach.java new file mode 100644 index 0000000..6188ff3 --- /dev/null +++ b/src/main/java/electrosphere/main/core/btree/methods/ClientAttach.java @@ -0,0 +1,54 @@ +package electrosphere.main.core.btree.methods; + +import java.util.List; + +import electrosphere.main.project.ProjectStructure; +import electrosphere.main.source.VirtualMethod; +import electrosphere.main.util.TemplateInjectionUtils; + +/** + * Attach method for the b tree + */ +public class ClientAttach implements VirtualMethod { + + //The name of the btree + String name; + + //The classname of the btree + String className; + + /** + * Constructor + * @param name + * @param className + */ + public ClientAttach(String name, String className){ + this.name = name; + this.className = className; + } + + @Override + public String getName(ProjectStructure projectStructure) { + String rVal = ""; + rVal = "attachTree"; + return rVal; + } + + @Override + public String getContent(ProjectStructure projectStructure) { + String rVal = TemplateInjectionUtils.getFragmentWithReplacement("/client/AttachBTree.java", this.name.toUpperCase(), "TREE_" + this.name.toUpperCase(), className); + return rVal; + } + + @Override + public List getImports(ProjectStructure projectStructure) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getImports'"); + } + + @Override + public boolean shouldOverwrite(){ + return false; + } + +} diff --git a/src/main/java/electrosphere/main/core/btree/methods/ClientDetach.java b/src/main/java/electrosphere/main/core/btree/methods/ClientDetach.java new file mode 100644 index 0000000..b29b3db --- /dev/null +++ b/src/main/java/electrosphere/main/core/btree/methods/ClientDetach.java @@ -0,0 +1,53 @@ +package electrosphere.main.core.btree.methods; + +import java.util.List; + +import electrosphere.main.project.ProjectStructure; +import electrosphere.main.source.VirtualMethod; +import electrosphere.main.util.TemplateInjectionUtils; + +/** + * Client detatch method + */ +public class ClientDetach implements VirtualMethod { + + //The name of the btree + String name; + + //The classname of the btree + String className; + + /** + * Constructor + * @param name + * @param className + */ + public ClientDetach(String name, String className){ + this.name = name; + this.className = className; + } + + @Override + public String getName(ProjectStructure projectStructure) { + String rVal = ""; + rVal = "detachTree"; + return rVal; + } + + @Override + public String getContent(ProjectStructure projectStructure) { + String rVal = TemplateInjectionUtils.getFragmentWithReplacement("/client/DetachBTree.java", this.name.toUpperCase()); + return rVal; + } + + @Override + public List getImports(ProjectStructure projectStructure) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getImports'"); + } + + @Override + public boolean shouldOverwrite(){ + return true; + } +} diff --git a/src/main/java/electrosphere/main/core/btree/methods/ClientFieldGetter.java b/src/main/java/electrosphere/main/core/btree/methods/ClientFieldGetter.java new file mode 100644 index 0000000..14d5ba8 --- /dev/null +++ b/src/main/java/electrosphere/main/core/btree/methods/ClientFieldGetter.java @@ -0,0 +1,61 @@ +package electrosphere.main.core.btree.methods; + +import java.util.List; + +import electrosphere.main.core.btree.BehaviorTree; +import electrosphere.main.project.ProjectStructure; +import electrosphere.main.source.VirtualMethod; +import electrosphere.main.util.TemplateInjectionUtils; +import electrosphere.main.util.Utilities; + +/** + * A getter for a client field + */ +public class ClientFieldGetter implements VirtualMethod { + + //the name of the field + String fieldName; + + //the name of the type of the field + String typeName; + + //The parent behavior tree + BehaviorTree parent; + + /** + * Constructor + * @param tree The parent behavior tree + * @param typeName The name of the type of the field + * @param fieldName The name of the field + */ + public ClientFieldGetter(BehaviorTree tree, String typeName, String fieldName){ + this.parent = tree; + this.typeName = typeName; + this.fieldName = fieldName; + } + + @Override + public String getName(ProjectStructure projectStructure) { + String rVal = ""; + rVal = "get" + Utilities.camelCase(fieldName); + return rVal; + } + + @Override + public String getContent(ProjectStructure projectStructure) { + String rVal = TemplateInjectionUtils.getFragmentWithReplacement("/client/Getter.java", fieldName, Utilities.camelCase(fieldName), typeName); + return rVal; + } + + @Override + public List getImports(ProjectStructure projectStructure) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getImports'"); + } + + @Override + public boolean shouldOverwrite(){ + return true; + } + +} diff --git a/src/main/java/electrosphere/main/core/btree/methods/ClientFieldSetter.java b/src/main/java/electrosphere/main/core/btree/methods/ClientFieldSetter.java new file mode 100644 index 0000000..df6a9bc --- /dev/null +++ b/src/main/java/electrosphere/main/core/btree/methods/ClientFieldSetter.java @@ -0,0 +1,61 @@ +package electrosphere.main.core.btree.methods; + +import java.util.List; + +import electrosphere.main.core.btree.BehaviorTree; +import electrosphere.main.project.ProjectStructure; +import electrosphere.main.source.VirtualMethod; +import electrosphere.main.util.TemplateInjectionUtils; +import electrosphere.main.util.Utilities; + +/** + * A setter on the client + */ +public class ClientFieldSetter implements VirtualMethod { + + //the name of the field + String fieldName; + + //the name of the type of the field + String typeName; + + //The parent behavior tree + BehaviorTree parent; + + /** + * Constructor + * @param tree The parent behavior tree + * @param typeName The name of the type of the field + * @param fieldName The name of the field + */ + public ClientFieldSetter(BehaviorTree tree, String typeName, String fieldName){ + this.parent = tree; + this.typeName = typeName; + this.fieldName = fieldName; + } + + @Override + public String getName(ProjectStructure projectStructure) { + String rVal = ""; + rVal = "set" + Utilities.camelCase(fieldName); + return rVal; + } + + @Override + public String getContent(ProjectStructure projectStructure) { + String rVal = TemplateInjectionUtils.getFragmentWithReplacement("/client/Setter.java", fieldName, Utilities.camelCase(fieldName), typeName); + return rVal; + } + + @Override + public List getImports(ProjectStructure projectStructure) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getImports'"); + } + + @Override + public boolean shouldOverwrite(){ + return true; + } + +} diff --git a/src/main/java/electrosphere/main/core/btree/methods/Constructor.java b/src/main/java/electrosphere/main/core/btree/methods/Constructor.java new file mode 100644 index 0000000..7ca4671 --- /dev/null +++ b/src/main/java/electrosphere/main/core/btree/methods/Constructor.java @@ -0,0 +1,53 @@ +package electrosphere.main.core.btree.methods; + +import java.util.List; + +import electrosphere.main.project.ProjectStructure; +import electrosphere.main.source.VirtualMethod; +import electrosphere.main.util.TemplateInjectionUtils; + +/** + * Private constructor method + */ +public class Constructor implements VirtualMethod { + + //The name of the btree + String name; + + //The classname of the btree + String className; + + /** + * Constructor + * @param name + * @param className + */ + public Constructor(String name, String className){ + this.name = name; + this.className = className; + } + + @Override + public String getName(ProjectStructure projectStructure) { + String rVal = className; + return rVal; + } + + @Override + public String getContent(ProjectStructure projectStructure) { + String rVal = TemplateInjectionUtils.getFragmentWithReplacement("/btree/Constructor.java", this.className); + return rVal; + } + + @Override + public List getImports(ProjectStructure projectStructure) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getImports'"); + } + + @Override + public boolean shouldOverwrite(){ + return false; + } + +} diff --git a/src/main/java/electrosphere/main/core/btree/methods/Fetch.java b/src/main/java/electrosphere/main/core/btree/methods/Fetch.java new file mode 100644 index 0000000..155a4b6 --- /dev/null +++ b/src/main/java/electrosphere/main/core/btree/methods/Fetch.java @@ -0,0 +1,53 @@ +package electrosphere.main.core.btree.methods; + +import java.util.List; + +import electrosphere.main.project.ProjectStructure; +import electrosphere.main.source.VirtualMethod; +import electrosphere.main.util.TemplateInjectionUtils; + +/** + * Fetch btree from entity method + */ +public class Fetch implements VirtualMethod { + + //The name of the btree + String name; + + //The classname of the btree + String className; + + /** + * Constructor + * @param name + * @param className + */ + public Fetch(String name, String className){ + this.name = name; + this.className = className; + } + + @Override + public String getName(ProjectStructure projectStructure) { + String rVal = "get" + className; + return rVal; + } + + @Override + public String getContent(ProjectStructure projectStructure) { + String rVal = TemplateInjectionUtils.getFragmentWithReplacement("/btree/EntityFetchMethod.java", this.className, "TREE_" + this.name.toUpperCase()); + return rVal; + } + + @Override + public List getImports(ProjectStructure projectStructure) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getImports'"); + } + + @Override + public boolean shouldOverwrite(){ + return true; + } + +} diff --git a/src/main/java/electrosphere/main/core/btree/methods/FromTypeConversion.java b/src/main/java/electrosphere/main/core/btree/methods/FromTypeConversion.java new file mode 100644 index 0000000..752709f --- /dev/null +++ b/src/main/java/electrosphere/main/core/btree/methods/FromTypeConversion.java @@ -0,0 +1,59 @@ +package electrosphere.main.core.btree.methods; + +import java.util.List; + +import electrosphere.main.core.syncfield.SynchronizedType; +import electrosphere.main.project.ProjectStructure; +import electrosphere.main.source.VirtualMethod; +import electrosphere.main.util.TemplateInjectionUtils; +import electrosphere.main.util.Utilities; + +/** + * Converts an enum into a short + */ +public class FromTypeConversion implements VirtualMethod { + + //The synchronized type we're converting in this function + SynchronizedType type; + + /** + * Constructor + * @param type The synchronized type + */ + public FromTypeConversion(SynchronizedType type){ + this.type = type; + } + + @Override + public String getName(ProjectStructure projectStructure) { + return "get" + Utilities.camelCase(type.getName()) + "EnumAsShort"; + } + + @Override + public String getContent(ProjectStructure projectStructure) { + //get enum switch array + String enumSwitchArray = ""; + int i = 0; + for(String value : this.type.getValues()){ + enumSwitchArray = enumSwitchArray + " case " + value + ":\n"; + enumSwitchArray = enumSwitchArray + " return " + i + ";\n"; + i++; + } + //chop off last newline + enumSwitchArray = enumSwitchArray.substring(0, enumSwitchArray.length() - 1); + String rVal = TemplateInjectionUtils.getFragmentWithReplacement("/enum/EnumToShort.java", this.type.getName(), this.type.getName(), enumSwitchArray); + return rVal; + } + + @Override + public List getImports(ProjectStructure projectStructure) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getImports'"); + } + + @Override + public boolean shouldOverwrite(){ + return true; + } + +} diff --git a/src/main/java/electrosphere/main/core/btree/methods/ServerAttach.java b/src/main/java/electrosphere/main/core/btree/methods/ServerAttach.java new file mode 100644 index 0000000..6cabb1c --- /dev/null +++ b/src/main/java/electrosphere/main/core/btree/methods/ServerAttach.java @@ -0,0 +1,54 @@ +package electrosphere.main.core.btree.methods; + +import java.util.List; + +import electrosphere.main.project.ProjectStructure; +import electrosphere.main.source.VirtualMethod; +import electrosphere.main.util.TemplateInjectionUtils; + +/** + * Attach method for server btree + */ +public class ServerAttach implements VirtualMethod { + + //The name of the btree + String name; + + //The classname of the btree + String className; + + /** + * Constructor + * @param name + * @param className + */ + public ServerAttach(String name, String className){ + this.name = name; + this.className = className; + } + + @Override + public String getName(ProjectStructure projectStructure) { + String rVal = ""; + rVal = "attachTree"; + return rVal; + } + + @Override + public String getContent(ProjectStructure projectStructure) { + String rVal = TemplateInjectionUtils.getFragmentWithReplacement("/server/AttachBTree.java", this.name.toUpperCase(), "TREE_" + this.name.toUpperCase(), className); + return rVal; + } + + @Override + public List getImports(ProjectStructure projectStructure) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getImports'"); + } + + @Override + public boolean shouldOverwrite(){ + return false; + } + +} diff --git a/src/main/java/electrosphere/main/core/btree/methods/ServerDetach.java b/src/main/java/electrosphere/main/core/btree/methods/ServerDetach.java new file mode 100644 index 0000000..e060620 --- /dev/null +++ b/src/main/java/electrosphere/main/core/btree/methods/ServerDetach.java @@ -0,0 +1,54 @@ +package electrosphere.main.core.btree.methods; + +import java.util.List; + +import electrosphere.main.project.ProjectStructure; +import electrosphere.main.source.VirtualMethod; +import electrosphere.main.util.TemplateInjectionUtils; + +/** + * Server detach method + */ +public class ServerDetach implements VirtualMethod { + + //The name of the btree + String name; + + //The classname of the btree + String className; + + /** + * Constructor + * @param name + * @param className + */ + public ServerDetach(String name, String className){ + this.name = name; + this.className = className; + } + + @Override + public String getName(ProjectStructure projectStructure) { + String rVal = ""; + rVal = "detachTree"; + return rVal; + } + + @Override + public String getContent(ProjectStructure projectStructure) { + String rVal = TemplateInjectionUtils.getFragmentWithReplacement("/server/DetachBTree.java", this.name.toUpperCase()); + return rVal; + } + + @Override + public List getImports(ProjectStructure projectStructure) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getImports'"); + } + + @Override + public boolean shouldOverwrite(){ + return true; + } + +} diff --git a/src/main/java/electrosphere/main/core/btree/methods/ServerFieldGetter.java b/src/main/java/electrosphere/main/core/btree/methods/ServerFieldGetter.java new file mode 100644 index 0000000..26eb1f2 --- /dev/null +++ b/src/main/java/electrosphere/main/core/btree/methods/ServerFieldGetter.java @@ -0,0 +1,59 @@ +package electrosphere.main.core.btree.methods; + +import java.util.List; + +import electrosphere.main.core.btree.BehaviorTree; +import electrosphere.main.project.ProjectStructure; +import electrosphere.main.source.VirtualMethod; +import electrosphere.main.util.TemplateInjectionUtils; +import electrosphere.main.util.Utilities; + +/** + * A server getter for a field + */ +public class ServerFieldGetter implements VirtualMethod { + //the name of the field + String fieldName; + + //the name of the type of the field + String typeName; + + //The parent behavior tree + BehaviorTree parent; + + /** + * Constructor + * @param tree The parent behavior tree + * @param typeName The name of the type of the field + * @param fieldName The name of the field + */ + public ServerFieldGetter(BehaviorTree tree, String typeName, String fieldName){ + this.parent = tree; + this.typeName = typeName; + this.fieldName = fieldName; + } + + @Override + public String getName(ProjectStructure projectStructure) { + String rVal = ""; + rVal = "get" + Utilities.camelCase(fieldName); + return rVal; + } + + @Override + public String getContent(ProjectStructure projectStructure) { + String rVal = TemplateInjectionUtils.getFragmentWithReplacement("/server/Getter.java", fieldName, Utilities.camelCase(fieldName), typeName); + return rVal; + } + + @Override + public List getImports(ProjectStructure projectStructure) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getImports'"); + } + + @Override + public boolean shouldOverwrite(){ + return true; + } +} diff --git a/src/main/java/electrosphere/main/core/btree/methods/ServerFieldSetter.java b/src/main/java/electrosphere/main/core/btree/methods/ServerFieldSetter.java new file mode 100644 index 0000000..3c930b3 --- /dev/null +++ b/src/main/java/electrosphere/main/core/btree/methods/ServerFieldSetter.java @@ -0,0 +1,91 @@ +package electrosphere.main.core.btree.methods; + +import java.util.List; + +import electrosphere.main.core.btree.BehaviorTree; +import electrosphere.main.core.syncfield.SynchronizedType; +import electrosphere.main.project.ProjectStructure; +import electrosphere.main.source.VirtualMethod; +import electrosphere.main.util.TemplateInjectionUtils; +import electrosphere.main.util.Utilities; + +/** + * A server setter + */ +public class ServerFieldSetter implements VirtualMethod { + + //the name of the field + String fieldName; + + //the name of the type of the field + String typeName; + + //The parent behavior tree + BehaviorTree parent; + + /** + * Constructor + * @param tree The parent behavior tree + * @param typeName The name of the type of the field + * @param fieldName The name of the field + */ + public ServerFieldSetter(BehaviorTree tree, String typeName, String fieldName){ + this.parent = tree; + this.typeName = typeName; + this.fieldName = fieldName; + } + + @Override + public String getName(ProjectStructure projectStructure){ + return "set" + Utilities.camelCase(fieldName); + } + + @Override + public String getContent(ProjectStructure projectStructure){ + String rVal = null; + String bTreeIdVariable = "BehaviorTreeIdEnums.BTREE_" + this.parent.getName().toUpperCase() + "_ID"; + String fieldIdVariable = "FieldIdEnums.TREE_" + this.parent.getName().toUpperCase() + "_SYNCEDFIELD_" + this.fieldName.toUpperCase() + "_ID"; + switch(typeName){ + case "long": { + String packetContentFiller = " DataCellSearchUtils.getEntityDataCell(parent).broadcastNetworkMessage(SynchronizationMessage.constructUpdateClientLongStateMessage(parent.getId(), " + bTreeIdVariable + ", " + fieldIdVariable + ", " + fieldName + "));"; + rVal = TemplateInjectionUtils.getFragmentWithReplacement("/server/Setter.java", fieldName, Utilities.camelCase(fieldName), typeName, packetContentFiller); + } break; + case "float": { + String packetContentFiller = " DataCellSearchUtils.getEntityDataCell(parent).broadcastNetworkMessage(SynchronizationMessage.constructUpdateClientFloatStateMessage(parent.getId(), " + bTreeIdVariable + ", " + fieldIdVariable + ", " + fieldName + "));"; + rVal = TemplateInjectionUtils.getFragmentWithReplacement("/server/Setter.java", fieldName, Utilities.camelCase(fieldName), typeName, packetContentFiller); + } break; + case "double": + { + String packetContentFiller = " DataCellSearchUtils.getEntityDataCell(parent).broadcastNetworkMessage(SynchronizationMessage.constructUpdateClientDoubleStateMessage(parent.getId(), " + bTreeIdVariable + ", " + fieldIdVariable + ", " + fieldName + "));"; + rVal = TemplateInjectionUtils.getFragmentWithReplacement("/server/Setter.java", fieldName, Utilities.camelCase(fieldName), typeName, packetContentFiller); + } break; + case "int": { + String packetContentFiller = " DataCellSearchUtils.getEntityDataCell(parent).broadcastNetworkMessage(SynchronizationMessage.constructUpdateClientIntStateMessage(parent.getId(), " + bTreeIdVariable + ", " + fieldIdVariable + ", " + fieldName + "));"; + rVal = TemplateInjectionUtils.getFragmentWithReplacement("/server/Setter.java", fieldName, Utilities.camelCase(fieldName), typeName, packetContentFiller); + } break; + case "String": { + String packetContentFiller = " DataCellSearchUtils.getEntityDataCell(parent).broadcastNetworkMessage(SynchronizationMessage.constructUpdateClientStringStateMessage(parent.getId(), " + bTreeIdVariable + ", " + fieldIdVariable + ", " + fieldName + "));"; + rVal = TemplateInjectionUtils.getFragmentWithReplacement("/server/Setter.java", fieldName, Utilities.camelCase(fieldName), typeName, packetContentFiller); + } break; + default: { + SynchronizedType type = projectStructure.getType(typeName); + String packetContentFiller = " int value = " + type.getContainingClassName() + "." + type.getToShortConversionMethodName() + "(" + fieldName + ");\n"; + packetContentFiller = packetContentFiller + " DataCellSearchUtils.getEntityDataCell(parent).broadcastNetworkMessage(SynchronizationMessage.constructUpdateClientStateMessage(parent.getId(), " + bTreeIdVariable + ", " + fieldIdVariable + ", value));"; + rVal = TemplateInjectionUtils.getFragmentWithReplacement("/server/Setter.java", fieldName, Utilities.camelCase(fieldName), typeName, packetContentFiller); + } break; + } + return rVal; + } + + @Override + public List getImports(ProjectStructure projectStructure) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getImports'"); + } + + @Override + public boolean shouldOverwrite(){ + return true; + } + +} diff --git a/src/main/java/electrosphere/main/core/btree/methods/ToTypeConversion.java b/src/main/java/electrosphere/main/core/btree/methods/ToTypeConversion.java new file mode 100644 index 0000000..1d76c2a --- /dev/null +++ b/src/main/java/electrosphere/main/core/btree/methods/ToTypeConversion.java @@ -0,0 +1,62 @@ +package electrosphere.main.core.btree.methods; + +import java.util.List; + +import electrosphere.main.core.syncfield.SynchronizedType; +import electrosphere.main.project.ProjectStructure; +import electrosphere.main.source.VirtualMethod; +import electrosphere.main.util.TemplateInjectionUtils; +import electrosphere.main.util.Utilities; + +/** + * Converts a short to the specified synchronized type + */ +public class ToTypeConversion implements VirtualMethod { + + //The synchronized type we're converting in this function + SynchronizedType type; + + /** + * Constructor + * @param type The synchronized type + */ + public ToTypeConversion(SynchronizedType type){ + this.type = type; + } + + @Override + public String getName(ProjectStructure projectStructure) { + return "get" + Utilities.camelCase(type.getName()) + "ShortAsEnum"; + } + + @Override + public String getContent(ProjectStructure projectStructure) { + //get enum switch array + String enumSwitchArray = ""; + int i = 0; + for(String value : type.getValues()){ + enumSwitchArray = enumSwitchArray + " case " + i + ":\n"; + enumSwitchArray = enumSwitchArray + " return " + this.type.getName() + "." + value + ";\n"; + i++; + } + //add default case + enumSwitchArray = enumSwitchArray + " default:\n"; + enumSwitchArray = enumSwitchArray + " return " + this.type.getName() + "." + type.getValues().get(0) + ";\n"; + //chop off last newline + enumSwitchArray = enumSwitchArray.substring(0, enumSwitchArray.length() - 1); + String rVal = TemplateInjectionUtils.getFragmentWithReplacement("/enum/ShortToEnum.java", this.type.getName(), this.type.getName(), enumSwitchArray); + return rVal; + } + + @Override + public List getImports(ProjectStructure projectStructure) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getImports'"); + } + + @Override + public boolean shouldOverwrite(){ + return true; + } + +} diff --git a/src/main/java/electrosphere/main/core/syncfield/SynchronizedField.java b/src/main/java/electrosphere/main/core/syncfield/SynchronizedField.java index 9236235..f2bf089 100644 --- a/src/main/java/electrosphere/main/core/syncfield/SynchronizedField.java +++ b/src/main/java/electrosphere/main/core/syncfield/SynchronizedField.java @@ -1,9 +1,7 @@ package electrosphere.main.core.syncfield; import electrosphere.main.core.btree.BehaviorTree; -import electrosphere.main.project.ProjectStructure; import electrosphere.main.targets.TargetFile; -import electrosphere.main.util.TemplateInjectionUtils; import electrosphere.main.util.Utilities; /** @@ -41,22 +39,20 @@ public class SynchronizedField { this.targetFile = targetFile; } + /** + * Gets the name of the getter method for this field + * @return The name of the getter method + */ public String getGetterName(){ String rVal = ""; rVal = "get" + Utilities.camelCase(fieldName); return rVal; } - public String getServerGetterContent(){ - String rVal = TemplateInjectionUtils.getFragmentWithReplacement("/server/Getter.java", fieldName, Utilities.camelCase(fieldName), typeName); - return rVal; - } - - public String getClientGetterContent(){ - String rVal = TemplateInjectionUtils.getFragmentWithReplacement("/client/Getter.java", fieldName, Utilities.camelCase(fieldName), typeName); - return rVal; - } - + /** + * Gets the name of the setter method for this field + * @return The name of the setter method + */ public String getSetterName(){ String rVal = ""; rVal = "set" + Utilities.camelCase(fieldName); @@ -71,47 +67,6 @@ public class SynchronizedField { this.parent = tree; } - public String getServerSetterContent(ProjectStructure structure){ - String rVal = null; - String bTreeIdVariable = "BehaviorTreeIdEnums.BTREE_" + this.parent.getName().toUpperCase() + "_ID"; - String fieldIdVariable = "FieldIdEnums.TREE_" + this.parent.getName().toUpperCase() + "_SYNCEDFIELD_" + this.fieldName.toUpperCase() + "_ID"; - switch(typeName){ - case "long": { - String packetContentFiller = " DataCellSearchUtils.getEntityDataCell(parent).broadcastNetworkMessage(SynchronizationMessage.constructUpdateClientLongStateMessage(parent.getId(), " + bTreeIdVariable + ", " + fieldIdVariable + ", " + fieldName + "));"; - rVal = TemplateInjectionUtils.getFragmentWithReplacement("/server/Setter.java", fieldName, Utilities.camelCase(fieldName), typeName, packetContentFiller); - } break; - case "float": { - String packetContentFiller = " DataCellSearchUtils.getEntityDataCell(parent).broadcastNetworkMessage(SynchronizationMessage.constructUpdateClientFloatStateMessage(parent.getId(), " + bTreeIdVariable + ", " + fieldIdVariable + ", " + fieldName + "));"; - rVal = TemplateInjectionUtils.getFragmentWithReplacement("/server/Setter.java", fieldName, Utilities.camelCase(fieldName), typeName, packetContentFiller); - } break; - case "double": - { - String packetContentFiller = " DataCellSearchUtils.getEntityDataCell(parent).broadcastNetworkMessage(SynchronizationMessage.constructUpdateClientDoubleStateMessage(parent.getId(), " + bTreeIdVariable + ", " + fieldIdVariable + ", " + fieldName + "));"; - rVal = TemplateInjectionUtils.getFragmentWithReplacement("/server/Setter.java", fieldName, Utilities.camelCase(fieldName), typeName, packetContentFiller); - } break; - case "int": { - String packetContentFiller = " DataCellSearchUtils.getEntityDataCell(parent).broadcastNetworkMessage(SynchronizationMessage.constructUpdateClientIntStateMessage(parent.getId(), " + bTreeIdVariable + ", " + fieldIdVariable + ", " + fieldName + "));"; - rVal = TemplateInjectionUtils.getFragmentWithReplacement("/server/Setter.java", fieldName, Utilities.camelCase(fieldName), typeName, packetContentFiller); - } break; - case "String": { - String packetContentFiller = " DataCellSearchUtils.getEntityDataCell(parent).broadcastNetworkMessage(SynchronizationMessage.constructUpdateClientStringStateMessage(parent.getId(), " + bTreeIdVariable + ", " + fieldIdVariable + ", " + fieldName + "));"; - rVal = TemplateInjectionUtils.getFragmentWithReplacement("/server/Setter.java", fieldName, Utilities.camelCase(fieldName), typeName, packetContentFiller); - } break; - default: { - SynchronizedType type = structure.getType(typeName); - String packetContentFiller = " int value = " + type.getContainingClassName() + "." + type.getToShortConversionMethodName() + "(" + fieldName + ");\n"; - packetContentFiller = packetContentFiller + " DataCellSearchUtils.getEntityDataCell(parent).broadcastNetworkMessage(SynchronizationMessage.constructUpdateClientStateMessage(parent.getId(), " + bTreeIdVariable + ", " + fieldIdVariable + ", value));"; - rVal = TemplateInjectionUtils.getFragmentWithReplacement("/server/Setter.java", fieldName, Utilities.camelCase(fieldName), typeName, packetContentFiller); - } break; - } - return rVal; - } - - public String getClientSetterContent(ProjectStructure structure){ - String rVal = TemplateInjectionUtils.getFragmentWithReplacement("/client/Setter.java", fieldName, Utilities.camelCase(fieldName), typeName); - return rVal; - } - /** * Gets the id of this field * @return The id diff --git a/src/main/java/electrosphere/main/core/syncfield/SynchronizedType.java b/src/main/java/electrosphere/main/core/syncfield/SynchronizedType.java index 535674a..79b7e0b 100644 --- a/src/main/java/electrosphere/main/core/syncfield/SynchronizedType.java +++ b/src/main/java/electrosphere/main/core/syncfield/SynchronizedType.java @@ -123,4 +123,12 @@ public class SynchronizedType { return this.name; } + /** + * Gets the available values + * @return The available values + */ + public List getValues(){ + return this.values; + } + } diff --git a/src/main/java/electrosphere/main/project/ProjectStructure.java b/src/main/java/electrosphere/main/project/ProjectStructure.java index 364c1ca..f3f93fa 100644 --- a/src/main/java/electrosphere/main/project/ProjectStructure.java +++ b/src/main/java/electrosphere/main/project/ProjectStructure.java @@ -57,6 +57,7 @@ public class ProjectStructure { * Main code generation routine */ public void generate(){ + this.virtualProject.generate(this); //generate in-class functions for btrees for(BehaviorTree tree : this.getBehaviorTrees()){ if(Main.LOG){ @@ -67,13 +68,13 @@ public class ProjectStructure { - for(SynchronizedField field : tree.getSynchronizedFields()){ - ClassSourceUtils.addOrReplaceMethod(this, tree.getTargetFile(), field.getGetterName(), field.getServerGetterContent()); - ClassSourceUtils.addOrReplaceMethod(this, tree.getTargetFile(), field.getSetterName(), field.getServerSetterContent(this)); - } + // for(SynchronizedField field : tree.getSynchronizedFields()){ + // // ClassSourceUtils.addOrReplaceMethod(this, tree.getTargetFile(), field.getGetterName(), field.getServerGetterContent()); + // // ClassSourceUtils.addOrReplaceMethod(this, tree.getTargetFile(), field.getSetterName(), field.getServerSetterContent(this)); + // } //attach + detatch methods - ClassSourceUtils.addMethodIfAbsent(this, tree.getTargetFile(), tree.getAttachMethodName(), tree.getAttachMethodContent()); - ClassSourceUtils.addOrReplaceMethod(this, tree.getTargetFile(), tree.getDetachMethodName(), tree.getDetachMethodContent()); + // ClassSourceUtils.addMethodIfAbsent(this, tree.getTargetFile(), tree.getAttachMethodName(), tree.getAttachMethodContent()); + // ClassSourceUtils.addOrReplaceMethod(this, tree.getTargetFile(), tree.getDetachMethodName(), tree.getDetachMethodContent()); //imports ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.engine.Globals"); ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.server.datacell.utils.DataCellSearchUtils"); @@ -88,13 +89,13 @@ public class ProjectStructure { - for(SynchronizedField field : tree.getSynchronizedFields()){ - ClassSourceUtils.addOrReplaceMethod(this, tree.getTargetFile(), field.getGetterName(), field.getClientGetterContent()); - ClassSourceUtils.addOrReplaceMethod(this, tree.getTargetFile(), field.getSetterName(), field.getClientSetterContent(this)); - } + // for(SynchronizedField field : tree.getSynchronizedFields()){ + // // ClassSourceUtils.addOrReplaceMethod(this, tree.getTargetFile(), field.getGetterName(), field.getClientGetterContent()); + // // ClassSourceUtils.addOrReplaceMethod(this, tree.getTargetFile(), field.getSetterName(), field.getClientSetterContent(this)); + // } //attach + detatch methods - ClassSourceUtils.addMethodIfAbsent(this, tree.getTargetFile(), tree.getAttachMethodName(), tree.getAttachMethodContent()); - ClassSourceUtils.addOrReplaceMethod(this, tree.getTargetFile(), tree.getDetachMethodName(), tree.getDetachMethodContent()); + // ClassSourceUtils.addMethodIfAbsent(this, tree.getTargetFile(), tree.getAttachMethodName(), tree.getAttachMethodContent()); + // ClassSourceUtils.addOrReplaceMethod(this, tree.getTargetFile(), tree.getDetachMethodName(), tree.getDetachMethodContent()); //imports ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.engine.Globals"); // for client scene wrapper ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.entity.Entity"); @@ -105,22 +106,22 @@ public class ProjectStructure { //private constructor - ClassSourceUtils.addMethodIfAbsent(this, tree.getTargetFile(), tree.getConstructorMethodName(), tree.getConstructorMethodContent()); + // ClassSourceUtils.addMethodIfAbsent(this, tree.getTargetFile(), tree.getConstructorMethodName(), tree.getConstructorMethodContent()); //guarantee entity fetch method - ClassSourceUtils.addOrReplaceMethod(this, tree.getTargetFile(), tree.getEntityFetchMethodName(), tree.getEntityFetchMethodContent()); + // ClassSourceUtils.addOrReplaceMethod(this, tree.getTargetFile(), tree.getEntityFetchMethodName(), tree.getEntityFetchMethodContent()); //guarantee imports of required files (btree enum, etc) ClassSourceUtils.importClass(this, tree.getTargetFile(), this.virtualProject.getBTreeIdEnum().getQualifiedPath(this)); ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.entity.EntityDataStrings"); } //generate enums for all synchronized types - for(SynchronizedType type : this.mainParser.getSynchronizedTypes()){ - ClassSourceUtils.addOrReplaceMethod(this, type.getTargetFile(), type.getToShortConversionMethodName(), type.getToShortConversionMethodContent()); - ClassSourceUtils.addOrReplaceMethod(this, type.getTargetFile(), type.getFromShortConversionMethodName(), type.getFromShortConversionMethodContent()); - } - this.virtualProject.getBTreeIdEnum().generate(this); - this.virtualProject.getFieldIdEnum().generate(this); - //client sync manager - this.virtualProject.getClientSynchronizationManager().generate(this); + // for(SynchronizedType type : this.mainParser.getSynchronizedTypes()){ + // ClassSourceUtils.addOrReplaceMethod(this, type.getTargetFile(), type.getToShortConversionMethodName(), type.getToShortConversionMethodContent()); + // ClassSourceUtils.addOrReplaceMethod(this, type.getTargetFile(), type.getFromShortConversionMethodName(), type.getFromShortConversionMethodContent()); + // } + // this.virtualProject.getBTreeIdEnum().generate(this); + // this.virtualProject.getFieldIdEnum().generate(this); + // //client sync manager + // this.virtualProject.getClientSynchronizationManager().generate(this); } /** diff --git a/src/main/java/electrosphere/main/project/parsers/BTreeParser.java b/src/main/java/electrosphere/main/project/parsers/BTreeParser.java index 1dc3e38..49fccc7 100644 --- a/src/main/java/electrosphere/main/project/parsers/BTreeParser.java +++ b/src/main/java/electrosphere/main/project/parsers/BTreeParser.java @@ -11,6 +11,12 @@ 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.core.btree.methods.ClientAttach; +import electrosphere.main.core.btree.methods.ClientDetach; +import electrosphere.main.core.btree.methods.Constructor; +import electrosphere.main.core.btree.methods.Fetch; +import electrosphere.main.core.btree.methods.ServerAttach; +import electrosphere.main.core.btree.methods.ServerDetach; import electrosphere.main.targets.TargetFile; /** @@ -60,6 +66,22 @@ public class BTreeParser { target ); behaviorTrees.add(rVal); + + // + // + // ADD METHODS HERE + // + // + if(isServer){ + rVal.addMethod(new ServerAttach(bTreeName, target.getSource().getName())); + rVal.addMethod(new ServerDetach(bTreeName, target.getSource().getName())); + } else { + rVal.addMethod(new ClientAttach(bTreeName, target.getSource().getName())); + rVal.addMethod(new ClientDetach(bTreeName, target.getSource().getName())); + } + + rVal.addMethod(new Constructor(bTreeName, target.getSource().getName())); + rVal.addMethod(new Fetch(bTreeName, target.getSource().getName())); } // diff --git a/src/main/java/electrosphere/main/project/parsers/MainParser.java b/src/main/java/electrosphere/main/project/parsers/MainParser.java index 2dfeb12..13338b2 100644 --- a/src/main/java/electrosphere/main/project/parsers/MainParser.java +++ b/src/main/java/electrosphere/main/project/parsers/MainParser.java @@ -4,6 +4,10 @@ import java.util.List; import electrosphere.main.core.VirtualProject; import electrosphere.main.core.btree.BehaviorTree; +import electrosphere.main.core.btree.methods.ClientFieldSetter; +import electrosphere.main.core.btree.methods.FromTypeConversion; +import electrosphere.main.core.btree.methods.ServerFieldSetter; +import electrosphere.main.core.btree.methods.ToTypeConversion; import electrosphere.main.core.syncfield.SynchronizedField; import electrosphere.main.core.syncfield.SynchronizedType; import electrosphere.main.targets.TargetFile; @@ -47,7 +51,7 @@ public class MainParser { BehaviorTree bTree = this.bTreeParser.parse(target); List types = this.enumParser.parse(target, typeIterator); List fields = this.fieldParser.parse(target); - linkData(bTree, fields, types); + linkData(rVal, bTree, fields, types); // //specific cases @@ -63,16 +67,27 @@ public class MainParser { /** * Links all the values gathered from a behavior tree file + * @param rVal The virtual project * @param tree The tree * @param fields The list of fields * @param types The list of types */ - private void linkData(BehaviorTree tree, List fields, List types){ + private void linkData(VirtualProject rVal, BehaviorTree tree, List fields, List types){ if(tree != null){ + rVal.addClass(tree); //link fields for(SynchronizedField field : fields){ field.setParent(tree); tree.addSynchronizedField(field); + if(tree.isServer()){ + tree.addMethod(new ServerFieldSetter(tree, field.getTypeName(), field.getFieldName())); + } else { + tree.addMethod(new ClientFieldSetter(tree, field.getTypeName(), field.getFieldName())); + } + } + for(SynchronizedType type : types){ + tree.addMethod(new ToTypeConversion(type)); + tree.addMethod(new FromTypeConversion(type)); } } } diff --git a/src/main/java/electrosphere/main/source/VirtualClass.java b/src/main/java/electrosphere/main/source/VirtualClass.java index e700669..a669a3b 100644 --- a/src/main/java/electrosphere/main/source/VirtualClass.java +++ b/src/main/java/electrosphere/main/source/VirtualClass.java @@ -1,6 +1,7 @@ package electrosphere.main.source; import java.io.File; +import java.util.LinkedList; import java.util.List; import electrosphere.main.project.ProjectStructure; @@ -23,17 +24,17 @@ public abstract class VirtualClass { /** * All the imports in this virtual class */ - List imports; + List imports = new LinkedList(); /** * All the fields in this class */ - List fields; + List fields = new LinkedList(); /** * All the methods in this class */ - List methods; + List methods = new LinkedList(); /** * Constructor @@ -90,7 +91,20 @@ public abstract class VirtualClass { * @param structure The project */ public void updateFile(ProjectStructure structure){ - throw new UnsupportedOperationException("Not yet implemented!"); + for(VirtualMethod method : this.methods){ + System.out.println("Replace " + method.getName(structure)); + if(method.shouldOverwrite()){ + ClassSourceUtils.addOrReplaceMethod(structure, this.targetFile, method.getName(structure), method.getContent(structure)); + } else { + ClassSourceUtils.addMethodIfAbsent(structure, this.targetFile, method.getName(structure), method.getContent(structure)); + } + } + // for(VirtualField field : this.fields){ + // throw new UnsupportedOperationException("updating fields not supported yet!"); + // } + for(String currentImport : this.imports){ + ClassSourceUtils.importClass(structure, this.targetFile, currentImport); + } } /** @@ -101,5 +115,13 @@ public abstract class VirtualClass { throw new UnsupportedOperationException("Not yet implemented!"); } + /** + * Adds a virtual method + * @param method The virtual method + */ + public void addMethod(VirtualMethod method){ + this.methods.add(method); + } + } diff --git a/src/main/java/electrosphere/main/source/VirtualMethod.java b/src/main/java/electrosphere/main/source/VirtualMethod.java index 323271a..0866409 100644 --- a/src/main/java/electrosphere/main/source/VirtualMethod.java +++ b/src/main/java/electrosphere/main/source/VirtualMethod.java @@ -2,6 +2,8 @@ package electrosphere.main.source; import java.util.List; +import electrosphere.main.project.ProjectStructure; + /** * Represents a method in a class */ @@ -9,21 +11,30 @@ public interface VirtualMethod { /** * Gets the name of the method + * @param projectStructure The project structure * @return The name of the method */ - public String getName(); + public String getName(ProjectStructure projectStructure); /** * Gets the content of this method + * @param projectStructure The project structure * @return The content */ - public String getContent(); + public String getContent(ProjectStructure projectStructure); /** * Gets the list of imports required for this method + * @param projectStructure The project structure * @return The list of imports */ - public List getImports(); + public List getImports(ProjectStructure projectStructure); + + /** + * Gets whether the method should be overwritten, or just initially populated + * @return true if should be overwritten, false if should just be initially populated + */ + public boolean shouldOverwrite(); }