完善了一下下载库以及 TLib 类

This commit is contained in:
Izzel_Aliz 2018-04-05 02:53:25 +08:00
parent 30ed453a76
commit 96269db54d
12 changed files with 130 additions and 52 deletions

View File

@ -1,13 +1,13 @@
<component name="libraryTable"> <component name="libraryTable">
<library name="Maven: com.ilummc.eagletdl:EagletCore:1.0"> <library name="Maven: com.ilummc.eagletdl:EagletCore:1.1">
<CLASSES> <CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/ilummc/eagletdl/EagletCore/1.0/EagletCore-1.0.jar!/" /> <root url="jar://$MAVEN_REPOSITORY$/com/ilummc/eagletdl/EagletCore/1.1/EagletCore-1.1.jar!/" />
</CLASSES> </CLASSES>
<JAVADOC> <JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/com/ilummc/eagletdl/EagletCore/1.0/EagletCore-1.0-javadoc.jar!/" /> <root url="jar://$MAVEN_REPOSITORY$/com/ilummc/eagletdl/EagletCore/1.1/EagletCore-1.1-javadoc.jar!/" />
</JAVADOC> </JAVADOC>
<SOURCES> <SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/com/ilummc/eagletdl/EagletCore/1.0/EagletCore-1.0-sources.jar!/" /> <root url="jar://$MAVEN_REPOSITORY$/com/ilummc/eagletdl/EagletCore/1.1/EagletCore-1.1-sources.jar!/" />
</SOURCES> </SOURCES>
</library> </library>
</component> </component>

View File

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: com.zaxxer:HikariCP:3.0.0">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/zaxxer/HikariCP/3.0.0/HikariCP-3.0.0.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/com/zaxxer/HikariCP/3.0.0/HikariCP-3.0.0-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/com/zaxxer/HikariCP/3.0.0/HikariCP-3.0.0-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: org.slf4j:slf4j-api:1.7.25">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@ -61,7 +61,12 @@
<dependency> <dependency>
<groupId>com.ilummc.eagletdl</groupId> <groupId>com.ilummc.eagletdl</groupId>
<artifactId>EagletCore</artifactId> <artifactId>EagletCore</artifactId>
<version>1.0</version> <version>1.1</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.0.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.ow2.asm</groupId> <groupId>org.ow2.asm</groupId>

View File

@ -1,28 +1,51 @@
package com.ilummc.tlib; package com.ilummc.tlib;
import com.ilummc.tlib.annotations.Dependency;
import com.ilummc.tlib.annotations.Logger;
import com.ilummc.tlib.inject.DependencyInjector;
import com.ilummc.tlib.inject.TLibPluginManager; import com.ilummc.tlib.inject.TLibPluginManager;
import com.ilummc.tlib.util.TLogger;
import me.skymc.taboolib.Main; import me.skymc.taboolib.Main;
import me.skymc.taboolib.message.MsgUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import java.io.File; import java.io.File;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@Dependency(type = Dependency.Type.LIBRARY, maven = "org.ow2.asm:asm:6.1.1")
@Dependency(type = Dependency.Type.LIBRARY, maven = "com.zaxxer:HikariCP:3.0.0")
public class TLib { public class TLib {
private static TLib tLib;
@Logger("§3[§6TLib§3|{1}§3] §f{2}")
private TLogger tLogger;
private TLib() {
}
public TLogger getLogger() {
return tLogger;
}
public static TLib getTLib() {
return tLib;
}
@SuppressWarnings({"unchecked"}) @SuppressWarnings({"unchecked"})
public static void init() { public static void init() {
new File(Main.getInst().getDataFolder(), "/libs").mkdirs();
tLib = new TLib();
DependencyInjector.inject(Main.getInst(), tLib);
// 注入 PluginLoader 用于加载依赖 // 注入 PluginLoader 用于加载依赖
try { try {
Field field = Bukkit.getServer().getClass().getDeclaredField("pluginManager"); Field field = Bukkit.getServer().getClass().getDeclaredField("pluginManager");
field.setAccessible(true); field.setAccessible(true);
field.set(Bukkit.getServer(), new TLibPluginManager()); field.set(Bukkit.getServer(), new TLibPluginManager());
MsgUtils.send("注入成功"); tLib.getLogger().info("注入成功");
} catch (NoSuchFieldException | IllegalAccessException e) { } catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
MsgUtils.warn("注入失败"); tLib.getLogger().fatal("注入失败");
} }
new File(Main.getInst().getDataFolder(), "/libs").mkdirs();
} }
} }

View File

@ -11,7 +11,7 @@ public @interface Config {
String name() default "config.yml"; String name() default "config.yml";
boolean fromJar() default true; boolean fromJar() default false;
boolean saveOnExit() default false; boolean saveOnExit() default false;

View File

@ -1,5 +1,7 @@
package com.ilummc.tlib.annotations; package com.ilummc.tlib.annotations;
import com.ilummc.tlib.dependency.TDependency;
import java.lang.annotation.*; import java.lang.annotation.*;
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@ -11,6 +13,12 @@ public @interface Dependency {
Type type(); Type type();
String[] args(); String plugin() default "";
String maven() default "";
String mavenRepo() default TDependency.MAVEN_REPO;
String url() default "";
} }

View File

@ -11,7 +11,7 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface Logger { public @interface Logger {
String value() default "[{0}] {1}"; String value() default "[{0}|{1}] {1}";
int level() default TLogger.INFO; int level() default TLogger.INFO;

View File

@ -1,8 +1,9 @@
package com.ilummc.tlib.dependency; package com.ilummc.tlib.dependency;
import com.ilummc.eagletdl.EagletTask; import com.ilummc.eagletdl.EagletTask;
import com.ilummc.eagletdl.ProgressEvent;
import com.ilummc.tlib.TLib;
import me.skymc.taboolib.Main; import me.skymc.taboolib.Main;
import me.skymc.taboolib.message.MsgUtils;
import java.io.File; import java.io.File;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
@ -29,17 +30,17 @@ public class TDependency {
* <p> * <p>
* 阻塞线程进行下载/加载 * 阻塞线程进行下载/加载
* *
* @param args 依赖名格式为 groupId:artifactId:version * @param type 依赖名格式为 groupId:artifactId:version
* @return 是否成功加载库如果加载成功插件将可以任意调用使用的类 * @return 是否成功加载库如果加载成功插件将可以任意调用使用的类
*/ */
public static boolean requestLib(String... args) { public static boolean requestLib(String type, String repo, String url) {
if (args[0].matches(".*:.*:.*")) { if (type.matches(".*:.*:.*")) {
String[] arr = args[0].split(":"); String[] arr = type.split(":");
File file = new File(Main.getInst().getDataFolder(), "/libs/" + String.join("-", arr) + ".jar"); File file = new File(Main.getInst().getDataFolder(), "/libs/" + String.join("-", arr) + ".jar");
if (file.exists()) { if (file.exists()) {
TDependencyLoader.addToPath(Main.getInst(), file); TDependencyLoader.addToPath(Main.getInst(), file);
return true; return true;
} else if (downloadMaven(MAVEN_REPO, arr[0], arr[1], arr[2], file)) { } else if (downloadMaven(repo, arr[0], arr[1], arr[2], file, url)) {
TDependencyLoader.addToPath(Main.getInst(), file); TDependencyLoader.addToPath(Main.getInst(), file);
return true; return true;
} else return false; } else return false;
@ -47,30 +48,32 @@ public class TDependency {
return false; return false;
} }
private static boolean downloadMaven(String url, String groupId, String artifactId, String version, File target) { private static boolean downloadMaven(String url, String groupId, String artifactId, String version, File target, String dl) {
ReentrantLock lock = new ReentrantLock(); ReentrantLock lock = new ReentrantLock();
AtomicBoolean failed = new AtomicBoolean(false); AtomicBoolean failed = new AtomicBoolean(false);
new EagletTask() EagletTask task = new EagletTask()
.url(url + "/" + groupId.replace('.', '/') + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + ".jar") .url(dl == null ? url + "/" + groupId.replace('.', '/') + "/" +
artifactId + "/" + version + "/" + artifactId + "-" + version + ".jar" : dl)
.file(target) .file(target)
.setThreads(8) .setThreads(8)
.setOnStart(event -> lock.lock()) .setOnStart(event -> lock.lock())
.setOnProgress(event -> MsgUtils.send(" 下载速度 " + event.getSpeedFormatted())) .setOnConnected(event -> TLib.getTLib().getLogger().info(" 正在下载 " + String.join(":",
.setOnConnected(event -> MsgUtils.send(" 正在下载 " + String.join(":", new String[]{groupId, artifactId, version}) + new String[]{groupId, artifactId, version}) +
" 大小 " + event.getContentLength())) " 大小 " + ProgressEvent.format(event.getContentLength())))
.setOnError(event -> failed.set(true)) .setOnProgress(event -> TLib.getTLib().getLogger().info(" 下载速度 " + event.getSpeedFormatted()
+ " 进度 " + event.getPercentageFormatted()))
.setOnComplete(event -> { .setOnComplete(event -> {
if (event.isSuccess()) {
TLib.getTLib().getLogger().info(" 下载 " + String.join(":", new String[]{groupId, artifactId, version}) + " 完成");
} else {
failed.set(true);
}
lock.unlock(); lock.unlock();
MsgUtils.send(" 下载 " + String.join(":", new String[]{groupId, artifactId, version}) + " 完成"); });
}) task.start();
.start(); while (lock.tryLock()) lock.unlock();
try { lock.lock();
while (lock.tryLock()) lock.unlock(); lock.unlock();
} catch (Exception ignored) {
} finally {
lock.lock();
lock.unlock();
}
return !failed.get(); return !failed.get();
} }

View File

@ -1,12 +1,12 @@
package com.ilummc.tlib.inject; package com.ilummc.tlib.inject;
import com.ilummc.tlib.TLib;
import com.ilummc.tlib.annotations.Dependencies; import com.ilummc.tlib.annotations.Dependencies;
import com.ilummc.tlib.annotations.Dependency; import com.ilummc.tlib.annotations.Dependency;
import com.ilummc.tlib.annotations.Logger; import com.ilummc.tlib.annotations.Logger;
import com.ilummc.tlib.annotations.PluginInstance; import com.ilummc.tlib.annotations.PluginInstance;
import com.ilummc.tlib.dependency.TDependency; import com.ilummc.tlib.dependency.TDependency;
import com.ilummc.tlib.util.TLogger; import com.ilummc.tlib.util.TLogger;
import me.skymc.taboolib.message.MsgUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -19,6 +19,7 @@ public class DependencyInjector {
injectLogger(plugin, o); injectLogger(plugin, o);
injectPluginInstance(plugin, o); injectPluginInstance(plugin, o);
injectDependencies(plugin, o); injectDependencies(plugin, o);
injectConfig(plugin, o);
} }
static void injectOnEnable(Plugin plugin) { static void injectOnEnable(Plugin plugin) {
@ -29,6 +30,10 @@ public class DependencyInjector {
} }
private static void injectConfig(Plugin plugin, Object o) {
}
private static void injectLogger(Plugin plugin, Object o) { private static void injectLogger(Plugin plugin, Object o) {
try { try {
for (Field field : o.getClass().getDeclaredFields()) { for (Field field : o.getClass().getDeclaredFields()) {
@ -56,7 +61,7 @@ public class DependencyInjector {
Plugin pl; Plugin pl;
if ((pl = Bukkit.getPluginManager().getPlugin(instance.value())) == null) { if ((pl = Bukkit.getPluginManager().getPlugin(instance.value())) == null) {
if (!TDependency.requestPlugin(instance.value())) { if (!TDependency.requestPlugin(instance.value())) {
MsgUtils.warn(plugin.getName() + " 所需的依赖插件 " + instance.value() + " 自动加载失败"); TLib.getTLib().getLogger().warn(plugin.getName() + " 所需的依赖插件 " + instance.value() + " 自动加载失败");
return; return;
} else { } else {
pl = Bukkit.getPluginManager().getPlugin(instance.value()); pl = Bukkit.getPluginManager().getPlugin(instance.value());
@ -79,20 +84,22 @@ public class DependencyInjector {
if (d2 != null) dependencies = new Dependency[]{d2}; if (d2 != null) dependencies = new Dependency[]{d2};
} }
if (dependencies.length != 0) { if (dependencies.length != 0) {
MsgUtils.send("正在加载 " + plugin.getName() + " 插件所需的依赖"); TLib.getTLib().getLogger().info("正在加载 " + plugin.getName() + " 插件所需的依赖");
for (Dependency dependency : dependencies) { for (Dependency dependency : dependencies) {
if (dependency.type() == Dependency.Type.PLUGIN) if (dependency.type() == Dependency.Type.PLUGIN)
if (TDependency.requestPlugin(dependency.args())) if (TDependency.requestPlugin(dependency.plugin()))
MsgUtils.send(plugin.getName() + " 请求的插件 " + dependency.args()[0] + " 加载成功。"); TLib.getTLib().getLogger().info(" " + plugin.getName() + " 请求的插件 " + dependency.plugin() + " 加载成功。");
else else
MsgUtils.warn(plugin.getName() + " 请求的插件 " + dependency.args()[0] + " 加载失败。"); TLib.getTLib().getLogger().warn(" " + plugin.getName() + " 请求的插件 " + dependency.plugin() + " 加载失败。");
if (dependency.type() == Dependency.Type.LIBRARY) if (dependency.type() == Dependency.Type.LIBRARY)
if (TDependency.requestLib(dependency.args())) if (TDependency.requestLib(dependency.maven(), dependency.mavenRepo(), dependency.url()))
MsgUtils.send(plugin.getName() + " 请求的库文件 " + String.join(":", dependency.args()) + " 加载成功。"); TLib.getTLib().getLogger().info(" " + plugin.getName() + " 请求的库文件 " + String.join(":",
dependency.maven()) + " 加载成功。");
else else
MsgUtils.send(plugin.getName() + " 请求的库文件 " + String.join(":", dependency.args()) + " 加载失败。"); TLib.getTLib().getLogger().warn(" " + plugin.getName() + " 请求的库文件 " + String.join(":",
dependency.maven()) + " 加载失败。");
} }
MsgUtils.send("依赖加载完成"); TLib.getTLib().getLogger().info("依赖加载完成");
} }
} }

View File

@ -0,0 +1,6 @@
package com.ilummc.tlib.inject;
public class TConfigInjector {
}

View File

@ -27,37 +27,37 @@ public class TLogger {
public void verbose(String msg) { public void verbose(String msg) {
if (level >= VERBOSE) if (level >= VERBOSE)
Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), msg)); Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), "§f全部", msg));
} }
public void finest(String msg) { public void finest(String msg) {
if (level >= FINEST) if (level >= FINEST)
Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), msg)); Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), "§e良好", msg));
} }
public void fine(String msg) { public void fine(String msg) {
if (level >= FINE) if (level >= FINE)
Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), msg)); Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), "§a正常", msg));
} }
public void info(String msg) { public void info(String msg) {
if (level >= INFO) if (level >= INFO)
Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), msg)); Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), "§b信息", msg));
} }
public void warn(String msg) { public void warn(String msg) {
if (level >= WARN) if (level >= WARN)
Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), msg)); Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), "§6警告", msg));
} }
public void error(String msg) { public void error(String msg) {
if (level >= ERROR) if (level >= ERROR)
Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), msg)); Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), "§c错误", msg));
} }
public void fatal(String msg) { public void fatal(String msg) {
if (level >= FATAL) if (level >= FATAL)
Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), msg)); Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), "§4致命错误", msg));
} }
} }