This commit is contained in:
huangyuhui 2018-04-30 01:59:33 +08:00
parent b925423d61
commit bda2149c02
3 changed files with 51 additions and 23 deletions

View File

@ -17,10 +17,12 @@
*/ */
package org.jackhuang.hmcl.download.game; package org.jackhuang.hmcl.download.game;
import org.jackhuang.hmcl.game.CompatibilityRule;
import org.jackhuang.hmcl.game.Library; import org.jackhuang.hmcl.game.Library;
import org.jackhuang.hmcl.game.Version; import org.jackhuang.hmcl.game.Version;
import org.jackhuang.hmcl.task.TaskResult; import org.jackhuang.hmcl.task.TaskResult;
import org.jackhuang.hmcl.util.Constants; import org.jackhuang.hmcl.util.Constants;
import org.jackhuang.hmcl.util.Platform;
import org.jackhuang.hmcl.util.SimpleMultimap; import org.jackhuang.hmcl.util.SimpleMultimap;
import org.jackhuang.hmcl.util.VersionNumber; import org.jackhuang.hmcl.util.VersionNumber;
@ -44,7 +46,6 @@ public class LibrariesUniqueTask extends TaskResult<Version> {
public void execute() { public void execute() {
List<Library> libraries = new ArrayList<>(version.getLibraries()); List<Library> libraries = new ArrayList<>(version.getLibraries());
Map<String, VersionNumber> versionMap = new HashMap<>();
SimpleMultimap<String, Library> multimap = new SimpleMultimap<String, Library>(HashMap::new, LinkedList::new); SimpleMultimap<String, Library> multimap = new SimpleMultimap<String, Library>(HashMap::new, LinkedList::new);
for (Library library : libraries) { for (Library library : libraries) {
@ -52,32 +53,36 @@ public class LibrariesUniqueTask extends TaskResult<Version> {
VersionNumber number = VersionNumber.asVersion(library.getVersion()); VersionNumber number = VersionNumber.asVersion(library.getVersion());
String serialized = Constants.GSON.toJson(library); String serialized = Constants.GSON.toJson(library);
if (versionMap.containsKey(id)) { if (multimap.containsKey(id)) {
VersionNumber otherNumber = versionMap.get(id); boolean hasEqualRule = false;
if (number.compareTo(otherNumber) > 0) { for (Library otherLibrary : multimap.get(id)) {
multimap.removeKey(id); VersionNumber otherNumber = VersionNumber.asVersion(otherLibrary.getVersion());
versionMap.put(id, number); if (CompatibilityRule.equals(library.getRules(), otherLibrary.getRules())) { // rules equal, ignore older version.
multimap.put(id, library); hasEqualRule = true;
} else if (number.compareTo(otherNumber) == 0) { // same library id. if (number.compareTo(otherNumber) > 0) { // if this library is newer
boolean flag = false; multimap.removeValue(otherLibrary);
// prevent from duplicated libraries multimap.put(id, library);
for (Library otherLibrary : multimap.get(id)) break;
if (library.equals(otherLibrary)) { } else if (number.compareTo(otherNumber) == 0) { // same library id.
String otherSerialized = Constants.GSON.toJson(otherLibrary); // prevent from duplicated libraries
// A trick, the library that has more information is better, which can be if (library.equals(otherLibrary)) {
// considered whose serialized JSON text will be longer. String otherSerialized = Constants.GSON.toJson(otherLibrary);
if (serialized.length() <= otherSerialized.length()) { // A trick, the library that has more information is better, which can be
flag = true; // considered whose serialized JSON text will be longer.
break; if (serialized.length() > otherSerialized.length()) {
} else { multimap.removeValue(id, otherLibrary);
multimap.removeValue(id, otherLibrary); multimap.put(id, library);
break;
}
} }
} }
if (!flag) }
multimap.put(id, library); }
if (!hasEqualRule) {
multimap.put(id, library);
} }
} else { } else {
versionMap.put(id, number);
multimap.put(id, library); multimap.put(id, library);
} }
} }

View File

@ -76,6 +76,25 @@ public final class CompatibilityRule {
return action == Action.ALLOW; return action == Action.ALLOW;
} }
public static boolean equals(Collection<CompatibilityRule> rules1, Collection<CompatibilityRule> rules2) {
return Objects.hashCode(rules1) == Objects.hashCode(rules2);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CompatibilityRule that = (CompatibilityRule) o;
return action == that.action &&
Objects.equals(os, that.os) &&
Objects.equals(features, that.features);
}
@Override
public int hashCode() {
return Objects.hash(action, os, features);
}
public enum Action { public enum Action {
ALLOW, ALLOW,
DISALLOW DISALLOW

View File

@ -137,6 +137,10 @@ public class Library implements Comparable<Library> {
return checksums; return checksums;
} }
public List<CompatibilityRule> getRules() {
return rules;
}
public boolean is(String groupId, String artifactId) { public boolean is(String groupId, String artifactId) {
return this.groupId.equals(groupId) && this.artifactId.equals(artifactId); return this.groupId.equals(groupId) && this.artifactId.equals(artifactId);
} }