Skip to content

Commit

Permalink
Add ResourceResult
Browse files Browse the repository at this point in the history
  • Loading branch information
FirstMegaGame4 committed Dec 26, 2023
1 parent 7fefe83 commit 91e5e15
Show file tree
Hide file tree
Showing 14 changed files with 358 additions and 34 deletions.
43 changes: 43 additions & 0 deletions src/main/java/fr/firstmegagame4/env/json/impl/EnvJsonUtils.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package fr.firstmegagame4.env.json.impl;

import fr.firstmegagame4.env.json.api.EnvJson;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.resource.InputSupplier;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.ApiStatus;

import java.io.IOException;
import java.io.InputStream;

@ApiStatus.Internal
public class EnvJsonUtils {

public static final EnvJson ENV_JSON_NONE = null;

public static final InputSupplier<EnvJson> ENV_JSON_NONE_SUPPLIER = () -> EnvJsonUtils.ENV_JSON_NONE;

private EnvJsonUtils() {
throw new RuntimeException("Class EnvJsonUtils only contains static definitions");
}
Expand All @@ -21,4 +30,38 @@ public static <T> TagKey<T> tryParse(RegistryKey<? extends Registry<T>> registry
throw new IllegalArgumentException("Not a valid tag representation");
}
}

public static boolean isEnvJson(Identifier identifier) {
return identifier.getPath().endsWith(".env.json");
}

public static String getFileExtension(Identifier identifier) {
return identifier.getPath().substring(identifier.getPath().lastIndexOf("."));
}

public static Identifier getEnvJsonFileName(Identifier identifier) {
return identifier.withPath(identifier.getPath().substring(0, identifier.getPath().length() - ".env.json".length()));
}

public static Identifier getEnvJsonPath(Identifier identifier) {
String path;
if (identifier.getPath().contains(".")) {
path = identifier.getPath().substring(0, identifier.getPath().lastIndexOf("."));
}
else {
path = identifier.getPath();
}
return identifier.withPath(path + ".env.json");
}

public static InputSupplier<EnvJson> getEnvJsonSupplier(InputSupplier<InputStream> supplier) {
return () -> EnvJsonUtils.loadEnvJson(supplier);
}

public static EnvJson loadEnvJson(InputSupplier<InputStream> supplier) throws IOException {
InputStream inputStream = supplier.get();
EnvJson envJson = EnvJson.parse(inputStream);
inputStream.close();
return envJson;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package fr.firstmegagame4.env.json.impl.resource;

import net.minecraft.resource.InputSupplier;
import net.minecraft.resource.ResourcePack;
import net.minecraft.util.Identifier;

import java.io.InputStream;
import java.util.Map;

public interface EntryListDuckInterface {

Identifier env_json$getEnvJsonIdentifier();

Map<ResourcePack, InputSupplier<InputStream>> env_json$getEnvJsonResources();

void env_json$putEnvJsonSource(ResourcePack resourcePack, InputSupplier<InputStream> inputSupplier);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ public class ExtendedResourceImpl extends Resource implements ExtendedResource {
public static ExtendedResource of(Resource resource) {
ResourceAccessor accessor = (ResourceAccessor) resource;
ResourceDuckInterface ducked = (ResourceDuckInterface) resource;
return new ExtendedResourceImpl(accessor.getPack(), accessor.getInputSupplier(), accessor.getMetadataSupplier(), ducked.env_json$getEnvJsonSupplier(), accessor.getMetadata(), ducked.env_json$getEnvJson());
return new ExtendedResourceImpl(accessor.getPack(), accessor.getInputSupplier(), accessor.getMetadataSupplier(), ducked.env_json$getEnvJsonSupplier(), accessor.getRawMetadata(), ducked.env_json$getEnvJson());
}

private ExtendedResourceImpl(ResourcePack pack, InputSupplier<InputStream> inputSupplier, InputSupplier<ResourceMetadata> metadataSupplier, InputSupplier<EnvJson> envJsonSupplier, ResourceMetadata resourceMetadata, EnvJson envJson) {
super(pack, inputSupplier, metadataSupplier);
((ResourceAccessor) this).setRawMetaData(resourceMetadata);
ResourceDuckInterface ducked = (ResourceDuckInterface) this;
ducked.env_json$initEnvJsonSupplier(envJsonSupplier);
ducked.env_json$initEnvJson(envJson);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fr.firstmegagame4.env.json.impl.resource;

import net.minecraft.resource.InputSupplier;
import net.minecraft.resource.ResourcePack;
import org.jetbrains.annotations.ApiStatus;

import java.io.InputStream;

@ApiStatus.Internal
public record ResourceResult(ResourcePack pack, InputSupplier<InputStream> supplier, int packIndex) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package fr.firstmegagame4.env.json.impl.resource;

import net.minecraft.resource.InputSupplier;
import net.minecraft.resource.ResourcePack;
import org.jetbrains.annotations.ApiStatus;

import java.io.InputStream;

@ApiStatus.Internal
public interface ResultAccess {

ResourcePack invokePack();

InputSupplier<InputStream> invokeSupplier();

int invokePackIndex();
}
54 changes: 54 additions & 0 deletions src/main/java/fr/firstmegagame4/env/json/mixin/EntryListMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package fr.firstmegagame4.env.json.mixin;

import fr.firstmegagame4.env.json.impl.EnvJsonUtils;
import fr.firstmegagame4.env.json.impl.resource.EntryListDuckInterface;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import net.minecraft.resource.InputSupplier;
import net.minecraft.resource.NamespaceResourceManager;
import net.minecraft.resource.ResourcePack;
import net.minecraft.util.Identifier;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Unique;
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.InputStream;
import java.util.Map;

@Mixin(NamespaceResourceManager.EntryList.class)
public class EntryListMixin implements EntryListDuckInterface {

@Unique
@Final
@Mutable
private Identifier envJsonIdentifier;

@Unique
@Final
@Mutable
private Map<ResourcePack, InputSupplier<InputStream>> envJsonSources;

@Inject(method = "<init>(Lnet/minecraft/util/Identifier;)V", at = @At("TAIL"))
private void envJsonInit(Identifier id, CallbackInfo ci) {
this.envJsonIdentifier = EnvJsonUtils.getEnvJsonPath(id);
this.envJsonSources = new Object2ObjectArrayMap<>();
}

@Override
public Identifier env_json$getEnvJsonIdentifier() {
return this.envJsonIdentifier;
}

@Override
public Map<ResourcePack, InputSupplier<InputStream>> env_json$getEnvJsonResources() {
return this.envJsonSources;
}

@Override
public void env_json$putEnvJsonSource(ResourcePack resourcePack, InputSupplier<InputStream> inputSupplier) {
this.envJsonSources.put(resourcePack, inputSupplier);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package fr.firstmegagame4.env.json.mixin;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Share;
import com.llamalad7.mixinextras.sugar.ref.LocalRef;
import fr.firstmegagame4.env.json.api.EnvJson;
import fr.firstmegagame4.env.json.impl.EnvJsonUtils;
import fr.firstmegagame4.env.json.impl.resource.ResourceDuckInterface;
import net.fabricmc.fabric.impl.resource.loader.GroupResourcePack;
import net.minecraft.resource.InputSupplier;
import net.minecraft.resource.ResourcePack;
import net.minecraft.resource.ResourceType;
import net.minecraft.util.Identifier;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;

import java.io.InputStream;

// I know I shouldn't use an impl class, but I have no choice
// Should try to load it with QSL when this one will be available for 1.20.4
@SuppressWarnings("UnstableApiUsage")
@Mixin(value = GroupResourcePack.class, remap = false)
public class GroupResourcePackMixin {

@WrapOperation(method = "appendResources", at = @At(value = "INVOKE", target = "Lnet/minecraft/resource/ResourcePack;open(Lnet/minecraft/resource/ResourceType;Lnet/minecraft/util/Identifier;)Lnet/minecraft/resource/InputSupplier;"))
private InputSupplier<InputStream> onResourceAdd(ResourcePack instance, ResourceType resourceType, Identifier identifier, Operation<InputSupplier<InputStream>> original, @Share("envJsonInputSupplier") LocalRef<InputSupplier<EnvJson>> ref) {
InputSupplier<InputStream> inputSupplier = original.call(instance, resourceType, identifier);
Identifier envJsonPath = EnvJsonUtils.getEnvJsonPath(identifier);
if (inputSupplier != null) {
InputSupplier<EnvJson> envJsonInputSupplier = () -> {
InputSupplier<InputStream> supplier = instance.open(resourceType, envJsonPath);
return supplier != null ? EnvJsonUtils.loadEnvJson(supplier) : EnvJsonUtils.ENV_JSON_NONE;
};
ref.set(envJsonInputSupplier);
}
return inputSupplier;
}

@ModifyArg(method = "appendResources", at = @At(value = "INVOKE", target = "Ljava/util/List;add(Ljava/lang/Object;)Z"))
private <E> E onResourceCreated(E e, @Share("envJsonInputSupplier") LocalRef<InputSupplier<EnvJson>> ref) {
((ResourceDuckInterface) e).env_json$initEnvJsonSupplier(ref.get());
return e;
}
}
Loading

0 comments on commit 91e5e15

Please sign in to comment.