Skip to content

Commit aeabd7e

Browse files
committed
TestMCDownloadLaunch now works like a charm :O
1 parent 58fb64b commit aeabd7e

File tree

11 files changed

+157
-37
lines changed

11 files changed

+157
-37
lines changed

src/sk/tomsik68/mclauncher/api/login/ISession.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package sk.tomsik68.mclauncher.api.login;
22

3+
import java.util.List;
4+
5+
import sk.tomsik68.mclauncher.impl.login.yggdrasil.YDUserObject.Prop;
6+
37
public interface ISession {
48
public String getUsername();
59

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

1014
public ESessionType getType();
15+
16+
public List<Prop> getProperties();
1117
}

src/sk/tomsik68/mclauncher/impl/login/legacy/LegacySession.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package sk.tomsik68.mclauncher.impl.login.legacy;
22

3+
import java.util.List;
4+
35
import sk.tomsik68.mclauncher.api.login.ESessionType;
46
import sk.tomsik68.mclauncher.api.login.ISession;
7+
import sk.tomsik68.mclauncher.impl.login.yggdrasil.YDUserObject.Prop;
58

69
public class LegacySession implements ISession {
710
private final String userName, sessionID, uuid, downloadTicket, lastVersion;
@@ -42,4 +45,9 @@ public ESessionType getType() {
4245
return ESessionType.LEGACY;
4346
}
4447

48+
@Override
49+
public List<Prop> getProperties() {
50+
return null;
51+
}
52+
4553
}

src/sk/tomsik68/mclauncher/impl/login/yggdrasil/YDAuthProfile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void setProfileName(String profileName) {
7272
}
7373

7474
public YDPartialGameProfile getYDGameProfile() {
75-
YDPartialGameProfile result = new YDPartialGameProfile(userName, uuid);
75+
YDPartialGameProfile result = new YDPartialGameProfile(userName, uuid, false);
7676
return result;
7777
}
7878

src/sk/tomsik68/mclauncher/impl/login/yggdrasil/YDLoginResponse.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class YDLoginResponse extends YDResponse {
99
private final String sessionID, clientToken;
1010
private final YDPartialGameProfile selectedProfile;
1111
private HashMap<String, YDPartialGameProfile> profiles = new HashMap<String, YDPartialGameProfile>();
12+
private YDUserObject user;
1213

1314
public YDLoginResponse(JSONObject json) {
1415
super(json);
@@ -23,6 +24,8 @@ public YDLoginResponse(JSONObject json) {
2324
this.profiles.put(p.getName(), p);
2425
}
2526
}
27+
if(json.containsKey("user"))
28+
user = new YDUserObject((JSONObject)json.get("user"));
2629
}
2730

2831
public String getSessionID() {
@@ -40,4 +43,11 @@ public YDPartialGameProfile getSelectedProfile() {
4043
public YDPartialGameProfile getProfile(String name) {
4144
return profiles.get(name);
4245
}
46+
47+
public YDUserObject getUserObject() {
48+
if(user == null){
49+
user = new YDUserObject(selectedProfile.getName());
50+
}
51+
return user;
52+
}
4353
}

src/sk/tomsik68/mclauncher/impl/login/yggdrasil/YDPartialGameProfile.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
package sk.tomsik68.mclauncher.impl.login.yggdrasil;
22

33
import net.minidev.json.JSONObject;
4+
import net.minidev.json.JSONStyle;
45
import sk.tomsik68.mclauncher.api.json.IJSONSerializable;
56

67
public class YDPartialGameProfile implements IJSONSerializable {
78
private final String name, id;
9+
private final boolean legacy;
810

9-
public YDPartialGameProfile(String name, String id) {
11+
public YDPartialGameProfile(String name, String id, boolean isLegacy) {
1012
this.name = name;
1113
this.id = id;
14+
this.legacy = isLegacy;
1215
}
1316

1417
public YDPartialGameProfile(JSONObject jsonObj) {
15-
this(jsonObj.get("name").toString(),jsonObj.get("id").toString());
18+
this(jsonObj.get("name").toString(), jsonObj.get("id").toString(), false);
19+
System.out.println("Building YDPartialGameProfile: "+jsonObj.toJSONString(JSONStyle.NO_COMPRESS));
20+
1621
}
1722

1823
public String getName() {
@@ -29,4 +34,8 @@ public JSONObject toJSON() {
2934
obj.put("name", name);
3035
return obj;
3136
}
37+
38+
public boolean isLegacy() {
39+
return legacy;
40+
}
3241
}

src/sk/tomsik68/mclauncher/impl/login/yggdrasil/YDSession.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package sk.tomsik68.mclauncher.impl.login.yggdrasil;
22

3+
import java.util.List;
4+
35
import sk.tomsik68.mclauncher.api.login.ESessionType;
46
import sk.tomsik68.mclauncher.api.login.ISession;
7+
import sk.tomsik68.mclauncher.impl.login.yggdrasil.YDUserObject.Prop;
58

69
public class YDSession implements ISession {
710
private final String username, sessid, uuid;
11+
private final YDUserObject user;
812

913
public YDSession(YDLoginResponse r) {
1014
username = r.getSelectedProfile().getName();
1115
sessid = r.getSessionID();
1216
uuid = r.getSelectedProfile().getId();
13-
17+
user = r.getUserObject();
1418
}
1519

1620
@Override
@@ -34,4 +38,12 @@ public ESessionType getType() {
3438
return ESessionType.MOJANG;
3539
}
3640

41+
public YDUserObject getUserObject() {
42+
return user;
43+
}
44+
45+
@Override
46+
public List<Prop> getProperties() {
47+
return user.getProperties();
48+
}
3749
}

src/sk/tomsik68/mclauncher/impl/versions/mcdownload/Library.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@
22

33
import java.util.ArrayList;
44
import java.util.HashMap;
5-
import java.util.HashSet;
6-
import java.util.Set;
7-
import java.util.logging.Logger;
85

6+
import net.minidev.json.JSONArray;
7+
import net.minidev.json.JSONObject;
98
import sk.tomsik68.mclauncher.api.common.IOperatingSystem;
109
import sk.tomsik68.mclauncher.impl.common.Platform;
1110
import sk.tomsik68.mclauncher.impl.versions.mcdownload.Rule.Action;
12-
import sk.tomsik68.mclauncher.util.FilePathBuilder;
13-
14-
import net.minidev.json.JSONArray;
15-
import net.minidev.json.JSONObject;
11+
import sk.tomsik68.mclauncher.util.IExtractRules;
1612

1713
public class Library {
1814
private final String name;
1915
private final HashMap<String, String> natives = new HashMap<String, String>();
2016
private final ArrayList<Rule> rules = new ArrayList<Rule>();
17+
private LibraryExtractRules extractRules;
2118

2219
public Library(JSONObject json) {
2320
name = json.get("name").toString();
@@ -33,6 +30,9 @@ public Library(JSONObject json) {
3330
rules.add(new Rule((JSONObject) rulz.get(i)));
3431
}
3532
}
33+
if (json.containsKey("extract")) {
34+
extractRules = new LibraryExtractRules((JSONObject) json.get("extract"));
35+
}
3636
}
3737

3838
public String getName() {
@@ -41,14 +41,14 @@ public String getName() {
4141

4242
public String getNatives(IOperatingSystem os) {
4343
if (!natives.containsKey(os.getMinecraftName()))
44-
return natives.get(Platform.wrapName(os.getMinecraftName()));
44+
return natives.get(Platform.wrapName(os.getMinecraftName())).replace("${arch}", System.getProperty("sun.arch.data.model"));
4545
return natives.get(os.getMinecraftName()).replace("${arch}", System.getProperty("sun.arch.data.model"));
4646
}
4747

4848
public String getPath() {
4949
String[] split = name.split(":");
5050
StringBuilder result = new StringBuilder();
51-
51+
5252
result = result.append(split[0].replace('.', '/'));// net/sf/jopt-simple
5353
result = result.append('/').append(split[1]).append('/').append(split[2]).append('/'); // /jopt-simple/4.4/
5454
result = result.append(split[1]).append('-').append(split[2]); // jopt-simple-4.4
@@ -64,11 +64,20 @@ public String getPath() {
6464
public boolean isCompatible() {
6565
Action action = Action.ALLOW;
6666
for (Rule rule : rules) {
67-
if (rule.applies()){
68-
if(action == Action.ALLOW)
67+
if (rule.applies()) {
68+
if (action == Action.ALLOW)
6969
action = rule.getAction();
7070
}
7171
}
72-
return action == Action.ALLOW;
72+
return action == Action.ALLOW && (natives== null || natives.containsKey(Platform.getCurrentPlatform().getMinecraftName()));
73+
}
74+
75+
public boolean hasNatives() {
76+
return !natives.isEmpty();
7377
}
78+
79+
public IExtractRules getExtractRules() {
80+
return extractRules;
81+
}
82+
7483
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package sk.tomsik68.mclauncher.impl.versions.mcdownload;
2+
3+
import java.util.ArrayList;
4+
import java.util.zip.ZipEntry;
5+
6+
import net.minidev.json.JSONArray;
7+
import net.minidev.json.JSONObject;
8+
9+
import sk.tomsik68.mclauncher.util.IExtractRules;
10+
11+
public class LibraryExtractRules implements IExtractRules {
12+
private ArrayList<String> exclude = new ArrayList<String>();
13+
14+
public LibraryExtractRules(JSONObject object) {
15+
JSONArray excludeArray = (JSONArray) object.get("exclude");
16+
for (Object obj : excludeArray) {
17+
exclude.add(obj.toString());
18+
}
19+
}
20+
21+
@Override
22+
public boolean accepts(ZipEntry entry) {
23+
String path = entry.getName();
24+
if (exclude != null && !exclude.isEmpty()) {
25+
for (String p : exclude) {
26+
if (path.startsWith(p))
27+
return false;
28+
29+
}
30+
}
31+
return true;
32+
}
33+
34+
}

src/sk/tomsik68/mclauncher/impl/versions/mcdownload/MCDownloadVersionInstaller.java

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
import sk.tomsik68.mclauncher.api.versions.IVersionInstallListener;
1515
import sk.tomsik68.mclauncher.api.versions.IVersionInstaller;
1616
import sk.tomsik68.mclauncher.impl.versions.mcdownload.assets.MCDResourcesInstaller;
17+
import sk.tomsik68.mclauncher.util.ExtractUtils;
1718
import sk.tomsik68.mclauncher.util.FileUtils;
1819

1920
public class MCDownloadVersionInstaller implements IVersionInstaller {
2021
private final ArrayList<IVersionInstallListener> listeners = new ArrayList<IVersionInstallListener>();
21-
2222

2323
@Override
2424
public void addVersionInstallListener(IVersionInstallListener listener) {
@@ -34,20 +34,42 @@ public void install(IVersion v, IMinecraftInstance mc, IProgressMonitor progress
3434
throw new VersionIncompatibleException(v);
3535
log.info("Version compatible");
3636
List<Library> toInstall = version.getLibraries();
37+
List<Library> toExtract = new ArrayList<Library>();
3738
log.info("Fetching libraries...");
39+
3840
for (Library lib : toInstall) {
39-
if (!mc.getLibraryProvider().isInstalled(lib) && lib.isCompatible()) {
40-
log.info("Installing " + lib.getName());
41-
try {
42-
installLibrary(lib, mc, progress);
43-
} catch (Exception e) {
44-
e.printStackTrace();
45-
log.info("Failed to install " + lib.getName());
41+
if (lib.isCompatible()) {
42+
if (!mc.getLibraryProvider().isInstalled(lib)) {
43+
log.info("Installing " + lib.getName());
44+
try {
45+
downloadLibrary(lib, mc, progress);
46+
} catch (Exception e) {
47+
e.printStackTrace();
48+
log.info("Failed to install " + lib.getName());
49+
}
50+
}
51+
if (lib.hasNatives()) {
52+
toExtract.add(lib);
4653
}
4754
} else {
48-
log.info(lib.getName() + " is already installed or incompatible.");
55+
log.info(lib.getName() + " is not compatible.");
56+
}
57+
}
58+
59+
log.info("Extracting natives...");
60+
File nativesDir = new File(mc.getJarProvider().getVersionFile(version.getUniqueID()).getParentFile(), "natives");
61+
// purge old natives
62+
if (nativesDir.exists()) {
63+
File[] contains = nativesDir.listFiles();
64+
for (File f : contains) {
65+
f.delete();
4966
}
5067
}
68+
for (Library lib : toExtract) {
69+
File libFile = mc.getLibraryProvider().getLibraryFile(lib);
70+
ExtractUtils.extractZipWithRules(libFile, nativesDir, lib.getExtractRules());
71+
}
72+
5173
log.info("Updating resources...");
5274
updateResources(mc, version, progress);
5375
File jarDest = mc.getJarProvider().getVersionFile(version.getUniqueID());
@@ -62,7 +84,7 @@ public void install(IVersion v, IMinecraftInstance mc, IProgressMonitor progress
6284
}
6385
}
6486
notifyListeners(version);
65-
if(progress != null)
87+
if (progress != null)
6688
progress.finish();
6789
}
6890

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

78-
79-
80-
private void installLibrary(Library lib, IMinecraftInstance mc, IProgressMonitor p) throws Exception {
100+
private void downloadLibrary(Library lib, IMinecraftInstance mc, IProgressMonitor p) throws Exception {
81101
String url = MCLauncherAPI.URLS.LIBRARY_BASE_URL.concat(lib.getPath());
82102
File dest = new File(mc.getLibraryProvider().getLibrariesDirectory(), lib.getPath());
83103
dest.mkdirs();

src/sk/tomsik68/mclauncher/impl/versions/mcdownload/MCDownloadVersionLauncher.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import java.io.FileInputStream;
55
import java.io.FileNotFoundException;
66
import java.util.ArrayList;
7+
import java.util.List;
78

89
import net.minidev.json.JSONObject;
10+
import net.minidev.json.JSONStyle;
911
import net.minidev.json.JSONValue;
1012

1113
import sk.tomsik68.mclauncher.api.common.ILaunchSettings;
@@ -15,6 +17,7 @@
1517
import sk.tomsik68.mclauncher.api.servers.ISavedServer;
1618
import sk.tomsik68.mclauncher.api.versions.IVersion;
1719
import sk.tomsik68.mclauncher.api.versions.IVersionLauncher;
20+
import sk.tomsik68.mclauncher.impl.login.yggdrasil.YDUserObject.Prop;
1821
import sk.tomsik68.mclauncher.util.StringSubstitutor;
1922

2023
public class MCDownloadVersionLauncher implements IVersionLauncher {
@@ -32,7 +35,16 @@ public String[] getMinecraftArguments(IMinecraftInstance mc, ISession session, I
3235
subst.setVariable("assets_root", mc.getAssetsDirectory().getAbsolutePath());
3336
subst.setVariable("assets_index_name", version.getAssetsIndexName());
3437
subst.setVariable("user_type", session.getType().toString().toLowerCase());
35-
subst.setVariable("user_properties", "{}");
38+
if(session.getProperties() != null && !session.getProperties().isEmpty()){
39+
JSONObject propertiesObj = new JSONObject();
40+
List<Prop> properties = session.getProperties();
41+
for(Prop p : properties){
42+
propertiesObj.put(p.name, p.value);
43+
}
44+
subst.setVariable("user_properties", propertiesObj.toJSONString(JSONStyle.NO_COMPRESS));
45+
}else
46+
subst.setVariable("user_properties", "{}");
47+
3648
for (int i = 0; i < args.length; i++) {
3749
args[i] = subst.substitute(args[i]);
3850
}
@@ -68,8 +80,8 @@ public Process launch(ISession session, IMinecraftInstance mc, ISavedServer serv
6880
command.add(arg);
6981
}
7082
// TODO minecraft natives
71-
72-
command.add("-Djava.library.path=/home/jasku/dev/Eclipse_workbench/MCLauncherAPI/testmc/bin/natives");
83+
File nativesDir = new File(jarFile.getParentFile(),"natives");
84+
command.add("-Djava.library.path="+nativesDir.getAbsolutePath());
7385
command.add("-cp");
7486
StringBuilder sb = new StringBuilder();
7587
for (Library lib : version.getLibraries()) {

0 commit comments

Comments
 (0)