Skip to content

Commit

Permalink
TestMCDownloadLaunch now works like a charm :O
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsik68 committed Dec 30, 2013
1 parent 58fb64b commit aeabd7e
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 37 deletions.
6 changes: 6 additions & 0 deletions src/sk/tomsik68/mclauncher/api/login/ISession.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package sk.tomsik68.mclauncher.api.login;

import java.util.List;

import sk.tomsik68.mclauncher.impl.login.yggdrasil.YDUserObject.Prop;

public interface ISession {
public String getUsername();

Expand All @@ -8,4 +12,6 @@ public interface ISession {
public String getUUID();

public ESessionType getType();

public List<Prop> getProperties();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package sk.tomsik68.mclauncher.impl.login.legacy;

import java.util.List;

import sk.tomsik68.mclauncher.api.login.ESessionType;
import sk.tomsik68.mclauncher.api.login.ISession;
import sk.tomsik68.mclauncher.impl.login.yggdrasil.YDUserObject.Prop;

public class LegacySession implements ISession {
private final String userName, sessionID, uuid, downloadTicket, lastVersion;
Expand Down Expand Up @@ -42,4 +45,9 @@ public ESessionType getType() {
return ESessionType.LEGACY;
}

@Override
public List<Prop> getProperties() {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void setProfileName(String profileName) {
}

public YDPartialGameProfile getYDGameProfile() {
YDPartialGameProfile result = new YDPartialGameProfile(userName, uuid);
YDPartialGameProfile result = new YDPartialGameProfile(userName, uuid, false);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class YDLoginResponse extends YDResponse {
private final String sessionID, clientToken;
private final YDPartialGameProfile selectedProfile;
private HashMap<String, YDPartialGameProfile> profiles = new HashMap<String, YDPartialGameProfile>();
private YDUserObject user;

public YDLoginResponse(JSONObject json) {
super(json);
Expand All @@ -23,6 +24,8 @@ public YDLoginResponse(JSONObject json) {
this.profiles.put(p.getName(), p);
}
}
if(json.containsKey("user"))
user = new YDUserObject((JSONObject)json.get("user"));
}

public String getSessionID() {
Expand All @@ -40,4 +43,11 @@ public YDPartialGameProfile getSelectedProfile() {
public YDPartialGameProfile getProfile(String name) {
return profiles.get(name);
}

public YDUserObject getUserObject() {
if(user == null){
user = new YDUserObject(selectedProfile.getName());
}
return user;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package sk.tomsik68.mclauncher.impl.login.yggdrasil;

import net.minidev.json.JSONObject;
import net.minidev.json.JSONStyle;
import sk.tomsik68.mclauncher.api.json.IJSONSerializable;

public class YDPartialGameProfile implements IJSONSerializable {
private final String name, id;
private final boolean legacy;

public YDPartialGameProfile(String name, String id) {
public YDPartialGameProfile(String name, String id, boolean isLegacy) {
this.name = name;
this.id = id;
this.legacy = isLegacy;
}

public YDPartialGameProfile(JSONObject jsonObj) {
this(jsonObj.get("name").toString(),jsonObj.get("id").toString());
this(jsonObj.get("name").toString(), jsonObj.get("id").toString(), false);
System.out.println("Building YDPartialGameProfile: "+jsonObj.toJSONString(JSONStyle.NO_COMPRESS));

}

public String getName() {
Expand All @@ -29,4 +34,8 @@ public JSONObject toJSON() {
obj.put("name", name);
return obj;
}

public boolean isLegacy() {
return legacy;
}
}
14 changes: 13 additions & 1 deletion src/sk/tomsik68/mclauncher/impl/login/yggdrasil/YDSession.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package sk.tomsik68.mclauncher.impl.login.yggdrasil;

import java.util.List;

import sk.tomsik68.mclauncher.api.login.ESessionType;
import sk.tomsik68.mclauncher.api.login.ISession;
import sk.tomsik68.mclauncher.impl.login.yggdrasil.YDUserObject.Prop;

public class YDSession implements ISession {
private final String username, sessid, uuid;
private final YDUserObject user;

public YDSession(YDLoginResponse r) {
username = r.getSelectedProfile().getName();
sessid = r.getSessionID();
uuid = r.getSelectedProfile().getId();

user = r.getUserObject();
}

@Override
Expand All @@ -34,4 +38,12 @@ public ESessionType getType() {
return ESessionType.MOJANG;
}

public YDUserObject getUserObject() {
return user;
}

@Override
public List<Prop> getProperties() {
return user.getProperties();
}
}
33 changes: 21 additions & 12 deletions src/sk/tomsik68/mclauncher/impl/versions/mcdownload/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;

import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import sk.tomsik68.mclauncher.api.common.IOperatingSystem;
import sk.tomsik68.mclauncher.impl.common.Platform;
import sk.tomsik68.mclauncher.impl.versions.mcdownload.Rule.Action;
import sk.tomsik68.mclauncher.util.FilePathBuilder;

import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import sk.tomsik68.mclauncher.util.IExtractRules;

public class Library {
private final String name;
private final HashMap<String, String> natives = new HashMap<String, String>();
private final ArrayList<Rule> rules = new ArrayList<Rule>();
private LibraryExtractRules extractRules;

public Library(JSONObject json) {
name = json.get("name").toString();
Expand All @@ -33,6 +30,9 @@ public Library(JSONObject json) {
rules.add(new Rule((JSONObject) rulz.get(i)));
}
}
if (json.containsKey("extract")) {
extractRules = new LibraryExtractRules((JSONObject) json.get("extract"));
}
}

public String getName() {
Expand All @@ -41,14 +41,14 @@ public String getName() {

public String getNatives(IOperatingSystem os) {
if (!natives.containsKey(os.getMinecraftName()))
return natives.get(Platform.wrapName(os.getMinecraftName()));
return natives.get(Platform.wrapName(os.getMinecraftName())).replace("${arch}", System.getProperty("sun.arch.data.model"));
return natives.get(os.getMinecraftName()).replace("${arch}", System.getProperty("sun.arch.data.model"));
}

public String getPath() {
String[] split = name.split(":");
StringBuilder result = new StringBuilder();

result = result.append(split[0].replace('.', '/'));// net/sf/jopt-simple
result = result.append('/').append(split[1]).append('/').append(split[2]).append('/'); // /jopt-simple/4.4/
result = result.append(split[1]).append('-').append(split[2]); // jopt-simple-4.4
Expand All @@ -64,11 +64,20 @@ public String getPath() {
public boolean isCompatible() {
Action action = Action.ALLOW;
for (Rule rule : rules) {
if (rule.applies()){
if(action == Action.ALLOW)
if (rule.applies()) {
if (action == Action.ALLOW)
action = rule.getAction();
}
}
return action == Action.ALLOW;
return action == Action.ALLOW && (natives== null || natives.containsKey(Platform.getCurrentPlatform().getMinecraftName()));
}

public boolean hasNatives() {
return !natives.isEmpty();
}

public IExtractRules getExtractRules() {
return extractRules;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package sk.tomsik68.mclauncher.impl.versions.mcdownload;

import java.util.ArrayList;
import java.util.zip.ZipEntry;

import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;

import sk.tomsik68.mclauncher.util.IExtractRules;

public class LibraryExtractRules implements IExtractRules {
private ArrayList<String> exclude = new ArrayList<String>();

public LibraryExtractRules(JSONObject object) {
JSONArray excludeArray = (JSONArray) object.get("exclude");
for (Object obj : excludeArray) {
exclude.add(obj.toString());
}
}

@Override
public boolean accepts(ZipEntry entry) {
String path = entry.getName();
if (exclude != null && !exclude.isEmpty()) {
for (String p : exclude) {
if (path.startsWith(p))
return false;

}
}
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
import sk.tomsik68.mclauncher.api.versions.IVersionInstallListener;
import sk.tomsik68.mclauncher.api.versions.IVersionInstaller;
import sk.tomsik68.mclauncher.impl.versions.mcdownload.assets.MCDResourcesInstaller;
import sk.tomsik68.mclauncher.util.ExtractUtils;
import sk.tomsik68.mclauncher.util.FileUtils;

public class MCDownloadVersionInstaller implements IVersionInstaller {
private final ArrayList<IVersionInstallListener> listeners = new ArrayList<IVersionInstallListener>();


@Override
public void addVersionInstallListener(IVersionInstallListener listener) {
Expand All @@ -34,20 +34,42 @@ public void install(IVersion v, IMinecraftInstance mc, IProgressMonitor progress
throw new VersionIncompatibleException(v);
log.info("Version compatible");
List<Library> toInstall = version.getLibraries();
List<Library> toExtract = new ArrayList<Library>();
log.info("Fetching libraries...");

for (Library lib : toInstall) {
if (!mc.getLibraryProvider().isInstalled(lib) && lib.isCompatible()) {
log.info("Installing " + lib.getName());
try {
installLibrary(lib, mc, progress);
} catch (Exception e) {
e.printStackTrace();
log.info("Failed to install " + lib.getName());
if (lib.isCompatible()) {
if (!mc.getLibraryProvider().isInstalled(lib)) {
log.info("Installing " + lib.getName());
try {
downloadLibrary(lib, mc, progress);
} catch (Exception e) {
e.printStackTrace();
log.info("Failed to install " + lib.getName());
}
}
if (lib.hasNatives()) {
toExtract.add(lib);
}
} else {
log.info(lib.getName() + " is already installed or incompatible.");
log.info(lib.getName() + " is not compatible.");
}
}

log.info("Extracting natives...");
File nativesDir = new File(mc.getJarProvider().getVersionFile(version.getUniqueID()).getParentFile(), "natives");
// purge old natives
if (nativesDir.exists()) {
File[] contains = nativesDir.listFiles();
for (File f : contains) {
f.delete();
}
}
for (Library lib : toExtract) {
File libFile = mc.getLibraryProvider().getLibraryFile(lib);
ExtractUtils.extractZipWithRules(libFile, nativesDir, lib.getExtractRules());
}

log.info("Updating resources...");
updateResources(mc, version, progress);
File jarDest = mc.getJarProvider().getVersionFile(version.getUniqueID());
Expand All @@ -62,7 +84,7 @@ public void install(IVersion v, IMinecraftInstance mc, IProgressMonitor progress
}
}
notifyListeners(version);
if(progress != null)
if (progress != null)
progress.finish();
}

Expand All @@ -75,9 +97,7 @@ private void updateResources(IMinecraftInstance mc, MCDownloadVersion version, I
resInstaller.install(version, progress);
}



private void installLibrary(Library lib, IMinecraftInstance mc, IProgressMonitor p) throws Exception {
private void downloadLibrary(Library lib, IMinecraftInstance mc, IProgressMonitor p) throws Exception {
String url = MCLauncherAPI.URLS.LIBRARY_BASE_URL.concat(lib.getPath());
File dest = new File(mc.getLibraryProvider().getLibrariesDirectory(), lib.getPath());
dest.mkdirs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import net.minidev.json.JSONObject;
import net.minidev.json.JSONStyle;
import net.minidev.json.JSONValue;

import sk.tomsik68.mclauncher.api.common.ILaunchSettings;
Expand All @@ -15,6 +17,7 @@
import sk.tomsik68.mclauncher.api.servers.ISavedServer;
import sk.tomsik68.mclauncher.api.versions.IVersion;
import sk.tomsik68.mclauncher.api.versions.IVersionLauncher;
import sk.tomsik68.mclauncher.impl.login.yggdrasil.YDUserObject.Prop;
import sk.tomsik68.mclauncher.util.StringSubstitutor;

public class MCDownloadVersionLauncher implements IVersionLauncher {
Expand All @@ -32,7 +35,16 @@ public String[] getMinecraftArguments(IMinecraftInstance mc, ISession session, I
subst.setVariable("assets_root", mc.getAssetsDirectory().getAbsolutePath());
subst.setVariable("assets_index_name", version.getAssetsIndexName());
subst.setVariable("user_type", session.getType().toString().toLowerCase());
subst.setVariable("user_properties", "{}");
if(session.getProperties() != null && !session.getProperties().isEmpty()){
JSONObject propertiesObj = new JSONObject();
List<Prop> properties = session.getProperties();
for(Prop p : properties){
propertiesObj.put(p.name, p.value);
}
subst.setVariable("user_properties", propertiesObj.toJSONString(JSONStyle.NO_COMPRESS));
}else
subst.setVariable("user_properties", "{}");

for (int i = 0; i < args.length; i++) {
args[i] = subst.substitute(args[i]);
}
Expand Down Expand Up @@ -68,8 +80,8 @@ public Process launch(ISession session, IMinecraftInstance mc, ISavedServer serv
command.add(arg);
}
// TODO minecraft natives

command.add("-Djava.library.path=/home/jasku/dev/Eclipse_workbench/MCLauncherAPI/testmc/bin/natives");
File nativesDir = new File(jarFile.getParentFile(),"natives");
command.add("-Djava.library.path="+nativesDir.getAbsolutePath());
command.add("-cp");
StringBuilder sb = new StringBuilder();
for (Library lib : version.getLibraries()) {
Expand Down
Loading

0 comments on commit aeabd7e

Please sign in to comment.