From 6390c0b0c22f7bee40885695f1316e17d2339adb Mon Sep 17 00:00:00 2001 From: austin Date: Wed, 31 Jul 2024 16:17:07 -0400 Subject: [PATCH] client sync manager update --- .../ClientSynchronizationManager.java | 35 ++++++ .../methods/UpdateEntityState.java} | 62 +++++----- .../main/core/VirtualProject.java | 2 +- .../main/core/btree/BehaviorTree.java | 19 --- .../main/core/syncfield/SynchronizedType.java | 42 ------- .../main/project/ProjectStructure.java | 110 +++++++++--------- .../main/source/VirtualClass.java | 2 +- 7 files changed, 119 insertions(+), 153 deletions(-) create mode 100644 src/main/java/electrosphere/main/client/syncmanager/ClientSynchronizationManager.java rename src/main/java/electrosphere/main/client/{ClientSynchronizationManager.java => syncmanager/methods/UpdateEntityState.java} (75%) diff --git a/src/main/java/electrosphere/main/client/syncmanager/ClientSynchronizationManager.java b/src/main/java/electrosphere/main/client/syncmanager/ClientSynchronizationManager.java new file mode 100644 index 0000000..b931757 --- /dev/null +++ b/src/main/java/electrosphere/main/client/syncmanager/ClientSynchronizationManager.java @@ -0,0 +1,35 @@ +package electrosphere.main.client.syncmanager; + +import electrosphere.main.client.syncmanager.methods.UpdateEntityState; +import electrosphere.main.source.VirtualClass; +import electrosphere.main.targets.TargetFile; + +/** + * The client synchronization manager class + */ +public class ClientSynchronizationManager extends VirtualClass { + + + //The target file for the current version of this class + TargetFile file; + + /** + * Constructor + */ + public ClientSynchronizationManager(TargetFile file) { + super( + file, + "ClientSynchronizationManager", + true + ); + this.file = file; + + // + // + // ADD METHODS HERE + // + // + this.addMethod(new UpdateEntityState()); + } + +} diff --git a/src/main/java/electrosphere/main/client/ClientSynchronizationManager.java b/src/main/java/electrosphere/main/client/syncmanager/methods/UpdateEntityState.java similarity index 75% rename from src/main/java/electrosphere/main/client/ClientSynchronizationManager.java rename to src/main/java/electrosphere/main/client/syncmanager/methods/UpdateEntityState.java index 42625d6..e06a55f 100644 --- a/src/main/java/electrosphere/main/client/ClientSynchronizationManager.java +++ b/src/main/java/electrosphere/main/client/syncmanager/methods/UpdateEntityState.java @@ -1,48 +1,32 @@ -package electrosphere.main.client; +package electrosphere.main.client.syncmanager.methods; + +import java.util.List; import electrosphere.main.core.btree.BehaviorTree; import electrosphere.main.core.statics.btreeenum.BTreeIdEnum; import electrosphere.main.core.syncfield.SynchronizedField; import electrosphere.main.core.syncfield.SynchronizedType; import electrosphere.main.project.ProjectStructure; -import electrosphere.main.source.VirtualClass; -import electrosphere.main.targets.TargetFile; -import electrosphere.main.util.ClassSourceUtils; +import electrosphere.main.source.VirtualMethod; import electrosphere.main.util.TemplateInjectionUtils; /** - * The client synchronization manager class + * Updates individual entity tree states */ -public class ClientSynchronizationManager extends VirtualClass { - +public class UpdateEntityState implements VirtualMethod { - //The target file for the current version of this class - TargetFile file; - - /** - * Constructor - */ - public ClientSynchronizationManager(TargetFile file) { - super( - file, - "ClientSynchronizationManager", - true - ); - this.file = file; + @Override + public String getName(ProjectStructure projectStructure) { + return "updateEntityState"; } - - /** - * Updates the ClientSynchronizationManager with latest data in specific functions - * @param structure The project texture - */ @Override - public void updateFile(ProjectStructure structure){ + public String getContent(ProjectStructure projectStructure) { String updateCases = ""; - for(BehaviorTree serverTree : structure.getBehaviorTrees()){ + for(BehaviorTree serverTree : projectStructure.getBehaviorTrees()){ //counterintuitively, want to only update client for server behavior tree ids if(serverTree.isServer()){ - BehaviorTree clientEquivalent = structure.getTree(serverTree.getCorrespondingTreeName()); + BehaviorTree clientEquivalent = projectStructure.getTree(serverTree.getCorrespondingTreeName()); updateCases = updateCases + " case BehaviorTreeIdEnums." + BTreeIdEnum.getTreeIdEnum(serverTree) + ": {\n"; updateCases = updateCases + " switch(message.getfieldId()){\n"; for(SynchronizedField field : serverTree.getSynchronizedFields()){ @@ -80,7 +64,7 @@ public class ClientSynchronizationManager extends VirtualClass { updateCases = updateCases + " } break;\n"; } break; default: { - SynchronizedType type = structure.getType(field.getTypeName()); + SynchronizedType type = projectStructure.getType(field.getTypeName()); String typeClass = type.getTargetFile().getSource().getName(); updateCases = updateCases + " case FieldIdEnums." + fieldIdVariable + ":{\n"; updateCases = updateCases + " " + treeName + " tree = " + treeName + ".get" + treeName + "(entity);\n"; @@ -92,13 +76,23 @@ public class ClientSynchronizationManager extends VirtualClass { updateCases = updateCases + " }\n"; updateCases = updateCases + " } break;\n"; - //guarantee import - // ClassSourceUtils.importClass(structure, clientSynchronizationManager, serverTree.getTargetFile().getQualifiedPath()); - ClassSourceUtils.importClass(structure, this.file, structure.getTree(serverTree.getCorrespondingTreeName()).getTargetFile().getQualifiedPath()); + // //guarantee import + // ClassSourceUtils.importClass(structure, this.file, structure.getTree(serverTree.getCorrespondingTreeName()).getTargetFile().getQualifiedPath()); } } - ClassSourceUtils.importClass(structure, this.file, "electrosphere.net.synchronization.FieldIdEnums"); String fullReplacementText = TemplateInjectionUtils.getFragmentWithReplacement("/client/UpdateEntityState.java", updateCases); - ClassSourceUtils.addOrReplaceMethod(structure, this.file, "updateEntityState", fullReplacementText); + return fullReplacementText; } + + @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/VirtualProject.java b/src/main/java/electrosphere/main/core/VirtualProject.java index 0f2404a..ab53240 100644 --- a/src/main/java/electrosphere/main/core/VirtualProject.java +++ b/src/main/java/electrosphere/main/core/VirtualProject.java @@ -3,7 +3,7 @@ package electrosphere.main.core; import java.util.LinkedList; import java.util.List; -import electrosphere.main.client.ClientSynchronizationManager; +import electrosphere.main.client.syncmanager.ClientSynchronizationManager; import electrosphere.main.core.statics.btreeenum.BTreeIdEnum; import electrosphere.main.core.statics.fieldenum.FieldIdEnum; import electrosphere.main.project.ProjectStructure; diff --git a/src/main/java/electrosphere/main/core/btree/BehaviorTree.java b/src/main/java/electrosphere/main/core/btree/BehaviorTree.java index 90301e2..e8941df 100644 --- a/src/main/java/electrosphere/main/core/btree/BehaviorTree.java +++ b/src/main/java/electrosphere/main/core/btree/BehaviorTree.java @@ -6,7 +6,6 @@ import java.util.List; import electrosphere.main.core.syncfield.SynchronizedField; import electrosphere.main.source.VirtualClass; import electrosphere.main.targets.TargetFile; -import electrosphere.main.util.TemplateInjectionUtils; /** * Represents a behavior tree in the source code @@ -151,15 +150,6 @@ public class BehaviorTree extends VirtualClass { return rVal; } - /** - * Gets the content for the fetch method - * @return The content - */ - public String getEntityFetchMethodContent(){ - String rVal = TemplateInjectionUtils.getFragmentWithReplacement("/btree/EntityFetchMethod.java", this.className, "TREE_" + this.name.toUpperCase()); - return rVal; - } - /** * Gets the constructor method's name * @return The constructor method's name @@ -169,15 +159,6 @@ public class BehaviorTree extends VirtualClass { return rVal; } - /** - * Gets the constructor method's content - * @return The content - */ - public String getConstructorMethodContent(){ - String rVal = TemplateInjectionUtils.getFragmentWithReplacement("/btree/Constructor.java", this.className); - return rVal; - } - diff --git a/src/main/java/electrosphere/main/core/syncfield/SynchronizedType.java b/src/main/java/electrosphere/main/core/syncfield/SynchronizedType.java index 79b7e0b..33d7621 100644 --- a/src/main/java/electrosphere/main/core/syncfield/SynchronizedType.java +++ b/src/main/java/electrosphere/main/core/syncfield/SynchronizedType.java @@ -3,7 +3,6 @@ package electrosphere.main.core.syncfield; import java.util.List; import electrosphere.main.targets.TargetFile; -import electrosphere.main.util.TemplateInjectionUtils; import electrosphere.main.util.Utilities; /** @@ -58,25 +57,6 @@ public class SynchronizedType { return "get" + Utilities.camelCase(name) + "EnumAsShort"; } - /** - * Gets the content of the function that converts this synchronized type TO a short - * @return The content - */ - public String getToShortConversionMethodContent(){ - //get enum switch array - String enumSwitchArray = ""; - int i = 0; - for(String value : values){ - 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.name, this.name, enumSwitchArray); - return rVal; - } - /** * Gets the name of the function that converts this synchronized type FROM a short * @return The name @@ -85,28 +65,6 @@ public class SynchronizedType { return "get" + Utilities.camelCase(name) + "ShortAsEnum"; } - /** - * Gets the content of the function that converts this synchronized type TO a short - * @return The content - */ - public String getFromShortConversionMethodContent(){ - //get enum switch array - String enumSwitchArray = ""; - int i = 0; - for(String value : values){ - enumSwitchArray = enumSwitchArray + " case " + i + ":\n"; - enumSwitchArray = enumSwitchArray + " return " + this.name + "." + value + ";\n"; - i++; - } - //add default case - enumSwitchArray = enumSwitchArray + " default:\n"; - enumSwitchArray = enumSwitchArray + " return " + this.name + "." + values.get(0) + ";\n"; - //chop off last newline - enumSwitchArray = enumSwitchArray.substring(0, enumSwitchArray.length() - 1); - String rVal = TemplateInjectionUtils.getFragmentWithReplacement("/enum/ShortToEnum.java", this.name, this.name, enumSwitchArray); - return rVal; - } - /** * Gets the name of the class containing the declaration of this type * @return diff --git a/src/main/java/electrosphere/main/project/ProjectStructure.java b/src/main/java/electrosphere/main/project/ProjectStructure.java index f3f93fa..7a11b01 100644 --- a/src/main/java/electrosphere/main/project/ProjectStructure.java +++ b/src/main/java/electrosphere/main/project/ProjectStructure.java @@ -4,14 +4,12 @@ import java.io.File; import java.util.List; -import electrosphere.main.Main; import electrosphere.main.core.VirtualProject; import electrosphere.main.core.btree.BehaviorTree; 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; /** * Container for overall structure of the project @@ -59,69 +57,69 @@ public class ProjectStructure { public void generate(){ this.virtualProject.generate(this); //generate in-class functions for btrees - for(BehaviorTree tree : this.getBehaviorTrees()){ - if(Main.LOG){ - System.out.println("Generating for " + tree.getClassName()); - } - //server side getter + setter - if(tree.isServer()){ + // for(BehaviorTree tree : this.getBehaviorTrees()){ + // if(Main.LOG){ + // System.out.println("Generating for " + tree.getClassName()); + // } + // //server side getter + setter + // if(tree.isServer()){ - // 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()); - //imports - ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.engine.Globals"); - ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.server.datacell.utils.DataCellSearchUtils"); - ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.net.parser.net.message.SynchronizationMessage"); - ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.server.datacell.utils.ServerBehaviorTreeUtils"); - ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.entity.Entity"); - ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.entity.EntityDataStrings"); - ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.entity.btree.BehaviorTree"); - ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.net.synchronization.BehaviorTreeIdEnums"); - ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.net.synchronization.FieldIdEnums"); - } else { + // // 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()); + // //imports + // // ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.engine.Globals"); + // // ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.server.datacell.utils.DataCellSearchUtils"); + // // ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.net.parser.net.message.SynchronizationMessage"); + // // ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.server.datacell.utils.ServerBehaviorTreeUtils"); + // // ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.entity.Entity"); + // // ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.entity.EntityDataStrings"); + // // ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.entity.btree.BehaviorTree"); + // // ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.net.synchronization.BehaviorTreeIdEnums"); + // // ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.net.synchronization.FieldIdEnums"); + // } else { - // 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()); - //imports - ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.engine.Globals"); // for client scene wrapper - ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.entity.Entity"); - ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.entity.EntityDataStrings"); - ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.entity.btree.BehaviorTree"); - ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.net.synchronization.BehaviorTreeIdEnums"); - } + // // 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()); + // //imports + // // ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.engine.Globals"); // for client scene wrapper + // // ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.entity.Entity"); + // // ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.entity.EntityDataStrings"); + // // ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.entity.btree.BehaviorTree"); + // // ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.net.synchronization.BehaviorTreeIdEnums"); + // } - //private constructor - // ClassSourceUtils.addMethodIfAbsent(this, tree.getTargetFile(), tree.getConstructorMethodName(), tree.getConstructorMethodContent()); - //guarantee entity fetch method - // 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()); + // //private constructor + // // ClassSourceUtils.addMethodIfAbsent(this, tree.getTargetFile(), tree.getConstructorMethodName(), tree.getConstructorMethodContent()); + // //guarantee entity fetch method + // // 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"); // } - // this.virtualProject.getBTreeIdEnum().generate(this); - // this.virtualProject.getFieldIdEnum().generate(this); - // //client sync manager - // this.virtualProject.getClientSynchronizationManager().generate(this); + // //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); } /** diff --git a/src/main/java/electrosphere/main/source/VirtualClass.java b/src/main/java/electrosphere/main/source/VirtualClass.java index a669a3b..6710ae4 100644 --- a/src/main/java/electrosphere/main/source/VirtualClass.java +++ b/src/main/java/electrosphere/main/source/VirtualClass.java @@ -79,6 +79,7 @@ public abstract class VirtualClass { * @param structure The project */ public void generate(ProjectStructure structure){ + System.out.println("Generating for " + this.CLASSNAME); if(this.shouldUpdate){ this.updateFile(structure); } else { @@ -92,7 +93,6 @@ public abstract class VirtualClass { */ public void updateFile(ProjectStructure structure){ 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 {