Add: Load plugins by command line

This commit is contained in:
huangyuhui 2017-02-12 23:05:43 +08:00
parent 80599013f2
commit eb311abe83
4 changed files with 23 additions and 14 deletions

View File

@ -107,8 +107,22 @@ public final class Main implements Runnable {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
{ {
PluginManager.getPlugin(DefaultPlugin.class); PluginManager.getPlugin(DefaultPlugin.class);
if (IUpgrader.NOW_UPGRADER.parseArguments(getVersionNumber(), args)) for (String s : args)
if (s.startsWith("--plugin=")) {
String c = s.substring("--plugin=".length());
try {
PluginManager.getPlugin(Class.forName(c));
} catch (ClassNotFoundException ex) {
System.out.println("Class: " + c + " not found, please add your plugin jar to class path.");
}
} else if (s.startsWith("--help")) {
System.out.println("HMCL command line help");
System.out.println("--noupdate: this arg will prevent HMCL from initializing the newest app version in %appdata%/.hmcl");
System.out.println("--plugin=<your plugin class>: this arg will allow a new plugin to be loaded, please keep your jar in system class path and this class extends IPlugin.");
return; return;
}
IUpgrader.NOW_UPGRADER.parseArguments(getVersionNumber(), args);
System.setProperty("awt.useSystemAAFontSettings", "on"); System.setProperty("awt.useSystemAAFontSettings", "on");
System.setProperty("swing.aatext", "true"); System.setProperty("swing.aatext", "true");

View File

@ -61,7 +61,7 @@ public class AppDataUpgrader extends IUpgrader {
String mainClass = jarFile.getManifest().getMainAttributes().getValue("Main-Class"); String mainClass = jarFile.getManifest().getMainAttributes().getValue("Main-Class");
if (mainClass != null) { if (mainClass != null) {
ArrayList<String> al = new ArrayList<>(Arrays.asList(args)); ArrayList<String> al = new ArrayList<>(Arrays.asList(args));
al.add("nofound"); al.add("--noupdate");
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> { AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
new URLClassLoader(new URL[] { jar.toURI().toURL() }, new URLClassLoader(new URL[] { jar.toURI().toURL() },
URLClassLoader.getSystemClassLoader().getParent()).loadClass(mainClass) URLClassLoader.getSystemClassLoader().getParent()).loadClass(mainClass)
@ -75,8 +75,8 @@ public class AppDataUpgrader extends IUpgrader {
} }
@Override @Override
public boolean parseArguments(VersionNumber nowVersion, String[] args) { public void parseArguments(VersionNumber nowVersion, String[] args) {
if (!ArrayUtils.contains(args, "nofound")) if (!ArrayUtils.contains(args, "--noupdate"))
try { try {
File f = AppDataUpgraderTask.HMCL_VER_FILE; File f = AppDataUpgraderTask.HMCL_VER_FILE;
if (f.exists()) { if (f.exists()) {
@ -86,15 +86,14 @@ public class AppDataUpgrader extends IUpgrader {
String j = m.get("loc"); String j = m.get("loc");
if (j != null) { if (j != null) {
File jar = new File(j); File jar = new File(j);
if (jar.exists()) if (jar.exists() && launchNewerVersion(args, jar))
return launchNewerVersion(args, jar); System.exit(0);
} }
} }
} }
} catch (Throwable t) { } catch (Throwable t) {
HMCLog.err("Failed to execute newer version application", t); HMCLog.err("Failed to execute newer version application", t);
} }
return false;
} }
@Override @Override

View File

@ -34,11 +34,8 @@ public abstract class IUpgrader implements Consumer<SimpleEvent<VersionNumber>>
* *
* @param nowVersion now launcher version * @param nowVersion now launcher version
* @param args Application CommandLine Arguments * @param args Application CommandLine Arguments
*
* @return true if it is needed to break the main thread to shutdown this
* application.
*/ */
public abstract boolean parseArguments(VersionNumber nowVersion, String[] args); public abstract void parseArguments(VersionNumber nowVersion, String[] args);
/** /**
* Just download the new app. * Just download the new app.

View File

@ -35,14 +35,13 @@ import org.jackhuang.hellominecraft.util.sys.IOUtils;
public class NewFileUpgrader extends IUpgrader { public class NewFileUpgrader extends IUpgrader {
@Override @Override
public boolean parseArguments(VersionNumber nowVersion, String[] args) { public void parseArguments(VersionNumber nowVersion, String[] args) {
int i = ArrayUtils.indexOf(args, "--removeOldLauncher"); int i = ArrayUtils.indexOf(args, "--removeOldLauncher");
if (i != -1 && i < args.length - 1) { if (i != -1 && i < args.length - 1) {
File f = new File(args[i + 1]); File f = new File(args[i + 1]);
if (f.exists()) if (f.exists())
f.deleteOnExit(); f.deleteOnExit();
} }
return false;
} }
@Override @Override