add sprite atlas path hooks, path-specific events (#195)

This commit is contained in:
Adrian Siekierka 2019-05-24 10:24:01 +02:00 committed by GitHub
parent 200eb5c2ac
commit d36c5fcf8e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 183 additions and 45 deletions

View file

@ -1,5 +1,5 @@
archivesBaseName = "fabric-textures-v0" archivesBaseName = "fabric-textures-v0"
version = getSubprojectVersion(project, "0.1.0") version = getSubprojectVersion(project, "0.1.1")
dependencies { dependencies {
compile project(path: ':fabric-api-base', configuration: 'dev') compile project(path: ':fabric-api-base', configuration: 'dev')

View file

@ -17,32 +17,43 @@
package net.fabricmc.fabric.api.event.client; package net.fabricmc.fabric.api.event.client;
import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory; import net.fabricmc.fabric.impl.client.texture.SpriteRegistryCallbackHolder;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.texture.Sprite; import net.minecraft.client.texture.Sprite;
import net.minecraft.client.texture.SpriteAtlasTexture; import net.minecraft.client.texture.SpriteAtlasTexture;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
public interface ClientSpriteRegistryCallback { public interface ClientSpriteRegistryCallback {
public static final Event<ClientSpriteRegistryCallback> EVENT = EventFactory.createArrayBacked(ClientSpriteRegistryCallback.class, /**
(listeners) -> (atlasTexture, registry) -> { * @deprecated Use the {@link ClientSpriteRegistryCallback#event(Identifier)} registration method. Since 1.14
for (ClientSpriteRegistryCallback callback : listeners) { * started making use of multiple sprite atlases, it is unwise to register sprites to *all* of them.
callback.registerSprites(atlasTexture, registry); */
} @Deprecated
} public static final Event<ClientSpriteRegistryCallback> EVENT = SpriteRegistryCallbackHolder.EVENT_GLOBAL;
);
void registerSprites(SpriteAtlasTexture atlasTexture, Registry registry); void registerSprites(SpriteAtlasTexture atlasTexture, Registry registry);
static void registerBlockAtlas(ClientSpriteRegistryCallback callback) { /**
EVENT.register((atlasTexture, registry) -> { * Get an event instance for a given atlas path.
if (atlasTexture == MinecraftClient.getInstance().getSpriteAtlas()) { *
callback.registerSprites(atlasTexture, registry); * @param atlasId The atlas texture ID you want to register to.
* @return The event for a given atlas path.
*
* @since 0.1.1
*/
static Event<ClientSpriteRegistryCallback> event(Identifier atlasId) {
return SpriteRegistryCallbackHolder.eventLocal(atlasId);
} }
});
/**
* @deprecated Use the {@link ClientSpriteRegistryCallback#event(Identifier)} registration method.
*/
@Deprecated
static void registerBlockAtlas(ClientSpriteRegistryCallback callback) {
event(SpriteAtlasTexture.BLOCK_ATLAS_TEX).register(callback);
} }
public static class Registry { public static class Registry {
@ -69,7 +80,7 @@ public interface ClientSpriteRegistryCallback {
* @param sprite The sprite to be added. * @param sprite The sprite to be added.
*/ */
public void register(Sprite sprite) { public void register(Sprite sprite) {
this.spriteMap.put(sprite.getId(), sprite); spriteMap.put(sprite.getId(), sprite);
} }
} }
} }

View file

@ -0,0 +1,23 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.impl.client.texture;
import net.minecraft.util.Identifier;
public interface SpriteAtlasTextureHooks {
void onRegisteredAs(Identifier id);
}

View file

@ -0,0 +1,48 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.impl.client.texture;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.fabricmc.fabric.api.event.client.ClientSpriteRegistryCallback;
import net.minecraft.util.Identifier;
import java.util.HashMap;
import java.util.Map;
public final class SpriteRegistryCallbackHolder {
public static final Event<ClientSpriteRegistryCallback> EVENT_GLOBAL = createEvent();
private static final Map<Identifier, Event<ClientSpriteRegistryCallback>> eventMap = new HashMap<>();
private SpriteRegistryCallbackHolder() {
}
public static Event<ClientSpriteRegistryCallback> eventLocal(Identifier key) {
return eventMap.computeIfAbsent(key, (a) -> createEvent());
}
private static Event<ClientSpriteRegistryCallback> createEvent() {
return EventFactory.createArrayBacked(ClientSpriteRegistryCallback.class,
(listeners) -> (atlasTexture, registry) -> {
for (ClientSpriteRegistryCallback callback : listeners) {
callback.registerSprites(atlasTexture, registry);
}
}
);
}
}

View file

@ -20,6 +20,8 @@ import com.google.common.base.Joiner;
import net.fabricmc.fabric.api.client.texture.*; import net.fabricmc.fabric.api.client.texture.*;
import net.fabricmc.fabric.api.event.client.ClientSpriteRegistryCallback; import net.fabricmc.fabric.api.event.client.ClientSpriteRegistryCallback;
import net.fabricmc.fabric.impl.client.texture.FabricSprite; import net.fabricmc.fabric.impl.client.texture.FabricSprite;
import net.fabricmc.fabric.impl.client.texture.SpriteAtlasTextureHooks;
import net.fabricmc.fabric.impl.client.texture.SpriteRegistryCallbackHolder;
import net.minecraft.client.resource.metadata.AnimationResourceMetadata; import net.minecraft.client.resource.metadata.AnimationResourceMetadata;
import net.minecraft.client.texture.Sprite; import net.minecraft.client.texture.Sprite;
import net.minecraft.client.texture.SpriteAtlasTexture; import net.minecraft.client.texture.SpriteAtlasTexture;
@ -42,7 +44,7 @@ import java.io.IOException;
import java.util.*; import java.util.*;
@Mixin(SpriteAtlasTexture.class) @Mixin(SpriteAtlasTexture.class)
public abstract class MixinSpriteAtlasTexture { public abstract class MixinSpriteAtlasTexture implements SpriteAtlasTextureHooks {
@Shadow @Shadow
private static Logger LOGGER; private static Logger LOGGER;
@Shadow @Shadow
@ -51,33 +53,38 @@ public abstract class MixinSpriteAtlasTexture {
@Shadow @Shadow
public abstract Sprite getSprite(Identifier id); public abstract Sprite getSprite(Identifier id);
private final Set<Identifier> fabric_localIds = new HashSet<>();
// EVENT/HOOKS LOGIC
@Override
public void onRegisteredAs(Identifier id) {
fabric_localIds.add(id);
}
// INJECTION LOGIC
private Map<Identifier, Sprite> fabric_injectedSprites; private Map<Identifier, Sprite> fabric_injectedSprites;
/** // Loads in custom sprite object injections.
* The purpose of this patch is to allow injecting sprites at the stage of Sprite instantiation, such as @Inject(at = @At("RETURN"), method = "loadSprites")
* Sprites with CustomSpriteLoaders. private void afterLoadSprites(ResourceManager resourceManager_1, Set<Identifier> set_1, CallbackInfoReturnable<Collection<Sprite>> info) {
* <p> if (fabric_injectedSprites != null) {
* FabricSprite is a red herring. It's only use to go around Sprite's constructors being protected. info.getReturnValue().addAll(fabric_injectedSprites.values());
* <p> fabric_injectedSprites = null;
* method_18160 is a lambda used in runAsync.
*/
@SuppressWarnings("JavaDoc")
@Redirect(method = "method_18160", at = @At(value = "NEW", target = "net/minecraft/client/texture/Sprite"))
public Sprite newSprite(Identifier id, PngFile pngFile, AnimationResourceMetadata animationMetadata) {
if (fabric_injectedSprites.containsKey(id)) {
return fabric_injectedSprites.get(id);
} else {
return new FabricSprite(id, pngFile, animationMetadata);
} }
} }
// Handles DependentSprite + custom sprite object injections.
@ModifyVariable(at = @At(value = "INVOKE", target = "Lnet/minecraft/client/texture/SpriteAtlasTexture;loadSprites(Lnet/minecraft/resource/ResourceManager;Ljava/util/Set;)Ljava/util/Collection;"), method = "stitch") @ModifyVariable(at = @At(value = "INVOKE", target = "Lnet/minecraft/client/texture/SpriteAtlasTexture;loadSprites(Lnet/minecraft/resource/ResourceManager;Ljava/util/Set;)Ljava/util/Collection;"), method = "stitch")
public Set<Identifier> setHook(Set<Identifier> set) { public Set<Identifier> beforeSpriteLoad(Set<Identifier> set) {
fabric_injectedSprites = new HashMap<>(); fabric_injectedSprites = new HashMap<>();
ClientSpriteRegistryCallback.Registry registry = new ClientSpriteRegistryCallback.Registry(fabric_injectedSprites, set::add); ClientSpriteRegistryCallback.Registry registry = new ClientSpriteRegistryCallback.Registry(fabric_injectedSprites, set::add);
//noinspection ConstantConditions //noinspection ConstantConditions
ClientSpriteRegistryCallback.EVENT.invoker().registerSprites((SpriteAtlasTexture) (Object) this, registry); for (Identifier id : fabric_localIds) {
SpriteRegistryCallbackHolder.eventLocal(id).invoker().registerSprites((SpriteAtlasTexture) (Object) this, registry);
}
SpriteRegistryCallbackHolder.EVENT_GLOBAL.invoker().registerSprites((SpriteAtlasTexture) (Object) this, registry);
// TODO: Unoptimized. // TODO: Unoptimized.
Set<DependentSprite> dependentSprites = new HashSet<>(); Set<DependentSprite> dependentSprites = new HashSet<>();
@ -90,8 +97,14 @@ public abstract class MixinSpriteAtlasTexture {
} }
} }
Set<Identifier> result = set;
boolean isResultNew = false;
if (!dependentSprites.isEmpty()) { if (!dependentSprites.isEmpty()) {
Set<Identifier> result = new LinkedHashSet<>(); if (!isResultNew) {
result = new LinkedHashSet<>();
isResultNew = true;
}
for (Identifier id : set) { for (Identifier id : set) {
if (!dependentSpriteIds.contains(id)) { if (!dependentSpriteIds.contains(id)) {
@ -123,13 +136,23 @@ public abstract class MixinSpriteAtlasTexture {
} }
throw new CrashException(report); throw new CrashException(report);
} }
}
if (!fabric_injectedSprites.isEmpty()) {
if (!isResultNew) {
result = new LinkedHashSet<>(set);
isResultNew = true;
}
result.removeAll(fabric_injectedSprites.keySet());
}
return result; return result;
} else {
return set;
}
} }
/**
* Handles CustomSpriteLoader.
*/
@Inject(at = @At("HEAD"), method = "loadSprite", cancellable = true) @Inject(at = @At("HEAD"), method = "loadSprite", cancellable = true)
public void loadSprite(ResourceManager manager, Sprite sprite, CallbackInfoReturnable<Boolean> info) { public void loadSprite(ResourceManager manager, Sprite sprite, CallbackInfoReturnable<Boolean> info) {
// refer SpriteAtlasTexture.loadSprite // refer SpriteAtlasTexture.loadSprite
@ -137,24 +160,20 @@ public abstract class MixinSpriteAtlasTexture {
try { try {
if (!((CustomSpriteLoader) sprite).load(manager, mipLevel)) { if (!((CustomSpriteLoader) sprite).load(manager, mipLevel)) {
info.setReturnValue(false); info.setReturnValue(false);
info.cancel();
return; return;
} }
} catch (RuntimeException | IOException e) { } catch (RuntimeException | IOException e) {
LOGGER.error("Unable to load custom sprite {}: {}", sprite.getId(), e); LOGGER.error("Unable to load custom sprite {}: {}", sprite.getId(), e);
info.setReturnValue(false); info.setReturnValue(false);
info.cancel();
return; return;
} }
try { try {
sprite.generateMipmaps(this.mipLevel); sprite.generateMipmaps(this.mipLevel);
info.setReturnValue(true); info.setReturnValue(true);
info.cancel();
} catch (Throwable e) { } catch (Throwable e) {
LOGGER.error("Unable to apply mipmap to custom sprite {}: {}", sprite.getId(), e); LOGGER.error("Unable to apply mipmap to custom sprite {}: {}", sprite.getId(), e);
info.setReturnValue(false); info.setReturnValue(false);
info.cancel();
} }
} }
} }

View file

@ -0,0 +1,36 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.mixin.client.texture;
import net.fabricmc.fabric.impl.client.texture.SpriteAtlasTextureHooks;
import net.minecraft.client.texture.Texture;
import net.minecraft.client.texture.TextureManager;
import net.minecraft.util.Identifier;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(TextureManager.class)
public class MixinTextureManager {
@Inject(at = @At("RETURN"), method = "registerTexture")
private void afterRegisterTexture(Identifier identifier, Texture texture, CallbackInfoReturnable<Boolean> info) {
if (texture instanceof SpriteAtlasTextureHooks) {
((SpriteAtlasTextureHooks) texture).onRegisteredAs(identifier);
}
}
}

View file

@ -3,7 +3,8 @@
"package": "net.fabricmc.fabric.mixin.client.texture", "package": "net.fabricmc.fabric.mixin.client.texture",
"compatibilityLevel": "JAVA_8", "compatibilityLevel": "JAVA_8",
"client": [ "client": [
"MixinSpriteAtlasTexture" "MixinSpriteAtlasTexture",
"MixinTextureManager"
], ],
"injectors": { "injectors": {
"defaultRequire": 1 "defaultRequire": 1