From dfd5932675232c8f6b22ba30e8e2fdd2651de98d Mon Sep 17 00:00:00 2001 From: austin Date: Fri, 16 May 2025 12:51:08 -0400 Subject: [PATCH] script recompilation work --- assets/Scripts/client/clienthooks.ts | 9 ++++++ assets/Scripts/compiler/context.js | 9 ++++++ assets/Scripts/engine/engine-init.ts | 3 ++ docs/src/progress/renderertodo.md | 1 + .../electrosphere/script/ScriptContext.java | 18 ++++++++---- .../electrosphere/script/ScriptEngine.java | 28 ++++++++++++++++--- 6 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 assets/Scripts/compiler/context.js diff --git a/assets/Scripts/client/clienthooks.ts b/assets/Scripts/client/clienthooks.ts index 51c7ae07..5c5e6a7c 100644 --- a/assets/Scripts/client/clienthooks.ts +++ b/assets/Scripts/client/clienthooks.ts @@ -66,9 +66,18 @@ export const clientHooks: Hook[] = [ engine.classes.areaUtils.static.selectAreaRectangular() } }, + { + signal: "ROOM_TOOL_TOGGLE", + callback: (engine: Engine) => { + engine.singletons.loggerScripts.WARNING('asdf') + engine.singletons.loggerScripts.WARNING(JSON.stringify(globalThis)) + } + }, { signal: "ROOM_TOOL_MENU", callback: (engine: Engine) => { + engine.singletons.loggerScripts.WARNING('asdf') + engine.singletons.loggerScripts.WARNING(JSON.stringify(globalThis)) engine.classes.menuUtils.static.openDialog('Data/menu/room/roomToolConfig.html') } }, diff --git a/assets/Scripts/compiler/context.js b/assets/Scripts/compiler/context.js new file mode 100644 index 00000000..a28df400 --- /dev/null +++ b/assets/Scripts/compiler/context.js @@ -0,0 +1,9 @@ + + +/** + * The top level variable that should be used to store data + */ +let Document = { + +} + diff --git a/assets/Scripts/engine/engine-init.ts b/assets/Scripts/engine/engine-init.ts index 0c2b08c2..bc6fc2d6 100644 --- a/assets/Scripts/engine/engine-init.ts +++ b/assets/Scripts/engine/engine-init.ts @@ -19,6 +19,9 @@ export const engine: Engine = { playerState: defaultPlayerState, } +//store engine in globalThis +globalThis.engine = engine; + /** * Called when the script engine first initializes */ diff --git a/docs/src/progress/renderertodo.md b/docs/src/progress/renderertodo.md index dc6f1bc7..fe2530c5 100644 --- a/docs/src/progress/renderertodo.md +++ b/docs/src/progress/renderertodo.md @@ -1843,6 +1843,7 @@ Capsule-BlockChunk collision correction in collidable trees Out-of-context typescript compilation that falls back to in-context compilation Fix opengl bug Fix typescript out-of-context compilation caching +Script recompilation work diff --git a/src/main/java/electrosphere/script/ScriptContext.java b/src/main/java/electrosphere/script/ScriptContext.java index 92211c89..2e603552 100644 --- a/src/main/java/electrosphere/script/ScriptContext.java +++ b/src/main/java/electrosphere/script/ScriptContext.java @@ -281,9 +281,17 @@ public class ScriptContext { { String pathRaw = toWriteFile.toPath() + ""; pathRaw = pathRaw.replace(".\\.cache\\tscache\\src\\", "./assets/"); - File correspondingFile = new File(pathRaw.replace(".\\.cache\\tscache\\src\\", "./assets/")); String cacheKey = pathRaw.replace("./assets", "").replace("\\", "/"); - checksumMap.getFileLastModifyMap().put(cacheKey, correspondingFile.lastModified() + ""); + long lastModified = 0; + try { + File correspondingSourceFile = new File(pathRaw.replace(".cache\\tscache\\src\\", "./assets/").replace(".js",".ts")); + FileTime time = Files.getLastModifiedTime(correspondingSourceFile.toPath()); + lastModified = time.toMillis(); + } catch (IOException e) { + throw new Error("Failed to gather last modified time! " + lastModified); + } + checksumMap.getFileLastModifyMap().put(cacheKey, lastModified + ""); + LoggerInterface.loggerScripts.DEBUG("Putting file in cache " + cacheKey + " " + lastModified); } //write the actual file @@ -327,11 +335,11 @@ public class ScriptContext { if(!currPath.toFile().isDirectory()){ String pathRaw = currPath.toString(); pathRaw = pathRaw.replace(".cache\\tscache\\src\\", "./assets/"); - File correspondingFile = new File(pathRaw.replace(".cache\\tscache\\src\\", "./assets/")); String cacheKey = pathRaw.replace("./assets", "").replace("\\", "/"); - long lastModified = correspondingFile.lastModified(); + long lastModified = 0; try { - FileTime time = Files.getLastModifiedTime(currPath); + File correspondingSourceFile = new File(pathRaw.replace(".cache\\tscache\\src\\", "./assets/").replace(".js",".ts")); + FileTime time = Files.getLastModifiedTime(correspondingSourceFile.toPath()); lastModified = time.toMillis(); } catch (IOException e) { throw new Error("Failed to gather last modified time! " + lastModified); diff --git a/src/main/java/electrosphere/script/ScriptEngine.java b/src/main/java/electrosphere/script/ScriptEngine.java index 2ac68acd..a7f90f87 100644 --- a/src/main/java/electrosphere/script/ScriptEngine.java +++ b/src/main/java/electrosphere/script/ScriptEngine.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.nio.file.FileSystem; import java.nio.file.Files; import java.nio.file.WatchService; +import java.nio.file.attribute.FileTime; import java.security.NoSuchAlgorithmException; import java.util.HashMap; import java.util.Map; @@ -96,6 +97,9 @@ public class ScriptEngine extends SignalServiceImpl { "Scripts/compiler/file_resolution.js", "Scripts/compiler/compiler.js", "Scripts/compiler/host_access.js", + + //global context + "Scripts/compiler/context.js", }; /** @@ -178,9 +182,13 @@ public class ScriptEngine extends SignalServiceImpl { //compile if(!readCache){ + LoggerInterface.loggerScripts.WARNING("Recompiling scripts"); if(!scriptContext.compileOutsideContext()){ scriptContext.compileInContext(); } + if(!this.initCache()){ + throw new Error("Failed to compile!"); + } } //post init logic @@ -272,13 +280,25 @@ public class ScriptEngine extends SignalServiceImpl { String relativePath = FileUtils.relativize(file, tsCache); String normalizedPath = "/" + relativePath; boolean shouldLoad = true; - File correspondingSourceFile = FileUtils.getAssetFile(compoundedPath + "/" + file.getName()); + File correspondingSourceFile = FileUtils.getAssetFile(compoundedPath + "/" + file.getName().replace(".js", ".ts")); + + long lastModified = 0; + if(correspondingSourceFile.exists()){ + try { + FileTime time = Files.getLastModifiedTime(correspondingSourceFile.toPath()); + lastModified = time.toMillis(); + } catch (IOException e) { + throw new Error("Failed to gather last modified time! " + lastModified); + } + } else { + shouldLoad = false; + } //determine if we should load the file if(!fileLastModifyMap.containsKey(normalizedPath)){ //cache does not contain this file shouldLoad = false; - } else if(!fileLastModifyMap.get(normalizedPath).contains(correspondingSourceFile.lastModified() + "")) { + } else if(!fileLastModifyMap.get(normalizedPath).contains(lastModified + "")) { //cache is up to date shouldLoad = false; } @@ -300,9 +320,9 @@ public class ScriptEngine extends SignalServiceImpl { boolean inMap = fileLastModifyMap.containsKey(normalizedPath); boolean timeMatch = false; if(inMap){ - timeMatch = fileLastModifyMap.get(normalizedPath).contains(correspondingSourceFile.lastModified() + ""); + timeMatch = fileLastModifyMap.get(normalizedPath).contains(lastModified + ""); } - LoggerInterface.loggerScripts.DEBUG("Skipping Preload: " + normalizedPath + " " + inMap + " " + timeMatch); + LoggerInterface.loggerScripts.DEBUG("Skipping Preload: " + normalizedPath + " " + inMap + " " + timeMatch + " " + correspondingSourceFile.exists() + " " + lastModified + " " + correspondingSourceFile.toString()); rVal = false; } } catch (IOException e) {