Load plugins from jar file

This commit is contained in:
huangyuhui
2017-02-17 20:00:45 +08:00
parent 413854d1f1
commit 5f65496500
19 changed files with 139 additions and 110 deletions

View File

@@ -46,7 +46,7 @@ def buildnumber = System.getenv("TRAVIS_BUILD_NUMBER")
if (buildnumber == null)
buildnumber = System.getenv("BUILD_NUMBER")
if (buildnumber == null)
buildnumber = "0"
buildnumber = "1"
def versionroot = System.getenv("VERSION_ROOT")
if (versionroot == null)

View File

@@ -120,6 +120,7 @@ public final class Main implements Runnable {
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;
}
PluginManager.loadPlugins();
IUpgrader.NOW_UPGRADER.parseArguments(HMCLApi.HMCL_VERSION, args);

View File

@@ -224,7 +224,7 @@ public final class Config implements Cloneable {
theme = LAFTheme.BLUE.id;
decorated = OS.os() == OS.LINUX;
auth = new HashMap<>();
commonpath = MCUtils.getLocation().getAbsolutePath();
commonpath = MCUtils.getLocation().getPath();
}
public DownloadType getDownloadSource() {

View File

@@ -17,28 +17,25 @@
*/
package org.jackhuang.hmcl.setting;
import org.jackhuang.hmcl.core.service.IProfile;
import com.google.gson.annotations.SerializedName;
import org.jackhuang.hmcl.util.HMCLGameLauncher;
import org.jackhuang.hmcl.util.HMCLMinecraftService;
import java.io.File;
import org.jackhuang.hmcl.core.MCUtils;
import org.jackhuang.hmcl.core.version.MinecraftVersion;
import org.jackhuang.hmcl.util.sys.IOUtils;
import org.jackhuang.hmcl.util.StrUtils;
import org.jackhuang.hmcl.api.event.EventHandler;
import org.jackhuang.hmcl.api.event.PropertyChangedEvent;
import org.jackhuang.hmcl.util.sys.OS;
/**
*
* @author huangyuhui
*/
public final class Profile {
public final class Profile implements IProfile {
@SerializedName("name")
private String name;
@SerializedName("selectedMinecraftVersion")
private String selectedMinecraftVersion = "";
private String selectedVersion = "";
@SerializedName("gameDir")
private String gameDir;
@SerializedName("noCommon")
@@ -46,7 +43,8 @@ public final class Profile {
@SerializedName("global")
private VersionSetting global;
private transient HMCLMinecraftService service;
private transient String name;
private transient HMCLMinecraftService service = new HMCLMinecraftService(this);
private transient HMCLGameLauncher launcher = new HMCLGameLauncher(this);
public transient final EventHandler<PropertyChangedEvent<String>> propertyChanged = new EventHandler<>();
@@ -55,7 +53,7 @@ public final class Profile {
}
public Profile(String name) {
this(name, new File(".minecraft").getPath());
this(name, ".minecraft");
}
public Profile(String name, String gameDir) {
@@ -72,9 +70,8 @@ public final class Profile {
gameDir = v.gameDir;
}
@Override
public HMCLMinecraftService service() {
if (service == null)
service = new HMCLMinecraftService(this);
return service;
}
@@ -111,7 +108,7 @@ public final class Profile {
vs.setUsesGlobal(false);
} else
vs.setUsesGlobal(false);
propertyChanged.fire(new PropertyChangedEvent<>(this, "selectedMinecraftVersion", selectedMinecraftVersion, selectedMinecraftVersion));
propertyChanged.fire(new PropertyChangedEvent<>(this, "selectedVersion", selectedVersion, selectedVersion));
}
public void makeVersionSettingGlobal(String id) {
@@ -120,55 +117,42 @@ public final class Profile {
if (vs == null)
return;
vs.setUsesGlobal(true);
propertyChanged.fire(new PropertyChangedEvent<>(this, "selectedMinecraftVersion", selectedMinecraftVersion, selectedMinecraftVersion));
}
public String getSettingsSelectedMinecraftVersion() {
return selectedMinecraftVersion;
propertyChanged.fire(new PropertyChangedEvent<>(this, "selectedVersion", selectedVersion, selectedVersion));
}
@Override
public String getSelectedVersion() {
String v = selectedMinecraftVersion;
String v = selectedVersion;
if (StrUtils.isBlank(v) || service().version().getVersionById(v) == null || service().version().getVersionById(v).hidden) {
MinecraftVersion mv = service().version().getOneVersion(t -> !t.hidden);
if (mv != null)
v = mv.id;
if (StrUtils.isNotBlank(v))
setSelectedMinecraftVersion(v);
setSelectedVersion(v);
}
return StrUtils.isBlank(v) ? null : v;
}
public void setSelectedMinecraftVersion(String selectedMinecraftVersion) {
PropertyChangedEvent event = new PropertyChangedEvent<>(this, "selectedMinecraftVersion", this.selectedMinecraftVersion, selectedMinecraftVersion);
this.selectedMinecraftVersion = selectedMinecraftVersion;
@Override
public void setSelectedVersion(String newVersion) {
PropertyChangedEvent event = new PropertyChangedEvent<>(this, "selectedVersion", this.selectedVersion, newVersion);
this.selectedVersion = newVersion;
propertyChanged.fire(event);
}
public String getGameDir() {
@Override
public File getGameDir() {
if (StrUtils.isBlank(gameDir))
gameDir = MCUtils.getInitGameDir().getPath();
return IOUtils.addSeparator(gameDir);
return new File(gameDir.replace('\\', '/'));
}
public String getCanonicalGameDir() {
return IOUtils.tryGetCanonicalFolderPath(getGameDirFile());
}
public File getCanonicalGameDirFile() {
return IOUtils.tryGetCanonicalFile(getGameDirFile());
}
public File getGameDirFile() {
return new File(getGameDir());
}
public Profile setGameDir(String gameDir) {
@Override
public void setGameDir(File gameDir) {
PropertyChangedEvent event = new PropertyChangedEvent<>(this, "gameDir", this.gameDir, gameDir);
this.gameDir = gameDir;
this.gameDir = gameDir.getPath();
service().version().refreshVersions();
propertyChanged.fire(event);
return this;
}
public boolean isNoCommon() {
@@ -181,6 +165,7 @@ public final class Profile {
propertyChanged.fire(event);
}
@Override
public String getName() {
return name;
}
@@ -191,10 +176,7 @@ public final class Profile {
propertyChanged.fire(event);
}
public void checkFormat() {
gameDir = gameDir.replace('/', OS.os().fileSeparator).replace('\\', OS.os().fileSeparator);
}
@Override
public void onSelected() {
service().version().refreshVersions();
}

View File

@@ -62,7 +62,6 @@ public final class Settings {
for (Map.Entry<String, Profile> entry : getProfiles().entrySet()) {
Profile e = entry.getValue();
e.setName(entry.getKey());
e.checkFormat();
e.propertyChanged.register(Settings::save);
}
}

View File

@@ -997,7 +997,9 @@ public final class GameSettingsPanel extends RepaintPage implements DropTargetLi
MessageBox.show(C.i18n("setupwindow.no_empty_name"));
return;
}
Settings.putProfile(new Profile(name).setGameDir(newGameDir.getAbsolutePath()));
Profile newProfile = new Profile(name);
newProfile.setGameDir(newGameDir);
Settings.putProfile(newProfile);
MessageBox.show(C.i18n("setupwindow.find_in_configurations"));
loadProfiles();
}
@@ -1042,7 +1044,7 @@ public final class GameSettingsPanel extends RepaintPage implements DropTargetLi
private void cboVersionsItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboVersionsItemStateChanged
if (isLoading || evt.getStateChange() != ItemEvent.SELECTED || cboVersions.getSelectedIndex() < 0 || StrUtils.isBlank((String) cboVersions.getSelectedItem()))
return;
Settings.getLastProfile().setSelectedMinecraftVersion((String) cboVersions.getSelectedItem());
Settings.getLastProfile().setSelectedVersion((String) cboVersions.getSelectedItem());
}//GEN-LAST:event_cboVersionsItemStateChanged
// <editor-fold defaultstate="collapsed" desc="UI Events">
@@ -1131,7 +1133,7 @@ public final class GameSettingsPanel extends RepaintPage implements DropTargetLi
try {
String path = fc.getSelectedFile().getCanonicalPath();
txtGameDir.setText(path);
Settings.getLastProfile().setGameDir(path);
Settings.getLastProfile().setGameDir(fc.getSelectedFile());
} catch (IOException e) {
HMCLog.warn("Failed to set game dir.", e);
MessageBox.show(C.i18n("ui.label.failed_set") + e.getMessage());
@@ -1216,7 +1218,7 @@ public final class GameSettingsPanel extends RepaintPage implements DropTargetLi
}//GEN-LAST:event_txtWidthFocusLost
private void txtGameDirFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtGameDirFocusLost
Settings.getLastProfile().setGameDir(txtGameDir.getText());
Settings.getLastProfile().setGameDir(new File(txtGameDir.getText()));
loadVersions();
}//GEN-LAST:event_txtGameDirFocusLost
@@ -1512,11 +1514,11 @@ public final class GameSettingsPanel extends RepaintPage implements DropTargetLi
final Consumer<ProfileChangedEvent> onSelectedProfilesChanged = event -> {
Profile t = event.getValue();
t.propertyChanged.register(e -> {
if ("selectedMinecraftVersion".equals(e.getPropertyName()))
if ("selectedVersion".equals(e.getPropertyName()))
versionChanged(e.getNewValue());
});
txtGameDir.setText(t.getGameDir());
txtGameDir.setText(t.getGameDir().getPath());
isLoading = true;
DefaultComboBoxModel<String> model = (DefaultComboBoxModel<String>) cboProfiles.getModel();

View File

@@ -352,7 +352,7 @@ public class MainPagePanel extends Page {
if (isLoading || evt.getStateChange() != ItemEvent.SELECTED || cboVersions.getSelectedIndex() < 0 || StrUtils.isBlank((String) cboVersions.getSelectedItem()))
return;
String mcv = (String) cboVersions.getSelectedItem();
Settings.getLastProfile().setSelectedMinecraftVersion(mcv);
Settings.getLastProfile().setSelectedVersion(mcv);
}//GEN-LAST:event_cboVersionsItemStateChanged
private void txtPasswordFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPasswordFocusGained
@@ -533,7 +533,7 @@ public class MainPagePanel extends Page {
final Consumer<ProfileChangedEvent> onSelectedProfilesChanged = event -> {
Profile t = event.getValue();
t.propertyChanged.register(e -> {
if ("selectedMinecraftVersion".equals(e.getPropertyName()))
if ("selectedVersion".equals(e.getPropertyName()))
versionChanged(e.getNewValue());
});

View File

@@ -64,8 +64,7 @@ public class HMCLGameLauncher {
return;
setLaunching(true);
HMCLog.log("Start generating launching command...");
File file = profile.getCanonicalGameDirFile();
if (!file.exists()) {
if (!profile.getGameDir().exists()) {
failed.accept(C.i18n("minecraft.wrong_path"));
setLaunching(false);
return;
@@ -84,7 +83,7 @@ public class HMCLGameLauncher {
public void run() {
Thread.currentThread().setName("Game Launcher");
try {
LaunchOptions options = profile.getSelectedVersionSetting().createLaunchOptions(profile.getCanonicalGameDirFile());
LaunchOptions options = profile.getSelectedVersionSetting().createLaunchOptions(profile.getGameDir());
HMCLApi.EVENT_BUS.fireChannel(new ProcessingLaunchOptionsEvent(this, options));
DefaultGameLauncher gl = new DefaultGameLauncher(options, profile.service(), li, l);
GameLauncherTag tag = new GameLauncherTag();

View File

@@ -22,7 +22,6 @@ import org.jackhuang.hmcl.core.service.IMinecraftAssetService;
import org.jackhuang.hmcl.core.service.IMinecraftLoader;
import org.jackhuang.hmcl.core.service.IMinecraftDownloadService;
import org.jackhuang.hmcl.core.service.IMinecraftProvider;
import org.jackhuang.hmcl.util.C;
import org.jackhuang.hmcl.core.service.IMinecraftModService;
import org.jackhuang.hmcl.core.service.IMinecraftInstallerService;
import com.google.gson.JsonSyntaxException;
@@ -141,7 +140,7 @@ public class HMCLMinecraftService extends IMinecraftService {
@Override
public File baseDirectory() {
return p.getCanonicalGameDirFile();
return p.getGameDir();
}
protected IMinecraftProvider provider;

View File

@@ -50,7 +50,7 @@ public class NewFileUpgrader extends IUpgrader {
File newf = new File(FileUtils.getName(str));
if (TaskWindow.factory().append(new FileDownloadTask(str, newf)).execute()) {
try {
new ProcessBuilder(new String[] { IOUtils.tryGetCanonicalFilePath(newf), "--removeOldLauncher", IOUtils.getRealPath() }).directory(new File(".")).start();
new ProcessBuilder(new String[] { newf.getCanonicalPath(), "--removeOldLauncher", IOUtils.getRealPath() }).directory(new File(".")).start();
} catch (IOException ex) {
HMCLog.err("Failed to start new app", ex);
}