Skip to content

Commit 8ed7a66

Browse files
committed
Rework rules strategy for version.json libs
Still a work in progress, needs proper testing with older versions (1.16.5-).
1 parent bfa46d8 commit 8ed7a66

File tree

1 file changed

+50
-13
lines changed

1 file changed

+50
-13
lines changed

json-data-utils/src/main/java/net/minecraftforge/util/data/json/MinecraftVersion.java

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99

1010
import java.net.URL;
1111
import java.util.ArrayList;
12+
import java.util.Arrays;
13+
import java.util.Collection;
14+
import java.util.Collections;
15+
import java.util.EnumSet;
1216
import java.util.List;
1317
import java.util.Map;
18+
import java.util.Objects;
1419

1520
// TODO: [MCMaven][Documentation][MinecraftVersion] What in here is nullable?
1621
/** Represents a Minecraft version and its artifacts. */
@@ -35,13 +40,35 @@ public List<Lib> getLibs() {
3540
List<Lib> libs = new ArrayList<>();
3641

3742
for (Library lib : this.libraries) {
38-
if (lib.rules != null && lib.rules.size() != 1)
39-
throw new UnsupportedOperationException("Rules were found, but wasn't precisely one??? Rules: " + lib.rules);
40-
41-
OS os = lib.rules == null ? null : lib.rules.get(0).os.toOS();
43+
EnumSet<OS> os = EnumSet.noneOf(OS.class);
44+
if (lib.rules != null) {
45+
for (Rule rule : lib.rules) {
46+
switch (rule.action) {
47+
case "allow":
48+
if (rule.os == null)
49+
os.addAll(Arrays.asList(OS.WINDOWS, OS.MACOS, OS.LINUX));
50+
else
51+
os.add(rule.os.toOS());
52+
break;
53+
case "disallow":
54+
if (os.isEmpty())
55+
os.addAll(Arrays.asList(OS.WINDOWS, OS.MACOS, OS.LINUX));
56+
57+
//noinspection DataFlowIssue
58+
os.remove(rule.os.toOS());
59+
break;
60+
}
61+
}
62+
}
4263
libs.add(new Lib(lib.name, lib.downloads.artifact, os));
43-
if (lib.downloads.classifiers != null)
44-
lib.downloads.classifiers.forEach((k, v) -> libs.add(new Lib(lib.name + ':' + k, v, os)));
64+
65+
if (lib.natives != null && lib.downloads.classifiers != null) {
66+
lib.natives.forEach((nativeOS, classifier) -> {
67+
LibraryDownload nativeLib = lib.downloads.classifiers.get(nativeOS);
68+
if (nativeLib != null)
69+
libs.add(new Lib(lib.name + ':' + classifier, nativeLib, EnumSet.of(Rule.OS.toOS(nativeOS))));
70+
});
71+
}
4572
}
4673

4774
return libs;
@@ -50,13 +77,17 @@ public List<Lib> getLibs() {
5077
public static final class Lib {
5178
public final String coord;
5279
public final LibraryDownload dl;
53-
public final OS os;
80+
public final EnumSet<OS> os;
5481

55-
public Lib(String coord, LibraryDownload dl, OS os) {
82+
public Lib(String coord, LibraryDownload dl, EnumSet<OS> os) {
5683
this.coord = coord;
5784
this.dl = dl;
5885
this.os = os;
5986
}
87+
88+
public boolean allows(OS os) {
89+
return this.os.isEmpty() || this.os.contains(os);
90+
}
6091
}
6192

6293
// TODO: [MCMaven][MinecraftVersion] Add function to filter libraries based on current operating systems
@@ -67,8 +98,10 @@ public static class Library {
6798
public String name;
6899
/** The downloads provided by the library. */
69100
public Downloads downloads;
101+
/** The natives. */
102+
public Map<String, String> natives;
70103
/** The rules for the library. */
71-
public List<Rules> rules;
104+
public List<Rule> rules;
72105
}
73106

74107
/** Represents the downloads for a library. */
@@ -96,15 +129,15 @@ public static class LibraryDownload extends Download {
96129
public String path;
97130
}
98131

99-
public static class Rules {
132+
public static class Rule {
100133
public String action;
101-
public OS os;
134+
public @Nullable OS os;
102135

103136
public static class OS {
104137
public String name;
105138

106-
public net.minecraftforge.util.data.OS toOS() {
107-
switch (this.name) {
139+
public static net.minecraftforge.util.data.OS toOS(String name) {
140+
switch (name) {
108141
case "windows":
109142
return net.minecraftforge.util.data.OS.WINDOWS;
110143
case "linux":
@@ -115,6 +148,10 @@ public net.minecraftforge.util.data.OS toOS() {
115148
return net.minecraftforge.util.data.OS.UNKNOWN;
116149
}
117150
}
151+
152+
public net.minecraftforge.util.data.OS toOS() {
153+
return toOS(this.name);
154+
}
118155
}
119156
}
120157

0 commit comments

Comments
 (0)