完善了一下下载库以及 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">
<library name="Maven: com.ilummc.eagletdl:EagletCore:1.0">
<library name="Maven: com.ilummc.eagletdl:EagletCore:1.1">
<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>
<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>
<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>
</library>
</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>
<groupId>com.ilummc.eagletdl</groupId>
<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>
<groupId>org.ow2.asm</groupId>

View File

@ -1,28 +1,51 @@
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.util.TLogger;
import me.skymc.taboolib.Main;
import me.skymc.taboolib.message.MsgUtils;
import org.bukkit.Bukkit;
import java.io.File;
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 {
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"})
public static void init() {
new File(Main.getInst().getDataFolder(), "/libs").mkdirs();
tLib = new TLib();
DependencyInjector.inject(Main.getInst(), tLib);
// 注入 PluginLoader 用于加载依赖
try {
Field field = Bukkit.getServer().getClass().getDeclaredField("pluginManager");
field.setAccessible(true);
field.set(Bukkit.getServer(), new TLibPluginManager());
MsgUtils.send("注入成功");
tLib.getLogger().info("注入成功");
} catch (NoSuchFieldException | IllegalAccessException e) {
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";
boolean fromJar() default true;
boolean fromJar() default false;
boolean saveOnExit() default false;

View File

@ -1,5 +1,7 @@
package com.ilummc.tlib.annotations;
import com.ilummc.tlib.dependency.TDependency;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@ -11,6 +13,12 @@ public @interface Dependency {
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)
public @interface Logger {
String value() default "[{0}] {1}";
String value() default "[{0}|{1}] {1}";
int level() default TLogger.INFO;

View File

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

View File

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