From 488eecba077d67facacbbeba4cecbe66cff553fc Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Sat, 29 Feb 2020 22:28:43 +0800 Subject: [PATCH] feat: add download and getPluginsFolder Signed-off-by: MiaoWoo --- packages/api/src/interfaces/plugin.ts | 2 +- packages/api/src/interfaces/server.ts | 1 + packages/bukkit/src/server.ts | 3 +++ packages/bungee/src/server.ts | 3 +++ packages/common/src/http.ts | 14 ++++++++++++-- packages/sponge/src/server.ts | 12 ++++++++++-- 6 files changed, 30 insertions(+), 5 deletions(-) diff --git a/packages/api/src/interfaces/plugin.ts b/packages/api/src/interfaces/plugin.ts index a5427c58..9f2ec4ab 100644 --- a/packages/api/src/interfaces/plugin.ts +++ b/packages/api/src/interfaces/plugin.ts @@ -4,7 +4,7 @@ export namespace plugin { */ export const Plugin = Symbol("Plugin"); /** - * MiaoScript Plugin + * MiaoScript Plugin Folder */ export const PluginFolder = Symbol("PluginFolder"); /** diff --git a/packages/api/src/interfaces/server.ts b/packages/api/src/interfaces/server.ts index 89fc7f38..f94744db 100644 --- a/packages/api/src/interfaces/server.ts +++ b/packages/api/src/interfaces/server.ts @@ -26,6 +26,7 @@ export namespace server { getService(service: string): any; dispatchCommand(sender: string | any, command: string): boolean; dispatchConsoleCommand(command: string): boolean; + getPluginsFolder(): string; sendJson(sender: string | any, json: object | string): void; } } diff --git a/packages/bukkit/src/server.ts b/packages/bukkit/src/server.ts index 23669b4b..a6e17a24 100644 --- a/packages/bukkit/src/server.ts +++ b/packages/bukkit/src/server.ts @@ -31,6 +31,9 @@ export class BukkitServer implements server.Server { dispatchConsoleCommand(command: string): boolean { return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command) } + getPluginsFolder(): string { + return Bukkit.getUpdateFolderFile().getParentFile().getCanonicalPath(); + } sendJson(sender: string | any, json: object | string): void { if (typeof sender === "string") { sender = this.getPlayer(sender) diff --git a/packages/bungee/src/server.ts b/packages/bungee/src/server.ts index e397f1a9..c43b2401 100644 --- a/packages/bungee/src/server.ts +++ b/packages/bungee/src/server.ts @@ -29,6 +29,9 @@ export class BungeeServer implements server.Server { dispatchConsoleCommand(command: string): boolean { return Bungee.getPluginManager().dispatchCommand(Bungee.getConsole(), command) } + getPluginsFolder(): string { + return Bungee.getPluginsFolder().getCanonicalPath(); + } sendJson(sender: string | any, json: string): void { throw new Error("Method not implemented."); } diff --git a/packages/common/src/http.ts b/packages/common/src/http.ts index 2d75ce22..e609b387 100644 --- a/packages/common/src/http.ts +++ b/packages/common/src/http.ts @@ -1,5 +1,10 @@ import '@ms/api' +const URL = Java.type('java.net.URL') +const Paths = Java.type('java.nio.file.Paths'); +const Files = Java.type('java.nio.file.Files'); +const StandardCopyOption = Java.type('java.nio.file.StandardCopyOption'); + export type Method = | 'get' | 'GET' | 'delete' | 'DELETE' @@ -31,8 +36,12 @@ function request(config: RequestConfig) { return xhr.get(); } +function download(url: string, target: string) { + Files.copy(new URL(url).openStream(), Paths.get(target), StandardCopyOption.REPLACE_EXISTING); +} + function _proxy(method: Method) { - return function(url: string, data?: any, config?: RequestConfig) { + return function (url: string, data?: any, config?: RequestConfig) { return request({ url, method, data, ...config }); } } @@ -40,5 +49,6 @@ function _proxy(method: Method) { export default { get: _proxy('GET'), post: _proxy('POST'), - request + request, + download } diff --git a/packages/sponge/src/server.ts b/packages/sponge/src/server.ts index ce27ad27..1790dd5f 100644 --- a/packages/sponge/src/server.ts +++ b/packages/sponge/src/server.ts @@ -1,8 +1,12 @@ import { server } from '@ms/api' import { provideSingleton } from '@ms/container'; -let Sponge = org.spongepowered.api.Sponge; -let TextSerializers = org.spongepowered.api.text.serializer.TextSerializers; +import * as fs from '@ms/common/dist/fs' + +const Sponge = org.spongepowered.api.Sponge; +const TextSerializers = org.spongepowered.api.text.serializer.TextSerializers; +const URL = Java.type("java.net.URL"); +const File = Java.type("java.io.File"); @provideSingleton(server.Server) export class SpongeServer implements server.Server { @@ -30,6 +34,10 @@ export class SpongeServer implements server.Server { dispatchConsoleCommand(command: string): boolean { return Sponge.getCommandManager().process(Sponge.getServer().getConsole(), command).getQueryResult() } + getPluginsFolder(): string { + let pluginFile = new URL(base.getInstance().getClass().getProtectionDomain().getCodeSource().getLocation().getPath().split("!")[0]).path + return new File(pluginFile).getParentFile().getCanonicalPath() + } sendJson(sender: string | any, json: string): void { if (typeof sender === "string") { sender = this.getPlayer(sender)