Skip to content

Commit

Permalink
Mixin: Load additional game assets from mods' ./assets/ folder
Browse files Browse the repository at this point in the history
  • Loading branch information
StartsMercury committed Oct 20, 2024
1 parent f77ee78 commit b7c2233
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<String, FileHandle> 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<String, FileHandle> assetConsumer,
final boolean includeDirectories,
final CallbackInfo callback,
final @Local(ordinal = 0) HashSet<Identifier> 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);
}
});
}
}
1 change: 1 addition & 0 deletions src/main/resources/fluxapi.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"package": "dev.crmodders.flux.mixins",
"compatibilityLevel": "JAVA_17",
"mixins": [
"assets.GameAssetLoaderMixin"
],
"client": [],
"server": [],
Expand Down

0 comments on commit b7c2233

Please sign in to comment.