From 155978662d30bb37f9c723342fb1130054b1f14d Mon Sep 17 00:00:00 2001 From: unknown <> Date: Sun, 25 Feb 2024 14:09:05 -0500 Subject: [PATCH] bugfixes --- .../main/structure/ProjectStructure.java | 23 +++++++++++--- .../main/structure/SynchronizedField.java | 10 ++++++ .../main/targets/TargetFile.java | 1 + .../main/util/ClassSourceUtils.java | 31 +++++++++++++++++++ src/main/resources/client/AttachBTree.java | 1 + src/main/resources/client/Getter.java | 9 ++++++ src/main/resources/client/Setter.java | 8 ++--- validationToBuild.md | 3 +- 8 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 src/main/resources/client/Getter.java diff --git a/src/main/java/electrosphere/main/structure/ProjectStructure.java b/src/main/java/electrosphere/main/structure/ProjectStructure.java index c7e4dc2..bad568a 100644 --- a/src/main/java/electrosphere/main/structure/ProjectStructure.java +++ b/src/main/java/electrosphere/main/structure/ProjectStructure.java @@ -134,24 +134,39 @@ public class ProjectStructure { for(BehaviorTree tree : behaviorTrees){ //server side getter + setter if(tree.isServer){ + + + for(SynchronizedField field : tree.synchronizedFields){ ClassSourceUtils.addOrReplaceMethod(this, tree.getTargetFile(), field.getGetterName(), field.getServerGetterContent()); ClassSourceUtils.addOrReplaceMethod(this, tree.getTargetFile(), field.getSetterName(), field.getServerSetterContent(this)); - ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.server.datacell.utils.DataCellSearchUtils"); - ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.net.parser.net.message.SynchronizationMessage"); } //attach + detatch methods - ClassSourceUtils.addOrReplaceMethod(this, tree.getTargetFile(), tree.getAttachMethodName(), tree.getAttachMethodContent(true)); + ClassSourceUtils.addMethodIfAbsent(this, tree.getTargetFile(), tree.getAttachMethodName(), tree.getAttachMethodContent(true)); ClassSourceUtils.addOrReplaceMethod(this, tree.getTargetFile(), tree.getDetachMethodName(), tree.getDetachMethodContent(true)); + //imports + 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"); } else { + + + + for(SynchronizedField field : tree.synchronizedFields){ + ClassSourceUtils.addOrReplaceMethod(this, tree.getTargetFile(), field.getGetterName(), field.getClientGetterContent()); + ClassSourceUtils.addOrReplaceMethod(this, tree.getTargetFile(), field.getSetterName(), field.getClientSetterContent(this)); + } //attach + detatch methods - ClassSourceUtils.addOrReplaceMethod(this, tree.getTargetFile(), tree.getAttachMethodName(), tree.getAttachMethodContent(false)); + ClassSourceUtils.addMethodIfAbsent(this, tree.getTargetFile(), tree.getAttachMethodName(), tree.getAttachMethodContent(false)); ClassSourceUtils.addOrReplaceMethod(this, tree.getTargetFile(), tree.getDetachMethodName(), tree.getDetachMethodContent(false)); } + + //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(), BTreeIdEnum.getQualifiedPath(this)); + ClassSourceUtils.importClass(this, tree.getTargetFile(), "electrosphere.entity.EntityDataStrings"); } //generate enums for all synchronized types for(SynchronizedType type : synchronizedTypes){ diff --git a/src/main/java/electrosphere/main/structure/SynchronizedField.java b/src/main/java/electrosphere/main/structure/SynchronizedField.java index 061c21a..66a493b 100644 --- a/src/main/java/electrosphere/main/structure/SynchronizedField.java +++ b/src/main/java/electrosphere/main/structure/SynchronizedField.java @@ -50,6 +50,11 @@ public class SynchronizedField { return rVal; } + public String getClientGetterContent(){ + String rVal = TemplateInjectionUtils.getFragmentWithReplacement("/client/Getter.java", fieldName, Utilities.camelCase(fieldName), typeName); + return rVal; + } + public String getSetterName(){ String rVal = ""; rVal = "set" + Utilities.camelCase(fieldName); @@ -72,4 +77,9 @@ public class SynchronizedField { return rVal; } + public String getClientSetterContent(ProjectStructure structure){ + String rVal = TemplateInjectionUtils.getFragmentWithReplacement("/client/Setter.java", fieldName, Utilities.camelCase(fieldName), typeName); + return rVal; + } + } diff --git a/src/main/java/electrosphere/main/targets/TargetFile.java b/src/main/java/electrosphere/main/targets/TargetFile.java index 82b7b02..831995e 100644 --- a/src/main/java/electrosphere/main/targets/TargetFile.java +++ b/src/main/java/electrosphere/main/targets/TargetFile.java @@ -82,6 +82,7 @@ public class TargetFile { public void setModifiedContent(String content){ this.setModified(true); this.modifiedContent = new StringBuilder(content); + reparse(); } /** diff --git a/src/main/java/electrosphere/main/util/ClassSourceUtils.java b/src/main/java/electrosphere/main/util/ClassSourceUtils.java index 184a0cb..6593864 100644 --- a/src/main/java/electrosphere/main/util/ClassSourceUtils.java +++ b/src/main/java/electrosphere/main/util/ClassSourceUtils.java @@ -59,6 +59,37 @@ public class ClassSourceUtils { targetFile.reparse(); } + /** + * Adds to replaces a method in a given behavior tree + * @param structure The project structure + * @param targetFile The targetFile + * @param methodName The name of the method + * @param methodContent The content of the method that you want to insert or replace with + */ + public static void addMethodIfAbsent(ProjectStructure structure, TargetFile targetFile, String methodName, String methodContent){ + //search for method + List> methods = targetFile.getSource().getMethods(); + MethodSource methodSource = null; + for(MethodSource methodRaw : methods){ + if(methodRaw.getName().equals(methodName)){ + if(methodSource != null){ + throw new UnknownError("Two methods in a source file have the same name. This is unsupported by the addOrReplaceMethod function at this time!"); + } + methodSource = methodRaw; + } + } + StringBuilder modifiedContent = targetFile.getModifiedContent(); + if(methodSource == null){ + int endOfClass = findEndOfClass(modifiedContent.toString()); + String indentedFragment = TemplateInjectionUtils.indentFragment(methodContent, DEFAULT_INDENT); + //must newline + String finalInsertion = indentedFragment + "\n"; + modifiedContent.insert(endOfClass, finalInsertion); + targetFile.setModifiedContent(modifiedContent.toString()); + targetFile.reparse(); + } + } + /** * Finds the end of a class file * @param sourceRaw The raw source diff --git a/src/main/resources/client/AttachBTree.java b/src/main/resources/client/AttachBTree.java index 029a5dd..d46c1fe 100644 --- a/src/main/resources/client/AttachBTree.java +++ b/src/main/resources/client/AttachBTree.java @@ -15,6 +15,7 @@ public static REPLACE_2_ME attachTree(Entity parent){ //!!WARNING!! from here below should not be touched //This was generated automatically to properly alert various systems that the btree exists and should be tracked parent.putData(EntityDataStrings.REPLACE_1_ME, rVal); + Globals.clientScene.registerBehaviorTree(rVal); Globals.entityValueTrackingService.attachTreeToEntity(parent, BehaviorTreeIdEnums.BTREE_REPLACE_0_ME_ID); return rVal; } \ No newline at end of file diff --git a/src/main/resources/client/Getter.java b/src/main/resources/client/Getter.java new file mode 100644 index 0000000..88214ef --- /dev/null +++ b/src/main/resources/client/Getter.java @@ -0,0 +1,9 @@ +/** + *

Automatically generated

+ *

+ * Gets REPLACE_0_ME. + *

+ */ +public REPLACE_2_ME getREPLACE_1_ME(){ + return REPLACE_0_ME; +} \ No newline at end of file diff --git a/src/main/resources/client/Setter.java b/src/main/resources/client/Setter.java index a41bbd8..7b96199 100644 --- a/src/main/resources/client/Setter.java +++ b/src/main/resources/client/Setter.java @@ -1,10 +1,10 @@ /** *

Automatically generated

*

- * Sets REPLACENAMENOTCAMEL and handles the synchronization logic for it. + * Sets REPLACE_0_ME and handles the synchronization logic for it. *

- * @param REPLACENAMENOTCAMEL The value to set REPLACENAMENOTCAMEL to. + * @param REPLACE_0_ME The value to set REPLACE_0_ME to. */ -public void setREPLACENAMECAMEL(REPLACETYPE REPLACENAMENOTCAMEL){ - this.REPLACENAMENOTCAMEL = REPLACENAMENOTCAMEL; +public void setREPLACE_1_ME(REPLACE_2_ME REPLACE_0_ME){ + this.REPLACE_0_ME = REPLACE_0_ME; } \ No newline at end of file diff --git a/validationToBuild.md b/validationToBuild.md index 0ae7556..e754a49 100644 --- a/validationToBuild.md +++ b/validationToBuild.md @@ -3,4 +3,5 @@ Validation to build into tool [ ]Check that behavior trees have corresponding receiver trees [ ]Check that all synchronized fields are private [ ]If a synchronized field is an enum, check that both the client and server versions use the same enum type so that there isn't misalignment -[ ]Check that all synchronized fields have corresponding fields on opposite tree \ No newline at end of file +[ ]Check that all synchronized fields have corresponding fields on opposite tree +[ ]Check constructors for btrees are private and instead all creation is done through attachTree() method \ No newline at end of file