Files
FalsePatternLib/src/main/java/com/falsepattern/lib/mixin/MixinInfo.java
T

136 lines
4.7 KiB
Java
Raw Normal View History

/*
* Copyright (C) 2022 FalsePattern
* All Rights Reserved
*
* The above copyright notice, this permission notice and the word "SNEED"
* shall be included in all copies or substantial portions of the Software.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2022-08-21 10:14:46 +02:00
package com.falsepattern.lib.mixin;
import com.falsepattern.lib.StableAPI;
2023-01-04 01:20:24 +01:00
import lombok.val;
2022-08-21 10:14:46 +02:00
import org.spongepowered.asm.launch.MixinBootstrap;
2023-01-04 01:20:24 +01:00
import net.minecraft.launchwrapper.Launch;
import java.io.IOException;
2022-08-21 10:14:46 +02:00
import java.util.Optional;
@StableAPI(since = "0.10.2")
public final class MixinInfo {
@StableAPI.Expose
2023-02-11 11:14:04 +01:00
public static final MixinBootstrapperType mixinBootstrapper;
static {
MixinInfoCompatCompanion.mixinInfoClassLoaded = true;
mixinBootstrapper = detect();
}
2022-08-21 10:14:46 +02:00
@StableAPI.Expose
public static boolean isMixinsInstalled() {
return mixinBootstrapper != MixinBootstrapperType.None;
}
@StableAPI.Expose
public static boolean isOfficialSpongeMixins() {
return (mixinBootstrapper == MixinBootstrapperType.SpongeMixins) && getVerUnsafe().equals("0.7.11");
}
/**
* The nh fork of spongemixins breaks some stuff since 1.5.0.
*/
@StableAPI.Expose
public static boolean isTamperedSpongeMixins() {
return (mixinBootstrapper == MixinBootstrapperType.SpongeMixins) && getVerUnsafe().equals("0.7.12");
}
@StableAPI.Expose
public static boolean isGrimoire() {
return mixinBootstrapper == MixinBootstrapperType.Grimoire;
}
@StableAPI.Expose
public static boolean isMixinBooterLegacy() {
return mixinBootstrapper == MixinBootstrapperType.MixinBooterLegacy;
}
2023-01-04 01:20:24 +01:00
@StableAPI.Expose(since = "0.10.14")
public static boolean isGasStation() {
return mixinBootstrapper == MixinBootstrapperType.GasStation;
}
2023-02-11 11:14:04 +01:00
@StableAPI.Expose(since = "0.10.15")
public static boolean isUniMixin() {
return mixinBootstrapper == MixinBootstrapperType.UniMixin;
}
2022-08-21 10:14:46 +02:00
@StableAPI.Expose
public static MixinBootstrapperType bootstrapperType() {
return mixinBootstrapper;
}
@StableAPI.Expose
public static Optional<String> mixinVersion() {
return mixinBootstrapper != MixinBootstrapperType.None ? Optional.of(getVerUnsafe()) : Optional.empty();
}
private static String getVerUnsafe() {
return MixinBootstrap.VERSION;
}
2023-01-04 01:20:24 +01:00
public static boolean isClassPresentSafe(String clazz) {
2022-08-21 10:14:46 +02:00
try {
2023-01-04 01:20:24 +01:00
val bytes = Launch.classLoader.getClassBytes(clazz);
if (bytes == null || bytes.length == 0) {
return false;
}
} catch (IOException e) {
return false;
2022-08-21 10:14:46 +02:00
}
2023-01-04 01:20:24 +01:00
return true;
}
private static MixinBootstrapperType detect() {
if (!isClassPresentSafe("org.spongepowered.asm.launch.MixinBootstrap"))
return MixinBootstrapperType.None;
2023-02-11 11:14:04 +01:00
for (val candidate: MixinInfoCompatCompanion.UNIMIXIN_CANDIDATES) {
if (isClassPresentSafe(candidate)) {
return MixinBootstrapperType.UniMixin;
}
}
2023-01-04 01:20:24 +01:00
if (isClassPresentSafe("com.falsepattern.gasstation.GasStation"))
2022-09-04 10:24:57 +02:00
return MixinBootstrapperType.GasStation;
2023-01-04 01:20:24 +01:00
if (isClassPresentSafe("ru.timeconqueror.spongemixins.core.SpongeMixinsCore"))
2022-08-21 10:14:46 +02:00
return MixinBootstrapperType.SpongeMixins;
2023-01-04 01:20:24 +01:00
if (isClassPresentSafe("io.github.crucible.grimoire.Grimoire") || isClassPresentSafe("io.github.crucible.grimoire.common.GrimoireCore"))
2022-08-21 10:14:46 +02:00
return MixinBootstrapperType.Grimoire;
2023-01-04 01:20:24 +01:00
if (isClassPresentSafe("io.github.tox1cozz.mixinbooterlegacy.MixinBooterLegacyPlugin"))
2022-08-21 10:14:46 +02:00
return MixinBootstrapperType.MixinBooterLegacy;
return MixinBootstrapperType.Other;
}
@StableAPI(since = "0.10.2")
public enum MixinBootstrapperType {
2022-08-21 10:20:40 +02:00
@StableAPI.Expose None,
2022-09-04 10:24:57 +02:00
@StableAPI.Expose(since = "0.10.9") GasStation,
2022-08-21 10:20:40 +02:00
@StableAPI.Expose SpongeMixins,
@StableAPI.Expose Grimoire,
@StableAPI.Expose MixinBooterLegacy,
2023-02-11 11:14:04 +01:00
@StableAPI.Expose Other,
@StableAPI.Expose(since = "0.10.15") UniMixin
2022-08-21 10:14:46 +02:00
}
}