From d2a26dd3fac403fbf34f0851ffbf7b2e9ec189b5 Mon Sep 17 00:00:00 2001 From: austin Date: Sun, 11 May 2025 16:17:01 -0400 Subject: [PATCH] server apply collection state work --- .../main/core/transport/StateCollection.java | 6 +- ...n.java => ClientApplyStateCollection.java} | 6 +- .../methods/ServerApplyStateCollection.java | 124 ++++++++++++++++++ .../ClientApplyStateCollection.java} | 2 +- .../server/ServerApplyStateCollection.java | 15 +++ 5 files changed, 147 insertions(+), 6 deletions(-) rename src/main/java/electrosphere/main/core/transport/methods/{ApplyStateCollection.java => ClientApplyStateCollection.java} (96%) create mode 100644 src/main/java/electrosphere/main/core/transport/methods/ServerApplyStateCollection.java rename src/main/resources/{server/ApplyStateCollection.java => client/ClientApplyStateCollection.java} (78%) create mode 100644 src/main/resources/server/ServerApplyStateCollection.java diff --git a/src/main/java/electrosphere/main/core/transport/StateCollection.java b/src/main/java/electrosphere/main/core/transport/StateCollection.java index 80b2876..d74fbb4 100644 --- a/src/main/java/electrosphere/main/core/transport/StateCollection.java +++ b/src/main/java/electrosphere/main/core/transport/StateCollection.java @@ -1,7 +1,8 @@ package electrosphere.main.core.transport; -import electrosphere.main.core.transport.methods.ApplyStateCollection; +import electrosphere.main.core.transport.methods.ClientApplyStateCollection; import electrosphere.main.core.transport.methods.GetStateCollection; +import electrosphere.main.core.transport.methods.ServerApplyStateCollection; import electrosphere.main.source.VirtualClass; import electrosphere.main.targets.TargetFile; @@ -31,7 +32,8 @@ public class StateCollection extends VirtualClass { // // this.addMethod(new GetStateCollection()); - this.addMethod(new ApplyStateCollection()); + this.addMethod(new ClientApplyStateCollection()); + this.addMethod(new ServerApplyStateCollection()); } } diff --git a/src/main/java/electrosphere/main/core/transport/methods/ApplyStateCollection.java b/src/main/java/electrosphere/main/core/transport/methods/ClientApplyStateCollection.java similarity index 96% rename from src/main/java/electrosphere/main/core/transport/methods/ApplyStateCollection.java rename to src/main/java/electrosphere/main/core/transport/methods/ClientApplyStateCollection.java index 5d071dd..5b13339 100644 --- a/src/main/java/electrosphere/main/core/transport/methods/ApplyStateCollection.java +++ b/src/main/java/electrosphere/main/core/transport/methods/ClientApplyStateCollection.java @@ -11,11 +11,11 @@ import electrosphere.main.source.VirtualMethod; import electrosphere.main.util.TemplateInjectionUtils; import electrosphere.main.util.Utilities; -public class ApplyStateCollection implements VirtualMethod { +public class ClientApplyStateCollection implements VirtualMethod { @Override public String getName(ProjectStructure projectStructure) { - return "applyStateCollection"; + return "clientApplyStateCollection"; } @Override @@ -46,7 +46,7 @@ public class ApplyStateCollection implements VirtualMethod { updateCases = updateCases + " } break;\n"; } } - String fullReplacementText = TemplateInjectionUtils.getFragmentWithReplacement("/server/ApplyStateCollection.java", updateCases); + String fullReplacementText = TemplateInjectionUtils.getFragmentWithReplacement("/client/ClientApplyStateCollection.java", updateCases); return fullReplacementText; } diff --git a/src/main/java/electrosphere/main/core/transport/methods/ServerApplyStateCollection.java b/src/main/java/electrosphere/main/core/transport/methods/ServerApplyStateCollection.java new file mode 100644 index 0000000..08d5c90 --- /dev/null +++ b/src/main/java/electrosphere/main/core/transport/methods/ServerApplyStateCollection.java @@ -0,0 +1,124 @@ +package electrosphere.main.core.transport.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.core.syncfield.SynchronizedField; +import electrosphere.main.project.ProjectStructure; +import electrosphere.main.source.VirtualMethod; +import electrosphere.main.util.TemplateInjectionUtils; +import electrosphere.main.util.Utilities; + +public class ServerApplyStateCollection implements VirtualMethod { + + @Override + public String getName(ProjectStructure projectStructure) { + return "serverApplyStateCollection"; + } + + @Override + public String getContent(ProjectStructure projectStructure) { + String updateCases = ""; + for(BehaviorTree serverTree : projectStructure.getBehaviorTrees()){ + if(serverTree.isServer() && shouldIncludeTree(projectStructure, serverTree)){ + String serverTreeName = serverTree.getClassName(); + BehaviorTree clientEquivalent = projectStructure.getTree(serverTree.getCorrespondingTreeName()); + updateCases = updateCases + " case BehaviorTreeIdEnums." + BTreeIdEnum.getTreeIdEnum(serverTree) + ": {\n"; + updateCases = updateCases + " " + serverTreeName + " tree = " + serverTreeName + ".get" + serverTreeName + "(entity);\n"; + //update each field + updateCases = updateCases + " switch(syncedValue.getFieldId()){\n"; + for(SynchronizedField field : serverTree.getSynchronizedFields()){ + String fieldIdVariable = "TREE_" + serverTree.getName().toUpperCase() + "_SYNCEDFIELD_" + field.getFieldName().toUpperCase() + "_ID"; + updateCases = updateCases + " case(FieldIdEnums." + fieldIdVariable + "): {\n"; + String getFieldValue = ""; + if(field.isSerialized()){ + getFieldValue = getFieldValueSerialized(field, getFieldValue); + } else { + getFieldValue = getFieldValueNonSerialized(field, clientEquivalent, getFieldValue); + } + updateCases = updateCases + " tree." + field.getSetterName() + "(" + getFieldValue + ");\n"; + updateCases = updateCases + " } break;\n"; + } + updateCases = updateCases + " }\n"; + updateCases = updateCases + " } break;\n"; + } + } + String fullReplacementText = TemplateInjectionUtils.getFragmentWithReplacement("/server/ServerApplyStateCollection.java", updateCases); + return fullReplacementText; + } + + /** + * Gets the field's value as a serialized string which will itself be serialized + * @param field + * @param getFieldValue + * @return + */ + private String getFieldValueSerialized(SynchronizedField field, String getFieldValue){ + getFieldValue = "Utilities.deserialize((String)syncedValue.getValue()," + field.getTypeName() + ".class)"; + return getFieldValue; + } + + /** + * Gets the field's value as a number or string directly + * @param field + * @param getFieldValue + * @param clientEquivalent + * @return + */ + private String getFieldValueNonSerialized(SynchronizedField field, BehaviorTree clientEquivalent, String getFieldValue){ + switch(field.getTypeName()){ + case "int": { + getFieldValue = "((Double)syncedValue.getValue()).intValue()"; + } break; + case "long": { + getFieldValue = "((Double)syncedValue.getValue()).longValue()"; + } break; + case "float": { + getFieldValue = "((Double)syncedValue.getValue()).floatValue()"; + } break; + case "double": { + getFieldValue = "(double)syncedValue.getValue()"; + } break; + case "String": { + getFieldValue = "(String)syncedValue.getValue()"; + } break; + default : { + getFieldValue = clientEquivalent.getClassName() + ".get" + Utilities.camelCase(field.getTypeName()) + "ShortAsEnum(((Double)syncedValue.getValue()).shortValue())"; + } break; + } + return getFieldValue; + } + + @Override + public List getImports(ProjectStructure projectStructure) { + List rVal = new LinkedList(); + + //add server trees + for(BehaviorTree bTree : projectStructure.getBehaviorTrees()){ + if(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){ + return + serverTree.getSynchronizedFields() != null && + serverTree.getSynchronizedFields().size() > 0 + ; + } + +} diff --git a/src/main/resources/server/ApplyStateCollection.java b/src/main/resources/client/ClientApplyStateCollection.java similarity index 78% rename from src/main/resources/server/ApplyStateCollection.java rename to src/main/resources/client/ClientApplyStateCollection.java index 69163b1..b069899 100644 --- a/src/main/resources/server/ApplyStateCollection.java +++ b/src/main/resources/client/ClientApplyStateCollection.java @@ -6,7 +6,7 @@ * @param entity The entity * @param collection The state collection */ -public static void applyStateCollection(Entity entity, StateCollection collection){ +public static void clientApplyStateCollection(Entity entity, StateCollection collection){ for(SynchronizedFieldValue syncedValue : collection.getValues()){ switch(syncedValue.getBehaviorTreeId()){ REPLACE_0_ME diff --git a/src/main/resources/server/ServerApplyStateCollection.java b/src/main/resources/server/ServerApplyStateCollection.java new file mode 100644 index 0000000..09c5130 --- /dev/null +++ b/src/main/resources/server/ServerApplyStateCollection.java @@ -0,0 +1,15 @@ +/** + *

Automatically generated

+ *

+ * Applies the state collection to the given entity + *

+ * @param entity The entity + * @param collection The state collection + */ +public static void serverApplyStateCollection(Entity entity, StateCollection collection){ + for(SynchronizedFieldValue syncedValue : collection.getValues()){ + switch(syncedValue.getBehaviorTreeId()){ +REPLACE_0_ME + } + } +} \ No newline at end of file