Skip to content

Add platform type to dependency check & platform api update #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 10, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ public enum DependencyType {
* Test if the mod satisfies the condition.
*/
MOD_ID,
/**
* Test if the platform satisfies the condition.
* (see {@link top.hendrixshen.magiclib.api.platform.PlatformType PlatformType}).
*/
PLATFORM,
/**
* Test if the predicate satisfies the condition.
* (see {@link top.hendrixshen.magiclib.util.collect.SimplePredicate SimplePredicate}).
*/
PREDICATE,
PREDICATE
;

public boolean matches(DependencyType type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import top.hendrixshen.magiclib.api.dependency.DependencyType;
import top.hendrixshen.magiclib.api.platform.DistType;
import top.hendrixshen.magiclib.api.platform.PlatformType;
import top.hendrixshen.magiclib.util.collect.SimplePredicate;

import java.lang.annotation.Retention;
Expand Down Expand Up @@ -38,9 +39,18 @@
*/
DependencyType dependencyType() default DependencyType.MOD_ID;

/**
* Platform type.
* <br/>The value is used if {@link Dependency#dependencyType()} == {@link DependencyType#PLATFORM}
* <p>Only if the specified platform satisfied condition.
*/
PlatformType platformType() default PlatformType.ANY;

/**
* Dist type.
* <p>Only if the specified dist satisfied condition.
* <br/>The value is used if {@link Dependency#dependencyType()} == {@link DependencyType#DIST}
* <p>
* Only if the specified dist satisfied condition.
*/
DistType distType() default DistType.ANY;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,65 @@
package top.hendrixshen.magiclib.api.platform;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.jetbrains.annotations.ApiStatus;

@Getter
@AllArgsConstructor
public enum PlatformType {
ANY("any"),
FABRIC("fabric"),
FABRIC_LIKE("fabric_like"),
FORGE("forge"),
NEOFORGE("neoforge"),
FORGE_LIKE("forge_like"),
QUILT("quilt");
NEOFORGE("neoforge"),
QUILT("quilt"),
UNKNOWN("unknown")
;

private final String name;

PlatformType(String name) {
this.name = name;
}

// This method should be static.
@Deprecated
@ApiStatus.ScheduledForRemoval
public boolean isForgeLike(PlatformType type) {
return this == PlatformType.FORGE_LIKE || type == PlatformType.FORGE_LIKE || type == this;
return type == PlatformType.FORGE_LIKE || type == PlatformType.FORGE || type == PlatformType.NEOFORGE;
}

// This method should be static.
@Deprecated
@ApiStatus.ScheduledForRemoval
public boolean isFabricLike(PlatformType type) {
return this == PlatformType.FABRIC_LIKE || type == PlatformType.FABRIC_LIKE || type == this;
return type == PlatformType.FABRIC_LIKE || type == PlatformType.FABRIC || type == PlatformType.QUILT;
}

public boolean isForgeLike() {
return this.isForgeLike(this);
}

public boolean isFabricLike() {
return this.isFabricLike(this);
}

public boolean matches(PlatformType type) {
return type == this || this.isFabricLike(type) || this.isForgeLike(type);
if (this == PlatformType.UNKNOWN || type == PlatformType.UNKNOWN) {
return false;
}

if (this == PlatformType.ANY || type == PlatformType.ANY) {
return true;
}

if (type == PlatformType.FABRIC_LIKE && this.isFabricLike() ||
this == PlatformType.FABRIC_LIKE && type.isFabricLike()) {
return true;
}

if (type == PlatformType.FORGE_LIKE && this.isForgeLike() ||
this == PlatformType.FORGE_LIKE && type.isForgeLike()) {
return true;
}

return type == this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import top.hendrixshen.magiclib.api.i18n.I18n;
import top.hendrixshen.magiclib.api.platform.DistType;
import top.hendrixshen.magiclib.api.platform.Platform;
import top.hendrixshen.magiclib.api.platform.PlatformType;
import top.hendrixshen.magiclib.util.VersionUtil;
import top.hendrixshen.magiclib.util.collect.SimplePredicate;
import top.hendrixshen.magiclib.util.collect.ValueContainer;
Expand All @@ -26,17 +27,19 @@ public class DependencyContainer<T> {
private final String value;
private final DependencyType dependencyType;
private final DistType distType;
private final PlatformType platformType;
private final List<String> versionPredicates;
private final SimplePredicate<T> predicate;
private final boolean optional;
private final T obj;

private DependencyContainer(String value, DependencyType dependencyType, DistType distType,
List<String> versionPredicates, SimplePredicate<T> predicate,
boolean optional, T obj) {
PlatformType platformType, List<String> versionPredicates,
SimplePredicate<T> predicate, boolean optional, T obj) {
this.value = value;
this.dependencyType = dependencyType;
this.distType = distType;
this.platformType = platformType;
this.versionPredicates = versionPredicates;
this.predicate = predicate;
this.optional = optional;
Expand Down Expand Up @@ -65,8 +68,19 @@ private DependencyContainer(String value, DependencyType dependencyType, DistTyp
}
}

// TODO: Remove this in the future
// For backward compatibility
PlatformType platformType;

try {
platformType = dependency.platformType();
} catch (NoSuchMethodError e) {
platformType = PlatformType.ANY;
}

return new DependencyContainer<>(dependency.value(), dependency.dependencyType(), dependency.distType(),
Lists.newArrayList(dependency.versionPredicates()), predicate, dependency.optional(), obj);
platformType, Lists.newArrayList(dependency.versionPredicates()),
predicate, dependency.optional(), obj);
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -101,6 +115,7 @@ private DependencyContainer(String value, DependencyType dependencyType, DistTyp
Annotations.getValue(annotationNode, "value"),
dependencyType,
Annotations.getValue(annotationNode, "distType", DistType.class, DistType.ANY),
Annotations.getValue(annotationNode, "platformType", PlatformType.class, PlatformType.ANY),
Lists.newArrayList(Annotations.getValue(annotationNode, "versionPredicates", Lists.newArrayList())),
predicate,
Annotations.getValue(annotationNode, "optional", Dependency.class),
Expand Down Expand Up @@ -147,8 +162,18 @@ public ValueContainer<DependencyCheckResult> checkAsRequire() {
return ValueContainer.of(new DependencyCheckResult(this.optional,
I18n.tr("magiclib.dependency.result.mod_id." + (this.optional ?
"optional.success" : "require.fail") + ".not_found", this.value,
this.versionPredicates.isEmpty() ? "[*]" : this.versionPredicates)
));
this.versionPredicates.isEmpty() ? "[*]" : this.versionPredicates)));
case PLATFORM:
PlatformType currentPlatformType = platform.getPlatformType();

if (this.platformType.matches(currentPlatformType)) {
return ValueContainer.of(new DependencyCheckResult(true,
I18n.tr("magiclib.dependency.result.platform.require.success", currentPlatformType)));
}

return ValueContainer.of(new DependencyCheckResult(false,
I18n.tr("magiclib.dependency.result.platform.require.fail",
this.platformType, currentPlatformType)));
case PREDICATE:
boolean testResult = this.predicate.test(this.obj);
return ValueContainer.of(new DependencyCheckResult(testResult, I18n.tr(
Expand Down Expand Up @@ -196,6 +221,16 @@ public ValueContainer<DependencyCheckResult> checkAsConflict() {
return ValueContainer.of(new DependencyCheckResult(true,
I18n.tr("magiclib.dependency.result.mod_id.conflict.success.not_found",
this.value)));
case PLATFORM:
PlatformType currentPlatformType = platform.getPlatformType();

if (this.platformType.matches(currentPlatformType)) {
return ValueContainer.of(new DependencyCheckResult(false,
I18n.tr("magiclib.dependency.result.platform.conflict.fail", this.platformType)));
}

return ValueContainer.of(new DependencyCheckResult(true,
I18n.tr("magiclib.dependency.result.platform.conflict.success", this.platformType)));
case PREDICATE:
boolean testResult = this.predicate.test(this.obj);
return ValueContainer.of(new DependencyCheckResult(!testResult,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package top.hendrixshen.magiclib.util;

import org.jetbrains.annotations.NotNull;

import java.util.function.Consumer;
import java.util.function.Supplier;

public class CommonUtil {
public static <T> T make(@NotNull Supplier<T> factory) {
return factory.get();
}

public static <T> T make(T object, @NotNull Consumer<T> initializer) {
initializer.accept(object);
return object;
}
}
Loading