This commit is contained in:
unknown 2024-02-25 14:09:05 -05:00
parent 9f1e5e9ae6
commit 155978662d
8 changed files with 77 additions and 9 deletions

View File

@ -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){

View File

@ -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;
}
}

View File

@ -82,6 +82,7 @@ public class TargetFile {
public void setModifiedContent(String content){
this.setModified(true);
this.modifiedContent = new StringBuilder(content);
reparse();
}
/**

View File

@ -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<MethodSource<JavaClassSource>> methods = targetFile.getSource().getMethods();
MethodSource<JavaClassSource> methodSource = null;
for(MethodSource<JavaClassSource> 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

View File

@ -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;
}

View File

@ -0,0 +1,9 @@
/**
* <p> Automatically generated </p>
* <p>
* Gets REPLACE_0_ME.
* </p>
*/
public REPLACE_2_ME getREPLACE_1_ME(){
return REPLACE_0_ME;
}

View File

@ -1,10 +1,10 @@
/**
* <p> Automatically generated </p>
* <p>
* Sets REPLACENAMENOTCAMEL and handles the synchronization logic for it.
* Sets REPLACE_0_ME and handles the synchronization logic for it.
* </p>
* @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;
}

View File

@ -4,3 +4,4 @@ Validation to build into tool
[ ]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
[ ]Check constructors for btrees are private and instead all creation is done through attachTree() method