From e66b36c305750842e95375ebd2aa86ff2dd1bf2c Mon Sep 17 00:00:00 2001 From: austin Date: Wed, 31 Jul 2024 18:16:52 -0400 Subject: [PATCH] support server synchronization manager --- src/main/java/electrosphere/main/Main.java | 1 + .../main/core/VirtualProject.java | 9 +++ .../main/core/btree/BehaviorTree.java | 23 +++++- .../main/project/parsers/BTreeParser.java | 4 + .../main/project/parsers/MainParser.java | 5 ++ .../ServerSynchronizationManager.java | 35 +++++++++ .../methods/UpdateEntityState.java | 74 +++++++++++++++++++ src/main/resources/client/InterruptBTree.java | 2 +- src/main/resources/client/StartBTree.java | 2 +- .../resources/server/UpdateEntityState.java | 14 ++++ 10 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 src/main/java/electrosphere/main/server/syncmanager/ServerSynchronizationManager.java create mode 100644 src/main/java/electrosphere/main/server/syncmanager/methods/UpdateEntityState.java create mode 100644 src/main/resources/server/UpdateEntityState.java diff --git a/src/main/java/electrosphere/main/Main.java b/src/main/java/electrosphere/main/Main.java index 6fe4155..7ff2bfb 100644 --- a/src/main/java/electrosphere/main/Main.java +++ b/src/main/java/electrosphere/main/Main.java @@ -34,6 +34,7 @@ public class Main { //hard-coded white list files to always parse static final String[] ALWAYS_PARSED_CLASSES = new String[]{ "ClientSynchronizationManager", + "ServerSynchronizationManager", "BehaviorTreeIdEnums", "FieldIdEnums", }; diff --git a/src/main/java/electrosphere/main/core/VirtualProject.java b/src/main/java/electrosphere/main/core/VirtualProject.java index ab53240..8374f34 100644 --- a/src/main/java/electrosphere/main/core/VirtualProject.java +++ b/src/main/java/electrosphere/main/core/VirtualProject.java @@ -7,6 +7,7 @@ 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; +import electrosphere.main.server.syncmanager.ServerSynchronizationManager; import electrosphere.main.source.VirtualClass; import electrosphere.main.targets.TargetFile; @@ -71,6 +72,14 @@ public class VirtualProject { return this.clientSynchronizationManager; } + /** + * Creates a server synchronization manager file + * @param file The target file + */ + public void createServerSynchronizationManager(TargetFile file){ + this.classes.add(new ServerSynchronizationManager(file)); + } + /** * Adds a virtual class to the project * @param virtualClass The virtual class diff --git a/src/main/java/electrosphere/main/core/btree/BehaviorTree.java b/src/main/java/electrosphere/main/core/btree/BehaviorTree.java index e8941df..4c5eaf5 100644 --- a/src/main/java/electrosphere/main/core/btree/BehaviorTree.java +++ b/src/main/java/electrosphere/main/core/btree/BehaviorTree.java @@ -1,7 +1,10 @@ package electrosphere.main.core.btree; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; +import java.util.Set; import electrosphere.main.core.syncfield.SynchronizedField; import electrosphere.main.source.VirtualClass; @@ -33,6 +36,9 @@ public class BehaviorTree extends VirtualClass { //The file this type appears in TargetFile targetFile; + //The annotations that were applied to this tree + Map annotations = new HashMap(); + /** * Constructor * @param id @@ -160,9 +166,22 @@ public class BehaviorTree extends VirtualClass { } + /** + * Adds an annotation to the btree + * @param annotationName The name of the annotation + * @param value The value of the annotation + */ + public void addAnnotation(String annotationName, Object value){ + this.annotations.put(annotationName, value); + } - - + /** + * Gets the set of annotations applied to this tree + * @return The set of annotations + */ + public Set getAnnotations(){ + return this.annotations.keySet(); + } diff --git a/src/main/java/electrosphere/main/project/parsers/BTreeParser.java b/src/main/java/electrosphere/main/project/parsers/BTreeParser.java index 1e9d8cb..a9887cb 100644 --- a/src/main/java/electrosphere/main/project/parsers/BTreeParser.java +++ b/src/main/java/electrosphere/main/project/parsers/BTreeParser.java @@ -67,6 +67,9 @@ public class BTreeParser { isServer, target ); + rVal.addAnnotation("name", bTreeName); + rVal.addAnnotation("correspondingTree", bTreeCorrespondingName); + rVal.addAnnotation("isServer", isServer); behaviorTrees.add(rVal); // @@ -117,6 +120,7 @@ public class BTreeParser { TargetFile targetFile = tree.getTargetFile(); if(mainAnnotation.getStringValue("genStartInt") != null){ boolean genStartInt = Boolean.parseBoolean(mainAnnotation.getStringValue("genStartInt")); + tree.addAnnotation("genStartInt", genStartInt); if(genStartInt){ tree.addMethod(new ClientStart(tree.getName(), targetFile.getSource().getName())); tree.addMethod(new ClientInterrupt(tree.getName(), targetFile.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 13338b2..8ee6768 100644 --- a/src/main/java/electrosphere/main/project/parsers/MainParser.java +++ b/src/main/java/electrosphere/main/project/parsers/MainParser.java @@ -60,6 +60,11 @@ public class MainParser { if(target.getName().contains("ClientSynchronizationManager")){ rVal.createClientSynchronizationManager(target); } + + //server synchronization manager + if(target.getName().contains("ServerSynchronizationManager")){ + rVal.createServerSynchronizationManager(target); + } } return rVal; diff --git a/src/main/java/electrosphere/main/server/syncmanager/ServerSynchronizationManager.java b/src/main/java/electrosphere/main/server/syncmanager/ServerSynchronizationManager.java new file mode 100644 index 0000000..d0481f6 --- /dev/null +++ b/src/main/java/electrosphere/main/server/syncmanager/ServerSynchronizationManager.java @@ -0,0 +1,35 @@ +package electrosphere.main.server.syncmanager; + +import electrosphere.main.server.syncmanager.methods.UpdateEntityState; +import electrosphere.main.source.VirtualClass; +import electrosphere.main.targets.TargetFile; + +/** + * The client synchronization manager class + */ +public class ServerSynchronizationManager extends VirtualClass { + + + //The target file for the current version of this class + TargetFile file; + + /** + * Constructor + */ + public ServerSynchronizationManager(TargetFile file) { + super( + file, + "ServerSynchronizationManager", + true + ); + this.file = file; + + // + // + // ADD METHODS HERE + // + // + this.addMethod(new UpdateEntityState()); + } + +} diff --git a/src/main/java/electrosphere/main/server/syncmanager/methods/UpdateEntityState.java b/src/main/java/electrosphere/main/server/syncmanager/methods/UpdateEntityState.java new file mode 100644 index 0000000..4ea30d0 --- /dev/null +++ b/src/main/java/electrosphere/main/server/syncmanager/methods/UpdateEntityState.java @@ -0,0 +1,74 @@ +package electrosphere.main.server.syncmanager.methods; + +import java.util.LinkedList; +import java.util.List; + +import electrosphere.main.core.btree.BehaviorTree; +import electrosphere.main.core.statics.btreeenum.BTreeIdEnum; +import electrosphere.main.project.ProjectStructure; +import electrosphere.main.source.VirtualMethod; +import electrosphere.main.util.TemplateInjectionUtils; + +/** + * Updates individual entity tree states + */ +public class UpdateEntityState implements VirtualMethod { + + @Override + public String getName(ProjectStructure projectStructure) { + return "updateEntityState"; + } + + @Override + public String getContent(ProjectStructure projectStructure) { + String updateCases = ""; + for(BehaviorTree serverTree : projectStructure.getBehaviorTrees()){ + //counterintuitively, want to only update client for server behavior tree ids + if(serverTree.isServer() && this.shouldIncludeTree(projectStructure, serverTree)){ + BehaviorTree clientEquivalent = projectStructure.getTree(serverTree.getCorrespondingTreeName()); + String serverTreeName = serverTree.getClassName(); + updateCases = updateCases + " case BehaviorTreeIdEnums." + BTreeIdEnum.getTreeIdEnum(clientEquivalent) + ": {\n"; + updateCases = updateCases + " " + serverTreeName + " tree = " + serverTreeName + ".get" + serverTreeName + "(entity);\n"; + updateCases = updateCases + " tree.start();\n"; + updateCases = updateCases + " } break;\n"; + } + } + String fullReplacementText = TemplateInjectionUtils.getFragmentWithReplacement("/server/UpdateEntityState.java", updateCases); + return fullReplacementText; + } + + @Override + public List getImports(ProjectStructure projectStructure) { + List rVal = new LinkedList(); + + //add server trees + for(BehaviorTree bTree : projectStructure.getBehaviorTrees()){ + if(bTree.getName().contains("server") && this.shouldIncludeTree(projectStructure,bTree)){ + rVal.add(bTree.getTargetFile().getQualifiedPath()); + } + } + return rVal; + } + + @Override + public boolean shouldOverwrite() { + return true; + } + + + /** + * Checks if this tree should be included in the synchronization manager + * @param serverTree The server behavior tree + * @return true if should be included, false otherwise + */ + private boolean shouldIncludeTree(ProjectStructure projectStructure, BehaviorTree serverTree){ + BehaviorTree clientEquivalent = projectStructure.getTree(serverTree.getCorrespondingTreeName()); + for(String annotation : clientEquivalent.getAnnotations()){ + if(annotation.contains("genStartInt")){ + return true; + } + } + return false; + } + +} diff --git a/src/main/resources/client/InterruptBTree.java b/src/main/resources/client/InterruptBTree.java index 93f5045..345a3b3 100644 --- a/src/main/resources/client/InterruptBTree.java +++ b/src/main/resources/client/InterruptBTree.java @@ -8,7 +8,7 @@ public void interrupt(){ Globals.clientConnection.queueOutgoingMessage( SynchronizationMessage.constructClientRequestBTreeActionMessage( Globals.clientSceneWrapper.mapClientToServerId(parent.getId()), - BehaviorTreeIdEnums.BTREE_CLIENTGROUNDMOVEMENTTREE_ID, + BehaviorTreeIdEnums.REPLACE_0_ME, ServerSynchronizationManager.SERVER_SYNC_INTERRUPT ) ); diff --git a/src/main/resources/client/StartBTree.java b/src/main/resources/client/StartBTree.java index 6b70ad0..bcf6859 100644 --- a/src/main/resources/client/StartBTree.java +++ b/src/main/resources/client/StartBTree.java @@ -8,7 +8,7 @@ public void start(){ Globals.clientConnection.queueOutgoingMessage( SynchronizationMessage.constructClientRequestBTreeActionMessage( Globals.clientSceneWrapper.mapClientToServerId(parent.getId()), - BehaviorTreeIdEnums.BTREE_CLIENTGROUNDMOVEMENTTREE_ID, + BehaviorTreeIdEnums.REPLACE_0_ME, ServerSynchronizationManager.SERVER_SYNC_START ) ); diff --git a/src/main/resources/server/UpdateEntityState.java b/src/main/resources/server/UpdateEntityState.java new file mode 100644 index 0000000..bc337c3 --- /dev/null +++ b/src/main/resources/server/UpdateEntityState.java @@ -0,0 +1,14 @@ +/** + *

Automatically generated

+ *

+ * Performs actions requested by the client + *

+ * @param entity The entity + * @param bTreeId The id of the behavior tree + * @param message The raw synchronization message holding the update data + */ +private void updateEntityState(Entity entity, int bTreeId, SynchronizationMessage message){ + switch(bTreeId){ +REPLACE_0_ME + } +} \ No newline at end of file