From f5ff55898da1db969170e5cbede09ef8d053e52e Mon Sep 17 00:00:00 2001 From: austin Date: Mon, 5 May 2025 22:16:23 -0400 Subject: [PATCH] code cleanup --- docs/src/progress/renderertodo.md | 1 + .../client/ui/parsing/HtmlParser.java | 3 + .../assetmanager/queue/QueuedTexture.java | 19 ++++ .../entity/state/attach/AttachUtils.java | 1 + .../entity/state/attack/ClientAttackTree.java | 1 + .../entity/state/attack/ServerAttackTree.java | 1 + .../entity/state/equip/ClientEquipState.java | 1 + .../entity/state/equip/ServerEquipState.java | 1 + .../entity/state/rotator/RotatorTree.java | 13 ++- .../state/rotator/ServerRotatorTree.java | 13 ++- .../entity/types/item/ItemUtils.java | 1 + .../net/client/protocol/LoreProtocol.java | 1 + .../renderer/texture/Texture.java | 93 ------------------- .../renderer/ui/font/FontUtils.java | 7 +- .../electrosphere/script/ScriptContext.java | 42 ++++++--- .../electrosphere/script/ScriptEngine.java | 18 +++- .../physics/fluid/diskmap/FluidDiskMap.java | 4 +- 17 files changed, 93 insertions(+), 127 deletions(-) diff --git a/docs/src/progress/renderertodo.md b/docs/src/progress/renderertodo.md index 9bdd7a91..0558f81d 100644 --- a/docs/src/progress/renderertodo.md +++ b/docs/src/progress/renderertodo.md @@ -1686,6 +1686,7 @@ Styling support for html-defined menus Dynamic html-defined menus support button elements that call a client hook when clicked Dynamic menu navigation between windows Starting formatting npc intro page +Code cleanup diff --git a/src/main/java/electrosphere/client/ui/parsing/HtmlParser.java b/src/main/java/electrosphere/client/ui/parsing/HtmlParser.java index e925ad91..1513cb2a 100644 --- a/src/main/java/electrosphere/client/ui/parsing/HtmlParser.java +++ b/src/main/java/electrosphere/client/ui/parsing/HtmlParser.java @@ -91,6 +91,9 @@ public class HtmlParser { } } } break; + case "script": { + LoggerInterface.loggerUI.WARNING("Script tag not implemented yet"); + } break; case "style": //silently ignore break; diff --git a/src/main/java/electrosphere/engine/assetmanager/queue/QueuedTexture.java b/src/main/java/electrosphere/engine/assetmanager/queue/QueuedTexture.java index d7dce438..c323015d 100644 --- a/src/main/java/electrosphere/engine/assetmanager/queue/QueuedTexture.java +++ b/src/main/java/electrosphere/engine/assetmanager/queue/QueuedTexture.java @@ -2,6 +2,7 @@ package electrosphere.engine.assetmanager.queue; import java.awt.image.BufferedImage; import java.nio.ByteBuffer; +import java.util.function.Consumer; import org.lwjgl.BufferUtils; @@ -72,6 +73,11 @@ public class QueuedTexture implements QueuedAsset { */ boolean suppliedPath = false; + /** + * The runnable to invoke to actually load the model + */ + Consumer loadFunc; + /** * Creates the queued texture object * @param buffer The data to buffer @@ -147,6 +153,16 @@ public class QueuedTexture implements QueuedAsset { return rVal; } + /** + * Creates the queued texture object + * @param image the image to load to gpu + */ + public static QueuedTexture createFromImage(BufferedImage bufferedImage, Consumer loadFunc){ + QueuedTexture rVal = QueuedTexture.createFromImage(bufferedImage); + rVal.loadFunc = loadFunc; + return rVal; + } + /** * Creates the queued texture object * @param path The path to register this texture to @@ -170,6 +186,9 @@ public class QueuedTexture implements QueuedAsset { texture = new Texture(Globals.renderingEngine.getOpenGLState(), data, buffer); } break; } + if(this.loadFunc != null){ + this.loadFunc.accept(texture); + } hasLoaded = true; } diff --git a/src/main/java/electrosphere/entity/state/attach/AttachUtils.java b/src/main/java/electrosphere/entity/state/attach/AttachUtils.java index 659e973b..261487a5 100644 --- a/src/main/java/electrosphere/entity/state/attach/AttachUtils.java +++ b/src/main/java/electrosphere/entity/state/attach/AttachUtils.java @@ -764,6 +764,7 @@ public class AttachUtils { * @param parentEntity * @return The list of entities that are attached to this parent entity, or null if undefined */ + @SuppressWarnings("unchecked") public static List getChildrenList(Entity parentEntity){ return (List)parentEntity.getData(EntityDataStrings.ATTACH_CHILDREN_LIST); } diff --git a/src/main/java/electrosphere/entity/state/attack/ClientAttackTree.java b/src/main/java/electrosphere/entity/state/attack/ClientAttackTree.java index dd06eab8..2af56110 100644 --- a/src/main/java/electrosphere/entity/state/attack/ClientAttackTree.java +++ b/src/main/java/electrosphere/entity/state/attack/ClientAttackTree.java @@ -538,6 +538,7 @@ public class ClientAttackTree implements BehaviorTree { * @param attackType the attack type * @return The moveset if it exists */ + @SuppressWarnings("unchecked") public List getMoveset(String attackType){ return (List)parent.getData(attackType); } diff --git a/src/main/java/electrosphere/entity/state/attack/ServerAttackTree.java b/src/main/java/electrosphere/entity/state/attack/ServerAttackTree.java index 1885ddcb..1676c751 100644 --- a/src/main/java/electrosphere/entity/state/attack/ServerAttackTree.java +++ b/src/main/java/electrosphere/entity/state/attack/ServerAttackTree.java @@ -634,6 +634,7 @@ public class ServerAttackTree implements BehaviorTree { * @param attackType the attack type * @return The moveset if it exists */ + @SuppressWarnings("unchecked") public List getMoveset(String attackType){ return (List)parent.getData(attackType); } diff --git a/src/main/java/electrosphere/entity/state/equip/ClientEquipState.java b/src/main/java/electrosphere/entity/state/equip/ClientEquipState.java index 2fb541c2..352985c0 100644 --- a/src/main/java/electrosphere/entity/state/equip/ClientEquipState.java +++ b/src/main/java/electrosphere/entity/state/equip/ClientEquipState.java @@ -53,6 +53,7 @@ public class ClientEquipState implements BehaviorTree { * @param equipPoints the list of available points */ private ClientEquipState(Entity parent, Object ... params){ + @SuppressWarnings("unchecked") List equipPoints = (List)params[0]; this.parent = parent; for(EquipPoint point : equipPoints){ diff --git a/src/main/java/electrosphere/entity/state/equip/ServerEquipState.java b/src/main/java/electrosphere/entity/state/equip/ServerEquipState.java index be546ee9..a098b245 100644 --- a/src/main/java/electrosphere/entity/state/equip/ServerEquipState.java +++ b/src/main/java/electrosphere/entity/state/equip/ServerEquipState.java @@ -60,6 +60,7 @@ public class ServerEquipState implements BehaviorTree { Map equipMap = new HashMap(); public ServerEquipState(Entity parent, Object ... params){ + @SuppressWarnings("unchecked") List equipPoints = (List)params[0]; this.parent = parent; for(EquipPoint point : equipPoints){ diff --git a/src/main/java/electrosphere/entity/state/rotator/RotatorTree.java b/src/main/java/electrosphere/entity/state/rotator/RotatorTree.java index 20efd5a8..d1e74cd5 100644 --- a/src/main/java/electrosphere/entity/state/rotator/RotatorTree.java +++ b/src/main/java/electrosphere/entity/state/rotator/RotatorTree.java @@ -3,7 +3,6 @@ package electrosphere.entity.state.rotator; import java.util.LinkedList; import java.util.List; -import org.joml.Quaterniond; import org.joml.Quaternionf; import org.joml.Vector3f; @@ -58,16 +57,16 @@ public class RotatorTree implements BehaviorTree{ public void applyRotatorNode(RotatorHierarchyNode node){ //apply - String nodeBoneName = node.getBone(); - Quaterniond currentRotation = entityActor.getBoneRotation(nodeBoneName); + // String nodeBoneName = node.getBone(); + // Quaterniond currentRotation = entityActor.getBoneRotation(nodeBoneName); for(RotatorConstraint constraint : node.getRotatorContraints()){ - float allowedMarginPitch = constraint.getAllowedMarginPitch(); - float allowedMarginYaw = constraint.getAllowedMarginYaw(); + // float allowedMarginPitch = constraint.getAllowedMarginPitch(); + // float allowedMarginYaw = constraint.getAllowedMarginYaw(); boolean followsBone = constraint.getFollowsBone(); boolean followsView = constraint.getFollowsView(); if(followsBone){ - String parentBone = constraint.getParentBone(); - Quaterniond parentBoneRotation = entityActor.getBoneRotation(parentBone); + // String parentBone = constraint.getParentBone(); + // Quaterniond parentBoneRotation = entityActor.getBoneRotation(parentBone); // currentRotation. } if(followsView){ diff --git a/src/main/java/electrosphere/entity/state/rotator/ServerRotatorTree.java b/src/main/java/electrosphere/entity/state/rotator/ServerRotatorTree.java index c406fa32..8642e9b6 100644 --- a/src/main/java/electrosphere/entity/state/rotator/ServerRotatorTree.java +++ b/src/main/java/electrosphere/entity/state/rotator/ServerRotatorTree.java @@ -3,7 +3,6 @@ package electrosphere.entity.state.rotator; import java.util.LinkedList; import java.util.List; -import org.joml.Quaterniond; import org.joml.Quaternionf; import org.joml.Vector3f; @@ -58,16 +57,16 @@ public class ServerRotatorTree implements BehaviorTree{ public void applyRotatorNode(RotatorHierarchyNode node){ //apply - String nodeBoneName = node.getBone(); - Quaterniond currentRotation = entityPoseActor.getBoneRotation(nodeBoneName); + // String nodeBoneName = node.getBone(); + // Quaterniond currentRotation = entityPoseActor.getBoneRotation(nodeBoneName); for(RotatorConstraint constraint : node.getRotatorContraints()){ - float allowedMarginPitch = constraint.getAllowedMarginPitch(); - float allowedMarginYaw = constraint.getAllowedMarginYaw(); + // float allowedMarginPitch = constraint.getAllowedMarginPitch(); + // float allowedMarginYaw = constraint.getAllowedMarginYaw(); boolean followsBone = constraint.getFollowsBone(); boolean followsView = constraint.getFollowsView(); if(followsBone){ - String parentBone = constraint.getParentBone(); - Quaterniond parentBoneRotation = entityPoseActor.getBoneRotation(parentBone); + // String parentBone = constraint.getParentBone(); + // Quaterniond parentBoneRotation = entityPoseActor.getBoneRotation(parentBone); // currentRotation. } if(followsView){ diff --git a/src/main/java/electrosphere/entity/types/item/ItemUtils.java b/src/main/java/electrosphere/entity/types/item/ItemUtils.java index b8a50e78..99cd5ed4 100644 --- a/src/main/java/electrosphere/entity/types/item/ItemUtils.java +++ b/src/main/java/electrosphere/entity/types/item/ItemUtils.java @@ -451,6 +451,7 @@ public class ItemUtils { return item.containsKey(EntityDataStrings.ITEM_EQUIP_WHITELIST); } + @SuppressWarnings("unchecked") public static List getEquipWhitelist(Entity item){ return (List)item.getData(EntityDataStrings.ITEM_EQUIP_WHITELIST); } diff --git a/src/main/java/electrosphere/net/client/protocol/LoreProtocol.java b/src/main/java/electrosphere/net/client/protocol/LoreProtocol.java index 64663e85..05932db6 100644 --- a/src/main/java/electrosphere/net/client/protocol/LoreProtocol.java +++ b/src/main/java/electrosphere/net/client/protocol/LoreProtocol.java @@ -20,6 +20,7 @@ public class LoreProtocol implements ClientProtocolTemplate { switch(message.getMessageSubtype()){ case RESPONSERACES: //we get back the race list as a json array, deserialize, and push into type loader + @SuppressWarnings("unchecked") List playableRaces = new Gson().fromJson(message.getdata(), List.class); Globals.gameConfigCurrent.getCreatureTypeLoader().loadPlayableRaces(playableRaces); break; diff --git a/src/main/java/electrosphere/renderer/texture/Texture.java b/src/main/java/electrosphere/renderer/texture/Texture.java index b970f3a5..a6a7e518 100644 --- a/src/main/java/electrosphere/renderer/texture/Texture.java +++ b/src/main/java/electrosphere/renderer/texture/Texture.java @@ -77,99 +77,6 @@ public class Texture { Globals.renderingEngine.checkError(); } - @Deprecated - /** - * Creates an in engine texture object from a java bufferedimage object - * @param bufferedImage The java bufferedimage object - */ - public Texture(OpenGLState openGlState, BufferedImage bufferedImage){ - this.texturePointer = GL40.glGenTextures(); - Globals.renderingEngine.checkError(); - //bind the new texture - openGlState.glBindTexture(GL_TEXTURE_2D, texturePointer); - //how are we gonna wrap the texture?? - this.setWrap(openGlState, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); - this.setWrap(openGlState, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); - //set the border color to black - this.setBorderColor(openGlState, new float[]{ 0.0f, 0.0f, 0.0f, 1.0f }); - //set magnification and minification operation sampling strategies - this.setMinFilter(openGlState, GL_LINEAR); - this.setMagFilter(openGlState, GL_LINEAR); - //load the image here - ByteBuffer data; - width = 1; - height = 1; - BufferedImage image_data = bufferedImage; - if ( - image_data.getType() == BufferedImage.TYPE_3BYTE_BGR || - image_data.getType() == BufferedImage.TYPE_INT_RGB - ){ - hasTransparency = false; - } else if( - image_data.getType() == BufferedImage.TYPE_4BYTE_ABGR || - image_data.getType() == BufferedImage.TYPE_INT_ARGB - ){ - hasTransparency = true; - } - width = image_data.getWidth(); - height = image_data.getHeight(); - if(hasTransparency){ - data = BufferUtils.createByteBuffer(width * height * 4); - } else { - data = BufferUtils.createByteBuffer(width * height * 3); - } - for(int y = height - 1; y > -1; y--){ - for(int x = 0; x < width; x++){ - int color = image_data.getRGB(x, y); - - // data.put((byte)temp.getRed()); - // data.put((byte)temp.getGreen()); - // data.put((byte)temp.getBlue()); - if(hasTransparency){ - int blue = color & 0xff; - int green = (color & 0xff00) >> 8; - int red = (color & 0xff0000) >> 16; - int alpha = (color & 0xff000000) >>> 24; - data.put((byte)red); - data.put((byte)green); - data.put((byte)blue); - data.put((byte)alpha); - } else { - int blue = color & 0xff; - int green = (color & 0xff00) >> 8; - int red = (color & 0xff0000) >> 16; - data.put((byte)red); - data.put((byte)green); - data.put((byte)blue); - } - } - } - if(data.position() > 0){ - data.flip(); - } - //call if width != height so opengl figures out how to unpack it properly - if(width != height){ - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - } - //buffer the texture information - if(hasTransparency){ - this.pixelFormat = GL_RGBA; - this.datatype = GL_UNSIGNED_BYTE; - this.glTexImage2D(openGlState, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data); - } else { - this.pixelFormat = GL_RGB; - this.datatype = GL_UNSIGNED_BYTE; - this.glTexImage2D(openGlState, width, height, GL_RGB, GL_UNSIGNED_BYTE, data); - } - glGenerateMipmap(GL_TEXTURE_2D); - //check build status - String errorMessage = RenderingEngine.getErrorInEnglish(Globals.renderingEngine.getError()); - if(errorMessage != null){ - LoggerInterface.loggerRenderer.ERROR(new IllegalStateException("Texture Constructor[from bufferedimage]: " + errorMessage)); - } - openGlState.glBindTexture(GL_TEXTURE_2D, 0); - } - /** * Creates an in engine texture object from a java bufferedimage object * @param bufferedImage The java bufferedimage object diff --git a/src/main/java/electrosphere/renderer/ui/font/FontUtils.java b/src/main/java/electrosphere/renderer/ui/font/FontUtils.java index 9f0563f7..c3c6c994 100644 --- a/src/main/java/electrosphere/renderer/ui/font/FontUtils.java +++ b/src/main/java/electrosphere/renderer/ui/font/FontUtils.java @@ -1,5 +1,7 @@ package electrosphere.renderer.ui.font; +import electrosphere.engine.Globals; +import electrosphere.engine.assetmanager.queue.QueuedTexture; import electrosphere.logger.LoggerInterface; import electrosphere.renderer.OpenGLState; import electrosphere.renderer.model.Material; @@ -184,8 +186,9 @@ public class FontUtils { //create material with new font image Material uiMat = new Material(); - Texture texture = new Texture(openGLState, image); - uiMat.setTexturePointer(texture.getTexturePointer()); + Globals.assetManager.queuedAsset(QueuedTexture.createFromImage(image, (Texture tex) -> { + uiMat.setTexturePointer(tex.getTexturePointer()); + })); //construct final font object and return diff --git a/src/main/java/electrosphere/script/ScriptContext.java b/src/main/java/electrosphere/script/ScriptContext.java index 1bef3109..f670c538 100644 --- a/src/main/java/electrosphere/script/ScriptContext.java +++ b/src/main/java/electrosphere/script/ScriptContext.java @@ -23,24 +23,44 @@ import electrosphere.util.FileUtils; */ public class ScriptContext { - //the default namespaces for - public static String SCRIPT_NAMESPACE_ENGINE = "engine"; //namespace for the engine functions exposed to the script engine - public static String SCRIPT_NAMESPACE_SCRIPT = "script"; //namespace for the core typescript functionsw - public static String SCRIPT_NAMESPACE_SCENE = "scene"; //namespace for the current scene + /** + * namespace for the engine functions exposed to the script engine + */ + public static final String SCRIPT_NAMESPACE_ENGINE = "engine"; - //the graal context + /** + * namespace for the core typescript functions + */ + public static final String SCRIPT_NAMESPACE_SCRIPT = "script"; + + /** + * namespace for the current scene + */ + public static final String SCRIPT_NAMESPACE_SCENE = "scene"; + + /** + * the graal context + */ Context context; - //used to build source objects + /** + * used to build source objects + */ Builder builder; - //the javascript object that stores values + /** + * the javascript object that stores values + */ Value topLevelValue; - //the object that contains all host values accessible to javascript land + /** + * the object that contains all host values accessible to javascript land + */ Value hostObject; - //the engine object + /** + * the engine object + */ Value engineObject; /** @@ -148,7 +168,7 @@ public class ScriptContext { * Loads a script from disk * @param path The path to the script file */ - void loadDependency(String path){ + private void loadDependency(String path){ String content; Source source = null; try { @@ -357,7 +377,7 @@ public class ScriptContext { * @param filePath The filepath of the module */ public void requireModule(String filePath){ - invokeFunction("require", filePath); + this.invokeFunction("require", filePath); } /** diff --git a/src/main/java/electrosphere/script/ScriptEngine.java b/src/main/java/electrosphere/script/ScriptEngine.java index b2cc57df..e11952ca 100644 --- a/src/main/java/electrosphere/script/ScriptEngine.java +++ b/src/main/java/electrosphere/script/ScriptEngine.java @@ -53,7 +53,9 @@ public class ScriptEngine extends SignalServiceImpl { */ public static final int GLOBAL_SCENE = -1; - //the map of script filepaths to parsed, in-memory scripts + /** + * the map of script filepaths to parsed, in-memory scripts + */ Map sourceMap; /** @@ -81,7 +83,9 @@ public class ScriptEngine extends SignalServiceImpl { */ boolean initialized = false; - //The files that are loaded on init to bootstrap the script engine + /** + * The files that are loaded on init to bootstrap the script engine + */ public static final String[] filesToLoadOnInit = new String[]{ //polyfills "Scripts/compiler/require_polyfill.js", @@ -102,8 +106,10 @@ public class ScriptEngine extends SignalServiceImpl { "/Scripts/compiler/host_access.ts", }; - //The classes that will be provided to the scripting engine - //https://stackoverflow.com/a/65942034 + /** + * The classes that will be provided to the scripting engine + * https://stackoverflow.com/a/65942034 + */ public static final Object[][] staticClasses = new Object[][]{ {"mathUtils",SpatialMathUtils.class}, {"simulation",Main.class}, @@ -116,7 +122,9 @@ public class ScriptEngine extends SignalServiceImpl { {"areaUtils",ScriptClientAreaUtils.class}, }; - //singletons from the host that are provided to the javascript context + /** + * singletons from the host that are provided to the javascript context + */ public static final Object[][] hostSingletops = new Object[][]{ {"timekeeper",Globals.timekeeper}, {"currentPlayer",Globals.clientPlayer}, diff --git a/src/main/java/electrosphere/server/physics/fluid/diskmap/FluidDiskMap.java b/src/main/java/electrosphere/server/physics/fluid/diskmap/FluidDiskMap.java index 80304e06..19ecba44 100644 --- a/src/main/java/electrosphere/server/physics/fluid/diskmap/FluidDiskMap.java +++ b/src/main/java/electrosphere/server/physics/fluid/diskmap/FluidDiskMap.java @@ -48,7 +48,7 @@ public class FluidDiskMap { public void init(String saveName){ LoggerInterface.loggerEngine.DEBUG("INIT CHUNK MAP " + saveName); if(FileUtils.getSaveFile(saveName, "chunk.map").exists()){ - worldPosFileMap = FileUtils.loadObjectFromSavePath(saveName, "fluid.map", Map.class); + worldPosFileMap = FileUtils.loadObjectFromSavePath(saveName, "fluid.map", FluidDiskMap.class).worldPosFileMap; LoggerInterface.loggerEngine.DEBUG("POS FILE MAP: " + worldPosFileMap.keySet()); } else { worldPosFileMap = new HashMap(); @@ -59,7 +59,7 @@ public class FluidDiskMap { * Saves the disk map to disk */ public void save(){ - FileUtils.serializeObjectToSavePath(Globals.currentSave.getName(), "fluid.map", worldPosFileMap); + FileUtils.serializeObjectToSavePath(Globals.currentSave.getName(), "fluid.map", this); } /**