|
| 1 | +package dev.upcraft.sparkweave.neoforge.impl.client; |
| 2 | + |
| 3 | +import dev.upcraft.sparkweave.SparkweaveMod; |
| 4 | +import dev.upcraft.sparkweave.api.logging.SparkweaveLoggerFactory; |
| 5 | +import net.neoforged.api.distmarker.Dist; |
| 6 | +import net.neoforged.bus.api.IEventBus; |
| 7 | +import net.neoforged.fml.ModContainer; |
| 8 | +import net.neoforged.fml.ModLoader; |
| 9 | +import net.neoforged.fml.ModLoadingIssue; |
| 10 | +import net.neoforged.fml.common.Mod; |
| 11 | +import net.neoforged.fml.loading.FMLPaths; |
| 12 | +import net.neoforged.fml.loading.progress.StartupNotificationManager; |
| 13 | +import org.apache.logging.log4j.Logger; |
| 14 | + |
| 15 | +import javax.annotation.Nullable; |
| 16 | +import java.io.FileNotFoundException; |
| 17 | +import java.io.IOException; |
| 18 | +import java.lang.invoke.MethodHandles; |
| 19 | +import java.lang.invoke.MethodType; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.StandardCopyOption; |
| 22 | +import java.util.List; |
| 23 | +import java.util.jar.Attributes; |
| 24 | +import java.util.jar.JarInputStream; |
| 25 | + |
| 26 | +@Mod(value = SparkweaveMod.MODID, dist = Dist.CLIENT) |
| 27 | +public class LaunchBootstrap { |
| 28 | + |
| 29 | + private static final Logger LOGGER = SparkweaveLoggerFactory.getLogger(); |
| 30 | + |
| 31 | + private static final Boolean LOAD_RENDERDOC = Boolean.getBoolean("sparkweave.debug.render.load_renderdoc"); |
| 32 | + |
| 33 | + public LaunchBootstrap(ModContainer modContainer, IEventBus modBus) throws IOException { |
| 34 | + var currentModVersion = modContainer.getModInfo().getVersion().toString(); |
| 35 | + if(LOAD_RENDERDOC) { |
| 36 | + var pb = StartupNotificationManager.prependProgressBar("Extracting RenderDoc", 2); |
| 37 | + |
| 38 | + var helperClass = getClass("dev.upcraft.sparkweave.renderdoc.client.RenderDocHelper"); |
| 39 | + if(helperClass != null) { |
| 40 | + try { |
| 41 | + var isLoadedMethod = MethodHandles.publicLookup().findStatic(helperClass, "isLoaded", MethodType.methodType(boolean.class)); |
| 42 | + if((boolean) isLoadedMethod.invokeExact()) { |
| 43 | + LOGGER.info("RenderDoc already loaded, skipping jar extraction!"); |
| 44 | + pb.complete(); |
| 45 | + return; |
| 46 | + } |
| 47 | + } catch (Throwable e) { |
| 48 | + throw new RuntimeException("Reflection error trying to check RenderDocHelper API", e); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + LOGGER.debug("Locating RenderDoc loader Jar file..."); |
| 53 | + var modsDir = FMLPaths.MODSDIR.get(); |
| 54 | + var jarName = "%s-renderdoc-loader.jar".formatted(SparkweaveMod.MODID); |
| 55 | + var jarFile = modsDir.resolve(jarName); |
| 56 | + if(Files.exists(jarFile)) { |
| 57 | + try(var stream = new JarInputStream(Files.newInputStream(jarFile))) { |
| 58 | + var foundVersion = stream.getManifest().getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_VERSION); |
| 59 | + LOGGER.debug("Found existing loader file, version {}", foundVersion); |
| 60 | + if(foundVersion.equals(currentModVersion)) { |
| 61 | + LOGGER.info("Version matches, continuing launch!"); |
| 62 | + pb.complete(); |
| 63 | + return; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + pb.increment(); |
| 68 | + |
| 69 | + var internalJarName = "META-INF/jarjar/%s-renderdoc-loader-%s.jar".formatted(SparkweaveMod.MODID, currentModVersion); |
| 70 | + var internalJarFile = modContainer.getModInfo().getOwningFile().getFile().findResource(internalJarName); |
| 71 | + if(!Files.exists(internalJarFile)) { |
| 72 | + throw new FileNotFoundException("Unable to find %s in mod resources!".formatted(internalJarName)); |
| 73 | + } |
| 74 | + LOGGER.debug("Extracting new file..."); |
| 75 | + Files.copy(internalJarFile, jarFile, StandardCopyOption.REPLACE_EXISTING); |
| 76 | + pb.increment(); |
| 77 | + |
| 78 | + pb.label("RenderDoc loader file extracted."); |
| 79 | + // TODO make translatable once NEO fixes this issue |
| 80 | + //noinspection UnstableApiUsage |
| 81 | + ModLoader.addLoadingIssue(new ModLoadingIssue(ModLoadingIssue.Severity.ERROR, "RenderDoc loader successfully extracted, please restart your game!", List.of())); |
| 82 | + |
| 83 | + StartupNotificationManager.addModMessage("Successfully extracted RenderDoc loader, waiting for restart."); |
| 84 | + pb.complete(); |
| 85 | + |
| 86 | +// throw new ModLoadingException(new ModLoadingIssue(ModLoadingIssue.Severity.ERROR, "RenderDoc loader successfully extracted, please restart your game!", List.of())); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @SuppressWarnings("unchecked") |
| 91 | + @Nullable |
| 92 | + private static <T> Class<T> getClass(String name) { |
| 93 | + try { |
| 94 | + return (Class<T>) Class.forName(name); |
| 95 | + } catch (ClassNotFoundException e) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments