Skip to content

Commit

Permalink
Added support for the %INSTANCEDIR% variable in configuration paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
sk89q committed Jun 6, 2013
1 parent 8b17396 commit 0ec3f75
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 54 deletions.
17 changes: 7 additions & 10 deletions src/main/java/com/sk89q/mclauncher/ConfigurationDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ private void setup(OptionsDialog owner, ConfigurationsManager configsManager) {
boolean usingDefault = configuration.isUsingDefaultPath();
customPathCheck.setSelected(!usingDefault);
if (!usingDefault) {
File f = configuration.getBaseDir();
pathText.setText(f != null ? f.getPath() : "");
pathText.setText(configuration.getDirForOptions());
}
}
URL updateUrl = configuration.getUpdateUrl();
Expand Down Expand Up @@ -313,12 +312,9 @@ private boolean complete() {
boolean builtIn = configuration != null && configuration.isBuiltIn();

String name = nameText.getText().trim();
String pathStr = customPathCheck.isSelected() ? pathText.getText()
.trim() : null;
String updateURLStr = customUpdateCheck.isSelected() ? urlText
.getText() : null;
String pathStr = customPathCheck.isSelected() ? pathText.getText().trim() : null;
String updateURLStr = customUpdateCheck.isSelected() ? urlText.getText() : null;
URL updateUrl = null;
File f = null;

if (!builtIn) {
if (name.length() == 0) {
Expand Down Expand Up @@ -346,7 +342,8 @@ private boolean complete() {
}

if (pathStr != null) {
f = new File(pathStr);
File f = Launcher.replacePathTokens(pathStr);
f.mkdirs();
if (!f.isDirectory()) {
UIUtil.showError(this, "Invalid path", "The path that you entered does not exist or is not a directory.");
return false;
Expand All @@ -360,14 +357,14 @@ private boolean complete() {

if (configuration == null) { // New configuration
String id = UUID.randomUUID().toString();
Configuration config = new Configuration(id, name, f, updateUrl);
Configuration config = new Configuration(id, name, pathStr, updateUrl, true);
config.setSettings(settings);
configsManager.register(config);
this.configuration = config;
} else {
if (!builtIn) {
configuration.setName(name);
configuration.setCustomBasePath(f);
configuration.setCustomBasePath(pathStr);
configuration.setUpdateUrl(updateUrl);
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/sk89q/mclauncher/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ public static File getLauncherDataDir() {
default:
workingDir = new File(homeDir, "SKMCLauncher");
}

if (!new File(workingDir, "config.xml").exists()) {
workingDir = getOfficialDataDir();
}
Expand All @@ -320,6 +321,26 @@ public static File getLauncherDataDir() {
return workingDir;
}

/**
* Get the built-in directory for instances.
*
* @return the directory for instances
*/
public static File getInstanceDataDir() {
return new File(getLauncherDataDir(), "instances");
}

/**
* Replace path tokens (like %INSTANCES_DIR%).
*
* @param path the path to replace
* @return the result path
*/
public static File replacePathTokens(String path) {
return new File(path.replace("%INSTANCEDIR%",
getInstanceDataDir().getAbsolutePath() + File.separator));
}

/**
* Import old launcher settings.
*
Expand Down
73 changes: 38 additions & 35 deletions src/main/java/com/sk89q/mclauncher/config/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.HashMap;
import java.util.logging.Logger;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.imageio.ImageIO;

import com.sk89q.mclauncher.Launcher;
import com.sk89q.mclauncher.MinecraftJar;
import com.sk89q.mclauncher.addons.AddonsProfile;
import com.sk89q.mclauncher.util.SettingsList;

import org.spout.nbt.CompoundMap;
import org.spout.nbt.CompoundTag;
import org.spout.nbt.ListTag;
import org.spout.nbt.Tag;
import org.spout.nbt.stream.NBTInputStream;

import com.sk89q.mclauncher.Launcher;
import com.sk89q.mclauncher.MinecraftJar;
import com.sk89q.mclauncher.addons.AddonsProfile;
import com.sk89q.mclauncher.util.SettingsList;

/**
* Represents a configuration for the game.
*
Expand All @@ -55,7 +55,7 @@ public class Configuration {
private static final Logger logger = Logger.getLogger(Configuration.class.getCanonicalName());

private String id;
private File customBasePath;
private String customBasePath;
private String appDir;
private String name;
private URL updateUrl;
Expand All @@ -69,34 +69,22 @@ public class Configuration {
*
* @param id id
* @param name name
* @param appDir data directory name
* @param dir data directory name
* @param updateUrl URL to update from, or null to use default
* @param isCustom true if it's a custom path
*/
public Configuration(String id, String name, String appDir, URL updateUrl) {
public Configuration(String id, String name, String dir, URL updateUrl,
boolean isCustom) {
if (!id.matches("^[A-Za-z0-9\\-]+{1,64}$")) {
throw new IllegalArgumentException("Invalid configuration name");
}
this.id = id;
setName(name);
setAppDir(appDir);
setUpdateUrl(updateUrl);
}

/**
* Construct a configuration.
*
* @param id id
* @param name name
* @param customBasePath base path to use
* @param updateUrl URL to update from, or null to use default
*/
public Configuration(String id, String name, File customBasePath, URL updateUrl) {
if (!id.matches("^[A-Za-z0-9\\-]+{1,64}$")) {
throw new IllegalArgumentException("Invalid configuration name");
if (isCustom) {
setCustomBasePath(dir);
} else {
setAppDir(dir);
}
this.id = id;
setName(name);
setCustomBasePath(customBasePath);
setUpdateUrl(updateUrl);
}

Expand Down Expand Up @@ -146,7 +134,7 @@ public boolean isUsingDefaultPath() {
*
* @return path or null
*/
public File getCustomBasePath() {
public String getCustomBasePath() {
return customBasePath;
}

Expand All @@ -157,14 +145,14 @@ public File getCustomBasePath() {
*
* @param customBasePath path or null
*/
public void setCustomBasePath(File customBasePath) {
public void setCustomBasePath(String customBasePath) {
this.customBasePath = customBasePath;
}

/**
* Get the data directory name.
*
* @see #setCustomBasePath(File) can override
* @see #setCustomBasePath(String) can override
* @return name or null for default
*/
public String getAppDir() {
Expand All @@ -174,7 +162,7 @@ public String getAppDir() {
/**
* Set the data directory name.
*
* @see #setCustomBasePath(File) can override
* @see #setCustomBasePath(String) can override
* @param appDir name
*/
public void setAppDir(String appDir) {
Expand Down Expand Up @@ -264,7 +252,7 @@ public void setSettings(SettingsList settings) {
public File getBaseDir() {
File path;
if (getCustomBasePath() != null) {
return getCustomBasePath();
return Launcher.replacePathTokens(getCustomBasePath());
} else if (getAppDir() == null) {
path = Launcher.getAppDataDir();
} else {
Expand All @@ -275,6 +263,21 @@ public File getBaseDir() {
return path;
}

/**
* Shortcut method to get the directory show in an options dialog.
*
* @return directory
*/
public String getDirForOptions() {
if (getCustomBasePath() != null) {
return getCustomBasePath();
} else if (getAppDir() == null) {
return Launcher.getAppDataDir().getAbsolutePath();
} else {
return Launcher.getAppDataDir(getAppDir()).getAbsolutePath();
}
}

/**
* Shortcut method to get the directory that Minecraft will actually use.
*
Expand All @@ -283,7 +286,7 @@ public File getBaseDir() {
public File getMinecraftDir() {
File path;
if (getCustomBasePath() != null) {
return Launcher.toMinecraftDir(getCustomBasePath());
return Launcher.toMinecraftDir(Launcher.replacePathTokens(getCustomBasePath()));
} else if (getAppDir() == null) {
path = Launcher.getOfficialDataDir();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public Configuration registerBuiltIn(String id, String name, String appDir, Stri
}
Configuration config = get(id);
if (config == null) {
config = new Configuration(id, name, appDir, url);
config = new Configuration(id, name, appDir, url, false);
register(config);
}
config.setName(name);
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/sk89q/mclauncher/config/LauncherOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package com.sk89q.mclauncher.config;

import static com.sk89q.mclauncher.util.XMLUtil.*;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -51,16 +53,14 @@
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import util.Base64;

import com.sk89q.mclauncher.Launcher;
import com.sk89q.mclauncher.util.SettingsList;
import com.sk89q.mclauncher.util.SimpleNode;
import com.sk89q.mclauncher.util.Util;
import com.sk89q.mclauncher.util.XMLUtil;

import static com.sk89q.mclauncher.util.XMLUtil.*;

import util.Base64;

/**
* Stores options for the launcher.
*
Expand Down Expand Up @@ -333,9 +333,9 @@ public void read() throws IOException {
URL updateUrl = urlString != null ? new URL(urlString) : null;
Configuration config;
if (basePath != null) {
config = new Configuration(id, name, new File(basePath), updateUrl);
config = new Configuration(id, name, basePath, updateUrl, true);
} else {
config = new Configuration(id, name, appDir, updateUrl);
config = new Configuration(id, name, appDir, updateUrl, false);
}

Node settingsNode = XMLUtil.getNode(node, settingsExpr);
Expand Down Expand Up @@ -422,11 +422,11 @@ public void write() throws IOException {
SimpleNode configurationsNode = root.addNode("configurations");
for (Configuration config : configsManager) {
SimpleNode configurationNode = configurationsNode.addNode("configuration");
File f = config.getCustomBasePath();
String path = config.getCustomBasePath();
configurationNode.addNode("id").addValue(config.getId());
configurationNode.addNode("name").addValue(config.getName());
configurationNode.addNode("appDir").addValue(config.getAppDir());
configurationNode.addNode("basePath").addValue(f != null ? f.getPath() : null);
configurationNode.addNode("basePath").addValue(path);
configurationNode.addNode("updateURL").addValue(config.getUpdateUrl() != null ?
config.getUpdateUrl().toString() : null);
configurationNode.addNode("lastJar").addValue(config.getLastActiveJar());
Expand Down

0 comments on commit 0ec3f75

Please sign in to comment.