Skip to content

Commit

Permalink
Re-add more testing and fix stuff; seemingly functional on fabric
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebemish committed Apr 25, 2024
1 parent a293c4d commit d7e1a63
Show file tree
Hide file tree
Showing 25 changed files with 120 additions and 106 deletions.
17 changes: 1 addition & 16 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import org.groovymc.modsdotgroovy.core.Platform

plugins {
id 'maven-publish'
Expand Down Expand Up @@ -133,8 +132,6 @@ modsDotGroovy {
}
}

import org.groovymc.modsdotgroovy.core.Platform

sourceSets.neoforge.modsDotGroovy {
platform = Platform.NEOFORGE
inferGather.set false
Expand Down Expand Up @@ -242,18 +239,6 @@ publishing {
}
}

processNeoforgeResources {
doLast {
fileTree(dir: outputs.files.asPath, include: "mixin.*.json").each { File file ->
def obj = new JsonSlurper().parse(file)
if (obj['refmap']) {
obj.remove('refmap')
}
file.text = JsonOutput.toJson(obj)
}
}
}

tasks.register('combinedJavadoc', Javadoc) {
source sourceSets.main.allJava
source sourceSets.fabric.allJava
Expand Down
13 changes: 13 additions & 0 deletions fabric/run/resourcepacks/test/assets/minecraft/atlases/blocks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"sources": [
{
"type": "dynamic_asset_generator:tex_sources",
"sources": {
"minecraft:block/clay": {
"type" : "dynamic_asset_generator:texture",
"path" : "minecraft:block/magma"
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type" : "dynamic_asset_generator:texture",
"output_location" : "minecraft:item/gold_ingot",
"input" : {
"type" : "dynamic_asset_generator:texture",
"path" : "minecraft:item/apple"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type" : "dynamic_asset_generator:texture",
"output_location" : "minecraft:block/gravel",
"input" : {
"type" : "dynamic_asset_generator:shadowed",
"background": {
"type" : "dynamic_asset_generator:texture",
"path" : "minecraft:block/stone"
},
"foreground": {
"type" : "dynamic_asset_generator:texture",
"path" : "test:block/gravel"
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions fabric/run/resourcepacks/test/pack.mcmeta
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"description": "Dynamic Asset Generator - Test",
"pack_format": 18
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"sources": [
{
"type": "dynamic_asset_generator:tex_sources",
"sources": {
"minecraft:block/clay": {
"type" : "dynamic_asset_generator:texture",
"path" : "minecraft:block/magma"
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type" : "dynamic_asset_generator:texture",
"output_location" : "minecraft:item/gold_ingot",
"input" : {
"type" : "dynamic_asset_generator:texture",
"path" : "minecraft:item/apple"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type" : "dynamic_asset_generator:texture",
"output_location" : "minecraft:block/gravel",
"input" : {
"type" : "dynamic_asset_generator:shadowed",
"background": {
"type" : "dynamic_asset_generator:texture",
"path" : "minecraft:block/stone"
},
"foreground": {
"type" : "dynamic_asset_generator:texture",
"path" : "test:block/gravel"
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions neoforge/run/resourcepacks/test/pack.mcmeta
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"description": "Dynamic Asset Generator - Test",
"pack_format": 18
}
}
5 changes: 1 addition & 4 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@ pluginManagement {
url "https://maven.architectury.dev/"
}

// TODO: remove
mavenLocal()

mavenCentral()
gradlePluginPortal()
}
}

plugins {
id 'dev.lukebemish.conventions' version '0.1.9'
id 'dev.lukebemish.multisource' version '0.1.2-dirty'
id 'dev.lukebemish.multisource' version '0.1.4'
}

rootProject.name = 'dynamicassetgenerator'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
public class PlatformClientImpl implements PlatformClient {
@Override
public void addSpriteSource(ResourceLocation location, MapCodec<? extends SpriteSource> codec) {
SpriteSourcesAccessor.dynamicassetgenerator$invokeRegister(location.toString(), codec);
SpriteSourcesAccessor.dynamic_asset_generator$invokeRegister(location.toString(), codec);
}
}
Original file line number Diff line number Diff line change
@@ -1,56 +1,18 @@
package dev.lukebemish.dynamicassetgenerator.impl.fabriquilt.fabric;

import dev.lukebemish.dynamicassetgenerator.impl.DynamicAssetGenerator;
import dev.lukebemish.dynamicassetgenerator.impl.fabriquilt.FabriQuiltShared;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.server.packs.PackResources;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.repository.RepositorySource;
import org.jspecify.annotations.Nullable;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.nio.file.Path;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;

public class FabricPlatform implements FabriQuiltShared {
public static final FabriQuiltShared INSTANCE = new FabricPlatform();

private static final String GROUP_PACK_CLASS = "net.fabricmc.fabric.impl.resource.loader.GroupResourcePack";
private static final @Nullable Class<?> GROUP_PACK_RESOURCES;
private static final @Nullable MethodHandle GET_GROUP_PACK_PACKS;

static {
Class<?> clazz;
try {
clazz = FabricPlatform.class.getClassLoader().loadClass(GROUP_PACK_CLASS);
} catch (ClassNotFoundException e) {
clazz = null;
}
if (clazz == null) {
GROUP_PACK_RESOURCES = null;
GET_GROUP_PACK_PACKS = null;
} else {
GROUP_PACK_RESOURCES = clazz;
var lookup = MethodHandles.lookup();
MethodHandle getter;
try {
var privateLookup = MethodHandles.privateLookupIn(clazz, lookup);
@SuppressWarnings("rawtypes") Class<List> listClazz = List.class;
getter = privateLookup.findGetter(clazz, "packs", listClazz);
} catch (IllegalAccessException | NoSuchFieldException e) {
getter = null;
}
GET_GROUP_PACK_PACKS = getter;
}
if (GROUP_PACK_RESOURCES == null || GET_GROUP_PACK_PACKS == null) {
if (!FabricLoader.getInstance().isModLoaded("quilted_fabric_api")) {
DynamicAssetGenerator.LOGGER.warn("On normal fabric API but could not find fabric internal class/field to unwrap grouped resources - If you are using a recent fabric API version, this may be ignored");
}
}
}

@Override
public void packForType(PackType type, RepositorySource source) {
PackPlanner.forType(type).register(source);
Expand All @@ -76,32 +38,8 @@ public Path cacheDir() {
return FabricLoader.getInstance().getGameDir().resolve(".cache");
}

private static final boolean[] LOGGED_ERROR = new boolean[1];

private synchronized void logError(@SuppressWarnings("SameParameterValue") int i) {
if (!LOGGED_ERROR[i]) {
if (i == 0) {
DynamicAssetGenerator.LOGGER.error("Found fabric-api delegating pack but could not properly use fabric internal class/field to unwrap grouped resources - Dynamic Asset Generator may not work right!");
}
LOGGED_ERROR[i] = true;
}
}

@Override
public Stream<PackResources> unpackPacks(Stream<? extends PackResources> packs) {
return packs.flatMap(pack -> {
if (GROUP_PACK_RESOURCES != null && GET_GROUP_PACK_PACKS != null) {
if (GROUP_PACK_RESOURCES.isInstance(pack)) {
try {
@SuppressWarnings({"unchecked", "rawtypes"}) List<? extends PackResources> unpackedPacks =
(List<? extends PackResources>) (List) GET_GROUP_PACK_PACKS.invoke(pack);
return Stream.concat(Stream.of(pack), unpackPacks(unpackedPacks.stream()));
} catch (Throwable e) {
logError(0);
}
}
}
return Stream.of(pack);
});
return packs.map(Function.identity());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@Mixin(BuiltInPackSource.class)
public class BuiltInPackSourceMixin {
@Inject(method = "listBundledPacks", at = @At("RETURN"))
@Inject(method = "listBundledPacks(Ljava/util/function/Consumer;)V", at = @At("RETURN"))
private void addAdditionalPacks(Consumer<Pack> profileAdder, CallbackInfo ci) {
//noinspection ConstantValue
if (((Object) this) instanceof ServerPacksSource) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@Mixin(BuiltInPackSource.class)
public class ClientBuiltInPackSourceMixin {
@Inject(method = "listBundledPacks", at = @At("RETURN"))
@Inject(method = "listBundledPacks(Ljava/util/function/Consumer;)V", at = @At("RETURN"))
private void addAdditionalPacks(Consumer<Pack> profileAdder, CallbackInfo ci) {
//noinspection ConstantValue
if (((Object) this) instanceof ClientPackSource) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
public class PackSelectionScreenMixin {
@ModifyVariable(method = "updateList", at = @At("HEAD"), ordinal = 0, argsOnly = true)
private Stream<PackSelectionModel.Entry> dynamic_asset_generator$hidePacks(Stream<PackSelectionModel.Entry> pModels) {
return pModels.filter(e -> !e.getTitle().getString().startsWith(DynamicAssetGenerator.MOD_ID+"/"));
return pModels.filter(e -> !e.getId().startsWith(DynamicAssetGenerator.MOD_ID+"/"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@
"server": [],
"injectors": {
"defaultRequire": 1
},
"refmap": "dynamic_asset_generator.refmap.json"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public final boolean allowAccess(String packId) {
return true;
}
String remainder = packId.substring(prefix.length());
@Nullable ResourceLocation targetName = ResourceLocation.read(remainder).result().orElse(null);
ResourceLocation targetName = ResourceLocation.read(remainder).result().orElse(null);
return targetName == null || getDependencies().contains(targetName);
}

Expand Down Expand Up @@ -94,14 +94,22 @@ public Map<ResourceLocation, IoSupplier<InputStream>> getResources() {
if (DynamicAssetGenerator.TIME_RESOURCES) {
rls.forEach(rl -> {
long startTime = System.nanoTime();
outputs.put(rl, ResourceUtils.wrapSafeData(rl, source, makeContext(false)));
var supplier = ResourceUtils.wrapSafeData(rl, source, makeContext(false));
if (supplier != null) {
outputs.put(rl, supplier);
}
long endTime = System.nanoTime();

long duration = (endTime - startTime)/1000;
Timing.recordPartialTime(this.getName().toString(), rl, duration);
});
} else {
rls.forEach(rl -> outputs.put(rl, ResourceUtils.wrapSafeData(rl, source, makeContext(false))));
rls.forEach(rl -> {
var supplier = ResourceUtils.wrapSafeData(rl, source, makeContext(false));
if (supplier != null) {
outputs.put(rl, supplier);
}
});
}
} catch (Throwable e) {
DynamicAssetGenerator.LOGGER.error("Issue setting up PathAwareInputStreamSource:",e);
Expand All @@ -111,7 +119,7 @@ public Map<ResourceLocation, IoSupplier<InputStream>> getResources() {
return outputs;
}

private ResourceGenerationContext.ResourceSource filteredSource = null;
private ResourceGenerationContext.@Nullable ResourceSource filteredSource = null;

/**
* Creates a context for generating resources within this cache.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public void run(ResourceManager resourceManager, Output output) {

@Override
public @NonNull SpriteSourceType type() {
return SpriteSourcesAccessor.dynamicassetgenerator$getTypes().get(source.getLocation());
return SpriteSourcesAccessor.dynamic_asset_generator$getTypes().get(source.getLocation());
}

public T unwrap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.IoSupplier;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;

import java.io.IOException;
import java.util.Base64;
Expand Down Expand Up @@ -45,7 +46,9 @@ public IoSupplier<NativeImage> getSupplier(TexSourceDataHolder data, ResourceGen
}
data.getLogger().error("Texture not available in context: {}", this.getPath());
} catch (IOException e) {
data.getLogger().error("Issue loading texture: {}", this.getPath());
var message = "Issue loading texture: " + this.getPath();
data.getLogger().error(message);
throw new IOException(message, e);
}
throw new IOException("Issue loading texture: " + this.getPath());
};
Expand All @@ -72,7 +75,7 @@ public ResourceLocation getPath() {
}

public static class Builder {
private ResourceLocation path;
private @Nullable ResourceLocation path;

/**
* Sets the path to the texture to read, excluding the {@code "textures/"} prefix and {@code ".png"} file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
public interface SpriteSourcesAccessor {
@SuppressWarnings("UnusedReturnValue")
@Invoker("register")
static SpriteSourceType dynamicassetgenerator$invokeRegister(String pName, MapCodec<? extends SpriteSource> mapCodec) {
static SpriteSourceType dynamic_asset_generator$invokeRegister(String pName, MapCodec<? extends SpriteSource> mapCodec) {
throw new AssertionError("Mixin failed to apply");
}

@Accessor("TYPES")
static BiMap<ResourceLocation, SpriteSourceType> dynamicassetgenerator$getTypes() {
static BiMap<ResourceLocation, SpriteSourceType> dynamic_asset_generator$getTypes() {
throw new AssertionError("Mixin failed to apply");
}
}
Loading

0 comments on commit d7e1a63

Please sign in to comment.