From 068f3979b8132fb13d96293e1d0a35d831237d87 Mon Sep 17 00:00:00 2001 From: austin Date: Fri, 12 Jul 2024 17:56:23 -0400 Subject: [PATCH] hookmanager in scriptengine, upgrade to junit 5 --- assets/Scripts/client/entity/inventory.ts | 41 ++--- assets/Scripts/engine/engine-init.ts | 11 +- assets/Scripts/engine/engine-interface.ts | 19 +- assets/Scripts/engine/entity/entity.ts | 10 ++ assets/Scripts/engine/hook-manager.ts | 170 ++++++++++++++++++ assets/Scripts/types/hook.ts | 39 +--- assets/Scripts/types/scene.ts | 15 -- buildNumber.properties | 4 +- pom.xml | 7 +- src/main/java/electrosphere/entity/Scene.java | 9 +- src/test/java/TestRunner.java | 50 ------ .../ui/elements/WindowIntegrationTest.java | 14 +- .../java/entity/SpawningCreaturesTest.java | 6 +- src/test/java/startup/StartupTest.java | 6 +- 14 files changed, 246 insertions(+), 155 deletions(-) create mode 100644 assets/Scripts/engine/entity/entity.ts create mode 100644 assets/Scripts/engine/hook-manager.ts delete mode 100644 src/test/java/TestRunner.java diff --git a/assets/Scripts/client/entity/inventory.ts b/assets/Scripts/client/entity/inventory.ts index 7038b47e..90610277 100644 --- a/assets/Scripts/client/entity/inventory.ts +++ b/assets/Scripts/client/entity/inventory.ts @@ -1,30 +1,23 @@ +import { Entity } from "/Scripts/engine/entity/entity" /** - * Called every time an item is moved to a container + * A type of entity */ -export const onMoveItemContainer: Array = [ ] +export type Item = Entity -/** - * Called every time an item is equipped - */ -export const onEquipItem: Array = [ ] - -/** - * Called every time an item is unequipped - */ -export const onUnequipItem: Array = [ ] - -/** - * Called every time the server commands the client to move items to another container - */ -export const onServerMoveItemContainer: Array = [ ] - -/** - * Callbacks that fire every time the player's entity does specific actions - */ -export interface ClientInventory { - onMoveItemContainer: Array, - onEquipItem: Array, - onUnequipItem: Array, +export interface Inventory { + } + + +/** + * Fires any time an item changes containers + */ +export const SIGNAL_MOVE_ITEM: string = "moveItem" + +/** + * A callback that fires when an item is moved from one container to another + */ +export type MoveItemCallback = (item: Item) => void + diff --git a/assets/Scripts/engine/engine-init.ts b/assets/Scripts/engine/engine-init.ts index e28a5445..1a9e4b4a 100644 --- a/assets/Scripts/engine/engine-init.ts +++ b/assets/Scripts/engine/engine-init.ts @@ -1,9 +1,16 @@ import { Engine } from '/Scripts/engine/engine-interface' import { loggerScripts } from '/Scripts/compiler/host_access' import { Client, NamespaceClient } from '/Scripts/client/client' +import { HookManager } from '/Scripts/engine/hook-manager' -//the core engine -let engine: Engine +/** + * The core engine values + */ +export const engine: Engine = { + classes: [], + singletons: [], + hookManager: new HookManager(), +} /** * Called when the script engine first initializes diff --git a/assets/Scripts/engine/engine-interface.ts b/assets/Scripts/engine/engine-interface.ts index 1a47844f..7cefc9fd 100644 --- a/assets/Scripts/engine/engine-interface.ts +++ b/assets/Scripts/engine/engine-interface.ts @@ -1,9 +1,24 @@ +import { HookManager } from "/Scripts/engine/hook-manager"; /** * The host context that contains all core engine functions */ export interface Engine { - classes: any, //the host's view of the scripting engine - singletons: any, //the singletons available to the script engine + + /** + * The host's view of the scripting engine + */ + classes: any, + + /** + * The singletons available to the script engine + */ + singletons: any, + + /** + * Manages all script-defined hooks in the engine + */ + hookManager: HookManager, + } diff --git a/assets/Scripts/engine/entity/entity.ts b/assets/Scripts/engine/entity/entity.ts new file mode 100644 index 00000000..81bf3d94 --- /dev/null +++ b/assets/Scripts/engine/entity/entity.ts @@ -0,0 +1,10 @@ + + +/** + * An entity + */ +export interface Entity { + id: number, +} + + diff --git a/assets/Scripts/engine/hook-manager.ts b/assets/Scripts/engine/hook-manager.ts new file mode 100644 index 00000000..94b6f8ba --- /dev/null +++ b/assets/Scripts/engine/hook-manager.ts @@ -0,0 +1,170 @@ +import { Hook } from "/Scripts/types/hook" +import { Scene } from "/Scripts/types/scene" + + +/** + * The scope that the hook is firing from + */ +export enum HookScope { + CORE, + SCRIPT, + SCENE_CLIENT, + SCENE_SERVER, +} + +/** + * A hook that is tracked by the manager + */ +interface TrackedHook extends Hook { + + /** + * The scope that this hook was defined at + */ + scope: HookScope, + + /** + * The scene that added the hook + */ + scene?: TrackedScene, + +} + +/** + * A scene being tracked by the manager + */ +interface TrackedScene { + + /** + * The id assigned to the scene + */ + sceneId: number, + + /** + * The scene itself + */ + scene: Scene, + +} + +/** + * Manages hooks for the engine + */ +export class HookManager { + + /** + * The list of all hooks currently tracked by this manager + */ + hooks: Array = [] + + /** + * A map of a scene to all hooks related to the scene + */ + sceneHookMap: Record> = { } + + /** + * A map of engine signal to the list of hooks that should be called when that signal fires + */ + signalHookMap: Record> = { } + + /** + * The list of all scenes tracked by the manager + */ + trackedScenes: Array<{ sceneId: number, scene: Scene }> = [] + + /** + * A scene + */ + sceneIdIncrementer: number = 0 + + /** + * Registers all hooks in a scene to the hook manager + * @param scene The scene + */ + registerScene(scene: Scene, isServerScene: boolean){ + const shouldRegister: boolean = !this.containsScene(scene) + if(shouldRegister){ + + //add to the list of tracked scenes + const trackedScene: TrackedScene = { + sceneId: this.sceneIdIncrementer++, + scene: scene, + } + this.trackedScenes.push(trackedScene) + + //load all hooks from the scene + scene.hooks.forEach((hook: Hook) => { + this.registerHook(trackedScene,hook,isServerScene) + }) + + } + } + + /** + * Deregisters all hooks in a scene from the hook manager + * @param scene The scene + */ + deregisterScene(scene: Scene){ + throw new Error("Unsupported Operation!") + } + + /** + * Checks if the manager is tracking a given scene + * @param scene The scene + * @returns true if it is being tracked already, false otherwise + */ + containsScene(scene: Scene): boolean { + this.trackedScenes.forEach(trackedScene => { + if(trackedScene.scene === scene){ + return true + } + }) + return false + } + + /** + * Registers a hook + * @param scene The scene introducing the hook + * @param hook The hook + */ + registerHook(scene: TrackedScene, hook: Hook, isServerScene: boolean){ + const trackedHook: TrackedHook = { + ...hook, + scope: isServerScene ? HookScope.SCENE_SERVER : HookScope.SCENE_CLIENT, + scene: scene, + } + //add to flat array + this.hooks.push(trackedHook) + //add to signal array + const hookSignal: string = hook.signal + const signalArray: Array = this.signalHookMap?.[hookSignal] ? this.signalHookMap?.[hookSignal] : [] + signalArray.push(trackedHook) + this.signalHookMap[hookSignal] = signalArray + //add to scene array + const sceneArray: Array = this.sceneHookMap?.[scene.sceneId] ? this.sceneHookMap?.[scene.sceneId] : [] + sceneArray.push(trackedHook) + this.sceneHookMap[scene.sceneId] = sceneArray + } + + /** + * Deregisters a hook + * @param scene The scene which introduced the hook + * @param hook The hook + */ + deregisterHook(scene: Scene, hook: Hook){ + throw new Error("Supported operation!") + } + + /** + * Fires a signal + * @param signal The signal + * @param value The value associated with the signal + */ + fireSignal(signal: string, value: any){ + const hooks: Array = this.signalHookMap[signal] + hooks.forEach(trackedHook => { + trackedHook.callback(value) + }) + } + +} + diff --git a/assets/Scripts/types/hook.ts b/assets/Scripts/types/hook.ts index c327a1dd..6f4cefbf 100644 --- a/assets/Scripts/types/hook.ts +++ b/assets/Scripts/types/hook.ts @@ -1,4 +1,3 @@ -import { Scene } from "/Scripts/types/scene"; /** @@ -9,47 +8,11 @@ export interface Hook { /** * The signal that triggers this hook in particular */ - signal: any, + signal: string, /** * The function to call when the signal is fired */ callback: Function, -} - -/** - * The scope that the hook is firing from - */ -export enum HookScope { - CORE, - SCRIPT, - SCENE_CLIENT, - SCENE_SERVER, -} - -/** - * A hook that is tracked by the manager - */ -export interface TrackedHook extends Hook { - - /** - * The scope that this hook was defined at - */ - scope: HookScope, - - /** - * The scene that added the hook - */ - scene?: Scene, - -} - -/** - * Manages hooks for the engine - */ -export class HookManager { - - - } diff --git a/assets/Scripts/types/scene.ts b/assets/Scripts/types/scene.ts index 71af0fa4..9e29b8d0 100644 --- a/assets/Scripts/types/scene.ts +++ b/assets/Scripts/types/scene.ts @@ -16,21 +16,6 @@ export function SynchronizedType() { */ export interface Scene extends Namespace { - /** - * Called when the current instance scene is first loaded - */ - onLoad?: Function - - /** - * Called when the current instance of the scene is unloaded - */ - onUnload?: Function - - /** - * Called when the current instance of the scene is saved -- either periodically or when the scene is being stored to disk - */ - onSave?: Function - /** * Values that are synchronized between the client and server. They are also stored to disk when the scene saves */ diff --git a/buildNumber.properties b/buildNumber.properties index 7c93a3cb..dcff934b 100644 --- a/buildNumber.properties +++ b/buildNumber.properties @@ -1,3 +1,3 @@ #maven.buildNumber.plugin properties file -#Sun Jul 07 21:24:47 EDT 2024 -buildNumber=180 +#Fri Jul 12 17:46:19 EDT 2024 +buildNumber=182 diff --git a/pom.xml b/pom.xml index 594983f4..9ff6d560 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,7 @@ 1.5.7 1.86.11 23.1.3 + 5.10.3 @@ -166,9 +167,9 @@ - junit - junit - 4.12 + org.junit.jupiter + junit-jupiter + ${junit.version} diff --git a/src/main/java/electrosphere/entity/Scene.java b/src/main/java/electrosphere/entity/Scene.java index 2ae5839e..bdaf6a85 100644 --- a/src/main/java/electrosphere/entity/Scene.java +++ b/src/main/java/electrosphere/entity/Scene.java @@ -3,23 +3,16 @@ package electrosphere.entity; import electrosphere.engine.Globals; import electrosphere.entity.btree.BehaviorTree; import electrosphere.entity.types.attach.AttachUtils; -import electrosphere.logger.LoggerInterface; -import java.lang.reflect.Field; import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; -import org.joml.Vector3d; -import org.joml.Vector3f; /** - * - * @author satellite + * A game scene */ public class Scene { diff --git a/src/test/java/TestRunner.java b/src/test/java/TestRunner.java deleted file mode 100644 index 0da4af27..00000000 --- a/src/test/java/TestRunner.java +++ /dev/null @@ -1,50 +0,0 @@ -import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; - -import org.junit.runner.JUnitCore; -import org.junit.runner.Result; -import org.junit.runner.notification.Failure; - -import entity.SpawningCreaturesTest; -import startup.StartupTest; -import testutils.TestEngineUtils; - -public class TestRunner { - - /** - * The list of classes to run tests for - */ - static final List> classes = Arrays.asList(new Class[]{ - StartupTest.class, - SpawningCreaturesTest.class, - }); - - /** - * Runs tests - * @param args Args provided - */ - public static void main(String[] args){ - - boolean success = true; - List failures = new LinkedList(); - - //run tests - for(Class classObject : classes){ - TestEngineUtils.log("CLASS " + classObject.getCanonicalName()); - Result result = JUnitCore.runClasses(classObject); - for(Failure failure : result.getFailures()){ - failures.add(failure); - } - success = success && result.wasSuccessful(); - } - - //print failures - for(Failure failure : failures){ - TestEngineUtils.log(failure + ""); - } - - TestEngineUtils.log("Testing was successful: " + success); - } - -} diff --git a/src/test/java/electrosphere/renderer/ui/elements/WindowIntegrationTest.java b/src/test/java/electrosphere/renderer/ui/elements/WindowIntegrationTest.java index c66a0a4f..41e0af92 100644 --- a/src/test/java/electrosphere/renderer/ui/elements/WindowIntegrationTest.java +++ b/src/test/java/electrosphere/renderer/ui/elements/WindowIntegrationTest.java @@ -2,9 +2,10 @@ package electrosphere.renderer.ui.elements; import java.awt.image.BufferedImage; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; import electrosphere.engine.Globals; import electrosphere.engine.Main; @@ -18,7 +19,8 @@ import testutils.TestEngineUtils; */ public class WindowIntegrationTest { - @Before + @BeforeEach + @Tag("integration") /** * Initializes the engine */ @@ -27,6 +29,7 @@ public class WindowIntegrationTest { } @Test + @Tag("integration") /** * Tests creating a window */ @@ -39,7 +42,8 @@ public class WindowIntegrationTest { } - @After + @AfterEach + @Tag("integration") /** * Shuts down the engine */ diff --git a/src/test/java/entity/SpawningCreaturesTest.java b/src/test/java/entity/SpawningCreaturesTest.java index 7cbf1c0d..408c9c3c 100644 --- a/src/test/java/entity/SpawningCreaturesTest.java +++ b/src/test/java/entity/SpawningCreaturesTest.java @@ -1,7 +1,7 @@ package entity; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import electrosphere.engine.Globals; import electrosphere.engine.Main; @@ -11,7 +11,7 @@ import electrosphere.net.NetUtils; public class SpawningCreaturesTest { - @Before + @BeforeEach public void initEngine(){ LoggerInterface.loggerEngine.INFO("[Test] Spawn many creatures"); Globals.RUN_CLIENT = true; diff --git a/src/test/java/startup/StartupTest.java b/src/test/java/startup/StartupTest.java index 6a450e9f..08817f67 100644 --- a/src/test/java/startup/StartupTest.java +++ b/src/test/java/startup/StartupTest.java @@ -1,14 +1,14 @@ package startup; -import org.junit.Test; + +import org.junit.jupiter.api.Test; import electrosphere.engine.Globals; import electrosphere.engine.Main; import electrosphere.engine.profiler.Profiler; import electrosphere.logger.LoggerInterface; import electrosphere.net.NetUtils; -import junit.framework.TestCase; -public class StartupTest extends TestCase { +public class StartupTest { @Test public void testStartupHeadless(){