Fix TLocale

This commit is contained in:
坏黑 2019-07-08 15:56:40 +08:00
parent f8c028e12d
commit 077b09f5ac
6 changed files with 47 additions and 42 deletions

View File

@ -1,14 +1,5 @@
DEPENDENCY: DEPENDENCY:
DOWNLOAD-OFFLINE: '已启用离线模式, 将不会下载第三方依赖库' DOWNLOAD-START: ' 正在下载 {0} 库文件'
DOWNLOAD-CONNECTED: ' 正在下载 {0} 大小 {1}'
DOWNLOAD-PROGRESS: ' 下载速度 {0} 进度 {1}'
DOWNLOAD-SUCCESS: ' 下载 {0} 完成'
DOWNLOAD-FAILED:
- ' 下载 {0} 失败'
- ' 请手动下载 {1} 并重命名为 {2} 后放在 /{0}/libs 文件夹内'
PLUGIN-AUTOLOAD-FAIL: '{0} 所依赖的插件 {1} 尝试自动加载失败,请尝试手动下载'
PLUGIN-LOAD-SUCCESS: ' {0} 请求的插件 {1} 加载成功'
PLUGIN-LOAD-FAIL: ' {0} 请求的插件 {1} 加载失败'
LIBRARY-LOAD-FAIL: ' {0} 请求的库文件 {1} 加载失败' LIBRARY-LOAD-FAIL: ' {0} 请求的库文件 {1} 加载失败'
LOCALE: LOCALE:

View File

@ -160,8 +160,8 @@ public class TabooLibPluginCommand extends BaseMainCommand {
} else if (PluginUtils.isIgnored(plugin)) { } else if (PluginUtils.isIgnored(plugin)) {
TLocale.sendTo(sender, "COMMANDS.TPLUGIN.RELOAD.INVALID-PLUGIN-IGNORED", name); TLocale.sendTo(sender, "COMMANDS.TPLUGIN.RELOAD.INVALID-PLUGIN-IGNORED", name);
} else { } else {
TabooLibAPI.dispatchCommand(sender, "taboolibplugin unload " + plugin.getName()); TabooLibAPI.dispatchCommand(sender, "tPlugin unload " + plugin.getName());
TabooLibAPI.dispatchCommand(sender, "taboolibplugin load " + plugin.getName()); TabooLibAPI.dispatchCommand(sender, "tPlugin load " + plugin.getName());
} }
} }
}; };

View File

@ -50,7 +50,7 @@ public class InternalPlugin implements Plugin {
@Override @Override
public InputStream getResource(String s) { public InputStream getResource(String s) {
return Files.getResourceTabooLib(s); return Files.getTabooLibResource(s);
} }
@Override @Override

View File

@ -1,6 +1,7 @@
package io.izzel.taboolib.module.dependency; package io.izzel.taboolib.module.dependency;
import io.izzel.taboolib.TabooLib; import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.module.locale.TLocale;
import io.izzel.taboolib.util.Files; import io.izzel.taboolib.util.Files;
import java.io.File; import java.io.File;
@ -56,6 +57,7 @@ public class TDependency {
} }
private static boolean downloadMaven(String url, String groupId, String artifactId, String version, File target, String dl) { private static boolean downloadMaven(String url, String groupId, String artifactId, String version, File target, String dl) {
TLocale.Logger.info("DEPENDENCY.DOWNLOAD-START", target.getName());
Files.toFile(Files.readFromURL(dl.length() == 0 ? url + "/" + groupId.replace('.', '/') + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + ".jar" : dl, ""), Files.file(target)); Files.toFile(Files.readFromURL(dl.length() == 0 ? url + "/" + groupId.replace('.', '/') + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + ".jar" : dl, ""), Files.file(target));
return target.length() > 0; return target.length() > 0;
} }

View File

@ -2,11 +2,10 @@ package io.izzel.taboolib.module.locale;
import io.izzel.taboolib.TabooLib; import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.TabooLibAPI; import io.izzel.taboolib.TabooLibAPI;
import io.izzel.taboolib.module.locale.type.*;
import io.izzel.taboolib.module.config.TConfigWatcher; import io.izzel.taboolib.module.config.TConfigWatcher;
import io.izzel.taboolib.module.locale.logger.TLogger; import io.izzel.taboolib.module.locale.logger.TLogger;
import io.izzel.taboolib.module.locale.type.*;
import io.izzel.taboolib.util.Files; import io.izzel.taboolib.util.Files;
import io.izzel.taboolib.util.IO;
import io.izzel.taboolib.util.Strings; import io.izzel.taboolib.util.Strings;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -16,7 +15,8 @@ import org.bukkit.plugin.Plugin;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.Charset; import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -75,12 +75,12 @@ public class TLocaleLoader {
} }
// 加载文件 // 加载文件
YamlConfiguration localeConfiguration = Files.loadYaml(localeFile); YamlConfiguration localeConfiguration = Files.loadYaml(localeFile);
YamlConfiguration localeConfigurationAtStream = getLocaleAtStream(plugin, localeFile); YamlConfiguration localeConfigurationAtStream = getLocaleAsPlugin(plugin, localeFile);
// 载入配置 // 载入配置
loadPluginLocale(plugin, localeFile, localeConfiguration, localeConfigurationAtStream); loadPluginLocale(plugin, localeFile, localeConfiguration, localeConfigurationAtStream);
// 注册监听 // 注册监听
TConfigWatcher.getInst().removeListener(localeFile); TConfigWatcher.getInst().removeListener(localeFile);
TConfigWatcher.getInst().addListener(localeFile, null, obj -> loadPluginLocale(plugin, localeFile, Files.loadYaml(localeFile), getLocaleAtStream(plugin, localeFile))); TConfigWatcher.getInst().addListener(localeFile, null, obj -> loadPluginLocale(plugin, localeFile, Files.loadYaml(localeFile), getLocaleAsPlugin(plugin, localeFile)));
} }
} catch (Exception e) { } catch (Exception e) {
errorLogger("ERROR-LOADING-LANG", plugin.getName(), e.toString() + "\n" + e.getStackTrace()[0].toString()); errorLogger("ERROR-LOADING-LANG", plugin.getName(), e.toString() + "\n" + e.getStackTrace()[0].toString());
@ -116,29 +116,27 @@ public class TLocaleLoader {
} }
private static File getLocaleFile(Plugin plugin) { private static File getLocaleFile(Plugin plugin) {
releaseLocales(plugin); getLocalePriority().forEach(localeName -> Files.releaseResource(plugin, "lang/" + localeName + ".yml", false));
return getLocalePriority().stream().map(localeName -> new File("plugins/" + plugin.getName() + "/lang/" + localeName + ".yml")).filter(File::exists).findFirst().orElse(null); return getLocalePriority().stream().map(localeName -> new File("plugins/" + plugin.getName() + "/lang/" + localeName + ".yml")).filter(File::exists).findFirst().orElse(null);
} }
private static void releaseLocales(Plugin plugin) {
getLocalePriority().stream().filter(localeName -> !new File("plugins/" + plugin.getName() + "/lang/" + localeName + ".yml").exists() && plugin.getResource("lang/" + localeName + ".yml") != null).forEach(localeName -> plugin.saveResource("lang/" + localeName + ".yml", true));
}
private static TLocaleInstance getLocaleInstance(Plugin plugin) { private static TLocaleInstance getLocaleInstance(Plugin plugin) {
TLocaleInstance instance = new TLocaleInstance(plugin); TLocaleInstance instance = new TLocaleInstance(plugin);
map.put(plugin.getName(), instance); map.put(plugin.getName(), instance);
return instance; return instance;
} }
private static YamlConfiguration getLocaleAtStream(Plugin plugin, File localeFile) { private static YamlConfiguration getLocaleAsPlugin(Plugin plugin, File localeFile) {
InputStream localeInputSteam = Files.getResource(plugin, "lang/" + localeFile.getName()); try (InputStream canonicalResource = Files.getCanonicalResource(plugin, "lang/" + localeFile.getName())) {
try { if (canonicalResource != null) {
YamlConfiguration yaml = new YamlConfiguration(); return YamlConfiguration.loadConfiguration(new InputStreamReader(canonicalResource, StandardCharsets.UTF_8));
yaml.loadFromString(IO.readFully(localeInputSteam, Charset.forName("utf-8"))); }
return yaml; } catch (Throwable t) {
} catch (Exception ignored) { t.printStackTrace();
return null; } finally {
Files.clearTempFiles();
} }
return new YamlConfiguration();
} }
private static void loadPluginLocale(Plugin plugin, File localeFile, YamlConfiguration localeConfiguration, YamlConfiguration localeConfigurationAtStream) { private static void loadPluginLocale(Plugin plugin, File localeFile, YamlConfiguration localeConfiguration, YamlConfiguration localeConfigurationAtStream) {

View File

@ -2,6 +2,7 @@ package io.izzel.taboolib.util;
import io.izzel.taboolib.TabooLib; import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.common.plugin.InternalPlugin; import io.izzel.taboolib.common.plugin.InternalPlugin;
import io.izzel.taboolib.module.inject.TSchedule;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@ -12,10 +13,7 @@ import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Arrays; import java.util.*;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
@ -54,31 +52,47 @@ public class Files {
return classes; return classes;
} }
@TSchedule(period = 100, async = true)
public static void clearTempFiles() {
deepDelete(new File("plugins/TabooLib/temp"));
}
public static InputStream getResource(String filename) { public static InputStream getResource(String filename) {
return getResource(TabooLib.getPlugin(), filename); return getResource(TabooLib.getPlugin(), filename);
} }
public static InputStream getResource(Plugin plugin, String filename) { public static InputStream getResource(Plugin plugin, String filename) {
return plugin instanceof InternalPlugin ? getResourceTabooLib(filename) : plugin.getClass().getClassLoader().getResourceAsStream(filename); return plugin instanceof InternalPlugin ? getTabooLibResource(filename) : plugin.getClass().getClassLoader().getResourceAsStream(filename);
} }
public static InputStream getResourceTabooLib(String filename) { public static InputStream getTabooLibResource(String filename) {
return getCanonicalResource(TabooLib.getPlugin(), filename);
}
public static InputStream getCanonicalResource(Plugin plugin, String filename) {
File file = Files.file(new File("plugins/TabooLib/temp/" + UUID.randomUUID()));
try { try {
ZipFile zipFile = new ZipFile(new File("TabooLib.jar")); ZipFile zipFile = new ZipFile(Files.toFile(plugin.getClass().getProtectionDomain().getCodeSource().getLocation().openStream(), file));
ZipEntry entry = zipFile.getEntry(filename); ZipEntry entry = zipFile.getEntry(filename);
if (entry != null) { if (entry != null) {
return zipFile.getInputStream(entry); return zipFile.getInputStream(entry);
} }
} catch (IOException e) { } catch (Exception t) {
e.printStackTrace(); t.printStackTrace();
} }
return TabooLib.class.getResourceAsStream(filename); return null;
} }
public static void releaseResource(Plugin plugin, String path, boolean replace) { public static void releaseResource(Plugin plugin, String path, boolean replace) {
File file = new File(plugin.getDataFolder(), path); File file = new File(plugin.getDataFolder(), path);
if (!file.exists() || replace) { if (!file.exists() || replace) {
toFile(getResource(plugin, path), Files.file(file)); try (InputStream inputStream = getCanonicalResource(plugin, path)) {
if (inputStream != null) {
toFile(inputStream, Files.file(file));
}
} catch (Throwable t) {
t.printStackTrace();
}
} }
} }