Skip to content

Commit

Permalink
Simplified nested annotation usage (#76)
Browse files Browse the repository at this point in the history
* Make Dependencies Repeatable via CompositeDependencies

* Simplified CompositeDependencies usage in Mixin

* Apply CompositeDependencies changes for malilib-extra

* Merge common logic

---------

Signed-off-by: Hendrix-Shen <HendrixShen@hendrixshen.top>
  • Loading branch information
Hendrix-Shen committed Jun 9, 2024
1 parent 2fb2bd6 commit 16d035d
Show file tree
Hide file tree
Showing 35 changed files with 282 additions and 268 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,14 @@
import org.spongepowered.asm.mixin.injection.Coerce;
import org.spongepowered.asm.mixin.injection.Redirect;
import top.hendrixshen.magiclib.api.dependency.DependencyType;
import top.hendrixshen.magiclib.api.dependency.annotation.CompositeDependencies;
import top.hendrixshen.magiclib.api.dependency.annotation.Dependencies;
import top.hendrixshen.magiclib.api.dependency.annotation.Dependency;
import top.hendrixshen.magiclib.impl.dev.MixinPredicates;

/**
* Reference to <a href="https://github.com/Fallen-Breath/tweakermore/blob/10e1a937aadcefb1f2d9d9bab8badc873d4a5b3d/versions/1.16.5/src/main/java/me/fallenbreath/tweakermore/mixins/util/qol/MinecraftClientMixin.java">TweakerMore<a/>
*/
@CompositeDependencies(
@Dependencies(
require = @Dependency(
dependencyType = DependencyType.PREDICATE,
predicate = MixinPredicates.AuthVerifyPredicate.class
)
)
)
@Dependencies(require = @Dependency(dependencyType = DependencyType.PREDICATE, predicate = MixinPredicates.AuthVerifyPredicate.class))
@Environment(EnvType.CLIENT)
@Mixin(Minecraft.class)
public class MinecraftMixin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,14 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import top.hendrixshen.magiclib.api.dependency.DependencyType;
import top.hendrixshen.magiclib.api.dependency.annotation.CompositeDependencies;
import top.hendrixshen.magiclib.api.dependency.annotation.Dependencies;
import top.hendrixshen.magiclib.api.dependency.annotation.Dependency;
import top.hendrixshen.magiclib.impl.dev.MixinPredicates;

/**
* Reference to <a href="https://github.com/magistermaks/mod-fungible/blob/9cd81f1d8ebcef43cff3df279aaef9bb68950e7c/src/main/java/net/darktree/fungible/mixin/chunk_loading/MinecraftServerMixin.java">Fungible<a/>
*/
@CompositeDependencies(
@Dependencies(
require = @Dependency(
dependencyType = DependencyType.PREDICATE,
predicate = MixinPredicates.ChunkPredicate.class
)
)
)
@Dependencies(require = @Dependency(dependencyType = DependencyType.PREDICATE, predicate = MixinPredicates.ChunkPredicate.class))
@Mixin(MinecraftServer.class)
public class MinecraftServerMixin {
@Inject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,14 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import top.hendrixshen.magiclib.api.dependency.DependencyType;
import top.hendrixshen.magiclib.api.dependency.annotation.CompositeDependencies;
import top.hendrixshen.magiclib.api.dependency.annotation.Dependencies;
import top.hendrixshen.magiclib.api.dependency.annotation.Dependency;
import top.hendrixshen.magiclib.impl.dev.MixinPredicates;

/**
* Reference to <a href="https://github.com/magistermaks/mod-fungible/blob/9cd81f1d8ebcef43cff3df279aaef9bb68950e7c/src/main/java/net/darktree/fungible/mixin/dfu/DatafixTypesMixin.java">Fungible<a/>
*/
@CompositeDependencies(
@Dependencies(
require = @Dependency(
dependencyType = DependencyType.PREDICATE,
predicate = MixinPredicates.DestroyDFUPredicate.class
)
)
)
@Dependencies(require = @Dependency(dependencyType = DependencyType.PREDICATE, predicate = MixinPredicates.DestroyDFUPredicate.class))
@Mixin(value = DataFixerUpper.class, remap = false)
public class DataFixerUpperMixin {
@Inject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,11 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import top.hendrixshen.magiclib.api.dependency.DependencyType;
import top.hendrixshen.magiclib.api.dependency.annotation.CompositeDependencies;
import top.hendrixshen.magiclib.api.dependency.annotation.Dependencies;
import top.hendrixshen.magiclib.api.dependency.annotation.Dependency;
import top.hendrixshen.magiclib.impl.dev.MixinPredicates;

@CompositeDependencies(
@Dependencies(
require = @Dependency(
dependencyType = DependencyType.PREDICATE,
predicate = MixinPredicates.DestroyDFUPredicate.class
)
)
)
@Dependencies(require = @Dependency(dependencyType = DependencyType.PREDICATE, predicate = MixinPredicates.DestroyDFUPredicate.class))
@Mixin(value = Schema.class, remap = false)
public class SchemaMixin {
@Inject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import top.hendrixshen.magiclib.api.dependency.DependencyType;
import top.hendrixshen.magiclib.api.dependency.annotation.CompositeDependencies;
import top.hendrixshen.magiclib.api.dependency.annotation.Dependencies;
import top.hendrixshen.magiclib.api.dependency.annotation.Dependency;
import top.hendrixshen.magiclib.impl.dev.MixinPredicates;
Expand All @@ -40,14 +39,7 @@
/**
* Reference to <a href="https://github.com/astei/lazydfu/blob/385764a6fb4cf57b7a39e0ff367a704f74f12497/src/main/java/me/steinborn/lazydfu/mixin/SchemasMixin.java">LazyDFU</a>
*/
@CompositeDependencies(
@Dependencies(
require = @Dependency(
dependencyType = DependencyType.PREDICATE,
predicate = MixinPredicates.LazyDFUPredicate.class
)
)
)
@Dependencies(require = @Dependency(dependencyType = DependencyType.PREDICATE, predicate = MixinPredicates.LazyDFUPredicate.class))
@Mixin(value = DataFixers.class, remap = false)
public class DataFixersMixin {
@Redirect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import top.hendrixshen.magiclib.api.dependency.DependencyType;
import top.hendrixshen.magiclib.api.dependency.annotation.CompositeDependencies;
import top.hendrixshen.magiclib.api.dependency.annotation.Dependencies;
import top.hendrixshen.magiclib.api.dependency.annotation.Dependency;
import top.hendrixshen.magiclib.impl.dev.MixinPredicates;
Expand All @@ -41,14 +40,7 @@
/**
* Reference to <a href="https://github.com/UltimateBoomer/mc-smoothboot/blob/9a519ade89af24aa8b337dfed7d8eb8c0b62ec81/src/main/java/io/github/ultimateboomer/smoothboot/mixin/client/MainMixin.java">SmoothBoot<a/>
*/
@CompositeDependencies(
@Dependencies(
require = @Dependency(
dependencyType = DependencyType.PREDICATE,
predicate = MixinPredicates.TheadTweakPredicate.class
)
)
)
@Dependencies(require = @Dependency(dependencyType = DependencyType.PREDICATE, predicate = MixinPredicates.TheadTweakPredicate.class))
@Environment(EnvType.CLIENT)
@Mixin(Main.class)
public class ClientMainMixin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import top.hendrixshen.magiclib.api.dependency.DependencyType;
import top.hendrixshen.magiclib.api.dependency.annotation.CompositeDependencies;
import top.hendrixshen.magiclib.api.dependency.annotation.Dependencies;
import top.hendrixshen.magiclib.api.dependency.annotation.Dependency;
import top.hendrixshen.magiclib.impl.dev.MixinPredicates;
Expand Down Expand Up @@ -66,14 +65,7 @@
/**
* Reference to <a href="https://github.com/UltimateBoomer/mc-smoothboot/blob/9a519ade89af24aa8b337dfed7d8eb8c0b62ec81/src/main/java/io/github/ultimateboomer/smoothboot/mixin/client/IntegratedServerMixin.java">SmoothBoot<a/>
*/
@CompositeDependencies(
@Dependencies(
require = @Dependency(
dependencyType = DependencyType.PREDICATE,
predicate = MixinPredicates.TheadTweakPredicate.class
)
)
)
@Dependencies(require = @Dependency(dependencyType = DependencyType.PREDICATE, predicate = MixinPredicates.TheadTweakPredicate.class))
@Environment(EnvType.CLIENT)
@Mixin(IntegratedServer.class)
public class IntegratedServerMixin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import top.hendrixshen.magiclib.api.dependency.DependencyType;
import top.hendrixshen.magiclib.api.dependency.annotation.CompositeDependencies;
import top.hendrixshen.magiclib.api.dependency.annotation.Dependencies;
import top.hendrixshen.magiclib.api.dependency.annotation.Dependency;
import top.hendrixshen.magiclib.impl.dev.MixinPredicates;
Expand All @@ -44,14 +43,7 @@
/**
* Reference to <a href="https://github.com/UltimateBoomer/mc-smoothboot/blob/9a519ade89af24aa8b337dfed7d8eb8c0b62ec81/src/main/java/io/github/ultimateboomer/smoothboot/mixin/server/MainMixin.java">SmoothBoot<a/>
*/
@CompositeDependencies(
@Dependencies(
require = @Dependency(
dependencyType = DependencyType.PREDICATE,
predicate = MixinPredicates.TheadTweakPredicate.class
)
)
)
@Dependencies(require = @Dependency(dependencyType = DependencyType.PREDICATE, predicate = MixinPredicates.TheadTweakPredicate.class))
@Mixin(
//#if MC > 11502
Main.class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import top.hendrixshen.magiclib.MagicLib;
import top.hendrixshen.magiclib.api.dependency.DependencyType;
import top.hendrixshen.magiclib.api.dependency.annotation.CompositeDependencies;
import top.hendrixshen.magiclib.api.dependency.annotation.Dependencies;
import top.hendrixshen.magiclib.api.dependency.annotation.Dependency;
import top.hendrixshen.magiclib.impl.dev.MixinPredicates;
Expand All @@ -43,14 +42,7 @@
/**
* Reference to <a href="https://github.com/UltimateBoomer/mc-smoothboot/blob/9a519ade89af24aa8b337dfed7d8eb8c0b62ec81/src/main/java/io/github/ultimateboomer/smoothboot/mixin/UtilMixin.java">SmoothBoot<a/>
*/
@CompositeDependencies(
@Dependencies(
require = @Dependency(
dependencyType = DependencyType.PREDICATE,
predicate = MixinPredicates.TheadTweakPredicate.class
)
)
)
@Dependencies(require = @Dependency(dependencyType = DependencyType.PREDICATE, predicate = MixinPredicates.TheadTweakPredicate.class))
@Mixin(Util.class)
public class UtilMixin {
//#if MC > 11502
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,14 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import top.hendrixshen.magiclib.api.dependency.DependencyType;
import top.hendrixshen.magiclib.api.dependency.annotation.CompositeDependencies;
import top.hendrixshen.magiclib.api.dependency.annotation.Dependencies;
import top.hendrixshen.magiclib.api.dependency.annotation.Dependency;
import top.hendrixshen.magiclib.impl.dev.MixinPredicates;

/**
* Reference to <a href="https://github.com/astei/lazydfu/blob/a83ce2f3e7deb429d48134700424b4454f3bee8b/src/main/java/me/steinborn/lazydfu/mixin/SharedConstantsMixin.java">LazyDFU<a/>
*/
@CompositeDependencies(
@Dependencies(
require = @Dependency(
dependencyType = DependencyType.PREDICATE,
predicate = MixinPredicates.LazyDFUPredicate.class
)
)
)
@Dependencies(require = @Dependency(dependencyType = DependencyType.PREDICATE, predicate = MixinPredicates.LazyDFUPredicate.class))
@Mixin(SharedConstants.class)
public class SharedConstantsMixin {
@Inject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import top.hendrixshen.magiclib.api.dependency.DependencyType;
import top.hendrixshen.magiclib.api.dependency.annotation.CompositeDependencies;
import top.hendrixshen.magiclib.api.dependency.annotation.Dependencies;
import top.hendrixshen.magiclib.api.dependency.annotation.Dependency;
import top.hendrixshen.magiclib.impl.dev.MixinPredicates;
Expand All @@ -39,14 +38,7 @@
/**
* Reference to <a href="https://github.com/magistermaks/mod-fungible/blob/9cd81f1d8ebcef43cff3df279aaef9bb68950e7c/src/main/java/net/darktree/fungible/mixin/auth/ProfileKeysImplMixin.java">mod-fungible</a>
*/
@CompositeDependencies(
@Dependencies(
require = @Dependency(
dependencyType = DependencyType.PREDICATE,
predicate = MixinPredicates.AuthEmptyKeyPredicate.class
)
)
)
@Dependencies(require = @Dependency(dependencyType = DependencyType.PREDICATE, predicate = MixinPredicates.AuthEmptyKeyPredicate.class))
@Environment(EnvType.CLIENT)
@Mixin(AccountProfileKeyPairManager.class)
public class AccountProfileKeyPairManagerMixin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* CompositeDependencies annotation.
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface CompositeDependencies {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package top.hendrixshen.magiclib.api.dependency.annotation;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.*;

/**
* Dependencies annotation.
*/
@Target({ /* No targets allowed */})
@Repeatable(CompositeDependencies.class)
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Dependencies {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
package top.hendrixshen.magiclib.impl.dependency;

import com.google.common.collect.Lists;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;
import org.spongepowered.asm.util.Annotations;
import top.hendrixshen.magiclib.MagicLib;
import top.hendrixshen.magiclib.api.dependency.DependencyCheckException;
import top.hendrixshen.magiclib.api.dependency.annotation.CompositeDependencies;
import top.hendrixshen.magiclib.api.i18n.I18n;
import top.hendrixshen.magiclib.api.platform.DistType;
import top.hendrixshen.magiclib.api.platform.adapter.ModContainerAdapter;
import top.hendrixshen.magiclib.api.platform.adapter.ModMetaDataAdapter;
import top.hendrixshen.magiclib.impl.gui.fabric.FabricGuiEntry;
import top.hendrixshen.magiclib.util.ASMUtil;
import top.hendrixshen.magiclib.util.DependencyUtil;
import top.hendrixshen.magiclib.util.MiscUtil;
import top.hendrixshen.magiclib.util.collect.InfoNode;
import top.hendrixshen.magiclib.util.collect.ValueContainer;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

@ApiStatus.Internal
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class EntryPointDependency {
@Getter(lazy = true)
private static final EntryPointDependency instance = new EntryPointDependency();

private final AtomicBoolean isChecked = new AtomicBoolean();

private EntryPointDependency() {
}

public void check() {
if (this.isChecked.get()) {
throw new IllegalStateException("Re-trigger EntryPointDependency check.");
Expand All @@ -61,7 +57,7 @@ public void check() {
}

private @Nullable DependencyCheckException check(ModMetaDataAdapter modMetaDataAdapter, ClassNode entryPoint) {
List<DependenciesContainer<?>> dependencies = Lists.newArrayList();
List<DependenciesContainer<Object>> dependencies = Lists.newArrayList();

if (MagicLib.getInstance().getCurrentPlatform().getCurrentDistType()
.matches(DistType.CLIENT)) {
Expand Down Expand Up @@ -89,19 +85,9 @@ public void check() {
continue;
}

ValueContainer<AnnotationNode> annotation = ASMUtil.getVisibleAnnotations(method,
CompositeDependencies.class);

if (!annotation.isPresent()) {
continue;
}

return Annotations.<AnnotationNode>getValue(annotation.get(), "value", true)
.stream()
.map(node -> DependenciesContainer.of(node, null))
.collect(Collectors.toList());
return DependencyUtil.parseDependencies(method, null);
}

return Lists.newArrayList();
return Collections.emptyList();
}
}
Loading

0 comments on commit 16d035d

Please sign in to comment.