Skip to content

Commit 4857a7d

Browse files
authored
Add platform type to dependency check & platform api update (#111)
* Support for PlatformTypes as dependency check condition * Update Mixin Dependency check * Reimplement getNamedMappingName api (forge-like) * Fix crash caused by FML changes * Update ReflectionUtil * Add CommonUtil * Make VersionUtil::isVersionSatisfyPredicate public * Fix getNamedMappingName impl (forge) * Break changes with ModListAdapter (forge) - Use Reflection to adapt to Minecraft Forge 1.17 changes * Minecraft Forge 1.20.5+ always uses mojmap - But if you set it to yarn via architectury-loom, it's undetectable! * Fix crash with getNamedMappingName (neoforge) --------- Signed-off-by: Hendrix-Shen <[email protected]>
1 parent 331b5ef commit 4857a7d

File tree

19 files changed

+410
-88
lines changed

19 files changed

+410
-88
lines changed

magiclib-core/common/src/main/java/top/hendrixshen/magiclib/api/dependency/DependencyType.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ public enum DependencyType {
1212
* Test if the mod satisfies the condition.
1313
*/
1414
MOD_ID,
15+
/**
16+
* Test if the platform satisfies the condition.
17+
* (see {@link top.hendrixshen.magiclib.api.platform.PlatformType PlatformType}).
18+
*/
19+
PLATFORM,
1520
/**
1621
* Test if the predicate satisfies the condition.
1722
* (see {@link top.hendrixshen.magiclib.util.collect.SimplePredicate SimplePredicate}).
1823
*/
19-
PREDICATE,
24+
PREDICATE
2025
;
2126

2227
public boolean matches(DependencyType type) {

magiclib-core/common/src/main/java/top/hendrixshen/magiclib/api/dependency/annotation/Dependency.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import top.hendrixshen.magiclib.api.dependency.DependencyType;
44
import top.hendrixshen.magiclib.api.platform.DistType;
5+
import top.hendrixshen.magiclib.api.platform.PlatformType;
56
import top.hendrixshen.magiclib.util.collect.SimplePredicate;
67

78
import java.lang.annotation.Retention;
@@ -38,9 +39,18 @@
3839
*/
3940
DependencyType dependencyType() default DependencyType.MOD_ID;
4041

42+
/**
43+
* Platform type.
44+
* <br/>The value is used if {@link Dependency#dependencyType()} == {@link DependencyType#PLATFORM}
45+
* <p>Only if the specified platform satisfied condition.
46+
*/
47+
PlatformType platformType() default PlatformType.ANY;
48+
4149
/**
4250
* Dist type.
43-
* <p>Only if the specified dist satisfied condition.
51+
* <br/>The value is used if {@link Dependency#dependencyType()} == {@link DependencyType#DIST}
52+
* <p>
53+
* Only if the specified dist satisfied condition.
4454
*/
4555
DistType distType() default DistType.ANY;
4656

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,65 @@
11
package top.hendrixshen.magiclib.api.platform;
22

3+
import lombok.AllArgsConstructor;
34
import lombok.Getter;
5+
import org.jetbrains.annotations.ApiStatus;
46

57
@Getter
8+
@AllArgsConstructor
69
public enum PlatformType {
10+
ANY("any"),
711
FABRIC("fabric"),
812
FABRIC_LIKE("fabric_like"),
913
FORGE("forge"),
10-
NEOFORGE("neoforge"),
1114
FORGE_LIKE("forge_like"),
12-
QUILT("quilt");
15+
NEOFORGE("neoforge"),
16+
QUILT("quilt"),
17+
UNKNOWN("unknown")
18+
;
1319

1420
private final String name;
1521

16-
PlatformType(String name) {
17-
this.name = name;
18-
}
19-
22+
// This method should be static.
23+
@Deprecated
24+
@ApiStatus.ScheduledForRemoval
2025
public boolean isForgeLike(PlatformType type) {
21-
return this == PlatformType.FORGE_LIKE || type == PlatformType.FORGE_LIKE || type == this;
26+
return type == PlatformType.FORGE_LIKE || type == PlatformType.FORGE || type == PlatformType.NEOFORGE;
2227
}
2328

29+
// This method should be static.
30+
@Deprecated
31+
@ApiStatus.ScheduledForRemoval
2432
public boolean isFabricLike(PlatformType type) {
25-
return this == PlatformType.FABRIC_LIKE || type == PlatformType.FABRIC_LIKE || type == this;
33+
return type == PlatformType.FABRIC_LIKE || type == PlatformType.FABRIC || type == PlatformType.QUILT;
34+
}
35+
36+
public boolean isForgeLike() {
37+
return this.isForgeLike(this);
38+
}
39+
40+
public boolean isFabricLike() {
41+
return this.isFabricLike(this);
2642
}
2743

2844
public boolean matches(PlatformType type) {
29-
return type == this || this.isFabricLike(type) || this.isForgeLike(type);
45+
if (this == PlatformType.UNKNOWN || type == PlatformType.UNKNOWN) {
46+
return false;
47+
}
48+
49+
if (this == PlatformType.ANY || type == PlatformType.ANY) {
50+
return true;
51+
}
52+
53+
if (type == PlatformType.FABRIC_LIKE && this.isFabricLike() ||
54+
this == PlatformType.FABRIC_LIKE && type.isFabricLike()) {
55+
return true;
56+
}
57+
58+
if (type == PlatformType.FORGE_LIKE && this.isForgeLike() ||
59+
this == PlatformType.FORGE_LIKE && type.isForgeLike()) {
60+
return true;
61+
}
62+
63+
return type == this;
3064
}
3165
}

magiclib-core/common/src/main/java/top/hendrixshen/magiclib/impl/dependency/DependencyContainer.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import top.hendrixshen.magiclib.api.i18n.I18n;
1414
import top.hendrixshen.magiclib.api.platform.DistType;
1515
import top.hendrixshen.magiclib.api.platform.Platform;
16+
import top.hendrixshen.magiclib.api.platform.PlatformType;
1617
import top.hendrixshen.magiclib.util.VersionUtil;
1718
import top.hendrixshen.magiclib.util.collect.SimplePredicate;
1819
import top.hendrixshen.magiclib.util.collect.ValueContainer;
@@ -26,17 +27,19 @@ public class DependencyContainer<T> {
2627
private final String value;
2728
private final DependencyType dependencyType;
2829
private final DistType distType;
30+
private final PlatformType platformType;
2931
private final List<String> versionPredicates;
3032
private final SimplePredicate<T> predicate;
3133
private final boolean optional;
3234
private final T obj;
3335

3436
private DependencyContainer(String value, DependencyType dependencyType, DistType distType,
35-
List<String> versionPredicates, SimplePredicate<T> predicate,
36-
boolean optional, T obj) {
37+
PlatformType platformType, List<String> versionPredicates,
38+
SimplePredicate<T> predicate, boolean optional, T obj) {
3739
this.value = value;
3840
this.dependencyType = dependencyType;
3941
this.distType = distType;
42+
this.platformType = platformType;
4043
this.versionPredicates = versionPredicates;
4144
this.predicate = predicate;
4245
this.optional = optional;
@@ -65,8 +68,19 @@ private DependencyContainer(String value, DependencyType dependencyType, DistTyp
6568
}
6669
}
6770

71+
// TODO: Remove this in the future
72+
// For backward compatibility
73+
PlatformType platformType;
74+
75+
try {
76+
platformType = dependency.platformType();
77+
} catch (NoSuchMethodError e) {
78+
platformType = PlatformType.ANY;
79+
}
80+
6881
return new DependencyContainer<>(dependency.value(), dependency.dependencyType(), dependency.distType(),
69-
Lists.newArrayList(dependency.versionPredicates()), predicate, dependency.optional(), obj);
82+
platformType, Lists.newArrayList(dependency.versionPredicates()),
83+
predicate, dependency.optional(), obj);
7084
}
7185

7286
@SuppressWarnings("unchecked")
@@ -101,6 +115,7 @@ private DependencyContainer(String value, DependencyType dependencyType, DistTyp
101115
Annotations.getValue(annotationNode, "value"),
102116
dependencyType,
103117
Annotations.getValue(annotationNode, "distType", DistType.class, DistType.ANY),
118+
Annotations.getValue(annotationNode, "platformType", PlatformType.class, PlatformType.ANY),
104119
Lists.newArrayList(Annotations.getValue(annotationNode, "versionPredicates", Lists.newArrayList())),
105120
predicate,
106121
Annotations.getValue(annotationNode, "optional", Dependency.class),
@@ -147,8 +162,18 @@ public ValueContainer<DependencyCheckResult> checkAsRequire() {
147162
return ValueContainer.of(new DependencyCheckResult(this.optional,
148163
I18n.tr("magiclib.dependency.result.mod_id." + (this.optional ?
149164
"optional.success" : "require.fail") + ".not_found", this.value,
150-
this.versionPredicates.isEmpty() ? "[*]" : this.versionPredicates)
151-
));
165+
this.versionPredicates.isEmpty() ? "[*]" : this.versionPredicates)));
166+
case PLATFORM:
167+
PlatformType currentPlatformType = platform.getPlatformType();
168+
169+
if (this.platformType.matches(currentPlatformType)) {
170+
return ValueContainer.of(new DependencyCheckResult(true,
171+
I18n.tr("magiclib.dependency.result.platform.require.success", currentPlatformType)));
172+
}
173+
174+
return ValueContainer.of(new DependencyCheckResult(false,
175+
I18n.tr("magiclib.dependency.result.platform.require.fail",
176+
this.platformType, currentPlatformType)));
152177
case PREDICATE:
153178
boolean testResult = this.predicate.test(this.obj);
154179
return ValueContainer.of(new DependencyCheckResult(testResult, I18n.tr(
@@ -196,6 +221,16 @@ public ValueContainer<DependencyCheckResult> checkAsConflict() {
196221
return ValueContainer.of(new DependencyCheckResult(true,
197222
I18n.tr("magiclib.dependency.result.mod_id.conflict.success.not_found",
198223
this.value)));
224+
case PLATFORM:
225+
PlatformType currentPlatformType = platform.getPlatformType();
226+
227+
if (this.platformType.matches(currentPlatformType)) {
228+
return ValueContainer.of(new DependencyCheckResult(false,
229+
I18n.tr("magiclib.dependency.result.platform.conflict.fail", this.platformType)));
230+
}
231+
232+
return ValueContainer.of(new DependencyCheckResult(true,
233+
I18n.tr("magiclib.dependency.result.platform.conflict.success", this.platformType)));
199234
case PREDICATE:
200235
boolean testResult = this.predicate.test(this.obj);
201236
return ValueContainer.of(new DependencyCheckResult(!testResult,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package top.hendrixshen.magiclib.util;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.util.function.Consumer;
6+
import java.util.function.Supplier;
7+
8+
public class CommonUtil {
9+
public static <T> T make(@NotNull Supplier<T> factory) {
10+
return factory.get();
11+
}
12+
13+
public static <T> T make(T object, @NotNull Consumer<T> initializer) {
14+
initializer.accept(object);
15+
return object;
16+
}
17+
}

0 commit comments

Comments
 (0)