|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2023-Present Bawnorton |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package com.railwayteam.railways.util; |
| 26 | + |
| 27 | +import com.railwayteam.railways.annotation.ConditionalMixin; |
| 28 | +import com.railwayteam.railways.compat.Mods; |
| 29 | +import com.railwayteam.railways.mixin.CRMixinPlugin; |
| 30 | +import org.objectweb.asm.Type; |
| 31 | +import org.objectweb.asm.tree.AnnotationNode; |
| 32 | +import org.spongepowered.asm.service.MixinService; |
| 33 | +import org.spongepowered.asm.util.Annotations; |
| 34 | + |
| 35 | +import java.io.IOException; |
| 36 | +import java.util.List; |
| 37 | + |
| 38 | +public class ConditionalMixinManager { |
| 39 | + public static boolean shouldApply(String className) { |
| 40 | + try { |
| 41 | + List<AnnotationNode> annotationNodes = MixinService.getService().getBytecodeProvider().getClassNode(className).visibleAnnotations; |
| 42 | + if (annotationNodes == null) return true; |
| 43 | + |
| 44 | + boolean shouldApply = true; |
| 45 | + for (AnnotationNode node : annotationNodes) { |
| 46 | + if (node.desc.equals(Type.getDescriptor(ConditionalMixin.class))) { |
| 47 | + List<Mods> mods = Annotations.getValue(node, "mods", true, Mods.class); |
| 48 | + boolean applyIfPresent = Annotations.getValue(node, "applyIfPresent", Boolean.TRUE); |
| 49 | + boolean anyModsLoaded = anyModsLoaded(mods); |
| 50 | + shouldApply = anyModsLoaded == applyIfPresent; |
| 51 | + CRMixinPlugin.LOGGER.info("{} is{}being applied because the mod(s) {} are{}loaded", className, shouldApply ? " " : " not ", mods, anyModsLoaded ? " " : " not "); |
| 52 | + } |
| 53 | + } |
| 54 | + return shouldApply; |
| 55 | + } catch (ClassNotFoundException | IOException e) { |
| 56 | + throw new RuntimeException(e); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private static boolean anyModsLoaded(List<Mods> mods) { |
| 61 | + for (Mods mod : mods) { |
| 62 | + if (mod.isLoaded) return true; |
| 63 | + } |
| 64 | + return false; |
| 65 | + } |
| 66 | +} |
0 commit comments