-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose API for dynamic sprite sources
This does not currently compile on forge. Why? IDK. I broke the mixin annotation processor somehow.
- Loading branch information
1 parent
25f9ee7
commit 1291dfe
Showing
15 changed files
with
256 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...ain/java/dev/lukebemish/dynamicassetgenerator/impl/client/BuiltinDynamicSpriteSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (C) 2023 Luke Bemish and contributors | ||
* SPDX-License-Identifier: LGPL-3.0-or-later | ||
*/ | ||
|
||
package dev.lukebemish.dynamicassetgenerator.impl.client; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.JsonOps; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import dev.lukebemish.dynamicassetgenerator.api.ResourceGenerationContext; | ||
import dev.lukebemish.dynamicassetgenerator.api.client.DynamicSpriteSource; | ||
import dev.lukebemish.dynamicassetgenerator.api.client.generators.TexSource; | ||
import dev.lukebemish.dynamicassetgenerator.impl.DynamicAssetGenerator; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.server.packs.resources.ResourceManager; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public record BuiltinDynamicSpriteSource(Map<ResourceLocation, TexSource> sources, @Nullable ResourceLocation location) implements DynamicSpriteSource { | ||
public static final ResourceLocation LOCATION = new ResourceLocation(DynamicAssetGenerator.MOD_ID, "tex_sources"); | ||
public static Codec<BuiltinDynamicSpriteSource> CODEC = RecordCodecBuilder.create(i -> i.group( | ||
Codec.unboundedMap(ResourceLocation.CODEC, TexSource.CODEC).fieldOf("sources").forGetter(BuiltinDynamicSpriteSource::sources), | ||
ResourceLocation.CODEC.optionalFieldOf("location").forGetter(s -> Optional.ofNullable(s.location())) | ||
).apply(i, (sources, location) -> new BuiltinDynamicSpriteSource(sources, location.orElse(null)))); | ||
|
||
@Override | ||
public Map<ResourceLocation, TexSource> getSources(ResourceGenerationContext context, ResourceManager resourceManager) { | ||
Map<ResourceLocation, TexSource> outSources = new HashMap<>(sources()); | ||
if (location != null) { | ||
resourceManager.listResources(location.getNamespace() + "/" + location.getPath(), rl -> rl.getPath().endsWith(".json")).forEach((rl, resource) -> { | ||
try (var reader = resource.openAsReader()) { | ||
JsonElement json = DynamicAssetGenerator.GSON.fromJson(reader, JsonElement.class); | ||
var result = TexSource.CODEC.parse(JsonOps.INSTANCE, json); | ||
result.result().ifPresent(texSource -> { | ||
ResourceLocation sourceLocation = new ResourceLocation(rl.getNamespace(), rl.getPath().substring(0, rl.getPath().length() - 5)); | ||
outSources.put(sourceLocation, texSource); | ||
}); | ||
result.error().ifPresent(partial -> | ||
DynamicAssetGenerator.LOGGER.error("Failed to load tex source json for " + location + ": " + rl + ": " + partial.message())); | ||
} catch (IOException e) { | ||
DynamicAssetGenerator.LOGGER.error("Failed to load tex source json for " + location + ": " + rl, e); | ||
} | ||
}); | ||
} | ||
return outSources; | ||
} | ||
|
||
@Override | ||
public ResourceLocation getLocation() { | ||
return LOCATION; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
Common/src/main/java/dev/lukebemish/dynamicassetgenerator/impl/client/ExposesName.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package dev.lukebemish.dynamicassetgenerator.impl.client; | ||
|
||
import net.minecraft.resources.ResourceLocation; | ||
|
||
public interface ExposesName { | ||
ResourceLocation dynamicassetgenerator$getName(); | ||
} |
7 changes: 7 additions & 0 deletions
7
...c/main/java/dev/lukebemish/dynamicassetgenerator/impl/client/platform/ClientServices.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package dev.lukebemish.dynamicassetgenerator.impl.client.platform; | ||
|
||
import dev.lukebemish.dynamicassetgenerator.impl.platform.Services; | ||
|
||
public class ClientServices { | ||
public static final PlatformClient PLATFORM_CLIENT = Services.load(PlatformClient.class); | ||
} |
9 changes: 9 additions & 0 deletions
9
...c/main/java/dev/lukebemish/dynamicassetgenerator/impl/client/platform/PlatformClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package dev.lukebemish.dynamicassetgenerator.impl.client.platform; | ||
|
||
import com.mojang.serialization.Codec; | ||
import net.minecraft.client.renderer.texture.atlas.SpriteSource; | ||
import net.minecraft.resources.ResourceLocation; | ||
|
||
public interface PlatformClient { | ||
void addSpriteSource(ResourceLocation location, Codec<? extends SpriteSource> codec); | ||
} |
4 changes: 4 additions & 0 deletions
4
...src/main/java/dev/lukebemish/dynamicassetgenerator/impl/client/platform/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@ApiStatus.Internal | ||
package dev.lukebemish.dynamicassetgenerator.impl.client.platform; | ||
|
||
import org.jetbrains.annotations.ApiStatus; |
28 changes: 28 additions & 0 deletions
28
...n/src/main/java/dev/lukebemish/dynamicassetgenerator/mixin/SpriteResourceLoaderMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package dev.lukebemish.dynamicassetgenerator.mixin; | ||
|
||
import dev.lukebemish.dynamicassetgenerator.impl.client.ExposesName; | ||
import net.minecraft.client.renderer.texture.atlas.SpriteResourceLoader; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.server.packs.resources.ResourceManager; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
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.CallbackInfoReturnable; | ||
|
||
@Mixin(SpriteResourceLoader.class) | ||
public class SpriteResourceLoaderMixin implements ExposesName { | ||
@Unique | ||
private ResourceLocation name; | ||
|
||
@SuppressWarnings("DataFlowIssue") | ||
@Inject(method = "load", at = @At("RETURN")) | ||
private static void dynamicassetgenerator$load(ResourceManager pResourceManager, ResourceLocation pLocation, CallbackInfoReturnable<SpriteResourceLoader> cir) { | ||
((SpriteResourceLoaderMixin) (Object) cir.getReturnValue()).name = pLocation; | ||
} | ||
|
||
@Override | ||
public ResourceLocation dynamicassetgenerator$getName() { | ||
return name; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...main/java/dev/lukebemish/dynamicassetgenerator/mixin/SpriteResourceLoaderOutputMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package dev.lukebemish.dynamicassetgenerator.mixin; | ||
|
||
import dev.lukebemish.dynamicassetgenerator.impl.client.ExposesName; | ||
import net.minecraft.client.renderer.texture.atlas.SpriteResourceLoader; | ||
import net.minecraft.resources.ResourceLocation; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
|
||
@Mixin(targets = "net.minecraft.client.renderer.texture.atlas.SpriteResourceLoader$1") | ||
public class SpriteResourceLoaderOutputMixin implements ExposesName { | ||
|
||
@Shadow(aliases = {"field_41390", "f_260614_"}) | ||
@Final | ||
private SpriteResourceLoader this$0; | ||
|
||
@Override | ||
public ResourceLocation dynamicassetgenerator$getName() { | ||
return ((ExposesName) this$0).dynamicassetgenerator$getName(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
Forge/src/main/java/dev/lukebemish/dynamicassetgenerator/forge/PlatformClientImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package dev.lukebemish.dynamicassetgenerator.forge; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.mojang.datafixers.util.Pair; | ||
import com.mojang.serialization.Codec; | ||
import dev.lukebemish.dynamicassetgenerator.impl.client.platform.PlatformClient; | ||
import dev.lukebemish.dynamicassetgenerator.mixin.SpriteSourcesAccessor; | ||
import net.minecraft.client.renderer.texture.atlas.SpriteSource; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraftforge.client.event.RegisterClientReloadListenersEvent; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@AutoService(PlatformClient.class) | ||
public class PlatformClientImpl implements PlatformClient { | ||
private static final List<Pair<ResourceLocation, Codec<? extends SpriteSource>>> SPRITE_SOURCE_QUEUE = new ArrayList<>(); | ||
private static boolean SPRITE_SOURCES_REGISTERED = false; | ||
|
||
|
||
public void addSpriteSource(ResourceLocation location, Codec<? extends SpriteSource> codec) { | ||
if (SPRITE_SOURCES_REGISTERED) { | ||
throw new IllegalStateException("Sprite sources have already been registered. Try registering yours during mod initialization!"); | ||
} | ||
SPRITE_SOURCE_QUEUE.add(Pair.of(location, codec)); | ||
} | ||
|
||
public static void reloadListenerListener(RegisterClientReloadListenersEvent event) { | ||
if (SPRITE_SOURCES_REGISTERED) return; | ||
SPRITE_SOURCES_REGISTERED = true; | ||
for (var pair : SPRITE_SOURCE_QUEUE) { | ||
SpriteSourcesAccessor.invokeRegister(pair.getFirst().toString(), pair.getSecond()); | ||
} | ||
} | ||
} |
Oops, something went wrong.