From 869a5df1fd3f938bac218ee9e921041ead969989 Mon Sep 17 00:00:00 2001 From: CarmJos Date: Thu, 24 Feb 2022 04:39:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0JarResourceUtils=20=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E5=A4=8D=E5=88=B6=E7=A4=BA=E4=BE=8B=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 +- .../easysql/command/EasySQLCommand.java | 2 +- ...pertiesUtil.java => DBPropertiesUtil.java} | 26 +++-- .../plugin/easysql/util/JarResourceUtils.java | 105 ++++++++++++++++++ ...sourceReadUtil.java => MavenReadUtil.java} | 19 +--- .../cc/carm/plugin/easysql/EasySQLBukkit.java | 8 +- 6 files changed, 139 insertions(+), 33 deletions(-) rename easysql-plugin-core/src/main/java/cc/carm/plugin/easysql/util/{PropertiesUtil.java => DBPropertiesUtil.java} (50%) create mode 100644 easysql-plugin-core/src/main/java/cc/carm/plugin/easysql/util/JarResourceUtils.java rename easysql-plugin-core/src/main/java/cc/carm/plugin/easysql/util/{ResourceReadUtil.java => MavenReadUtil.java} (70%) diff --git a/README.md b/README.md index 36f8df1..e20b22d 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,14 @@ 轻松(用)SQL的独立运行库插件,支持多种服务端,适用于MineCraft全版本。 +## 使用场景 + +### 对于插件使用者 + +### 对于插件开发者 + +### 额外提醒 + ## 安装 ## 配置 @@ -56,12 +64,12 @@ databases: 示例配置请见 [示例MySQL数据源Properties](easysql-plugin-core/src/main/resources/db-properties/.example-mysql.properties)。 +Properties 文件的文件名几位数据源的ID,允许为英文、数字、下划线、短横线;请不要包含中文、其他特殊符号与空格,以`.`开头的文件将被忽略。 + 该功能一般用于专业开发者使用,若您不了解该功能,请尽量使用config.yml中提供的配置方式,简单便捷,能够满足大多数需求。 更多帮助详见 [BeeCP项目帮助](https://github.com/Chris2018998/BeeCP) 。 - - ## 开发 ### 依赖方式 diff --git a/easysql-plugin-core/src/main/java/cc/carm/plugin/easysql/command/EasySQLCommand.java b/easysql-plugin-core/src/main/java/cc/carm/plugin/easysql/command/EasySQLCommand.java index efbd702..871b0ca 100644 --- a/easysql-plugin-core/src/main/java/cc/carm/plugin/easysql/command/EasySQLCommand.java +++ b/easysql-plugin-core/src/main/java/cc/carm/plugin/easysql/command/EasySQLCommand.java @@ -11,7 +11,7 @@ import co.aikar.commands.annotation.*; import java.util.Map; import java.util.UUID; -import static cc.carm.plugin.easysql.util.ResourceReadUtil.getVersion; +import static cc.carm.plugin.easysql.util.MavenReadUtil.getVersion; @SuppressWarnings("unused") diff --git a/easysql-plugin-core/src/main/java/cc/carm/plugin/easysql/util/PropertiesUtil.java b/easysql-plugin-core/src/main/java/cc/carm/plugin/easysql/util/DBPropertiesUtil.java similarity index 50% rename from easysql-plugin-core/src/main/java/cc/carm/plugin/easysql/util/PropertiesUtil.java rename to easysql-plugin-core/src/main/java/cc/carm/plugin/easysql/util/DBPropertiesUtil.java index 661d6f2..7e149a0 100644 --- a/easysql-plugin-core/src/main/java/cc/carm/plugin/easysql/util/PropertiesUtil.java +++ b/easysql-plugin-core/src/main/java/cc/carm/plugin/easysql/util/DBPropertiesUtil.java @@ -1,5 +1,7 @@ package cc.carm.plugin.easysql.util; +import org.jetbrains.annotations.NotNull; + import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -8,26 +10,34 @@ import java.util.HashMap; import java.util.Map; import java.util.Properties; -public class PropertiesUtil { +public class DBPropertiesUtil { - public static Map readDBProperties(File propertiesFolder) { + public static Map readFromFolder(File propertiesFolder) { Map propertiesMap = new HashMap<>(); if (!propertiesFolder.isDirectory()) return propertiesMap; File[] files = propertiesFolder.listFiles(); if (files == null || files.length == 0) return propertiesMap; for (File file : files) { - if (file.getName().startsWith(".") || !file.getName().endsWith(".properties")) continue; + if (!validateName(file.getName())) continue; String name = file.getName().substring(0, file.getName().lastIndexOf(".")); - try (InputStream stream = new FileInputStream(file)) { - Properties properties = new Properties(); - properties.load(stream); - propertiesMap.put(name, properties); + try (InputStream is = new FileInputStream(file)) { + propertiesMap.put(name, read(is)); } catch (IOException ignored) { } } - + return propertiesMap; } + public static @NotNull Properties read(InputStream stream) throws IOException { + Properties properties = new Properties(); + properties.load(stream); + return properties; + } + + public static boolean validateName(String name) { + return !name.contains(" ") && !name.startsWith(".") && name.endsWith(".properties"); + } + } diff --git a/easysql-plugin-core/src/main/java/cc/carm/plugin/easysql/util/JarResourceUtils.java b/easysql-plugin-core/src/main/java/cc/carm/plugin/easysql/util/JarResourceUtils.java new file mode 100644 index 0000000..da6ffd7 --- /dev/null +++ b/easysql-plugin-core/src/main/java/cc/carm/plugin/easysql/util/JarResourceUtils.java @@ -0,0 +1,105 @@ +package cc.carm.plugin.easysql.util; + +import org.jetbrains.annotations.Nullable; + +import java.io.*; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +@SuppressWarnings("ResultOfMethodCallIgnored") +public class JarResourceUtils { + public static final char JAR_SEPARATOR = '/'; + + public static @Nullable String[] readResource(@Nullable InputStream resourceStream) { + if (resourceStream == null) return null; + try (Scanner scanner = new Scanner(resourceStream, "UTF-8")) { + List contents = new ArrayList<>(); + while (scanner.hasNextLine()) { + contents.add(scanner.nextLine()); + } + return contents.toArray(new String[0]); + } catch (Exception e) { + return null; + } + } + + public static void copyFolderFromJar(String folderName, File destFolder, CopyOption option) + throws IOException { + copyFolderFromJar(folderName, destFolder, option, null); + } + + public static void copyFolderFromJar(String folderName, File destFolder, + CopyOption option, PathTrimmer trimmer) throws IOException { + if (!destFolder.exists()) + destFolder.mkdirs(); + + byte[] buffer = new byte[1024]; + + File fullPath; + String path = JarResourceUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath(); + if (trimmer != null) + path = trimmer.trim(path); + try { + if (!path.startsWith("file")) + path = "file://" + path; + + fullPath = new File(new URI(path)); + } catch (URISyntaxException e) { + e.printStackTrace(); + return; + } + + ZipInputStream zis = new ZipInputStream(new FileInputStream(fullPath)); + + ZipEntry entry; + while ((entry = zis.getNextEntry()) != null) { + if (!entry.getName().startsWith(folderName + JAR_SEPARATOR)) + continue; + + String fileName = entry.getName(); + + if (fileName.charAt(fileName.length() - 1) == JAR_SEPARATOR) { + File file = new File(destFolder + File.separator + fileName); + if (file.isFile()) { + file.delete(); + } + file.mkdirs(); + continue; + } + + File file = new File(destFolder + File.separator + fileName); + if (option == CopyOption.COPY_IF_NOT_EXIST && file.exists()) + continue; + + if (!file.getParentFile().exists()) + file.getParentFile().mkdirs(); + + if (!file.exists()) + file.createNewFile(); + FileOutputStream fos = new FileOutputStream(file); + + int len; + while ((len = zis.read(buffer)) > 0) { + fos.write(buffer, 0, len); + } + fos.close(); + } + + zis.closeEntry(); + zis.close(); + } + + public enum CopyOption { + COPY_IF_NOT_EXIST, REPLACE_IF_EXIST + } + + @FunctionalInterface + public interface PathTrimmer { + String trim(String original); + } +} \ No newline at end of file diff --git a/easysql-plugin-core/src/main/java/cc/carm/plugin/easysql/util/ResourceReadUtil.java b/easysql-plugin-core/src/main/java/cc/carm/plugin/easysql/util/MavenReadUtil.java similarity index 70% rename from easysql-plugin-core/src/main/java/cc/carm/plugin/easysql/util/ResourceReadUtil.java rename to easysql-plugin-core/src/main/java/cc/carm/plugin/easysql/util/MavenReadUtil.java index 04afa66..cdf82ca 100644 --- a/easysql-plugin-core/src/main/java/cc/carm/plugin/easysql/util/ResourceReadUtil.java +++ b/easysql-plugin-core/src/main/java/cc/carm/plugin/easysql/util/MavenReadUtil.java @@ -4,26 +4,9 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; import java.util.Properties; -import java.util.Scanner; -public class ResourceReadUtil { - - - public static @Nullable String[] readResource(@Nullable InputStream resourceStream) { - if (resourceStream == null) return null; - try (Scanner scanner = new Scanner(resourceStream, "UTF-8")) { - List contents = new ArrayList<>(); - while (scanner.hasNextLine()) { - contents.add(scanner.nextLine()); - } - return contents.toArray(new String[0]); - } catch (Exception e) { - return null; - } - } +public class MavenReadUtil { public static String getMavenPropertiesPath(@NotNull String groupID, @NotNull String artifactID) { return String.format("/META-INF/maven/%s/%s/pom.properties", groupID, artifactID); diff --git a/platforms/easysql-plugin-bukkit/src/main/java/cc/carm/plugin/easysql/EasySQLBukkit.java b/platforms/easysql-plugin-bukkit/src/main/java/cc/carm/plugin/easysql/EasySQLBukkit.java index 1b07f7a..171488c 100644 --- a/platforms/easysql-plugin-bukkit/src/main/java/cc/carm/plugin/easysql/EasySQLBukkit.java +++ b/platforms/easysql-plugin-bukkit/src/main/java/cc/carm/plugin/easysql/EasySQLBukkit.java @@ -4,8 +4,8 @@ import cc.carm.lib.easyplugin.EasyPlugin; import cc.carm.lib.easyplugin.i18n.EasyPluginMessageProvider; import cc.carm.lib.easysql.api.SQLManager; import cc.carm.plugin.easysql.api.DBConfiguration; -import cc.carm.plugin.easysql.util.PropertiesUtil; -import cc.carm.plugin.easysql.util.ResourceReadUtil; +import cc.carm.plugin.easysql.util.JarResourceUtils; +import cc.carm.plugin.easysql.util.DBPropertiesUtil; import cn.beecp.BeeDataSource; import co.aikar.commands.PaperCommandManager; import org.bstats.bukkit.Metrics; @@ -111,12 +111,12 @@ public class EasySQLBukkit extends EasyPlugin implements EasySQLPluginPlatform { if (!getConfiguration().isPropertiesEnabled()) return new HashMap<>(); String propertiesFolder = getConfiguration().getPropertiesFolder(); if (propertiesFolder == null || propertiesFolder.length() == 0) return new HashMap<>(); - else return PropertiesUtil.readDBProperties(new File(getDataFolder(), propertiesFolder)); + else return DBPropertiesUtil.readFromFolder(new File(getDataFolder(), propertiesFolder)); } @Override public void outputInfo() { - Optional.ofNullable(ResourceReadUtil.readResource(this.getResource("PLUGIN_INFO"))).ifPresent(this::log); + Optional.ofNullable(JarResourceUtils.readResource(this.getResource("PLUGIN_INFO"))).ifPresent(this::log); } public static EasySQLBukkit getInstance() {