From b7c2233b9456f8e5d6e4554258357f2405dd1004 Mon Sep 17 00:00:00 2001 From: StartsMercury <89975834+StartsMercury@users.noreply.github.com> Date: Sun, 20 Oct 2024 22:56:24 +0800 Subject: [PATCH] Mixin: Load additional game assets from mods' ./assets/ folder --- .../mixins/assets/GameAssetLoaderMixin.java | 67 +++++++++++++++++++ src/main/resources/fluxapi.mixins.json | 1 + 2 files changed, 68 insertions(+) create mode 100644 src/main/java/dev/crmodders/flux/mixins/assets/GameAssetLoaderMixin.java diff --git a/src/main/java/dev/crmodders/flux/mixins/assets/GameAssetLoaderMixin.java b/src/main/java/dev/crmodders/flux/mixins/assets/GameAssetLoaderMixin.java new file mode 100644 index 0000000..4ebf84e --- /dev/null +++ b/src/main/java/dev/crmodders/flux/mixins/assets/GameAssetLoaderMixin.java @@ -0,0 +1,67 @@ +package dev.crmodders.flux.mixins.assets; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.files.FileHandle; +import com.llamalad7.mixinextras.sugar.Local; +import dev.crmodders.flux.impl.assets.AssetFinder; +import dev.crmodders.flux.util.LoaderKind; +import finalforeach.cosmicreach.GameAssetLoader; +import finalforeach.cosmicreach.util.Identifier; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import java.io.IOException; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.ProviderNotFoundException; +import java.util.HashMap; +import java.util.HashSet; +import java.util.function.BiConsumer; + +@Mixin(GameAssetLoader.class) +public class GameAssetLoaderMixin { + @Shadow @Final private static HashMap ALL_ASSETS; + + @Inject( + method = "forEachAsset(Ljava/lang/String;Ljava/lang/String;Ljava/util/function/BiConsumer;Z)V", + at = @At("RETURN") + ) + private static void forEachIncludeJarMod( + final String prefix, + final String extension, + final BiConsumer assetConsumer, + final boolean includeDirectories, + final CallbackInfo callback, + final @Local(ordinal = 0) HashSet allPaths + ) { + final var subAssets = Identifier.of(prefix).getName(); + + final var finder = new AssetFinder(subAssets, extension, (identifier, path) -> { + final var id = identifier.toString(); + final var handle = Gdx.files.absolute(path.toString()); + ALL_ASSETS.put(id, handle); + assetConsumer.accept(id, handle); + }); + + LoaderKind.getModLocations() + .map(Path::normalize) + .forEach(root -> { + if (Files.isDirectory(root)) { + finder.scan(root); + return; + } + try (final var zfs = FileSystems.newFileSystem(root)) { + finder.scan(zfs.getPath("/")); + } catch (final ProviderNotFoundException cause) { + System.out.println("No file system for " + root + ": " + cause); + } catch (final IOException cause) { + System.out.println("Unable to access file system " + root + ": " + cause); + } + }); + } +} diff --git a/src/main/resources/fluxapi.mixins.json b/src/main/resources/fluxapi.mixins.json index 4547709..76a8f07 100644 --- a/src/main/resources/fluxapi.mixins.json +++ b/src/main/resources/fluxapi.mixins.json @@ -4,6 +4,7 @@ "package": "dev.crmodders.flux.mixins", "compatibilityLevel": "JAVA_17", "mixins": [ + "assets.GameAssetLoaderMixin" ], "client": [], "server": [],