From ecab7952b74acab96ee2c6231b37144b464d40de Mon Sep 17 00:00:00 2001 From: austin Date: Wed, 31 Jul 2024 14:57:30 -0400 Subject: [PATCH] more refactor --- .../client/ClientSynchronizationManager.java | 35 +++++++++++------ .../main/core/VirtualProject.java | 39 +++++++++++++++++++ .../core/statics/btreeenum/BTreeIdEnum.java | 5 ++- .../core/statics/fieldenum/FieldIdEnum.java | 6 +-- .../main/project/ProjectStructure.java | 3 +- .../main/project/parsers/MainParser.java | 13 +------ .../main/source/VirtualClass.java | 22 +++++++++-- 7 files changed, 90 insertions(+), 33 deletions(-) diff --git a/src/main/java/electrosphere/main/client/ClientSynchronizationManager.java b/src/main/java/electrosphere/main/client/ClientSynchronizationManager.java index 3f26b6a..47ec75e 100644 --- a/src/main/java/electrosphere/main/client/ClientSynchronizationManager.java +++ b/src/main/java/electrosphere/main/client/ClientSynchronizationManager.java @@ -5,26 +5,39 @@ 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.util.TemplateInjectionUtils; -public class ClientSynchronizationManager { +/** + * The client synchronization manager class + */ +public class ClientSynchronizationManager extends VirtualClass { - //The class name of this class - public static final String CLASSNAME = "ClientSynchronizationManager"; + //The target file for the current version of this class + TargetFile file; - //The location of the file - public static final String LOCATION = "src/main/java/electrosphere/net/synchronization/ClientSynchronizationManager.java"; + /** + * Constructor + */ + public ClientSynchronizationManager(TargetFile file) { + super( + "src/main/java/electrosphere/net/synchronization/ClientSynchronizationManager.java", + "ClientSynchronizationManager", + true + ); + this.file = file; + } /** * Updates the ClientSynchronizationManager with latest data in specific functions - * @param structure - * @param clientSynchronizationManager + * @param structure The project texture */ - public static void update(ProjectStructure structure, TargetFile clientSynchronizationManager){ + @Override + public void updateFile(ProjectStructure structure){ String updateCases = ""; for(BehaviorTree serverTree : structure.getBehaviorTrees()){ //counterintuitively, want to only update client for server behavior tree ids @@ -81,11 +94,11 @@ public class ClientSynchronizationManager { //guarantee import // ClassSourceUtils.importClass(structure, clientSynchronizationManager, serverTree.getTargetFile().getQualifiedPath()); - ClassSourceUtils.importClass(structure, clientSynchronizationManager, structure.getTree(serverTree.getCorrespondingTreeName()).getTargetFile().getQualifiedPath()); + ClassSourceUtils.importClass(structure, this.file, structure.getTree(serverTree.getCorrespondingTreeName()).getTargetFile().getQualifiedPath()); } } - ClassSourceUtils.importClass(structure, clientSynchronizationManager, "electrosphere.net.synchronization.FieldIdEnums"); + ClassSourceUtils.importClass(structure, this.file, "electrosphere.net.synchronization.FieldIdEnums"); String fullReplacementText = TemplateInjectionUtils.getFragmentWithReplacement("/client/UpdateEntityState.java", updateCases); - ClassSourceUtils.addOrReplaceMethod(structure, clientSynchronizationManager, "updateEntityState", fullReplacementText); + ClassSourceUtils.addOrReplaceMethod(structure, this.file, "updateEntityState", fullReplacementText); } } diff --git a/src/main/java/electrosphere/main/core/VirtualProject.java b/src/main/java/electrosphere/main/core/VirtualProject.java index 4f3106a..2e6ed5b 100644 --- a/src/main/java/electrosphere/main/core/VirtualProject.java +++ b/src/main/java/electrosphere/main/core/VirtualProject.java @@ -1,12 +1,21 @@ package electrosphere.main.core; +import java.util.LinkedList; +import java.util.List; + +import electrosphere.main.client.ClientSynchronizationManager; import electrosphere.main.core.statics.btreeenum.BTreeIdEnum; import electrosphere.main.core.statics.fieldenum.FieldIdEnum; +import electrosphere.main.source.VirtualClass; +import electrosphere.main.targets.TargetFile; /** * The rich data on all the classes in the project */ public class VirtualProject { + + //all virtual classes in this virtual project + List classes = new LinkedList(); //The BTree id enum file BTreeIdEnum bTreeIdEnum = new BTreeIdEnum(); @@ -14,6 +23,19 @@ public class VirtualProject { //field id enum FieldIdEnum fieldIdEnum = new FieldIdEnum(); + //The client synchronization manager class + ClientSynchronizationManager clientSynchronizationManager; + + + /** + * Constructor + */ + public VirtualProject(){ + this.classes.add(bTreeIdEnum); + this.classes.add(fieldIdEnum); + } + + /** * Gets the BTree id enum file's class @@ -31,4 +53,21 @@ public class VirtualProject { return this.fieldIdEnum; } + /** + * Creates the client synchronization manager + * @param file The target file + */ + public void createClientSynchronizationManager(TargetFile file){ + this.clientSynchronizationManager = new ClientSynchronizationManager(file); + this.classes.add(this.clientSynchronizationManager); + } + + /** + * Gets the client synchronization manager + * @return The client synchonization manager + */ + public ClientSynchronizationManager getClientSynchronizationManager(){ + return this.clientSynchronizationManager; + } + } diff --git a/src/main/java/electrosphere/main/core/statics/btreeenum/BTreeIdEnum.java b/src/main/java/electrosphere/main/core/statics/btreeenum/BTreeIdEnum.java index 178bae8..3728ccf 100644 --- a/src/main/java/electrosphere/main/core/statics/btreeenum/BTreeIdEnum.java +++ b/src/main/java/electrosphere/main/core/statics/btreeenum/BTreeIdEnum.java @@ -21,7 +21,8 @@ public class BTreeIdEnum extends VirtualClass { public BTreeIdEnum() { super( "src/main/java/electrosphere/net/synchronization/BehaviorTreeIdEnums.java", - "BehaviorTreeIdEnums" + "BehaviorTreeIdEnums", + false ); } @@ -30,7 +31,7 @@ public class BTreeIdEnum extends VirtualClass { * @param structure The project structure */ @Override - public void generate(ProjectStructure structure){ + public void createFile(ProjectStructure structure){ String source = ""; File outFile = new File(structure.getRootFolder().getAbsolutePath()).toPath().resolve(this.getLocation()).toFile(); diff --git a/src/main/java/electrosphere/main/core/statics/fieldenum/FieldIdEnum.java b/src/main/java/electrosphere/main/core/statics/fieldenum/FieldIdEnum.java index 7de096c..956d314 100644 --- a/src/main/java/electrosphere/main/core/statics/fieldenum/FieldIdEnum.java +++ b/src/main/java/electrosphere/main/core/statics/fieldenum/FieldIdEnum.java @@ -21,9 +21,9 @@ public class FieldIdEnum extends VirtualClass { public FieldIdEnum() { super( "src/main/java/electrosphere/net/synchronization/FieldIdEnums.java", - "FieldIdEnums" + "FieldIdEnums", + false ); - //TODO Auto-generated constructor stub } /** @@ -31,7 +31,7 @@ public class FieldIdEnum extends VirtualClass { * @param structure The project structure */ @Override - public void generate(ProjectStructure structure){ + public void createFile(ProjectStructure structure){ String source = ""; File outFile = new File(structure.getRootFolder().getAbsolutePath()).toPath().resolve(this.getLocation()).toFile(); diff --git a/src/main/java/electrosphere/main/project/ProjectStructure.java b/src/main/java/electrosphere/main/project/ProjectStructure.java index d08a6ec..364c1ca 100644 --- a/src/main/java/electrosphere/main/project/ProjectStructure.java +++ b/src/main/java/electrosphere/main/project/ProjectStructure.java @@ -5,7 +5,6 @@ import java.util.List; import electrosphere.main.Main; -import electrosphere.main.client.ClientSynchronizationManager; import electrosphere.main.core.VirtualProject; import electrosphere.main.core.btree.BehaviorTree; import electrosphere.main.core.syncfield.SynchronizedField; @@ -121,7 +120,7 @@ public class ProjectStructure { this.virtualProject.getBTreeIdEnum().generate(this); this.virtualProject.getFieldIdEnum().generate(this); //client sync manager - ClientSynchronizationManager.update(this, this.mainParser.getClientSynchronizationManager()); + this.virtualProject.getClientSynchronizationManager().generate(this); } /** diff --git a/src/main/java/electrosphere/main/project/parsers/MainParser.java b/src/main/java/electrosphere/main/project/parsers/MainParser.java index ae214e5..2dfeb12 100644 --- a/src/main/java/electrosphere/main/project/parsers/MainParser.java +++ b/src/main/java/electrosphere/main/project/parsers/MainParser.java @@ -28,9 +28,6 @@ public class MainParser { */ FieldParser fieldParser = new FieldParser(); - //the client synchronization manager - TargetFile clientSynchronizationManager; - /** * Parses all the target files * @param targetFiles The list of target files @@ -57,7 +54,7 @@ public class MainParser { //client synchronization manager if(target.getName().contains("ClientSynchronizationManager")){ - this.clientSynchronizationManager = target; + rVal.createClientSynchronizationManager(target); } } @@ -133,12 +130,4 @@ public class MainParser { return this.enumParser.getSynchronizedTypes(); } - /** - * Gets the client synchronization manager target file - * @return The target file - */ - public TargetFile getClientSynchronizationManager(){ - return this.clientSynchronizationManager; - } - } diff --git a/src/main/java/electrosphere/main/source/VirtualClass.java b/src/main/java/electrosphere/main/source/VirtualClass.java index acb0546..6035b65 100644 --- a/src/main/java/electrosphere/main/source/VirtualClass.java +++ b/src/main/java/electrosphere/main/source/VirtualClass.java @@ -15,6 +15,8 @@ public abstract class VirtualClass { private String LOCATION; //the class's name private String CLASSNAME; + //controls whether this class should update or generate from scratch + private boolean shouldUpdate; /** * All the imports in this virtual class @@ -35,10 +37,12 @@ public abstract class VirtualClass { * Constructor * @param location The location of the file * @param name The name of the class + * @param shouldUpdate Controls whether the class should generate the file from scratch or update its existing file */ - public VirtualClass(String location, String name){ + public VirtualClass(String location, String name, boolean shouldUpdate){ this.LOCATION = location; this.CLASSNAME = name; + this.shouldUpdate = shouldUpdate; } /** @@ -73,14 +77,26 @@ public abstract class VirtualClass { * @param structure The project */ public void generate(ProjectStructure structure){ - throw new UnsupportedOperationException("Not yet implemented!"); + if(this.shouldUpdate){ + this.updateFile(structure); + } else { + this.createFile(structure); + } } /** * Updates the existing file with the new contents of specific methods * @param structure The project */ - public void update(ProjectStructure structure){ + public void updateFile(ProjectStructure structure){ + throw new UnsupportedOperationException("Not yet implemented!"); + } + + /** + * Creates contents for a new file that represents this class + * @param structure The project + */ + public void createFile(ProjectStructure structure){ throw new UnsupportedOperationException("Not yet implemented!"); }