From e7cdc6d71bf7eaaf67215f5c9e1e3dd8e5199006 Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Wed, 19 Feb 2020 23:24:40 -0700 Subject: [PATCH 01/29] Updating readme --- README.md | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index edd5de50..8c74f479 100644 --- a/README.md +++ b/README.md @@ -15,25 +15,28 @@ Most linux servers already use OpenJDK, however if you're running windows or wan GraalVM-CE is a free open source java runtime created by Oracle that is pre-packaged with the GraalJS Engine allowing all javascript engine features including AOT compilation of javascript. -## Installing The Plugin -- Ensure your server is running OpenJDK 8+ or GraalVM -- [Download the latest ScriptableMC plugin](https://github.com/astorks/ScriptableMC-Engine/releases/latest) and place it in your plugins folder. -- Take a look at [ScriptableMC-TypeScript](https://github.com/astorks/ScriptableMC-TypeScript) for a full typescript plugin example. +## Installing The JavaScript Engine Plugin +##### If your server is running on a Standard JDK +- [Download the latest `ScriptableMC-Engine-JS-Bundled.jar`](https://github.com/astorks/ScriptableMC-Engine/releases/latest) and place it in your plugins folder. +##### If your server is running on a GraalVM JDK +- [Download the latest `ScriptableMC-Engine-JS.jar`](https://github.com/astorks/ScriptableMC-Engine/releases/latest) and place it in your plugins folder. + +Take a look at [ScriptableMC-TypeScript](https://github.com/astorks/ScriptableMC-TypeScript) for a full typescript plugin example. You can take the typescript example and compile it, then directly modify the javascript if you don't want to use typescript. ## Commands and Permissions ##### ScritableMC Base Command | Command | Alias | Description | Permission | | ------------- | ------------- |-----------------------------------------------------| ------------------------ | -| `/scriptablemc menu` | `/smc m` | Opens an inventory menu that allows you to control the scriptable engine. | `scriptablemc.menu` | -| `/scriptablemc info` | `/smc i` | Prints plugin version and GraalVM/GraalJS versions if available. | `scriptablemc.info` | -| `/scriptablemc reload` | `/smc rl` | Fully reloads all script engines. | `scriptablemc.reload` | +| `/scriptablemc menu` | `/smc m` | Opens an inventory menu that allows you to control the script engines. | `scriptablemc.menu` | +| `/scriptablemc info` | `/smc i` | Prints plugin version and all loaded script engine versions if available. | `scriptablemc.info` | +| `/scriptablemc reload` | `/smc rl` | Fully reloads all script engines and all script files. | `scriptablemc.reload` | | `/scriptablemc version` | `/smc v` | Check the github releases for any updates. | `scriptablemc.version` | -##### ScritableMC JavaScript Sub Command +##### ScritableMC JavaScript Engine Commands | Command | Alias | Description | Permission | | ------------- | ------------- |-----------------------------------------------------| ------------------------ | -| `/scriptablemc reload` | `/smc rl /jsrl` | Fully reloads the script engine and all script files. | `scriptablemc.reload` | +| `/scriptablemc javascript reload` | `/smc rl /jsrl` | Fully reloads the javascript engine and all script files. | `scriptablemc.js.reload` | | `/scriptablemc javascript execute ` | `/smc js ex ` | Executes javascript source and prints the return value. | `scriptablemc.js.execute` | | `/scriptablemc javascript execute stash` | `/smc js ex stash` | Executes javascript source stored in your stash. | `scriptablemc.js.execute` | | `/scriptablemc javascript file ` | `/smc js f ` | Executes javascript file from the scripts folder. | `scriptablemc.js.execute.file` | @@ -41,6 +44,17 @@ You can take the typescript example and compile it, then directly modify the jav | `/scriptablemc javascript stash ` | `/smc js st ` | Adds a line to your javascript stash. | `scriptablemc.js.execute` | | `/scriptablemc javascript stash clear` | `/smc js st clear` | Clears your javascript stash. | `scriptablemc.js.execute` | +##### ScritableMC Python Engine Commands +| Command | Alias | Description | Permission | +| ------------- | ------------- |-----------------------------------------------------| ------------------------ | +| `/scriptablemc python reload` | `/smc rl /pyrl` | Fully reloads the python script engine and all script files. | `scriptablemc.python.reload` | +| `/scriptablemc python execute ` | `/smc py ex /pyex` | Executes python source and prints the return value. | `scriptablemc.python.execute` | +| `/scriptablemc python execute stash` | `/smc py ex stash` | Executes python source stored in your stash. | `scriptablemc.python.execute` | +| `/scriptablemc python file ` | `/smc py f ` | Executes python file from the scripts folder. | `scriptablemc.python.execute.file` | +| `/scriptablemc python stash` | `/smc py st` | Prints all stored python lines in your stash. | `scriptablemc.python.execute` | +| `/scriptablemc python stash ` | `/smc py st ` | Adds a line to your python stash. | `scriptablemc.python.execute` | +| `/scriptablemc python stash clear` | `/smc py st clear` | Clears your python stash. | `scriptablemc.python.execute` | + ### `/smc javascript execute` Command Example > `/jsex return 1 + 1`
> ![2](https://i.imgur.com/1MkiDxW.png) @@ -54,4 +68,5 @@ https://github.com/MinusKube/SmartInvs - Inventory helper library
https://github.com/lucko/helper - MinecraftVersions helper library
https://github.com/jkcclemens/khttp - HTTP helper library
https://github.com/aikar/commands - Built-in commands and auto-completion
+https://github.com/apache/commons-io - FileUtils helper library
https://github.com/graalvm/graaljs - JavaScript engine From 13d40f083cc05b4b11cc8fea1136d0429635a12c Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Wed, 19 Feb 2020 23:26:41 -0700 Subject: [PATCH 02/29] Updating readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8c74f479..d56970e8 100644 --- a/README.md +++ b/README.md @@ -17,9 +17,9 @@ GraalVM-CE is a free open source java runtime created by Oracle that is pre-pack ## Installing The JavaScript Engine Plugin ##### If your server is running on a Standard JDK -- [Download the latest `ScriptableMC-Engine-JS-Bundled.jar`](https://github.com/astorks/ScriptableMC-Engine/releases/latest) and place it in your plugins folder. +- Download the latest [`ScriptableMC-Engine-JS-Bundled.jar`](https://github.com/astorks/ScriptableMC-Engine/releases/latest/download/ScriptableMC-Engine-JS-Bundled.jar) and place it in your plugins folder. ##### If your server is running on a GraalVM JDK -- [Download the latest `ScriptableMC-Engine-JS.jar`](https://github.com/astorks/ScriptableMC-Engine/releases/latest) and place it in your plugins folder. +- Download the latest [`ScriptableMC-Engine-JS.jar`](https://github.com/astorks/ScriptableMC-Engine/releases/latest/download/ScriptableMC-Engine-JS.jar) and place it in your plugins folder. Take a look at [ScriptableMC-TypeScript](https://github.com/astorks/ScriptableMC-TypeScript) for a full typescript plugin example. You can take the typescript example and compile it, then directly modify the javascript if you don't want to use typescript. From 97803e3d53636543018ef9cd24511385547c0680 Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Wed, 19 Feb 2020 23:50:37 -0700 Subject: [PATCH 03/29] Added commonjs build step to libraries, for use later. --- .github/workflows/build-dev.yml | 4 ++++ README.md | 1 + .../com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt | 3 ++- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build-dev.yml b/.github/workflows/build-dev.yml index d7bfcf7f..b1592717 100644 --- a/.github/workflows/build-dev.yml +++ b/.github/workflows/build-dev.yml @@ -28,10 +28,13 @@ jobs: cd ./ScriptableMC-Tools-TS/lib/ npm install npm run compile + npm run compile_commonjs cd ./ts/ zip -r ../ScriptableMC-TypeScript-Lib.zip ./* cd ../js/ zip -r ../ScriptableMC-JavaScript-Lib.zip ./* + cd ../commonjs/ + zip -r ../ScriptableMC-CommonJS-Lib.zip ./* cd ../ cp ./ScriptableMC-JavaScript-Lib.zip ../../ScriptableMC-Engine-JS/src/main/resources/libraries.zip - name: Build All Plugins with Gradle @@ -45,6 +48,7 @@ jobs: cp ./build/ScriptableMC-Engine-PY-Bundled.jar ./artifacts/ cp ./ScriptableMC-Tools-TS/lib/ScriptableMC-TypeScript-Lib.zip ./artifacts/ cp ./ScriptableMC-Tools-TS/lib/ScriptableMC-JavaScript-Lib.zip ./artifacts/ + cp ./ScriptableMC-Tools-TS/lib/ScriptableMC-CommonJS-Lib.zip ./artifacts/ - uses: actions/upload-artifact@v1 with: name: ScriptableMC-Engine-DEV diff --git a/README.md b/README.md index d56970e8..69f12399 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ [![GitHub release (latest by date)](https://img.shields.io/github/v/release/astorks/ScriptableMC-Engine?label=ScriptableMC&style=for-the-badge)](https://github.com/astorks/ScriptableMC-Engine/releases/latest) [![SpigotMC Resource](https://img.shields.io/badge/SpigotMC-Resource-yellow?style=for-the-badge)](https://www.spigotmc.org/resources/scriptablemc-engine.74690/) +![License](https://img.shields.io/github/license/astorks/ScriptableMC-Engine?style=for-the-badge) Run JavaScript/TypeScript plugins for Minecraft 1.15 using the GraalJS script engine.
Supported Minecraft Versions: Bukkit/Spigot/Paper 1.12, 1.13, 1.14, **1.15**
diff --git a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt index b7604754..b58e05e2 100644 --- a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt +++ b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt @@ -376,7 +376,8 @@ class TypescriptLibraryExporter { " \"version\": \"1.0.0\",\n" + " \"description\": \"Typescript plugin example and libraries for Minecraft 1.15\",\n" + " \"scripts\": {\n" + - " \"compile\": \"npx tsc\"\n" + + " \"compile\": \"npx tsc\",\n" + + " \"compile_commonjs\": \"npx tsc -m commonjs --outDir ./commonjs\"\n" + " },\n" + " \"author\": \"Ashton Storks\",\n" + " \"license\": \"ISC\",\n" + From e447c2fb9c7faeb85928fbb731e51726223d294e Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Thu, 20 Feb 2020 15:04:39 -0700 Subject: [PATCH 04/29] Bumped version to v1.2.1. Added CommonJS config options. Better config documentation. Added placeholders to read other config vars inside a config can be used like this: $\{config_val_name}. --- .../scriptablemc/ScriptEngineConfig.kt | 42 +++++++++++++++++++ .../scriptablemc/SMCJavaScriptConfig.kt | 28 ++++--------- .../core/JavaScriptPluginEngine.kt | 40 ++++++++++++++---- .../src/main/resources/config.yml | 25 ++++++++++- .../src/main/resources/plugin.yml | 2 +- .../pixlfox/scriptablemc/SMCPythonConfig.kt | 23 +--------- .../scriptablemc/core/PythonPluginEngine.kt | 12 +++--- .../src/main/resources/config.yml | 5 +++ .../src/main/resources/plugin.yml | 2 +- build.gradle.kts | 2 +- 10 files changed, 120 insertions(+), 61 deletions(-) create mode 100644 ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/ScriptEngineConfig.kt diff --git a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/ScriptEngineConfig.kt b/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/ScriptEngineConfig.kt new file mode 100644 index 00000000..7f063f58 --- /dev/null +++ b/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/ScriptEngineConfig.kt @@ -0,0 +1,42 @@ +package com.pixlfox.scriptablemc + +import org.bukkit.configuration.file.FileConfiguration + +abstract class ScriptEngineConfig(private val config: FileConfiguration) { + val rootScriptsFolder: String + get() = readConfigString("root_scripts_folder", "./scripts") + + val debug: Boolean + get() = readConfigBoolean("debug", false) + + val extractLibs: Boolean + get() = readConfigBoolean("extract_libs", true) + + val debugger: ScriptEngineDebuggerConfig + get() = ScriptEngineDebuggerConfig(this) + + fun readConfigString(path: String, def: String = ""): String { + val input = config.getString(path, def).orEmpty() + val regex = Regex("\\\$\\{(.*)}") + + return regex.replace(input) { + val configValue = readConfigString(it.groups[1]!!.value) + configValue + } + } + + fun readConfigBoolean(path: String, def: Boolean = false): Boolean { + return readConfigString(path, def.toString()).equals("true", true) + } +} + +class ScriptEngineDebuggerConfig(private val config: ScriptEngineConfig) { + val enabled: Boolean + get() = config.readConfigBoolean("debugger.enabled", false) + + val address: String + get() = config.readConfigString("debugger.address", "127.0.0.1:9229") + + val waitAttached: Boolean + get() = config.readConfigBoolean("debugger.wait_attached", true) +} \ No newline at end of file diff --git a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptConfig.kt b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptConfig.kt index 7abdadc3..ad70ccf6 100644 --- a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptConfig.kt +++ b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptConfig.kt @@ -2,27 +2,13 @@ package com.pixlfox.scriptablemc import org.bukkit.configuration.file.FileConfiguration -class SMCJavaScriptConfig(private val config: FileConfiguration) { - val rootScriptsFolder: String - get() = config.getString("root_scripts_folder", "./scripts").orEmpty() +class SMCJavaScriptConfig(config: FileConfiguration) : ScriptEngineConfig(config) { + val commonJsModulesEnabled: Boolean + get() = readConfigBoolean("common_js.modules_enabled", true) - val debug: Boolean - get() = config.getBoolean("debug", false) + val commonJsModulesPath: String + get() = readConfigString("common_js.modules_path", "\${root_scripts_folder}/node_modules") - val extractLibs: Boolean - get() = config.getBoolean("extract_libs", true) - - val debugger: SMCJavaScriptDebuggerConfig - get() = SMCJavaScriptDebuggerConfig(config) -} - -class SMCJavaScriptDebuggerConfig(private val config: FileConfiguration) { - val enabled: Boolean - get() = config.getBoolean("debugger.enabled", false) - - val address: String - get() = config.getString("debugger.address", "127.0.0.1:9229").orEmpty() - - val waitAttached: Boolean - get() = config.getBoolean("debugger.wait_attached", true) + val commonJsGlobalsPath: String + get() = readConfigString("common_js.globals_path", "\${root_scripts_folder}/__globals__.js") } \ No newline at end of file diff --git a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt index 99e1ddeb..df01d727 100644 --- a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt +++ b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt @@ -43,17 +43,41 @@ class JavaScriptPluginEngine(override val bootstrapPlugin: ScriptEngineMain, pri .allowCreateThread(true) .option("js.ecmascript-version", "2020") - contextBuilder = contextBuilder - .option("js.commonjs-require", "true") + if(config.commonJsModulesEnabled) { + if(config.debug) { + bootstrapPlugin.logger.info("Enabling CommonJS support...") + } - if(File("${config.rootScriptsFolder}/node_modules/").exists()) { - contextBuilder = contextBuilder - .option("js.commonjs-require-cwd", "${config.rootScriptsFolder}/node_modules") - } + if (!File(config.commonJsModulesPath).exists()) { + if(config.debug) { + bootstrapPlugin.logger.info("CommonJS creating modules folder: ${config.commonJsModulesPath}") + } + File(config.commonJsModulesPath).mkdirs() + } + else { + if(config.debug) { + bootstrapPlugin.logger.info("CommonJS using modules folder: ${config.commonJsModulesPath}") + } + } - if(File("${config.rootScriptsFolder}/__globals__.js").exists()) { contextBuilder = contextBuilder - .option("js.commonjs-global-properties", "${config.rootScriptsFolder}/__globals__.js") + .option("js.commonjs-require", "true") + .option("js.commonjs-require-cwd", config.commonJsModulesPath) + + if (File(config.commonJsGlobalsPath).exists()) { + if(config.debug) { + bootstrapPlugin.logger.info("CommonJS using globals: ${config.commonJsGlobalsPath}") + } + contextBuilder = contextBuilder + .option("js.commonjs-global-properties", config.commonJsGlobalsPath) + } + else { + if(config.debug) { + bootstrapPlugin.logger.warning("CommonJS unable to read globals: ${config.commonJsGlobalsPath}") + } + } + + bootstrapPlugin.logger.info("CommonJS support enabled.") } if(config.debugger.enabled) { diff --git a/ScriptableMC-Engine-JS/src/main/resources/config.yml b/ScriptableMC-Engine-JS/src/main/resources/config.yml index ebd32987..1773a99d 100644 --- a/ScriptableMC-Engine-JS/src/main/resources/config.yml +++ b/ScriptableMC-Engine-JS/src/main/resources/config.yml @@ -18,7 +18,30 @@ version_check: true # Default: true extract_libs: true +common_js: + # Enable CommonJS `require()` support + # Allows the use of CommonJS NPM modules + # Default: true + enabled: true + + # CommonJS modules path + # The default path to load CommonJS modules from. + # Default: ${root_scripts_folder}/node_modules + modules_path: '${root_scripts_folder}/node_modules' + + # CommonJS globals path + # The path to the CommonJS globals.js file used to define global CommonJS values + # Default: ${common_js.modules_path}/__globals__.js + globals_path: '${common_js.modules_path}/__globals__.js' + +# Chrome debugger settings +# **WARNING** THE DEBUGGER SHOULD NEVER BE ENABLED ON A PUBLIC SERVER +# IT IS INTENDED ONLY FOR DEBUGGING THE SCRIPT ENGINE AND CAN BREAK/FREEZE YOUR WHOLE SERVER +# Default: enabled: false, address: 127.0.0.1:9229, wait_attached: true +# If enabled: true and wait_attached: true the whole minecraft server will freeze until the chrome debugger is attached +# this is useful for debugging the script engine startup +# To enable remote debugging set address: 0.0.0.0:9229 debugger: - enabled: false + enabled: '${debug}' address: 127.0.0.1:9229 wait_attached: true \ No newline at end of file diff --git a/ScriptableMC-Engine-JS/src/main/resources/plugin.yml b/ScriptableMC-Engine-JS/src/main/resources/plugin.yml index a019cdc1..40b1e83a 100644 --- a/ScriptableMC-Engine-JS/src/main/resources/plugin.yml +++ b/ScriptableMC-Engine-JS/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: ScriptableMC-Engine-JS description: 'Run JavaScript/TypeScript plugins for Bukkit/Spigot/Paper Minecraft 1.15' -version: '1.2.0' +version: '1.2.1' api-version: '1.13' author: AStorks main: com.pixlfox.scriptablemc.SMCJavaScriptEngineMain diff --git a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonConfig.kt b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonConfig.kt index 4ce0675d..4ab45601 100644 --- a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonConfig.kt +++ b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonConfig.kt @@ -2,27 +2,6 @@ package com.pixlfox.scriptablemc import org.bukkit.configuration.file.FileConfiguration -class SMCPythonConfig(private val config: FileConfiguration) { - val rootScriptFolder: String - get() = config.getString("root_scripts_folder", "./scripts").orEmpty() +class SMCPythonConfig(config: FileConfiguration) : ScriptEngineConfig(config) { - val debug: Boolean - get() = config.getBoolean("debug", false) - - val extractLibs: Boolean - get() = config.getBoolean("extract_libs", true) - - val debugger: SMCPythonDebuggerConfig - get() = SMCPythonDebuggerConfig(config) -} - -class SMCPythonDebuggerConfig(private val config: FileConfiguration) { - val enabled: Boolean - get() = config.getBoolean("debugger.enabled", false) - - val address: String - get() = config.getString("debugger.address", "127.0.0.1:9229").orEmpty() - - val waitAttached: Boolean - get() = config.getBoolean("debugger.wait_attached", true) } \ No newline at end of file diff --git a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginEngine.kt b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginEngine.kt index 00e42d5d..74fa77ce 100644 --- a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginEngine.kt +++ b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginEngine.kt @@ -22,7 +22,7 @@ class PythonPluginEngine(override val bootstrapPlugin: ScriptEngineMain, private init { if(config.extractLibs) { val librariesResource = bootstrapPlugin.getResource("lib-py.zip") - val libFolder = File("${config.rootScriptFolder}/lib-py") + val libFolder = File("${config.rootScriptsFolder}/lib-py") if (librariesResource != null && !libFolder.exists()) { if(debugEnabled) { bootstrapPlugin.logger.info("Extracting python libraries from ScriptableMC-Engine-PY resources to ${libFolder.path}...") @@ -38,8 +38,8 @@ class PythonPluginEngine(override val bootstrapPlugin: ScriptEngineMain, private .allowHostAccess(HostAccess.ALL) .allowHostClassLoading(true) .allowIO(true) - .option("python.CoreHome", "${config.rootScriptFolder}/lib-py/lib-graalpython/") - .option("python.StdLibHome", "${config.rootScriptFolder}/lib-py/lib-python/3/") + .option("python.CoreHome", "${config.rootScriptsFolder}/lib-py/lib-graalpython/") + .option("python.StdLibHome", "${config.rootScriptsFolder}/lib-py/lib-python/3/") if(config.debugger.enabled) { contextBuilder = contextBuilder @@ -61,7 +61,7 @@ class PythonPluginEngine(override val bootstrapPlugin: ScriptEngineMain, private loadAllHelperClasses() - val mainScriptFile = File("${config.rootScriptFolder}/main.py") + val mainScriptFile = File("${config.rootScriptsFolder}/main.py") if(!mainScriptFile.parentFile.exists()) { mainScriptFile.parentFile.mkdirs() } @@ -102,7 +102,7 @@ class PythonPluginEngine(override val bootstrapPlugin: ScriptEngineMain, private } override fun evalFile(filePath: String): Value { - val scriptFile = File("${config.rootScriptFolder}/$filePath") + val scriptFile = File("${config.rootScriptsFolder}/$filePath") return if(scriptFile.exists()) { eval( @@ -139,7 +139,7 @@ class PythonPluginEngine(override val bootstrapPlugin: ScriptEngineMain, private } override fun evalCommandSender(source: String, sender: CommandSender): Value { - val tempScriptFile = File("${config.rootScriptFolder}/${UUID.randomUUID()}/__init__.py") + val tempScriptFile = File("${config.rootScriptsFolder}/${UUID.randomUUID()}/__init__.py") try { tempScriptFile.parentFile.mkdirs() tempScriptFile.writeText(source) diff --git a/ScriptableMC-Engine-PY/src/main/resources/config.yml b/ScriptableMC-Engine-PY/src/main/resources/config.yml index 12ec47ab..baaa01f6 100644 --- a/ScriptableMC-Engine-PY/src/main/resources/config.yml +++ b/ScriptableMC-Engine-PY/src/main/resources/config.yml @@ -18,6 +18,11 @@ version_check: true # Default: true extract_libs: true +# Chrome debugger settings +# Default: enabled: false, address: 127.0.0.1:9229, wait_attached: true +# If enabled: true and wait_attached: true the whole minecraft server will hang until chrome debugger is attached +# this is useful for debugging the script engine on startup +# To enable remote debugging set address: 0.0.0.0:9229 debugger: enabled: false address: 127.0.0.1:9229 diff --git a/ScriptableMC-Engine-PY/src/main/resources/plugin.yml b/ScriptableMC-Engine-PY/src/main/resources/plugin.yml index 7480341d..59101626 100644 --- a/ScriptableMC-Engine-PY/src/main/resources/plugin.yml +++ b/ScriptableMC-Engine-PY/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: ScriptableMC-Engine-PY description: 'Run Python plugins for Bukkit/Spigot/Paper Minecraft 1.15' -version: '1.2.0' +version: '1.2.1' api-version: '1.13' author: AStorks main: com.pixlfox.scriptablemc.SMCPythonEngineMain diff --git a/build.gradle.kts b/build.gradle.kts index 8d35fd86..1d5a5123 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { allprojects { group = "com.pixlfox.scriptablemc" - version = "1.2.0" + version = "1.2.1" repositories { mavenCentral() From f34cd24b992a8568d35e3cc50226fa0c52ae45e3 Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Thu, 20 Feb 2020 15:28:48 -0700 Subject: [PATCH 05/29] Updated python config docs. Fixed JavaScript config. --- ScriptableMC-Engine-JS/src/main/resources/config.yml | 2 +- ScriptableMC-Engine-PY/src/main/resources/config.yml | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/ScriptableMC-Engine-JS/src/main/resources/config.yml b/ScriptableMC-Engine-JS/src/main/resources/config.yml index 1773a99d..516fa5a3 100644 --- a/ScriptableMC-Engine-JS/src/main/resources/config.yml +++ b/ScriptableMC-Engine-JS/src/main/resources/config.yml @@ -42,6 +42,6 @@ common_js: # this is useful for debugging the script engine startup # To enable remote debugging set address: 0.0.0.0:9229 debugger: - enabled: '${debug}' + enabled: false address: 127.0.0.1:9229 wait_attached: true \ No newline at end of file diff --git a/ScriptableMC-Engine-PY/src/main/resources/config.yml b/ScriptableMC-Engine-PY/src/main/resources/config.yml index baaa01f6..ba5a1214 100644 --- a/ScriptableMC-Engine-PY/src/main/resources/config.yml +++ b/ScriptableMC-Engine-PY/src/main/resources/config.yml @@ -19,9 +19,11 @@ version_check: true extract_libs: true # Chrome debugger settings +# **WARNING** THE DEBUGGER SHOULD NEVER BE ENABLED ON A PUBLIC SERVER +# IT IS INTENDED ONLY FOR DEBUGGING THE SCRIPT ENGINE AND CAN BREAK/FREEZE YOUR WHOLE SERVER # Default: enabled: false, address: 127.0.0.1:9229, wait_attached: true -# If enabled: true and wait_attached: true the whole minecraft server will hang until chrome debugger is attached -# this is useful for debugging the script engine on startup +# If enabled: true and wait_attached: true the whole minecraft server will freeze until the chrome debugger is attached +# this is useful for debugging the script engine startup # To enable remote debugging set address: 0.0.0.0:9229 debugger: enabled: false From 168267fb0b96bce3b5725a49cd3da861afa2ea58 Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Thu, 20 Feb 2020 15:39:16 -0700 Subject: [PATCH 06/29] Added config option to change the main entrypoint file. --- .../kotlin/com/pixlfox/scriptablemc/ScriptEngineConfig.kt | 2 ++ .../kotlin/com/pixlfox/scriptablemc/SMCJavaScriptConfig.kt | 3 +++ .../com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt | 4 ++-- ScriptableMC-Engine-JS/src/main/resources/config.yml | 5 +++++ .../main/kotlin/com/pixlfox/scriptablemc/SMCPythonConfig.kt | 3 ++- .../com/pixlfox/scriptablemc/core/PythonPluginEngine.kt | 4 ++-- ScriptableMC-Engine-PY/src/main/resources/config.yml | 5 +++++ 7 files changed, 21 insertions(+), 5 deletions(-) diff --git a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/ScriptEngineConfig.kt b/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/ScriptEngineConfig.kt index 7f063f58..cea78eaf 100644 --- a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/ScriptEngineConfig.kt +++ b/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/ScriptEngineConfig.kt @@ -6,6 +6,8 @@ abstract class ScriptEngineConfig(private val config: FileConfiguration) { val rootScriptsFolder: String get() = readConfigString("root_scripts_folder", "./scripts") + abstract val mainScriptFile: String + val debug: Boolean get() = readConfigBoolean("debug", false) diff --git a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptConfig.kt b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptConfig.kt index ad70ccf6..db6c8e2c 100644 --- a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptConfig.kt +++ b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptConfig.kt @@ -3,6 +3,9 @@ package com.pixlfox.scriptablemc import org.bukkit.configuration.file.FileConfiguration class SMCJavaScriptConfig(config: FileConfiguration) : ScriptEngineConfig(config) { + override val mainScriptFile: String + get() = readConfigString("main_script_file", "\${root_scripts_folder}/main.js") + val commonJsModulesEnabled: Boolean get() = readConfigBoolean("common_js.modules_enabled", true) diff --git a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt index df01d727..ba7d85c9 100644 --- a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt +++ b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt @@ -101,7 +101,7 @@ class JavaScriptPluginEngine(override val bootstrapPlugin: ScriptEngineMain, pri loadAllHelperClasses() - val mainScriptFile = File("${config.rootScriptsFolder}/main.js") + val mainScriptFile = File(config.mainScriptFile) if(!mainScriptFile.parentFile.exists()) { mainScriptFile.parentFile.mkdirs() } @@ -109,7 +109,7 @@ class JavaScriptPluginEngine(override val bootstrapPlugin: ScriptEngineMain, pri if(mainScriptFile.exists()) { val mainReturn = eval( Source.newBuilder("js", mainScriptFile) - .name("main.js") + .name(mainScriptFile.name) .mimeType("application/javascript+module") .interactive(false) .build() diff --git a/ScriptableMC-Engine-JS/src/main/resources/config.yml b/ScriptableMC-Engine-JS/src/main/resources/config.yml index 516fa5a3..cb8ca922 100644 --- a/ScriptableMC-Engine-JS/src/main/resources/config.yml +++ b/ScriptableMC-Engine-JS/src/main/resources/config.yml @@ -3,6 +3,11 @@ # Default: ./scripts root_scripts_folder: './scripts' +# Main script file +# The main entry point javascript file +# Default: ${root_scripts_folder}/main.js +main_script_file: '${root_scripts_folder}/main.js' + # Prints some additional debugging messages # Default: false debug: false diff --git a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonConfig.kt b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonConfig.kt index 4ab45601..e14c56bd 100644 --- a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonConfig.kt +++ b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonConfig.kt @@ -3,5 +3,6 @@ package com.pixlfox.scriptablemc import org.bukkit.configuration.file.FileConfiguration class SMCPythonConfig(config: FileConfiguration) : ScriptEngineConfig(config) { - + override val mainScriptFile: String + get() = readConfigString("main_script_file", "\${root_scripts_folder}/main.py") } \ No newline at end of file diff --git a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginEngine.kt b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginEngine.kt index 74fa77ce..fc548da5 100644 --- a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginEngine.kt +++ b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginEngine.kt @@ -61,7 +61,7 @@ class PythonPluginEngine(override val bootstrapPlugin: ScriptEngineMain, private loadAllHelperClasses() - val mainScriptFile = File("${config.rootScriptsFolder}/main.py") + val mainScriptFile = File(config.mainScriptFile) if(!mainScriptFile.parentFile.exists()) { mainScriptFile.parentFile.mkdirs() } @@ -69,7 +69,7 @@ class PythonPluginEngine(override val bootstrapPlugin: ScriptEngineMain, private if(mainScriptFile.exists()) { val mainReturn = eval( Source.newBuilder("python", mainScriptFile) - .name("main.py") + .name(mainScriptFile.name) .interactive(false) .build() ) diff --git a/ScriptableMC-Engine-PY/src/main/resources/config.yml b/ScriptableMC-Engine-PY/src/main/resources/config.yml index ba5a1214..b78d3767 100644 --- a/ScriptableMC-Engine-PY/src/main/resources/config.yml +++ b/ScriptableMC-Engine-PY/src/main/resources/config.yml @@ -3,6 +3,11 @@ # Default: ./scripts root_scripts_folder: './scripts' +# Main script file +# The main entry point python script file +# Default: ${root_scripts_folder}/main.py +main_script_file: '${root_scripts_folder}/main.py' + # Prints some additional debugging messages # Default: false debug: false From eb2755c1db6666e3127d13e0809711574f747f30 Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Thu, 20 Feb 2020 16:10:58 -0700 Subject: [PATCH 07/29] Updated dev build to create lib-smc archive. --- .github/workflows/build-dev.yml | 11 ++-- .gitignore | 3 +- .../scriptablemc/TypescriptLibraryExporter.kt | 65 +++++++++++++++++++ 3 files changed, 74 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build-dev.yml b/.github/workflows/build-dev.yml index b1592717..4500f045 100644 --- a/.github/workflows/build-dev.yml +++ b/.github/workflows/build-dev.yml @@ -28,15 +28,18 @@ jobs: cd ./ScriptableMC-Tools-TS/lib/ npm install npm run compile - npm run compile_commonjs cd ./ts/ zip -r ../ScriptableMC-TypeScript-Lib.zip ./* cd ../js/ zip -r ../ScriptableMC-JavaScript-Lib.zip ./* - cd ../commonjs/ - zip -r ../ScriptableMC-CommonJS-Lib.zip ./* cd ../ cp ./ScriptableMC-JavaScript-Lib.zip ../../ScriptableMC-Engine-JS/src/main/resources/libraries.zip + - name: Compile lib-smc and create archive + run: | + cd ./ScriptableMC-Tools-TS/lib-smc/ + npm install + npm run compile + zip -r ./Lib-SMC.zip ./* - name: Build All Plugins with Gradle run: ./gradlew :shadowJarAll - name: Copy artifacts @@ -48,7 +51,7 @@ jobs: cp ./build/ScriptableMC-Engine-PY-Bundled.jar ./artifacts/ cp ./ScriptableMC-Tools-TS/lib/ScriptableMC-TypeScript-Lib.zip ./artifacts/ cp ./ScriptableMC-Tools-TS/lib/ScriptableMC-JavaScript-Lib.zip ./artifacts/ - cp ./ScriptableMC-Tools-TS/lib/ScriptableMC-CommonJS-Lib.zip ./artifacts/ + cp ./ScriptableMC-Tools-TS/lib-smc/Lib-SMC.zip ./artifacts/ - uses: actions/upload-artifact@v1 with: name: ScriptableMC-Engine-DEV diff --git a/.gitignore b/.gitignore index 929cb5f1..e19ebaef 100644 --- a/.gitignore +++ b/.gitignore @@ -42,4 +42,5 @@ node_modules/ .rpt2_cache/ libraries/tools/kotlin-test-nodejs-runner/lib/ local.properties -lib \ No newline at end of file +lib +lib-smc \ No newline at end of file diff --git a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt index b58e05e2..658fa98b 100644 --- a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt +++ b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt @@ -349,6 +349,18 @@ class TypescriptLibraryExporter { return this } + fun exportIndexLibrary(): TypescriptLibraryExporter { + val file = File("$basePath/ts/index.ts") + if(!file.exists()) { + file.parentFile.mkdirs() + file.createNewFile() + file.writeText(generateTypescriptGlobalExports()) + println("Exported index.ts.") + } + + return this + } + fun copyStaticSources(): TypescriptLibraryExporter { File("./src/main/ts/").copyRecursively(File("$basePath/ts/"), true) @@ -394,6 +406,49 @@ class TypescriptLibraryExporter { return this } + fun exportCommonJSProjectFiles(): TypescriptLibraryExporter { + File("$basePath/tsconfig.json").writeText("{\n" + + " \"compilerOptions\": {\n" + + " \"target\": \"es2019\",\n" + + " \"module\": \"commonjs\",\n" + + " \"sourceMap\": false,\n" + + " \"allowJs\": true,\n" + + " \"outDir\": \"js\",\n" + + " \"rootDir\": \"ts\",\n" + + " \"declaration\": true\n" + + " },\n" + + " \"include\": [\n" + + " \"ts/**/*\"\n" + + " ]\n" + + "}") + + File("$basePath/package.json").writeText("{\n" + + " \"name\": \"@astorks/lib-smc\",\n" + + " \"repository\": \"git@github.com:astorks/ScriptableMC-Engine.git\",\n" + + " \"version\": \"1.0.0\",\n" + + " \"description\": \"JavaScript CommonJS libraries for ScriptableMC\",\n" + + " \"publishConfig\": { \"registry\": \"https://npm.pkg.github.com/\" },\n" + + " \"directories\": {\n" + + " \"bin\": \"./js\"\n" + + " },\n" + + " \"scripts\": {\n" + + " \"compile\": \"npx tsc\"\n" + + " },\n" + + " \"author\": \"Ashton Storks\",\n" + + " \"license\": \"ISC\",\n" + + " \"bugs\": {\n" + + " \"url\": \"https://github.com/astorks/ScriptableMC-TypeScript/issues\"\n" + + " },\n" + + " \"homepage\": \"https://github.com/astorks/ScriptableMC-TypeScript#readme\",\n" + + " \"dependencies\": {},\n" + + " \"devDependencies\": {\n" + + " \"typescript\": \"^3.7.5\"\n" + + " }\n" + + "}") + + return this + } + @Suppress("UNCHECKED_CAST") private fun generateTypescriptSource(_class: Class<*>): String { var source = "declare var Java: any;\n" @@ -675,6 +730,16 @@ class TypescriptLibraryExporter { .exportGlobalLibrary() .copyStaticSources() .exportProjectFiles() + + TypescriptLibraryExporter() + .basePath("./lib-smc") + .addHelperClasses() + .addBukkitClasses() + .clean() + .exportLibraries() + .exportIndexLibrary() + .copyStaticSources() + .exportCommonJSProjectFiles() } } } \ No newline at end of file From 83f1a7202175c9a847cd7f96e4a35ab8f644d8e7 Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Thu, 20 Feb 2020 16:57:02 -0700 Subject: [PATCH 08/29] Updated dev build to publish lib-smc to github packages. --- .github/workflows/build-dev.yml | 3 ++- .../scriptablemc/TypescriptLibraryExporter.kt | 20 ++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build-dev.yml b/.github/workflows/build-dev.yml index 4500f045..3742a6f7 100644 --- a/.github/workflows/build-dev.yml +++ b/.github/workflows/build-dev.yml @@ -34,11 +34,12 @@ jobs: zip -r ../ScriptableMC-JavaScript-Lib.zip ./* cd ../ cp ./ScriptableMC-JavaScript-Lib.zip ../../ScriptableMC-Engine-JS/src/main/resources/libraries.zip - - name: Compile lib-smc and create archive + - name: Compile lib-smc, create archive, and publish run: | cd ./ScriptableMC-Tools-TS/lib-smc/ npm install npm run compile + npm run publish zip -r ./Lib-SMC.zip ./* - name: Build All Plugins with Gradle run: ./gradlew :shadowJarAll diff --git a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt index 658fa98b..efff5b18 100644 --- a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt +++ b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt @@ -5,6 +5,7 @@ import com.beust.klaxon.JsonObject import com.beust.klaxon.Parser import com.thoughtworks.paranamer.BytecodeReadingParanamer import com.thoughtworks.paranamer.Paranamer +import org.bukkit.plugin.PluginDescriptionFile import java.io.File import java.lang.reflect.* @@ -422,17 +423,25 @@ class TypescriptLibraryExporter { " ]\n" + "}") + File("$basePath/.npmrc").writeText("//npm.pkg.github.com/:_authToken=\${GITHUB_TOKEN}\n" + + "registry=https://npm.pkg.github.com/astorks") + + val pluginDescription = PluginDescriptionFile(File("../ScriptableMC-Engine-JS/src/main/resources/plugin.yml").inputStream()) + File("$basePath/package.json").writeText("{\n" + - " \"name\": \"@astorks/lib-smc\",\n" + - " \"repository\": \"git@github.com:astorks/ScriptableMC-Engine.git\",\n" + - " \"version\": \"1.0.0\",\n" + + " \"name\": \"lib-smc\",\n" + + " \"version\": \"${pluginDescription.version}\",\n" + " \"description\": \"JavaScript CommonJS libraries for ScriptableMC\",\n" + - " \"publishConfig\": { \"registry\": \"https://npm.pkg.github.com/\" },\n" + + " \"publishConfig\": {\n" + + " \"registry\": \"https://npm.pkg.github.com/\"\n" + + " },\n" + " \"directories\": {\n" + " \"bin\": \"./js\"\n" + " },\n" + " \"scripts\": {\n" + - " \"compile\": \"npx tsc\"\n" + + " \"compile\": \"npx tsc\",\n" + + " \"postcompile\": \"cp ./package.json ./js/package.json && cp ./.npmrc ./js/.npmrc\",\n" + + " \"publish\": \"npm publish ./js/\"\n" + " },\n" + " \"author\": \"Ashton Storks\",\n" + " \"license\": \"ISC\",\n" + @@ -460,6 +469,7 @@ class TypescriptLibraryExporter { return source } + private fun generateTypescriptImports(_class: Class<*>): String { var tsImportsSource = "" From 2bdd2a7aded8028fd335884af0a6c81b6d76079d Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Thu, 20 Feb 2020 17:00:11 -0700 Subject: [PATCH 09/29] Fixed missing environment variable GITHUB_TOKEN. --- .github/workflows/build-dev.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/build-dev.yml b/.github/workflows/build-dev.yml index 3742a6f7..02664001 100644 --- a/.github/workflows/build-dev.yml +++ b/.github/workflows/build-dev.yml @@ -35,6 +35,8 @@ jobs: cd ../ cp ./ScriptableMC-JavaScript-Lib.zip ../../ScriptableMC-Engine-JS/src/main/resources/libraries.zip - name: Compile lib-smc, create archive, and publish + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | cd ./ScriptableMC-Tools-TS/lib-smc/ npm install From 35d6ff201bf977d761d44e24353bb184bfd43743 Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Thu, 20 Feb 2020 17:05:37 -0700 Subject: [PATCH 10/29] Updated dev build. --- .github/workflows/build-dev.yml | 2 +- .../com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build-dev.yml b/.github/workflows/build-dev.yml index 02664001..c0df5fff 100644 --- a/.github/workflows/build-dev.yml +++ b/.github/workflows/build-dev.yml @@ -41,7 +41,7 @@ jobs: cd ./ScriptableMC-Tools-TS/lib-smc/ npm install npm run compile - npm run publish + npm run publish-dev zip -r ./Lib-SMC.zip ./* - name: Build All Plugins with Gradle run: ./gradlew :shadowJarAll diff --git a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt index efff5b18..688c3083 100644 --- a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt +++ b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt @@ -429,8 +429,8 @@ class TypescriptLibraryExporter { val pluginDescription = PluginDescriptionFile(File("../ScriptableMC-Engine-JS/src/main/resources/plugin.yml").inputStream()) File("$basePath/package.json").writeText("{\n" + - " \"name\": \"lib-smc\",\n" + - " \"version\": \"${pluginDescription.version}\",\n" + + " \"name\": \"@astorks/lib-smc\",\n" + + " \"version\": \"1.2.1\",\n" + " \"description\": \"JavaScript CommonJS libraries for ScriptableMC\",\n" + " \"publishConfig\": {\n" + " \"registry\": \"https://npm.pkg.github.com/\"\n" + @@ -441,7 +441,8 @@ class TypescriptLibraryExporter { " \"scripts\": {\n" + " \"compile\": \"npx tsc\",\n" + " \"postcompile\": \"cp ./package.json ./js/package.json && cp ./.npmrc ./js/.npmrc\",\n" + - " \"publish\": \"npm publish ./js/\"\n" + + " \"publish\": \"npm publish ./js/\",\n" + + " \"publish-dev\": \"npm publish ./js/ --tag dev-${GITHUB_SHA}\"\n" + " },\n" + " \"author\": \"Ashton Storks\",\n" + " \"license\": \"ISC\",\n" + From d53491d9b952a7973c13ac388d1b74cd6d289fac Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Thu, 20 Feb 2020 17:08:58 -0700 Subject: [PATCH 11/29] Fixed dev build --- .../com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt index 688c3083..8fcc7dc0 100644 --- a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt +++ b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt @@ -430,7 +430,7 @@ class TypescriptLibraryExporter { File("$basePath/package.json").writeText("{\n" + " \"name\": \"@astorks/lib-smc\",\n" + - " \"version\": \"1.2.1\",\n" + + " \"version\": \"${pluginDescription.version}\",\n" + " \"description\": \"JavaScript CommonJS libraries for ScriptableMC\",\n" + " \"publishConfig\": {\n" + " \"registry\": \"https://npm.pkg.github.com/\"\n" + @@ -442,7 +442,7 @@ class TypescriptLibraryExporter { " \"compile\": \"npx tsc\",\n" + " \"postcompile\": \"cp ./package.json ./js/package.json && cp ./.npmrc ./js/.npmrc\",\n" + " \"publish\": \"npm publish ./js/\",\n" + - " \"publish-dev\": \"npm publish ./js/ --tag dev-${GITHUB_SHA}\"\n" + + " \"publish-dev\": \"npm publish ./js/ --tag dev-\${GITHUB_SHA}\"\n" + " },\n" + " \"author\": \"Ashton Storks\",\n" + " \"license\": \"ISC\",\n" + From ac9e0780dcdfcfaca72383e9c3aa9803e6aa13d1 Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Thu, 20 Feb 2020 17:45:54 -0700 Subject: [PATCH 12/29] Hopefully fixed dev build --- .github/workflows/build-dev.yml | 4 ++-- .../scriptablemc/TypescriptLibraryExporter.kt | 14 +++++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/.github/workflows/build-dev.yml b/.github/workflows/build-dev.yml index c0df5fff..6bf9bc39 100644 --- a/.github/workflows/build-dev.yml +++ b/.github/workflows/build-dev.yml @@ -36,12 +36,12 @@ jobs: cp ./ScriptableMC-JavaScript-Lib.zip ../../ScriptableMC-Engine-JS/src/main/resources/libraries.zip - name: Compile lib-smc, create archive, and publish env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} run: | cd ./ScriptableMC-Tools-TS/lib-smc/ npm install npm run compile - npm run publish-dev + npm run publish zip -r ./Lib-SMC.zip ./* - name: Build All Plugins with Gradle run: ./gradlew :shadowJarAll diff --git a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt index 8fcc7dc0..2d4e8278 100644 --- a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt +++ b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt @@ -427,14 +427,19 @@ class TypescriptLibraryExporter { "registry=https://npm.pkg.github.com/astorks") val pluginDescription = PluginDescriptionFile(File("../ScriptableMC-Engine-JS/src/main/resources/plugin.yml").inputStream()) + val githubSha = System.getenv().getOrElse("GITHUB_SHA") { "" } + + val version = if(githubSha.isNullOrEmpty()) { + pluginDescription.version + } + else { + "${pluginDescription.version}-dev-$githubSha" + } File("$basePath/package.json").writeText("{\n" + " \"name\": \"@astorks/lib-smc\",\n" + - " \"version\": \"${pluginDescription.version}\",\n" + + " \"version\": \"$version\",\n" + " \"description\": \"JavaScript CommonJS libraries for ScriptableMC\",\n" + - " \"publishConfig\": {\n" + - " \"registry\": \"https://npm.pkg.github.com/\"\n" + - " },\n" + " \"directories\": {\n" + " \"bin\": \"./js\"\n" + " },\n" + @@ -442,7 +447,6 @@ class TypescriptLibraryExporter { " \"compile\": \"npx tsc\",\n" + " \"postcompile\": \"cp ./package.json ./js/package.json && cp ./.npmrc ./js/.npmrc\",\n" + " \"publish\": \"npm publish ./js/\",\n" + - " \"publish-dev\": \"npm publish ./js/ --tag dev-\${GITHUB_SHA}\"\n" + " },\n" + " \"author\": \"Ashton Storks\",\n" + " \"license\": \"ISC\",\n" + From 57fbb1bafcc8b9afe0a6de465e47c1ddcc7fc04f Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Thu, 20 Feb 2020 17:50:44 -0700 Subject: [PATCH 13/29] Hopefully fixed dev build --- .github/workflows/build-dev.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build-dev.yml b/.github/workflows/build-dev.yml index 6bf9bc39..4d3d54bb 100644 --- a/.github/workflows/build-dev.yml +++ b/.github/workflows/build-dev.yml @@ -37,6 +37,7 @@ jobs: - name: Compile lib-smc, create archive, and publish env: NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | cd ./ScriptableMC-Tools-TS/lib-smc/ npm install From 22869bafb7b4d3c5ecf99c4592cb1108bf396d6d Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Thu, 20 Feb 2020 17:55:23 -0700 Subject: [PATCH 14/29] Fixed a dumb bug --- .../com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt index 2d4e8278..12ab42f2 100644 --- a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt +++ b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt @@ -446,7 +446,7 @@ class TypescriptLibraryExporter { " \"scripts\": {\n" + " \"compile\": \"npx tsc\",\n" + " \"postcompile\": \"cp ./package.json ./js/package.json && cp ./.npmrc ./js/.npmrc\",\n" + - " \"publish\": \"npm publish ./js/\",\n" + + " \"publish\": \"npm publish ./js/\"\n" + " },\n" + " \"author\": \"Ashton Storks\",\n" + " \"license\": \"ISC\",\n" + From e19af6403ba6335353e4d76aab68c167a44b3b4b Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Thu, 20 Feb 2020 18:01:43 -0700 Subject: [PATCH 15/29] Please work --- .../kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt index 12ab42f2..5603dc3b 100644 --- a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt +++ b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt @@ -438,6 +438,7 @@ class TypescriptLibraryExporter { File("$basePath/package.json").writeText("{\n" + " \"name\": \"@astorks/lib-smc\",\n" + + " \"repository\": \"git@github.com:astorks/ScriptableMC-Engine.git\",\n" + " \"version\": \"$version\",\n" + " \"description\": \"JavaScript CommonJS libraries for ScriptableMC\",\n" + " \"directories\": {\n" + From a433b835daea3c738cb414cfb7b79f86f5ee6ab4 Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Thu, 20 Feb 2020 18:12:40 -0700 Subject: [PATCH 16/29] pls work --- .github/workflows/build-dev.yml | 2 +- .../com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build-dev.yml b/.github/workflows/build-dev.yml index 4d3d54bb..525a4ace 100644 --- a/.github/workflows/build-dev.yml +++ b/.github/workflows/build-dev.yml @@ -42,7 +42,7 @@ jobs: cd ./ScriptableMC-Tools-TS/lib-smc/ npm install npm run compile - npm run publish + npm publish ./js zip -r ./Lib-SMC.zip ./* - name: Build All Plugins with Gradle run: ./gradlew :shadowJarAll diff --git a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt index 5603dc3b..182ee85c 100644 --- a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt +++ b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt @@ -441,13 +441,9 @@ class TypescriptLibraryExporter { " \"repository\": \"git@github.com:astorks/ScriptableMC-Engine.git\",\n" + " \"version\": \"$version\",\n" + " \"description\": \"JavaScript CommonJS libraries for ScriptableMC\",\n" + - " \"directories\": {\n" + - " \"bin\": \"./js\"\n" + - " },\n" + " \"scripts\": {\n" + " \"compile\": \"npx tsc\",\n" + - " \"postcompile\": \"cp ./package.json ./js/package.json && cp ./.npmrc ./js/.npmrc\",\n" + - " \"publish\": \"npm publish ./js/\"\n" + + " \"postcompile\": \"cp ./package.json ./js/package.json && cp ./.npmrc ./js/.npmrc\"\n" + " },\n" + " \"author\": \"Ashton Storks\",\n" + " \"license\": \"ISC\",\n" + From 0f3fe0e5a81f8484bc8cc38872b82e449ec7ad04 Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Thu, 20 Feb 2020 20:34:12 -0700 Subject: [PATCH 17/29] Added github actions build and release support for lib-smc. --- .github/workflows/build-dev.yml | 1 - .github/workflows/release.yml | 9 +++++++++ .../pixlfox/scriptablemc/TypescriptLibraryExporter.kt | 10 +++++----- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build-dev.yml b/.github/workflows/build-dev.yml index 525a4ace..1cc97fd1 100644 --- a/.github/workflows/build-dev.yml +++ b/.github/workflows/build-dev.yml @@ -36,7 +36,6 @@ jobs: cp ./ScriptableMC-JavaScript-Lib.zip ../../ScriptableMC-Engine-JS/src/main/resources/libraries.zip - name: Compile lib-smc, create archive, and publish env: - NPM_TOKEN: ${{ secrets.NPM_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | cd ./ScriptableMC-Tools-TS/lib-smc/ diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c6dbf2a2..5a97897e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -30,6 +30,15 @@ jobs: zip -r ../ScriptableMC-JavaScript-Lib.zip ./* cd ../ cp ./ScriptableMC-JavaScript-Lib.zip ../../ScriptableMC-Engine-JS/src/main/resources/libraries.zip + - name: Compile lib-smc, create archive, and publish + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + cd ./ScriptableMC-Tools-TS/lib-smc/ + npm install + npm run compile + npm publish ./js + zip -r ./Lib-SMC.zip ./* - name: Build JS Engine with Gradle run: ./gradlew :shadowJarAll - name: Upload JavaScript Engine Plugin Jar diff --git a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt index 182ee85c..cc93922c 100644 --- a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt +++ b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt @@ -428,12 +428,12 @@ class TypescriptLibraryExporter { val pluginDescription = PluginDescriptionFile(File("../ScriptableMC-Engine-JS/src/main/resources/plugin.yml").inputStream()) val githubSha = System.getenv().getOrElse("GITHUB_SHA") { "" } + val githubTag = System.getenv().getOrElse("GITHUB_REF") { "" } - val version = if(githubSha.isNullOrEmpty()) { - pluginDescription.version - } - else { - "${pluginDescription.version}-dev-$githubSha" + val version = when { + githubTag.startsWith("v1") -> githubTag.substring(1) + githubSha.isNullOrEmpty() -> pluginDescription.version + else -> "${pluginDescription.version}-dev-$githubSha" } File("$basePath/package.json").writeText("{\n" + From 5e4cc89e8586e0c8d2df2c08b30ed61eceffcbfe Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Thu, 20 Feb 2020 20:49:41 -0700 Subject: [PATCH 18/29] Debugging release build. --- .github/workflows/release.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5a97897e..72ef6054 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -35,6 +35,8 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | cd ./ScriptableMC-Tools-TS/lib-smc/ + echo "$GITHUB_REF" + cat ./package.json npm install npm run compile npm publish ./js From 5418aad2f642f3ee7f08a40e48d2042f1cacd7f2 Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Thu, 20 Feb 2020 20:56:47 -0700 Subject: [PATCH 19/29] Fixed release build. --- .github/workflows/release.yml | 2 -- .../com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 72ef6054..5a97897e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -35,8 +35,6 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | cd ./ScriptableMC-Tools-TS/lib-smc/ - echo "$GITHUB_REF" - cat ./package.json npm install npm run compile npm publish ./js diff --git a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt index cc93922c..ee9f7681 100644 --- a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt +++ b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt @@ -431,7 +431,7 @@ class TypescriptLibraryExporter { val githubTag = System.getenv().getOrElse("GITHUB_REF") { "" } val version = when { - githubTag.startsWith("v1") -> githubTag.substring(1) + githubTag.startsWith("refs/tags/v") -> githubTag.substring(11) githubSha.isNullOrEmpty() -> pluginDescription.version else -> "${pluginDescription.version}-dev-$githubSha" } From a0f16f86cf382129e79e2e68ed4ec48ec517969f Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Thu, 20 Feb 2020 22:42:00 -0700 Subject: [PATCH 20/29] Updated release build script to publish to npm. --- .github/workflows/release.yml | 1 + .../scriptablemc/TypescriptLibraryExporter.kt | 15 +++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5a97897e..defeb55f 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -33,6 +33,7 @@ jobs: - name: Compile lib-smc, create archive, and publish env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} run: | cd ./ScriptableMC-Tools-TS/lib-smc/ npm install diff --git a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt index ee9f7681..d2fbab13 100644 --- a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt +++ b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt @@ -423,19 +423,26 @@ class TypescriptLibraryExporter { " ]\n" + "}") - File("$basePath/.npmrc").writeText("//npm.pkg.github.com/:_authToken=\${GITHUB_TOKEN}\n" + - "registry=https://npm.pkg.github.com/astorks") - val pluginDescription = PluginDescriptionFile(File("../ScriptableMC-Engine-JS/src/main/resources/plugin.yml").inputStream()) val githubSha = System.getenv().getOrElse("GITHUB_SHA") { "" } val githubTag = System.getenv().getOrElse("GITHUB_REF") { "" } + val isReleaseTag = githubTag.startsWith("refs/tags/v") + val version = when { - githubTag.startsWith("refs/tags/v") -> githubTag.substring(11) + isReleaseTag -> githubTag.substring(11) githubSha.isNullOrEmpty() -> pluginDescription.version else -> "${pluginDescription.version}-dev-$githubSha" } + if(isReleaseTag) { + File("$basePath/.npmrc").writeText("//npm.pkg.github.com/:_authToken=\${GITHUB_TOKEN}\n" + + "registry=https://npm.pkg.github.com/astorks") + } + else { + File("$basePath/.npmrc").writeText("//registry.npmjs.org/:_authToken=\${NPM_TOKEN}") + } + File("$basePath/package.json").writeText("{\n" + " \"name\": \"@astorks/lib-smc\",\n" + " \"repository\": \"git@github.com:astorks/ScriptableMC-Engine.git\",\n" + From 58675719a3d0c14253978af6a3d289fe3ce8bab0 Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Thu, 20 Feb 2020 23:33:46 -0700 Subject: [PATCH 21/29] Updated release build script to publish to npm. --- .../com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt index d2fbab13..f9209328 100644 --- a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt +++ b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt @@ -436,11 +436,11 @@ class TypescriptLibraryExporter { } if(isReleaseTag) { - File("$basePath/.npmrc").writeText("//npm.pkg.github.com/:_authToken=\${GITHUB_TOKEN}\n" + - "registry=https://npm.pkg.github.com/astorks") + File("$basePath/.npmrc").writeText("//registry.npmjs.org/:_authToken=\${NPM_TOKEN}") } else { - File("$basePath/.npmrc").writeText("//registry.npmjs.org/:_authToken=\${NPM_TOKEN}") + File("$basePath/.npmrc").writeText("//npm.pkg.github.com/:_authToken=\${GITHUB_TOKEN}\n" + + "registry=https://npm.pkg.github.com/astorks") } File("$basePath/package.json").writeText("{\n" + From db5695101051d699862536be28cfac881651159f Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Fri, 21 Feb 2020 11:23:26 -0700 Subject: [PATCH 22/29] Updated build system. --- .github/workflows/build-dev.yml | 2 + .github/workflows/release.yml | 2 + ScriptableMC-Tools-TS/build.gradle.kts | 17 ++++++- .../scriptablemc/TypescriptLibraryExporter.kt | 50 ++++++++++--------- 4 files changed, 47 insertions(+), 24 deletions(-) diff --git a/.github/workflows/build-dev.yml b/.github/workflows/build-dev.yml index 1cc97fd1..917d309e 100644 --- a/.github/workflows/build-dev.yml +++ b/.github/workflows/build-dev.yml @@ -34,6 +34,8 @@ jobs: zip -r ../ScriptableMC-JavaScript-Lib.zip ./* cd ../ cp ./ScriptableMC-JavaScript-Lib.zip ../../ScriptableMC-Engine-JS/src/main/resources/libraries.zip + - name: Export lib-smc with gradle + run: ./gradlew :ScriptableMC-Tools-TS:generateLibSMC - name: Compile lib-smc, create archive, and publish env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index defeb55f..ce963541 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -30,6 +30,8 @@ jobs: zip -r ../ScriptableMC-JavaScript-Lib.zip ./* cd ../ cp ./ScriptableMC-JavaScript-Lib.zip ../../ScriptableMC-Engine-JS/src/main/resources/libraries.zip + - name: Export lib-smc with gradle + run: ./gradlew :ScriptableMC-Tools-TS:generateLibSMCRelease - name: Compile lib-smc, create archive, and publish env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/ScriptableMC-Tools-TS/build.gradle.kts b/ScriptableMC-Tools-TS/build.gradle.kts index 132204bd..5e3f17a3 100644 --- a/ScriptableMC-Tools-TS/build.gradle.kts +++ b/ScriptableMC-Tools-TS/build.gradle.kts @@ -57,5 +57,20 @@ tasks.register("exportTypeScriptLibraries") { group = "libraries" main = "com.pixlfox.scriptablemc.TypescriptLibraryExporter" classpath = sourceSets["main"].runtimeClasspath - //classpath = sourceSets["main"].java.srcDirs +} + +tasks.register("generateLibSMC") { + dependsOn(":ScriptableMC-Tools-TS:classes") + group = "libraries" + main = "com.pixlfox.scriptablemc.TypescriptLibraryExporter" + args = mutableListOf("--lib-smc") + classpath = sourceSets["main"].runtimeClasspath +} + +tasks.register("generateLibSMCRelease") { + dependsOn(":ScriptableMC-Tools-TS:classes") + group = "libraries" + main = "com.pixlfox.scriptablemc.TypescriptLibraryExporter" + args = mutableListOf("--lib-smc", "--release") + classpath = sourceSets["main"].runtimeClasspath } \ No newline at end of file diff --git a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt index f9209328..c8a5f54a 100644 --- a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt +++ b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt @@ -11,11 +11,12 @@ import java.lang.reflect.* @Suppress("MemberVisibilityCanBePrivate", "UnstableApiUsage", "unused") -class TypescriptLibraryExporter { +class TypescriptLibraryExporter(private var args: Array = arrayOf()) { private var basePath: String = "./lib" private val classList = mutableListOf>() private var allowedPackagesRegex: Regex = Regex("(org\\.bukkit|com\\.pixlfox|com\\.smc|fr\\.minuskube\\.inv|com\\.google|java\\.sql|java\\.io|java\\.nio|khttp|org\\.apache\\.commons\\.io)(.*)?") private val paranamer: Paranamer = BytecodeReadingParanamer() + private val isRelease: Boolean = args.contains("--release") private fun safeName(name: String): String = when { name.equals("function", true) -> "_function" @@ -427,15 +428,15 @@ class TypescriptLibraryExporter { val githubSha = System.getenv().getOrElse("GITHUB_SHA") { "" } val githubTag = System.getenv().getOrElse("GITHUB_REF") { "" } - val isReleaseTag = githubTag.startsWith("refs/tags/v") + val isReleaseTag = githubTag.startsWith("refs/tags/v") && isRelease val version = when { isReleaseTag -> githubTag.substring(11) - githubSha.isNullOrEmpty() -> pluginDescription.version + isRelease && githubSha.isNullOrEmpty() -> pluginDescription.version else -> "${pluginDescription.version}-dev-$githubSha" } - if(isReleaseTag) { + if(isRelease) { File("$basePath/.npmrc").writeText("//registry.npmjs.org/:_authToken=\${NPM_TOKEN}") } else { @@ -444,7 +445,7 @@ class TypescriptLibraryExporter { } File("$basePath/package.json").writeText("{\n" + - " \"name\": \"@astorks/lib-smc\",\n" + + " \"name\": \"${if(isRelease) "lib-smc" else "@astorks/lib-smc"}\",\n" + " \"repository\": \"git@github.com:astorks/ScriptableMC-Engine.git\",\n" + " \"version\": \"$version\",\n" + " \"description\": \"JavaScript CommonJS libraries for ScriptableMC\",\n" + @@ -741,24 +742,27 @@ class TypescriptLibraryExporter { companion object { @JvmStatic fun main(args: Array) { - TypescriptLibraryExporter() - .addHelperClasses() - .addBukkitClasses() - .clean() - .exportLibraries() - .exportGlobalLibrary() - .copyStaticSources() - .exportProjectFiles() - - TypescriptLibraryExporter() - .basePath("./lib-smc") - .addHelperClasses() - .addBukkitClasses() - .clean() - .exportLibraries() - .exportIndexLibrary() - .copyStaticSources() - .exportCommonJSProjectFiles() + if(args.contains("--lib-smc")) { + TypescriptLibraryExporter(args) + .basePath("./lib-smc") + .addHelperClasses() + .addBukkitClasses() + .clean() + .exportLibraries() + .exportIndexLibrary() + .copyStaticSources() + .exportCommonJSProjectFiles() + } + else { + TypescriptLibraryExporter(args) + .addHelperClasses() + .addBukkitClasses() + .clean() + .exportLibraries() + .exportGlobalLibrary() + .copyStaticSources() + .exportProjectFiles() + } } } } \ No newline at end of file From b85e76427ec9ffd74b41f1db54a3a4b042e18c22 Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Fri, 21 Feb 2020 12:59:47 -0700 Subject: [PATCH 23/29] Testing npm build script. --- .github/workflows/release.yml | 2 +- .../com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ce963541..59a8c0e0 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -40,7 +40,7 @@ jobs: cd ./ScriptableMC-Tools-TS/lib-smc/ npm install npm run compile - npm publish ./js + npm publish ./js --access public zip -r ./Lib-SMC.zip ./* - name: Build JS Engine with Gradle run: ./gradlew :shadowJarAll diff --git a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt index c8a5f54a..f6d05e41 100644 --- a/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt +++ b/ScriptableMC-Tools-TS/src/main/kotlin/com/pixlfox/scriptablemc/TypescriptLibraryExporter.kt @@ -11,7 +11,7 @@ import java.lang.reflect.* @Suppress("MemberVisibilityCanBePrivate", "UnstableApiUsage", "unused") -class TypescriptLibraryExporter(private var args: Array = arrayOf()) { +class TypescriptLibraryExporter(args: Array = arrayOf()) { private var basePath: String = "./lib" private val classList = mutableListOf>() private var allowedPackagesRegex: Regex = Regex("(org\\.bukkit|com\\.pixlfox|com\\.smc|fr\\.minuskube\\.inv|com\\.google|java\\.sql|java\\.io|java\\.nio|khttp|org\\.apache\\.commons\\.io)(.*)?") @@ -445,7 +445,7 @@ class TypescriptLibraryExporter(private var args: Array = arrayOf()) { } File("$basePath/package.json").writeText("{\n" + - " \"name\": \"${if(isRelease) "lib-smc" else "@astorks/lib-smc"}\",\n" + + " \"name\": \"@astorks/lib-smc\",\n" + " \"repository\": \"git@github.com:astorks/ScriptableMC-Engine.git\",\n" + " \"version\": \"$version\",\n" + " \"description\": \"JavaScript CommonJS libraries for ScriptableMC\",\n" + From 5bdecf83b27591fa44e641b7e91fc82ab569936a Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Fri, 21 Feb 2020 19:32:39 -0700 Subject: [PATCH 24/29] Fixed js config common_js.globals_file should work properly now. --- .../com/pixlfox/scriptablemc/SMCJavaScriptConfig.kt | 4 ++-- .../pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt | 8 ++++---- ScriptableMC-Engine-JS/src/main/resources/config.yml | 9 +++++---- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptConfig.kt b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptConfig.kt index db6c8e2c..5dde4d69 100644 --- a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptConfig.kt +++ b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptConfig.kt @@ -12,6 +12,6 @@ class SMCJavaScriptConfig(config: FileConfiguration) : ScriptEngineConfig(config val commonJsModulesPath: String get() = readConfigString("common_js.modules_path", "\${root_scripts_folder}/node_modules") - val commonJsGlobalsPath: String - get() = readConfigString("common_js.globals_path", "\${root_scripts_folder}/__globals__.js") + val commonJsGlobalsFile: String + get() = readConfigString("common_js.globals_file", "globals.js") } \ No newline at end of file diff --git a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt index ba7d85c9..fd9a97ff 100644 --- a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt +++ b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt @@ -64,16 +64,16 @@ class JavaScriptPluginEngine(override val bootstrapPlugin: ScriptEngineMain, pri .option("js.commonjs-require", "true") .option("js.commonjs-require-cwd", config.commonJsModulesPath) - if (File(config.commonJsGlobalsPath).exists()) { + if (File(File(config.commonJsModulesPath), config.commonJsGlobalsFile).exists()) { if(config.debug) { - bootstrapPlugin.logger.info("CommonJS using globals: ${config.commonJsGlobalsPath}") + bootstrapPlugin.logger.info("CommonJS using globals: ${File(File(config.commonJsModulesPath), config.commonJsGlobalsFile).path}") } contextBuilder = contextBuilder - .option("js.commonjs-global-properties", config.commonJsGlobalsPath) + .option("js.commonjs-global-properties", config.commonJsGlobalsFile) } else { if(config.debug) { - bootstrapPlugin.logger.warning("CommonJS unable to read globals: ${config.commonJsGlobalsPath}") + bootstrapPlugin.logger.warning("CommonJS unable to read globals: ${File(File(config.commonJsModulesPath), config.commonJsGlobalsFile).path}") } } diff --git a/ScriptableMC-Engine-JS/src/main/resources/config.yml b/ScriptableMC-Engine-JS/src/main/resources/config.yml index cb8ca922..04e09b4a 100644 --- a/ScriptableMC-Engine-JS/src/main/resources/config.yml +++ b/ScriptableMC-Engine-JS/src/main/resources/config.yml @@ -34,10 +34,11 @@ common_js: # Default: ${root_scripts_folder}/node_modules modules_path: '${root_scripts_folder}/node_modules' - # CommonJS globals path - # The path to the CommonJS globals.js file used to define global CommonJS values - # Default: ${common_js.modules_path}/__globals__.js - globals_path: '${common_js.modules_path}/__globals__.js' + # CommonJS globals file name + # The CommonJS globals file used to define global CommonJS values + # Relative to the `common_js.modules_path` value + # Default: globals.js + globals_file: 'globals.js' # Chrome debugger settings # **WARNING** THE DEBUGGER SHOULD NEVER BE ENABLED ON A PUBLIC SERVER From 44fe98b6242c1c0fcc880b412b67d3f3a83a505c Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Sat, 22 Feb 2020 15:22:50 -0700 Subject: [PATCH 25/29] Continued cleanup and moving code shared between both js and py engines to the core engine. Added support for multiple main entrypoint script files. Added config option to disable the automatic scriptable plugin enabling. --- .../scriptablemc/ScriptEngineConfig.kt | 23 ++++++- .../core/ScriptablePluginEngine.kt | 66 +++++++++++++++++-- .../exceptions/ScriptNotFoundException.kt | 2 +- .../scriptablemc/SMCJavaScriptConfig.kt | 4 +- .../scriptablemc/SMCJavaScriptEngineMain.kt | 53 +++++++-------- .../core/JavaScriptPluginEngine.kt | 55 ++++------------ .../src/main/resources/config.yml | 13 +++- .../pixlfox/scriptablemc/SMCPythonConfig.kt | 4 +- .../scriptablemc/SMCPythonEngineMain.kt | 56 ++++++++-------- .../scriptablemc/core/PythonPluginEngine.kt | 50 ++++---------- .../src/main/resources/config.yml | 13 +++- 11 files changed, 184 insertions(+), 155 deletions(-) diff --git a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/ScriptEngineConfig.kt b/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/ScriptEngineConfig.kt index cea78eaf..7656bd35 100644 --- a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/ScriptEngineConfig.kt +++ b/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/ScriptEngineConfig.kt @@ -6,7 +6,10 @@ abstract class ScriptEngineConfig(private val config: FileConfiguration) { val rootScriptsFolder: String get() = readConfigString("root_scripts_folder", "./scripts") - abstract val mainScriptFile: String + abstract val mainScriptFiles: List + + val autoEnablePlugins: Boolean + get() = readConfigBoolean("auto_enable_plugins", true) val debug: Boolean get() = readConfigBoolean("debug", false) @@ -27,6 +30,24 @@ abstract class ScriptEngineConfig(private val config: FileConfiguration) { } } + fun readConfigStringList(path: String, def: List = listOf()): List { + val inputList = config.getStringList(path) + val regex = Regex("\\\$\\{(.*)}") + + if(inputList.isEmpty()) { + inputList.addAll(def) + } + + for((index, input) in inputList.withIndex()) { + inputList[index] = regex.replace(input) { + val configValue = readConfigString(it.groups[1]!!.value) + configValue + } + } + + return inputList + } + fun readConfigBoolean(path: String, def: Boolean = false): Boolean { return readConfigString(path, def.toString()).equals("true", true) } diff --git a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/core/ScriptablePluginEngine.kt b/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/core/ScriptablePluginEngine.kt index 9796e1be..50306663 100644 --- a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/core/ScriptablePluginEngine.kt +++ b/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/core/ScriptablePluginEngine.kt @@ -1,5 +1,6 @@ package com.pixlfox.scriptablemc.core +import com.pixlfox.scriptablemc.ScriptEngineConfig import com.pixlfox.scriptablemc.ScriptEngineMain import com.smc.version.Version import fr.minuskube.inv.InventoryManager @@ -18,13 +19,48 @@ abstract class ScriptablePluginEngine { abstract val graalContext: Context abstract val globalBindings: Value + abstract val config: ScriptEngineConfig + val pluginVersion: Version get() = bootstrapPlugin.pluginVersion - abstract fun start() - abstract fun close() + var enabledAllPlugins: Boolean = false + internal set + + private val _startupErrors = mutableListOf() + val startupErrors: Array + get() = _startupErrors.toTypedArray() + + open fun start() { + inventoryManager.init() + globalBindings.putMember("engine", this) + + loadAllHelperClasses() - fun loadAllHelperClasses() { + for(mainScriptFile in config.mainScriptFiles) { + try { + loadMainScript(mainScriptFile) + } + catch(ex: Exception) { + _startupErrors.add(ex) + } + } + + if(!enabledAllPlugins && config.autoEnablePlugins) { + enableAllPlugins() + } + } + + open fun close() { + for(scriptablePlugin in scriptablePlugins) { + scriptablePlugin.disable() + } + scriptablePlugins.clear() + + graalContext.close(true) + } + + open fun loadAllHelperClasses() { for(helperClass in preLoadClasses) { try { javaClass.classLoader.loadClass(helperClass.replace("*", "")) @@ -38,15 +74,31 @@ abstract class ScriptablePluginEngine { } } + open fun enableAllPlugins() { + for (pluginContext in scriptablePlugins) { + pluginContext.enable() + } + enabledAllPlugins = true + } + + open fun enablePlugin(pluginContext: ScriptablePluginContext) { + pluginContext.enable() + } + + open fun disablePlugin(pluginContext: ScriptablePluginContext) { + pluginContext.disable() + } + + open fun eval(source: Source): Value { + return graalContext.eval(source) + } + + abstract fun loadMainScript(path: String) abstract fun evalFile(filePath: String): Value abstract fun evalFile(scriptFile: File): Value abstract fun eval(source: String): Value abstract fun evalCommandSender(source: String, sender: CommandSender): Value - abstract fun eval(source: Source): Value abstract fun loadPlugin(scriptableClass: Value): ScriptablePluginContext - abstract fun enableAllPlugins() - abstract fun enablePlugin(pluginContext: ScriptablePluginContext) - abstract fun disablePlugin(pluginContext: ScriptablePluginContext) companion object { val preLoadClasses: Array = arrayOf( diff --git a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/exceptions/ScriptNotFoundException.kt b/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/exceptions/ScriptNotFoundException.kt index b49568bb..c211d78f 100644 --- a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/exceptions/ScriptNotFoundException.kt +++ b/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/exceptions/ScriptNotFoundException.kt @@ -2,4 +2,4 @@ package com.pixlfox.scriptablemc.exceptions import java.io.File -class ScriptNotFoundException(scriptFile: File) : Exception("Unable to load script: ${scriptFile.path}.") \ No newline at end of file +class ScriptNotFoundException(scriptFile: File) : Exception("Unable to load script: ${scriptFile.absolutePath}.") \ No newline at end of file diff --git a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptConfig.kt b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptConfig.kt index 5dde4d69..49f16eca 100644 --- a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptConfig.kt +++ b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptConfig.kt @@ -3,8 +3,8 @@ package com.pixlfox.scriptablemc import org.bukkit.configuration.file.FileConfiguration class SMCJavaScriptConfig(config: FileConfiguration) : ScriptEngineConfig(config) { - override val mainScriptFile: String - get() = readConfigString("main_script_file", "\${root_scripts_folder}/main.js") + override val mainScriptFiles: List + get() = readConfigStringList("main_script_files", listOf("\${root_scripts_folder}/main.js")) val commonJsModulesEnabled: Boolean get() = readConfigBoolean("common_js.modules_enabled", true) diff --git a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptEngineMain.kt b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptEngineMain.kt index 8fb0c127..feb6c8bc 100644 --- a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptEngineMain.kt +++ b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptEngineMain.kt @@ -53,33 +53,36 @@ class SMCJavaScriptEngineMain : ScriptEngineMain() { try { scriptEngine = JavaScriptPluginEngine(this, SMCJavaScriptConfig(config)) scriptEngine!!.start() - logger.info("JavaScript engine started.") - sender?.sendMessage("$chatMessagePrefix ${ChatColor.GREEN}JavaScript engine started.") - } catch (e: IllegalStateException) { - if (e.message?.contains("Make sure the truffle-api.jar is on the classpath.", true) == true) { - logger.warning("JavaScript engine failed to start.") - e.printStackTrace() - logger.severe("Unable to find truffle-api.jar. This shouldn't happen since it's now packaged inside the plugin, you might want to check for an updated ScriptableMC Engine release.") - logger.severe("ScriptableMC Engine Version: $pluginVersion") - logger.severe("ScriptableMC-Engine Download Page: https://github.com/astorks/ScriptableMC-Engine/releases/latest") - - sender?.sendMessage("$chatMessagePrefix ${ChatColor.DARK_RED}JavaScript engine failed to start. Check the server console.") - } else { - logger.warning("JavaScript engine failed to start.") - e.printStackTrace() + if(scriptEngine!!.startupErrors.any()) { + for(error in scriptEngine!!.startupErrors) { + error.printStackTrace() + if(sender != null) { + sender.sendMessage("$chatMessagePrefix ${ChatColor.RED}$error") + for (stackTrace in error.stackTrace.filter { it.fileName?.endsWith(".js") == true }) { + sender.sendMessage("${ChatColor.RED}$stackTrace") + } + } + } - sender?.sendMessage("$chatMessagePrefix ${ChatColor.DARK_RED}JavaScript engine failed to start. Check the server console.") + logger.warning("JavaScript engine started with errors.") + sender?.sendMessage("$chatMessagePrefix ${ChatColor.YELLOW}JavaScript engine started with errors.") } - } catch (e: Exception) { - logger.warning("JavaScript engine failed to start.") - e.printStackTrace() - + else { + logger.info("JavaScript engine started.") + sender?.sendMessage("$chatMessagePrefix ${ChatColor.GREEN}JavaScript engine started.") + } + } + catch (error: Exception) { + error.printStackTrace() if(sender != null) { - sender.sendMessage("$chatMessagePrefix ${ChatColor.DARK_RED}$e") - for (stackTrace in e.stackTrace) { - sender.sendMessage("${ChatColor.DARK_RED}$stackTrace") + sender.sendMessage("$chatMessagePrefix ${ChatColor.DARK_RED}$error") + for (stackTrace in error.stackTrace) { + sender.sendMessage("${ChatColor.RED}$stackTrace") } } + + logger.severe("JavaScript engine failed to start.") + sender?.sendMessage("$chatMessagePrefix ${ChatColor.DARK_RED}JavaScript engine failed to start.") } } } @@ -91,9 +94,7 @@ class SMCJavaScriptEngineMain : ScriptEngineMain() { } companion object { - private var inst: SMCJavaScriptEngineMain? = null - var instance: SMCJavaScriptEngineMain - internal set(value) { inst = value } - get() { return inst!! } + var instance: SMCJavaScriptEngineMain? = null + private set } } \ No newline at end of file diff --git a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt index fd9a97ff..79535469 100644 --- a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt +++ b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt @@ -12,14 +12,13 @@ import java.io.File import java.util.* @Suppress("MemberVisibilityCanBePrivate", "unused") -class JavaScriptPluginEngine(override val bootstrapPlugin: ScriptEngineMain, private val config: SMCJavaScriptConfig): ScriptablePluginEngine() { +class JavaScriptPluginEngine(override val bootstrapPlugin: ScriptEngineMain, override val config: SMCJavaScriptConfig): ScriptablePluginEngine() { override val debugEnabled: Boolean = config.debug override val graalContext: Context override val globalBindings: Value override val scriptablePlugins: MutableList = mutableListOf() override val inventoryManager: InventoryManager = InventoryManager(bootstrapPlugin) - private var enabledAllPlugins: Boolean = false init { if(config.extractLibs) { @@ -94,14 +93,8 @@ class JavaScriptPluginEngine(override val bootstrapPlugin: ScriptEngineMain, pri globalBindings = graalContext.getBindings("js") } - override fun start() { - instance = this - inventoryManager.init() - globalBindings.putMember("engine", this) - - loadAllHelperClasses() - - val mainScriptFile = File(config.mainScriptFile) + override fun loadMainScript(path: String) { + val mainScriptFile = File(path) if(!mainScriptFile.parentFile.exists()) { mainScriptFile.parentFile.mkdirs() } @@ -120,11 +113,6 @@ class JavaScriptPluginEngine(override val bootstrapPlugin: ScriptEngineMain, pri for (i in 0 until mainReturn.arraySize) { this.loadPlugin(mainReturn.getArrayElement(i)) } - - // Enable all plugins if not already enabled - if(!enabledAllPlugins) { - enableAllPlugins() - } } } else { @@ -132,14 +120,14 @@ class JavaScriptPluginEngine(override val bootstrapPlugin: ScriptEngineMain, pri } } + override fun start() { + instance = this + super.start() + } + override fun close() { instance = null - for(scriptablePlugin in scriptablePlugins) { - scriptablePlugin.disable() - } - scriptablePlugins.clear() - - graalContext.close(true) + super.close() } override fun evalFile(filePath: String): Value { @@ -203,10 +191,6 @@ class JavaScriptPluginEngine(override val bootstrapPlugin: ScriptEngineMain, pri } } - override fun eval(source: Source): Value { - return graalContext.eval(source) - } - override fun loadPlugin(scriptableClass: Value): ScriptablePluginContext { val pluginInstance = scriptableClass.newInstance() val pluginName = pluginInstance.getMember("pluginName").asString() @@ -217,25 +201,8 @@ class JavaScriptPluginEngine(override val bootstrapPlugin: ScriptEngineMain, pri return pluginContext } - override fun enableAllPlugins() { - for (pluginContext in scriptablePlugins) { - pluginContext.enable() - } - enabledAllPlugins = true - } - - override fun enablePlugin(pluginContext: ScriptablePluginContext) { - pluginContext.enable() - } - - override fun disablePlugin(pluginContext: ScriptablePluginContext) { - pluginContext.disable() - } - companion object { - private var inst: JavaScriptPluginEngine? = null - var instance: JavaScriptPluginEngine? - internal set(value) { inst = value } - get() { return inst } + var instance: JavaScriptPluginEngine? = null + private set } } \ No newline at end of file diff --git a/ScriptableMC-Engine-JS/src/main/resources/config.yml b/ScriptableMC-Engine-JS/src/main/resources/config.yml index 04e09b4a..9a7a0e64 100644 --- a/ScriptableMC-Engine-JS/src/main/resources/config.yml +++ b/ScriptableMC-Engine-JS/src/main/resources/config.yml @@ -3,10 +3,17 @@ # Default: ./scripts root_scripts_folder: './scripts' -# Main script file -# The main entry point javascript file +# Main script files +# The main entry point javascript files # Default: ${root_scripts_folder}/main.js -main_script_file: '${root_scripts_folder}/main.js' +main_script_files: + - '${root_scripts_folder}/main.js' + +# Auto-Enable all scriptable plugins after loading main scripts +# If set to false you will have to manually enable all plugins +# via engine.enableAllPlugins() after you have loaded all plugins +# Default: true +auto_enable_plugins: true # Prints some additional debugging messages # Default: false diff --git a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonConfig.kt b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonConfig.kt index e14c56bd..5435d4f1 100644 --- a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonConfig.kt +++ b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonConfig.kt @@ -3,6 +3,6 @@ package com.pixlfox.scriptablemc import org.bukkit.configuration.file.FileConfiguration class SMCPythonConfig(config: FileConfiguration) : ScriptEngineConfig(config) { - override val mainScriptFile: String - get() = readConfigString("main_script_file", "\${root_scripts_folder}/main.py") + override val mainScriptFiles: List + get() = readConfigStringList("main_script_files", listOf("\${root_scripts_folder}/main.py")) } \ No newline at end of file diff --git a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonEngineMain.kt b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonEngineMain.kt index 3de2c363..123835a7 100644 --- a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonEngineMain.kt +++ b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonEngineMain.kt @@ -53,33 +53,37 @@ class SMCPythonEngineMain : ScriptEngineMain() { try { scriptEngine = PythonPluginEngine(this, SMCPythonConfig(config)) scriptEngine!!.start() - logger.info("Python engine started.") - sender?.sendMessage("$chatMessagePrefix ${ChatColor.GREEN}Python engine started.") - } catch (e: IllegalStateException) { - if (e.message?.contains("Make sure the truffle-api.jar is on the classpath.", true) == true) { - logger.warning("Python engine failed to start.") - e.printStackTrace() - logger.severe("Unable to find truffle-api.jar. This shouldn't happen since it's now packaged inside the plugin, you might want to check for an updated ScriptableMC Engine release.") - logger.severe("ScriptableMC Engine Version: $pluginVersion") - logger.severe("ScriptableMC-Engine Download Page: https://github.com/astorks/ScriptableMC-Engine/releases/latest") - - sender?.sendMessage("$chatMessagePrefix ${ChatColor.DARK_RED}Python engine failed to start. Check the server console.") - } else { - logger.warning("Scriptable plugin engine failed to start.") - e.printStackTrace() - - sender?.sendMessage("$chatMessagePrefix ${ChatColor.DARK_RED}Python engine failed to start. Check the server console.") - } - } catch (e: Exception) { - logger.warning("Python engine failed to start.") - e.printStackTrace() + if(scriptEngine!!.startupErrors.any()) { + for(error in scriptEngine!!.startupErrors) { + error.printStackTrace() + if(sender != null) { + sender.sendMessage("$chatMessagePrefix ${ChatColor.RED}$error") + for (stackTrace in error.stackTrace.filter { it.fileName?.endsWith(".py") == true }) { + sender.sendMessage("${ChatColor.RED}$stackTrace") + } + } + } + + logger.warning("Python engine started with errors.") + sender?.sendMessage("$chatMessagePrefix ${ChatColor.YELLOW}Python engine started with errors.") + } + else { + logger.info("Python engine started.") + sender?.sendMessage("$chatMessagePrefix ${ChatColor.GREEN}Python engine started.") + } + } + catch (error: Exception) { + error.printStackTrace() if(sender != null) { - sender.sendMessage("$chatMessagePrefix ${ChatColor.DARK_RED}$e") - for (stackTrace in e.stackTrace) { - sender.sendMessage("${ChatColor.DARK_RED}$stackTrace") + sender.sendMessage("$chatMessagePrefix ${ChatColor.DARK_RED}$error") + for (stackTrace in error.stackTrace) { + sender.sendMessage("${ChatColor.RED}$stackTrace") } } + + logger.severe("Python engine failed to start.") + sender?.sendMessage("$chatMessagePrefix ${ChatColor.DARK_RED}Python engine failed to start.") } } } @@ -91,9 +95,7 @@ class SMCPythonEngineMain : ScriptEngineMain() { } companion object { - private var inst: SMCPythonEngineMain? = null - var instance: SMCPythonEngineMain - internal set(value) { inst = value } - get() { return inst!! } + var instance: SMCPythonEngineMain? = null + private set } } \ No newline at end of file diff --git a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginEngine.kt b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginEngine.kt index fc548da5..756d9991 100644 --- a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginEngine.kt +++ b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginEngine.kt @@ -11,13 +11,12 @@ import java.io.File import java.util.* @Suppress("MemberVisibilityCanBePrivate", "unused") -class PythonPluginEngine(override val bootstrapPlugin: ScriptEngineMain, private val config: SMCPythonConfig): ScriptablePluginEngine() { +class PythonPluginEngine(override val bootstrapPlugin: ScriptEngineMain, override val config: SMCPythonConfig): ScriptablePluginEngine() { override val graalContext: Context override val debugEnabled: Boolean = config.debug override val globalBindings: Value override val scriptablePlugins: MutableList = mutableListOf() override val inventoryManager: InventoryManager = InventoryManager(bootstrapPlugin) - private var enabledAllPlugins: Boolean = false init { if(config.extractLibs) { @@ -54,14 +53,8 @@ class PythonPluginEngine(override val bootstrapPlugin: ScriptEngineMain, private globalBindings = graalContext.getBindings("python") } - override fun start() { - instance = this - inventoryManager.init() - globalBindings.putMember("engine", this) - - loadAllHelperClasses() - - val mainScriptFile = File(config.mainScriptFile) + override fun loadMainScript(path: String) { + val mainScriptFile = File(path) if(!mainScriptFile.parentFile.exists()) { mainScriptFile.parentFile.mkdirs() } @@ -91,14 +84,14 @@ class PythonPluginEngine(override val bootstrapPlugin: ScriptEngineMain, private } } + override fun start() { + instance = this + super.start() + } + override fun close() { instance = null - for(scriptablePlugin in scriptablePlugins) { - scriptablePlugin.disable() - } - scriptablePlugins.clear() - - graalContext.close(true) + super.close() } override fun evalFile(filePath: String): Value { @@ -151,10 +144,6 @@ class PythonPluginEngine(override val bootstrapPlugin: ScriptEngineMain, private } } - override fun eval(source: Source): Value { - return graalContext.eval(source) - } - override fun loadPlugin(scriptableClass: Value): ScriptablePluginContext { val pluginInstance = scriptableClass.newInstance() val pluginName = pluginInstance.getMember("pluginName").asString() @@ -165,25 +154,8 @@ class PythonPluginEngine(override val bootstrapPlugin: ScriptEngineMain, private return pluginContext } - override fun enableAllPlugins() { - for (pluginContext in scriptablePlugins) { - pluginContext.enable() - } - enabledAllPlugins = true - } - - override fun enablePlugin(pluginContext: ScriptablePluginContext) { - pluginContext.enable() - } - - override fun disablePlugin(pluginContext: ScriptablePluginContext) { - pluginContext.disable() - } - companion object { - private var inst: PythonPluginEngine? = null - var instance: PythonPluginEngine? - internal set(value) { inst = value } - get() { return inst } + var instance: PythonPluginEngine? = null + private set } } \ No newline at end of file diff --git a/ScriptableMC-Engine-PY/src/main/resources/config.yml b/ScriptableMC-Engine-PY/src/main/resources/config.yml index b78d3767..8afd6b8b 100644 --- a/ScriptableMC-Engine-PY/src/main/resources/config.yml +++ b/ScriptableMC-Engine-PY/src/main/resources/config.yml @@ -3,10 +3,17 @@ # Default: ./scripts root_scripts_folder: './scripts' -# Main script file -# The main entry point python script file +# Main script files +# The main entry point python script files # Default: ${root_scripts_folder}/main.py -main_script_file: '${root_scripts_folder}/main.py' +main_script_files: + - '${root_scripts_folder}/main.py' + +# Auto-Enable all scriptable plugins after loading main scripts +# If set to false you will have to manually enable all plugins +# via engine.enableAllPlugins() after you have loaded all plugins +# Default: true +auto_enable_plugins: true # Prints some additional debugging messages # Default: false From f1bc6778939691cd5de0d81352ffa8387ed5c9f3 Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Sun, 23 Feb 2020 21:57:58 -0700 Subject: [PATCH 26/29] Added a few more config options and moving shared code to code engine. --- .../scriptablemc/ScriptEngineConfig.kt | 2 + .../core/ScriptablePluginEngine.kt | 74 +++++++++++++++++- .../exceptions/ScriptNotFoundException.kt | 2 +- .../scriptablemc/SMCJavaScriptConfig.kt | 11 +++ .../scriptablemc/SMCJavaScriptEngineMain.kt | 1 + .../core/JavaScriptPluginEngine.kt | 76 ++----------------- .../src/main/resources/config.yml | 29 +++++++ .../pixlfox/scriptablemc/SMCPythonConfig.kt | 6 ++ .../scriptablemc/SMCPythonEngineMain.kt | 1 + .../scriptablemc/core/PythonPluginEngine.kt | 62 ++------------- .../src/main/resources/config.yml | 17 +++++ 11 files changed, 151 insertions(+), 130 deletions(-) rename ScriptableMC-Engine-Core/src/main/kotlin/com/{pixlfox/scriptablemc => smc}/exceptions/ScriptNotFoundException.kt (75%) diff --git a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/ScriptEngineConfig.kt b/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/ScriptEngineConfig.kt index 7656bd35..7e590ffe 100644 --- a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/ScriptEngineConfig.kt +++ b/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/ScriptEngineConfig.kt @@ -7,6 +7,8 @@ abstract class ScriptEngineConfig(private val config: FileConfiguration) { get() = readConfigString("root_scripts_folder", "./scripts") abstract val mainScriptFiles: List + abstract val executeCommandTemplate: String + abstract val scriptMimeType: String val autoEnablePlugins: Boolean get() = readConfigBoolean("auto_enable_plugins", true) diff --git a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/core/ScriptablePluginEngine.kt b/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/core/ScriptablePluginEngine.kt index 50306663..e1ec4e0f 100644 --- a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/core/ScriptablePluginEngine.kt +++ b/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/core/ScriptablePluginEngine.kt @@ -2,14 +2,19 @@ package com.pixlfox.scriptablemc.core import com.pixlfox.scriptablemc.ScriptEngineConfig import com.pixlfox.scriptablemc.ScriptEngineMain +import com.smc.exceptions.ScriptNotFoundException import com.smc.version.Version import fr.minuskube.inv.InventoryManager +import org.bukkit.Bukkit import org.bukkit.command.CommandSender import org.graalvm.polyglot.* import java.io.File +import java.util.* @Suppress("MemberVisibilityCanBePrivate", "unused") abstract class ScriptablePluginEngine { + abstract val languageName: String + abstract val languageFileExtension: String abstract val bootstrapPlugin: ScriptEngineMain abstract val debugEnabled: Boolean @@ -93,11 +98,72 @@ abstract class ScriptablePluginEngine { return graalContext.eval(source) } + open fun evalCommandSender(source: String, sender: CommandSender): Value { + val tempScriptFile = File("${config.rootScriptsFolder}/${UUID.randomUUID()}.$languageFileExtension") + try { + tempScriptFile.writeText(config.executeCommandTemplate.replace("%SOURCE%", source)) + + var evalReturn = evalFile(tempScriptFile) + + if(evalReturn.canInstantiate()) { + evalReturn = evalReturn.newInstance() + } + + if(evalReturn.hasMember("execute") && evalReturn.canInvokeMember("execute")) { + evalReturn.putMember("sender", sender) + evalReturn.putMember("server", Bukkit.getServer()) + evalReturn.putMember("servicesManager", Bukkit.getServicesManager()) + return evalReturn.invokeMember("execute", sender, Bukkit.getServer(), Bukkit.getServicesManager()) + } + + return evalReturn + } + finally { + tempScriptFile.delete() + } + } + + open fun evalFile(filePath: String): Value { + val scriptFile = File("${config.rootScriptsFolder}/$filePath") + + return if(scriptFile.exists()) { + eval( + Source.newBuilder(languageName, scriptFile) + .name(scriptFile.name) + .mimeType(config.scriptMimeType) + .interactive(false) + .build() + ) + } else { + throw ScriptNotFoundException(scriptFile) + } + } + + open fun evalFile(scriptFile: File): Value { + return if(scriptFile.exists()) { + eval( + Source.newBuilder(languageName, scriptFile) + .name(scriptFile.name) + .mimeType(config.scriptMimeType) + .interactive(false) + .build() + ) + } else { + throw ScriptNotFoundException(scriptFile) + } + } + + open fun eval(source: String): Value { + return graalContext.eval( + Source.newBuilder(languageName, source,"${UUID.randomUUID()}.$languageFileExtension") + .mimeType(config.scriptMimeType) + .interactive(false) + .cached(false) + .build() + ) + } + abstract fun loadMainScript(path: String) - abstract fun evalFile(filePath: String): Value - abstract fun evalFile(scriptFile: File): Value - abstract fun eval(source: String): Value - abstract fun evalCommandSender(source: String, sender: CommandSender): Value abstract fun loadPlugin(scriptableClass: Value): ScriptablePluginContext companion object { diff --git a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/exceptions/ScriptNotFoundException.kt b/ScriptableMC-Engine-Core/src/main/kotlin/com/smc/exceptions/ScriptNotFoundException.kt similarity index 75% rename from ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/exceptions/ScriptNotFoundException.kt rename to ScriptableMC-Engine-Core/src/main/kotlin/com/smc/exceptions/ScriptNotFoundException.kt index c211d78f..9487a94e 100644 --- a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/exceptions/ScriptNotFoundException.kt +++ b/ScriptableMC-Engine-Core/src/main/kotlin/com/smc/exceptions/ScriptNotFoundException.kt @@ -1,4 +1,4 @@ -package com.pixlfox.scriptablemc.exceptions +package com.smc.exceptions import java.io.File diff --git a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptConfig.kt b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptConfig.kt index 49f16eca..d4b2f1bb 100644 --- a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptConfig.kt +++ b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptConfig.kt @@ -6,6 +6,17 @@ class SMCJavaScriptConfig(config: FileConfiguration) : ScriptEngineConfig(config override val mainScriptFiles: List get() = readConfigStringList("main_script_files", listOf("\${root_scripts_folder}/main.js")) + override val executeCommandTemplate: String + get() = readConfigString("execute_command_template", "import * as lib from './lib/global.js';\n" + + "new (class EvalCommandSenderContext {\n" + + " execute(sender, server, servicesManager) {\n" + + " %SOURCE%\n" + + " }\n" + + "})()") + + override val scriptMimeType: String + get() = readConfigString("script_mime_type", "application/javascript+module") + val commonJsModulesEnabled: Boolean get() = readConfigBoolean("common_js.modules_enabled", true) diff --git a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptEngineMain.kt b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptEngineMain.kt index feb6c8bc..4521c402 100644 --- a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptEngineMain.kt +++ b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/SMCJavaScriptEngineMain.kt @@ -89,6 +89,7 @@ class SMCJavaScriptEngineMain : ScriptEngineMain() { override fun reloadScriptEngine(sender: CommandSender?) { unloadScriptEngine(sender) + saveDefaultConfig() reloadConfig() loadScriptEngine(sender) } diff --git a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt index 79535469..97a1c646 100644 --- a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt +++ b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt @@ -2,18 +2,17 @@ package com.pixlfox.scriptablemc.core import com.pixlfox.scriptablemc.SMCJavaScriptConfig import com.pixlfox.scriptablemc.ScriptEngineMain -import com.pixlfox.scriptablemc.exceptions.ScriptNotFoundException +import com.smc.exceptions.ScriptNotFoundException import com.pixlfox.scriptablemc.utils.UnzipUtility import fr.minuskube.inv.InventoryManager -import org.bukkit.Bukkit -import org.bukkit.command.CommandSender import org.graalvm.polyglot.* import java.io.File -import java.util.* @Suppress("MemberVisibilityCanBePrivate", "unused") class JavaScriptPluginEngine(override val bootstrapPlugin: ScriptEngineMain, override val config: SMCJavaScriptConfig): ScriptablePluginEngine() { + override val languageName: String = "js" + override val languageFileExtension: String = "js" override val debugEnabled: Boolean = config.debug override val graalContext: Context override val globalBindings: Value @@ -33,7 +32,7 @@ class JavaScriptPluginEngine(override val bootstrapPlugin: ScriptEngineMain, ove } var contextBuilder = Context - .newBuilder("js") + .newBuilder(languageName) .allowAllAccess(true) .allowExperimentalOptions(true) .allowHostAccess(HostAccess.ALL) @@ -90,7 +89,7 @@ class JavaScriptPluginEngine(override val bootstrapPlugin: ScriptEngineMain, ove graalContext = contextBuilder.build() - globalBindings = graalContext.getBindings("js") + globalBindings = graalContext.getBindings(languageName) } override fun loadMainScript(path: String) { @@ -101,9 +100,9 @@ class JavaScriptPluginEngine(override val bootstrapPlugin: ScriptEngineMain, ove if(mainScriptFile.exists()) { val mainReturn = eval( - Source.newBuilder("js", mainScriptFile) + Source.newBuilder(languageName, mainScriptFile) .name(mainScriptFile.name) - .mimeType("application/javascript+module") + .mimeType(config.scriptMimeType) .interactive(false) .build() ) @@ -130,67 +129,6 @@ class JavaScriptPluginEngine(override val bootstrapPlugin: ScriptEngineMain, ove super.close() } - override fun evalFile(filePath: String): Value { - val scriptFile = File("${config.rootScriptsFolder}/$filePath") - - return if(scriptFile.exists()) { - eval( - Source.newBuilder("js", scriptFile) - .name(scriptFile.name) - .mimeType("application/javascript+module") - .interactive(false) - .build() - ) - } else { - throw ScriptNotFoundException(scriptFile) - } - } - - override fun evalFile(scriptFile: File): Value { - return if(scriptFile.exists()) { - eval( - Source.newBuilder("js", scriptFile) - .name(scriptFile.name) - .mimeType("application/javascript+module") - .interactive(false) - .build() - ) - } else { - throw ScriptNotFoundException(scriptFile) - } - } - - override fun eval(source: String): Value { - return graalContext.eval( - Source.newBuilder("js", source,"${UUID.randomUUID()}.js") - .mimeType("application/javascript+module") - .interactive(false) - .cached(false) - .build() - ) - } - - override fun evalCommandSender(source: String, sender: CommandSender): Value { - val tempScriptFile = File("${config.rootScriptsFolder}/${UUID.randomUUID()}.js") - try { - tempScriptFile.writeText("import * as lib from './lib/global.js';\n" + - "new (class EvalCommandSenderContext {\n" + - " run(sender, server, servicesManager) {\n" + - " $source\n" + - " }\n" + - "})()\n") - val evalCommandSenderContext = evalFile(tempScriptFile) - - evalCommandSenderContext.putMember("sender", sender) - evalCommandSenderContext.putMember("server", Bukkit.getServer()) - evalCommandSenderContext.putMember("servicesManager", Bukkit.getServicesManager()) - return evalCommandSenderContext.invokeMember("run", sender, Bukkit.getServer(), Bukkit.getServicesManager()) - } - finally { - tempScriptFile.delete() - } - } - override fun loadPlugin(scriptableClass: Value): ScriptablePluginContext { val pluginInstance = scriptableClass.newInstance() val pluginName = pluginInstance.getMember("pluginName").asString() diff --git a/ScriptableMC-Engine-JS/src/main/resources/config.yml b/ScriptableMC-Engine-JS/src/main/resources/config.yml index 9a7a0e64..784731f7 100644 --- a/ScriptableMC-Engine-JS/src/main/resources/config.yml +++ b/ScriptableMC-Engine-JS/src/main/resources/config.yml @@ -3,6 +3,12 @@ # Default: ./scripts root_scripts_folder: './scripts' +# Script MIME type +# ES2020 Module: application/javascript+module +# Pure JavaScript: application/javascript +# Default: application/javascript+module +script_mime_type: 'application/javascript+module' + # Main script files # The main entry point javascript files # Default: ${root_scripts_folder}/main.js @@ -30,6 +36,24 @@ version_check: true # Default: true extract_libs: true +# Execute command template +# This is the code that will be executed with /pyex and /smc python execute +# If a class is returned it will be instantiated and continued on as an object +# If object is returned and has a method execute script engine will call +# returnedObject.execute(sender: CommandSender, server: BukkitServer, servicesManager) +# as well as set returnedObject.sender, returnedObject.server, and returnedObject.servicesManager +# you can also add additional methods, vars to the template source below +# Otherwise the script engine will return last execute code +# %SOURCE% will be replaced with with the command source. +execute_command_template: " +import * as lib from './lib/global.js'; + +new (class EvalCommandSenderContext { + execute(sender, server, servicesManager) { + %SOURCE% + } +})()" + common_js: # Enable CommonJS `require()` support # Allows the use of CommonJS NPM modules @@ -47,6 +71,11 @@ common_js: # Default: globals.js globals_file: 'globals.js' +java_libraries: + - path: "" + preload_classes: + - "" + # Chrome debugger settings # **WARNING** THE DEBUGGER SHOULD NEVER BE ENABLED ON A PUBLIC SERVER # IT IS INTENDED ONLY FOR DEBUGGING THE SCRIPT ENGINE AND CAN BREAK/FREEZE YOUR WHOLE SERVER diff --git a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonConfig.kt b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonConfig.kt index 5435d4f1..a2d45f0d 100644 --- a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonConfig.kt +++ b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonConfig.kt @@ -5,4 +5,10 @@ import org.bukkit.configuration.file.FileConfiguration class SMCPythonConfig(config: FileConfiguration) : ScriptEngineConfig(config) { override val mainScriptFiles: List get() = readConfigStringList("main_script_files", listOf("\${root_scripts_folder}/main.py")) + + override val executeCommandTemplate: String + get() = readConfigString("execute_command_template", "") + + override val scriptMimeType: String + get() = readConfigString("script_mime_type", "text/x-python") } \ No newline at end of file diff --git a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonEngineMain.kt b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonEngineMain.kt index 123835a7..a06bf421 100644 --- a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonEngineMain.kt +++ b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/SMCPythonEngineMain.kt @@ -90,6 +90,7 @@ class SMCPythonEngineMain : ScriptEngineMain() { override fun reloadScriptEngine(sender: CommandSender?) { unloadScriptEngine(sender) + saveDefaultConfig() reloadConfig() loadScriptEngine(sender) } diff --git a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginEngine.kt b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginEngine.kt index 756d9991..8ee84ddb 100644 --- a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginEngine.kt +++ b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginEngine.kt @@ -2,16 +2,16 @@ package com.pixlfox.scriptablemc.core import com.pixlfox.scriptablemc.SMCPythonConfig import com.pixlfox.scriptablemc.ScriptEngineMain -import com.pixlfox.scriptablemc.exceptions.ScriptNotFoundException +import com.smc.exceptions.ScriptNotFoundException import com.pixlfox.scriptablemc.utils.UnzipUtility import fr.minuskube.inv.InventoryManager -import org.bukkit.command.CommandSender import org.graalvm.polyglot.* import java.io.File -import java.util.* @Suppress("MemberVisibilityCanBePrivate", "unused") class PythonPluginEngine(override val bootstrapPlugin: ScriptEngineMain, override val config: SMCPythonConfig): ScriptablePluginEngine() { + override val languageName: String = "python" + override val languageFileExtension: String = "py" override val graalContext: Context override val debugEnabled: Boolean = config.debug override val globalBindings: Value @@ -31,7 +31,7 @@ class PythonPluginEngine(override val bootstrapPlugin: ScriptEngineMain, overrid } var contextBuilder = Context - .newBuilder("python") + .newBuilder(languageName) .allowAllAccess(true) .allowExperimentalOptions(true) .allowHostAccess(HostAccess.ALL) @@ -50,7 +50,7 @@ class PythonPluginEngine(override val bootstrapPlugin: ScriptEngineMain, overrid } graalContext = contextBuilder.build() - globalBindings = graalContext.getBindings("python") + globalBindings = graalContext.getBindings(languageName) } override fun loadMainScript(path: String) { @@ -61,7 +61,7 @@ class PythonPluginEngine(override val bootstrapPlugin: ScriptEngineMain, overrid if(mainScriptFile.exists()) { val mainReturn = eval( - Source.newBuilder("python", mainScriptFile) + Source.newBuilder(languageName, mainScriptFile) .name(mainScriptFile.name) .interactive(false) .build() @@ -94,56 +94,6 @@ class PythonPluginEngine(override val bootstrapPlugin: ScriptEngineMain, overrid super.close() } - override fun evalFile(filePath: String): Value { - val scriptFile = File("${config.rootScriptsFolder}/$filePath") - - return if(scriptFile.exists()) { - eval( - Source.newBuilder("python", scriptFile) - .name(scriptFile.name) - .interactive(false) - .build() - ) - } else { - throw ScriptNotFoundException(scriptFile) - } - } - - override fun evalFile(scriptFile: File): Value { - return if(scriptFile.exists()) { - eval( - Source.newBuilder("python", scriptFile) - .name(scriptFile.name) - .interactive(false) - .build() - ) - } else { - throw ScriptNotFoundException(scriptFile) - } - } - - override fun eval(source: String): Value { - return graalContext.eval( - Source.newBuilder("python", source,"${UUID.randomUUID()}.py") - .interactive(false) - .cached(false) - .build() - ) - } - - override fun evalCommandSender(source: String, sender: CommandSender): Value { - val tempScriptFile = File("${config.rootScriptsFolder}/${UUID.randomUUID()}/__init__.py") - try { - tempScriptFile.parentFile.mkdirs() - tempScriptFile.writeText(source) - return evalFile(tempScriptFile) - } - finally { - tempScriptFile.delete() - tempScriptFile.parentFile.delete() - } - } - override fun loadPlugin(scriptableClass: Value): ScriptablePluginContext { val pluginInstance = scriptableClass.newInstance() val pluginName = pluginInstance.getMember("pluginName").asString() diff --git a/ScriptableMC-Engine-PY/src/main/resources/config.yml b/ScriptableMC-Engine-PY/src/main/resources/config.yml index 8afd6b8b..bce27697 100644 --- a/ScriptableMC-Engine-PY/src/main/resources/config.yml +++ b/ScriptableMC-Engine-PY/src/main/resources/config.yml @@ -3,6 +3,12 @@ # Default: ./scripts root_scripts_folder: './scripts' +# Script MIME type +# .py text/x-python +# .pyc application/x-python-code +# Default: text/x-python +script_mime_type: 'text/x-python' + # Main script files # The main entry point python script files # Default: ${root_scripts_folder}/main.py @@ -30,6 +36,17 @@ version_check: true # Default: true extract_libs: true +# Execute command template +# This is the code that will be executed with /pyex and /smc python execute +# If a class is returned it will be instantiated and continued on as an object +# If object is returned and has a method execute script engine will call +# returnedObject.execute(sender: CommandSender, server: BukkitServer, servicesManager) +# as well as set returnedObject.sender, returnedObject.server, and returnedObject.servicesManager +# you can also add additional methods, vars to the template source below +# Otherwise the script engine will return last execute code +# %SOURCE% will be replaced with with the command source. +execute_command_template: "%SOURCE%" + # Chrome debugger settings # **WARNING** THE DEBUGGER SHOULD NEVER BE ENABLED ON A PUBLIC SERVER # IT IS INTENDED ONLY FOR DEBUGGING THE SCRIPT ENGINE AND CAN BREAK/FREEZE YOUR WHOLE SERVER From 666bc89e877b134e70e2b423790fa7032e00bb71 Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Sun, 23 Feb 2020 22:04:54 -0700 Subject: [PATCH 27/29] Moved bundled plugin projects inside their base plugins project folder. --- .github/workflows/release.yml | 4 ++-- .../Bundled}/build.gradle.kts | 0 .../Bundled}/build.gradle.kts | 0 .../Bundled}/libraries/graalpython.jar | Bin .../Bundled}/libraries/sulong-api.jar | Bin .../Bundled}/libraries/sulong.jar | Bin build.gradle.kts | 8 ++++---- settings.gradle.kts | 4 ++-- 8 files changed, 8 insertions(+), 8 deletions(-) rename {ScriptableMC-Engine-JS-Bundled => ScriptableMC-Engine-JS/Bundled}/build.gradle.kts (100%) rename {ScriptableMC-Engine-PY-Bundled => ScriptableMC-Engine-PY/Bundled}/build.gradle.kts (100%) rename {ScriptableMC-Engine-PY-Bundled => ScriptableMC-Engine-PY/Bundled}/libraries/graalpython.jar (100%) rename {ScriptableMC-Engine-PY-Bundled => ScriptableMC-Engine-PY/Bundled}/libraries/sulong-api.jar (100%) rename {ScriptableMC-Engine-PY-Bundled => ScriptableMC-Engine-PY/Bundled}/libraries/sulong.jar (100%) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 59a8c0e0..20a98845 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -61,13 +61,13 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: - args: 'ScriptableMC-Engine-JS-Bundled/build/libs/ScriptableMC-Engine-JS-Bundled.jar' + args: 'ScriptableMC-Engine-JS/Bundled/build/libs/ScriptableMC-Engine-JS-Bundled.jar' - name: Upload Bundled Python Engine Plugin Jar uses: skx/github-action-publish-binaries@master env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: - args: 'ScriptableMC-Engine-PY-Bundled/build/libs/ScriptableMC-Engine-PY-Bundled.jar' + args: 'ScriptableMC-Engine-PY/Bundled/build/libs/ScriptableMC-Engine-PY-Bundled.jar' - name: Upload TypeScript Libraries uses: skx/github-action-publish-binaries@master env: diff --git a/ScriptableMC-Engine-JS-Bundled/build.gradle.kts b/ScriptableMC-Engine-JS/Bundled/build.gradle.kts similarity index 100% rename from ScriptableMC-Engine-JS-Bundled/build.gradle.kts rename to ScriptableMC-Engine-JS/Bundled/build.gradle.kts diff --git a/ScriptableMC-Engine-PY-Bundled/build.gradle.kts b/ScriptableMC-Engine-PY/Bundled/build.gradle.kts similarity index 100% rename from ScriptableMC-Engine-PY-Bundled/build.gradle.kts rename to ScriptableMC-Engine-PY/Bundled/build.gradle.kts diff --git a/ScriptableMC-Engine-PY-Bundled/libraries/graalpython.jar b/ScriptableMC-Engine-PY/Bundled/libraries/graalpython.jar similarity index 100% rename from ScriptableMC-Engine-PY-Bundled/libraries/graalpython.jar rename to ScriptableMC-Engine-PY/Bundled/libraries/graalpython.jar diff --git a/ScriptableMC-Engine-PY-Bundled/libraries/sulong-api.jar b/ScriptableMC-Engine-PY/Bundled/libraries/sulong-api.jar similarity index 100% rename from ScriptableMC-Engine-PY-Bundled/libraries/sulong-api.jar rename to ScriptableMC-Engine-PY/Bundled/libraries/sulong-api.jar diff --git a/ScriptableMC-Engine-PY-Bundled/libraries/sulong.jar b/ScriptableMC-Engine-PY/Bundled/libraries/sulong.jar similarity index 100% rename from ScriptableMC-Engine-PY-Bundled/libraries/sulong.jar rename to ScriptableMC-Engine-PY/Bundled/libraries/sulong.jar diff --git a/build.gradle.kts b/build.gradle.kts index 1d5a5123..e1c0ce13 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -47,9 +47,9 @@ tasks.register("shadowJarAll") { group = "shadow" dependsOn(":ScriptableMC-Engine-JS:shadowJar") - dependsOn(":ScriptableMC-Engine-JS-Bundled:shadowJar") + dependsOn(":ScriptableMC-Engine-JS:Bundled:shadowJar") dependsOn(":ScriptableMC-Engine-PY:shadowJar") - dependsOn(":ScriptableMC-Engine-PY-Bundled:shadowJar") + dependsOn(":ScriptableMC-Engine-PY:Bundled:shadowJar") doFirst { if(!file("./build").exists()) file("./build").mkdirs() @@ -61,9 +61,9 @@ tasks.register("shadowJarAll") { doLast { file("./ScriptableMC-Engine-JS/build/libs/ScriptableMC-Engine-JS.jar").copyTo(file("./build/ScriptableMC-Engine-JS.jar"), overwrite = true) - file("./ScriptableMC-Engine-JS-Bundled/build/libs/ScriptableMC-Engine-JS-Bundled.jar").copyTo(file("./build/ScriptableMC-Engine-JS-Bundled.jar"), overwrite = true) + file("./ScriptableMC-Engine-JS/Bundled/build/libs/ScriptableMC-Engine-JS-Bundled.jar").copyTo(file("./build/ScriptableMC-Engine-JS-Bundled.jar"), overwrite = true) file("./ScriptableMC-Engine-PY/build/libs/ScriptableMC-Engine-PY.jar").copyTo(file("./build/ScriptableMC-Engine-PY.jar"), overwrite = true) - file("./ScriptableMC-Engine-PY-Bundled/build/libs/ScriptableMC-Engine-PY-Bundled.jar").copyTo(file("./build/ScriptableMC-Engine-PY-Bundled.jar"), overwrite = true) + file("./ScriptableMC-Engine-PY/Bundled/build/libs/ScriptableMC-Engine-PY-Bundled.jar").copyTo(file("./build/ScriptableMC-Engine-PY-Bundled.jar"), overwrite = true) } } diff --git a/settings.gradle.kts b/settings.gradle.kts index de616a9d..193fbe93 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -2,7 +2,7 @@ rootProject.name = "ScriptableMC-Engine" include("ScriptableMC-Engine-Core") include("ScriptableMC-Engine-JS") -include("ScriptableMC-Engine-JS-Bundled") +include("ScriptableMC-Engine-JS:Bundled") include("ScriptableMC-Engine-PY") -include("ScriptableMC-Engine-PY-Bundled") +include("ScriptableMC-Engine-PY:Bundled") include("ScriptableMC-Tools-TS") \ No newline at end of file From 2275c536f455d9126a0091572fa51f841296ffe4 Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Mon, 24 Feb 2020 12:20:43 -0700 Subject: [PATCH 28/29] Better plugin context enabling and disabling. engine.enableAllPlugins() will only enable currently disabled plugins. engine.enablePlugin() will warn if plugin context is already enabled. engine.disablePlugin() will warn if plugin context is not enabled. Added engine.disableAllPlugins() to disable all currently enabled plugins. Fixed a few typos in js config.yml --- .../core/ScriptablePluginContext.kt | 3 +++ .../core/ScriptablePluginEngine.kt | 27 +++++++++++++++---- .../core/JavaScriptPluginContext.kt | 2 ++ .../src/main/resources/config.yml | 4 +-- .../scriptablemc/core/PythonPluginContext.kt | 2 ++ 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/core/ScriptablePluginContext.kt b/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/core/ScriptablePluginContext.kt index 47220277..1e0ff017 100644 --- a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/core/ScriptablePluginContext.kt +++ b/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/core/ScriptablePluginContext.kt @@ -29,6 +29,9 @@ abstract class ScriptablePluginContext: Listener { abstract val inventoryManager: InventoryManager abstract val pluginVersion: Version + var isEnabled: Boolean = false + internal set + val server: Server get() = Bukkit.getServer() diff --git a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/core/ScriptablePluginEngine.kt b/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/core/ScriptablePluginEngine.kt index e1ec4e0f..410a74c0 100644 --- a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/core/ScriptablePluginEngine.kt +++ b/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/core/ScriptablePluginEngine.kt @@ -68,7 +68,7 @@ abstract class ScriptablePluginEngine { open fun loadAllHelperClasses() { for(helperClass in preLoadClasses) { try { - javaClass.classLoader.loadClass(helperClass.replace("*", "")) + javaClass.classLoader.loadClass(if(helperClass.startsWith("*")) helperClass.substring(1) else helperClass) } catch (e: Exception) { if(!helperClass.startsWith("*")) { @@ -80,18 +80,35 @@ abstract class ScriptablePluginEngine { } open fun enableAllPlugins() { - for (pluginContext in scriptablePlugins) { - pluginContext.enable() + for (pluginContext in scriptablePlugins.filter { !it.isEnabled }) { + enablePlugin(pluginContext) } enabledAllPlugins = true } + open fun disableAllPlugins() { + for (pluginContext in scriptablePlugins.filter { it.isEnabled }) { + disablePlugin(pluginContext) + } + enabledAllPlugins = false + } + open fun enablePlugin(pluginContext: ScriptablePluginContext) { - pluginContext.enable() + if(!pluginContext.isEnabled) { + pluginContext.enable() + } + else { + bootstrapPlugin.logger.warning("Trying to enable an already-enabled scriptable plugin context.") + } } open fun disablePlugin(pluginContext: ScriptablePluginContext) { - pluginContext.disable() + if(pluginContext.isEnabled) { + pluginContext.disable() + } + else { + bootstrapPlugin.logger.warning("Trying to disabled an already-disabled scriptable plugin context.") + } } open fun eval(source: Source): Value { diff --git a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginContext.kt b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginContext.kt index 8d5cd585..84988681 100644 --- a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginContext.kt +++ b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginContext.kt @@ -28,6 +28,7 @@ class JavaScriptPluginContext(override val engine: ScriptablePluginEngine, overr } pluginInstance.invokeMember("onEnable") + isEnabled = true } override fun disable() { @@ -43,6 +44,7 @@ class JavaScriptPluginContext(override val engine: ScriptablePluginEngine, overr for(command in commands) { unregisterCommand(command) } + isEnabled = false } companion object { diff --git a/ScriptableMC-Engine-JS/src/main/resources/config.yml b/ScriptableMC-Engine-JS/src/main/resources/config.yml index 784731f7..63fe086b 100644 --- a/ScriptableMC-Engine-JS/src/main/resources/config.yml +++ b/ScriptableMC-Engine-JS/src/main/resources/config.yml @@ -15,7 +15,7 @@ script_mime_type: 'application/javascript+module' main_script_files: - '${root_scripts_folder}/main.js' -# Auto-Enable all scriptable plugins after loading main scripts +# Auto-Enable all scriptable plugins after executing all main scripts # If set to false you will have to manually enable all plugins # via engine.enableAllPlugins() after you have loaded all plugins # Default: true @@ -37,7 +37,7 @@ version_check: true extract_libs: true # Execute command template -# This is the code that will be executed with /pyex and /smc python execute +# This is the code that will be executed with /jsex and /smc javascript execute # If a class is returned it will be instantiated and continued on as an object # If object is returned and has a method execute script engine will call # returnedObject.execute(sender: CommandSender, server: BukkitServer, servicesManager) diff --git a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginContext.kt b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginContext.kt index f82452e8..8b9e1b87 100644 --- a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginContext.kt +++ b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginContext.kt @@ -28,6 +28,7 @@ class PythonPluginContext(override val engine: ScriptablePluginEngine, override } pluginInstance.invokeMember("onEnable") + isEnabled = true } override fun disable() { @@ -43,6 +44,7 @@ class PythonPluginContext(override val engine: ScriptablePluginEngine, override for(command in commands) { unregisterCommand(command) } + isEnabled = false } companion object { From e3d4fad372bfda6beba4dfd17500bf256dd6d9fb Mon Sep 17 00:00:00 2001 From: Ashton Storks Date: Mon, 24 Feb 2020 19:34:25 -0700 Subject: [PATCH 29/29] Moved try/catch handling to ScriptablePluginEngine.loadMainScript Added ScriptablePluginEngine.getStartupErrors() to get startup errors inside the script engine. --- .../scriptablemc/ScriptEngineConfig.kt | 3 ++ .../core/ScriptablePluginEngine.kt | 11 +---- .../core/JavaScriptPluginEngine.kt | 43 +++++++++-------- .../src/main/resources/config.yml | 7 +-- .../scriptablemc/core/PythonPluginEngine.kt | 47 ++++++++++--------- 5 files changed, 56 insertions(+), 55 deletions(-) diff --git a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/ScriptEngineConfig.kt b/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/ScriptEngineConfig.kt index 7e590ffe..c3fad357 100644 --- a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/ScriptEngineConfig.kt +++ b/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/ScriptEngineConfig.kt @@ -22,6 +22,7 @@ abstract class ScriptEngineConfig(private val config: FileConfiguration) { val debugger: ScriptEngineDebuggerConfig get() = ScriptEngineDebuggerConfig(this) + @JvmOverloads fun readConfigString(path: String, def: String = ""): String { val input = config.getString(path, def).orEmpty() val regex = Regex("\\\$\\{(.*)}") @@ -32,6 +33,7 @@ abstract class ScriptEngineConfig(private val config: FileConfiguration) { } } + @JvmOverloads fun readConfigStringList(path: String, def: List = listOf()): List { val inputList = config.getStringList(path) val regex = Regex("\\\$\\{(.*)}") @@ -50,6 +52,7 @@ abstract class ScriptEngineConfig(private val config: FileConfiguration) { return inputList } + @JvmOverloads fun readConfigBoolean(path: String, def: Boolean = false): Boolean { return readConfigString(path, def.toString()).equals("true", true) } diff --git a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/core/ScriptablePluginEngine.kt b/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/core/ScriptablePluginEngine.kt index 410a74c0..9c8ea70e 100644 --- a/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/core/ScriptablePluginEngine.kt +++ b/ScriptableMC-Engine-Core/src/main/kotlin/com/pixlfox/scriptablemc/core/ScriptablePluginEngine.kt @@ -32,9 +32,7 @@ abstract class ScriptablePluginEngine { var enabledAllPlugins: Boolean = false internal set - private val _startupErrors = mutableListOf() - val startupErrors: Array - get() = _startupErrors.toTypedArray() + val startupErrors: MutableList = mutableListOf() open fun start() { inventoryManager.init() @@ -43,12 +41,7 @@ abstract class ScriptablePluginEngine { loadAllHelperClasses() for(mainScriptFile in config.mainScriptFiles) { - try { - loadMainScript(mainScriptFile) - } - catch(ex: Exception) { - _startupErrors.add(ex) - } + loadMainScript(mainScriptFile) } if(!enabledAllPlugins && config.autoEnablePlugins) { diff --git a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt index 97a1c646..3baf2458 100644 --- a/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt +++ b/ScriptableMC-Engine-JS/src/main/kotlin/com/pixlfox/scriptablemc/core/JavaScriptPluginEngine.kt @@ -93,29 +93,34 @@ class JavaScriptPluginEngine(override val bootstrapPlugin: ScriptEngineMain, ove } override fun loadMainScript(path: String) { - val mainScriptFile = File(path) - if(!mainScriptFile.parentFile.exists()) { - mainScriptFile.parentFile.mkdirs() - } + try { + val mainScriptFile = File(path) + if(!mainScriptFile.parentFile.exists()) { + mainScriptFile.parentFile.mkdirs() + } - if(mainScriptFile.exists()) { - val mainReturn = eval( - Source.newBuilder(languageName, mainScriptFile) - .name(mainScriptFile.name) - .mimeType(config.scriptMimeType) - .interactive(false) - .build() - ) - - // Load all plugin types returned as an array - if(mainReturn.hasArrayElements()) { - for (i in 0 until mainReturn.arraySize) { - this.loadPlugin(mainReturn.getArrayElement(i)) + if(mainScriptFile.exists()) { + val mainReturn = eval( + Source.newBuilder(languageName, mainScriptFile) + .name(mainScriptFile.name) + .mimeType(config.scriptMimeType) + .interactive(false) + .build() + ) + + // Load all plugin types returned as an array + if(mainReturn.hasArrayElements()) { + for (i in 0 until mainReturn.arraySize) { + this.loadPlugin(mainReturn.getArrayElement(i)) + } } } + else { + throw ScriptNotFoundException(mainScriptFile) + } } - else { - throw ScriptNotFoundException(mainScriptFile) + catch(ex: Exception) { + startupErrors.add(ex) } } diff --git a/ScriptableMC-Engine-JS/src/main/resources/config.yml b/ScriptableMC-Engine-JS/src/main/resources/config.yml index 63fe086b..995f98ac 100644 --- a/ScriptableMC-Engine-JS/src/main/resources/config.yml +++ b/ScriptableMC-Engine-JS/src/main/resources/config.yml @@ -67,15 +67,10 @@ common_js: # CommonJS globals file name # The CommonJS globals file used to define global CommonJS values - # Relative to the `common_js.modules_path` value + # Relative to the `common_js.modules_path` folder # Default: globals.js globals_file: 'globals.js' -java_libraries: - - path: "" - preload_classes: - - "" - # Chrome debugger settings # **WARNING** THE DEBUGGER SHOULD NEVER BE ENABLED ON A PUBLIC SERVER # IT IS INTENDED ONLY FOR DEBUGGING THE SCRIPT ENGINE AND CAN BREAK/FREEZE YOUR WHOLE SERVER diff --git a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginEngine.kt b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginEngine.kt index 8ee84ddb..22f77fa2 100644 --- a/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginEngine.kt +++ b/ScriptableMC-Engine-PY/src/main/kotlin/com/pixlfox/scriptablemc/core/PythonPluginEngine.kt @@ -54,33 +54,38 @@ class PythonPluginEngine(override val bootstrapPlugin: ScriptEngineMain, overrid } override fun loadMainScript(path: String) { - val mainScriptFile = File(path) - if(!mainScriptFile.parentFile.exists()) { - mainScriptFile.parentFile.mkdirs() - } + try { + val mainScriptFile = File(path) + if(!mainScriptFile.parentFile.exists()) { + mainScriptFile.parentFile.mkdirs() + } - if(mainScriptFile.exists()) { - val mainReturn = eval( - Source.newBuilder(languageName, mainScriptFile) - .name(mainScriptFile.name) - .interactive(false) - .build() - ) + if(mainScriptFile.exists()) { + val mainReturn = eval( + Source.newBuilder(languageName, mainScriptFile) + .name(mainScriptFile.name) + .interactive(false) + .build() + ) - // Load all plugin types returned as an array - if(mainReturn.hasArrayElements()) { - for (i in 0 until mainReturn.arraySize) { - this.loadPlugin(mainReturn.getArrayElement(i)) - } + // Load all plugin types returned as an array + if(mainReturn.hasArrayElements()) { + for (i in 0 until mainReturn.arraySize) { + this.loadPlugin(mainReturn.getArrayElement(i)) + } - // Enable all plugins if not already enabled - if(!enabledAllPlugins) { - enableAllPlugins() + // Enable all plugins if not already enabled + if(!enabledAllPlugins) { + enableAllPlugins() + } } } + else { + throw ScriptNotFoundException(mainScriptFile) + } } - else { - throw ScriptNotFoundException(mainScriptFile) + catch(ex: Exception) { + startupErrors.add(ex) } }