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

View File

@ -76,6 +76,25 @@ public final class CompatibilityRule {
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 {
ALLOW,
DISALLOW

View File

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