Skip to content

Commit

Permalink
Remove <1.21 code and add PNG metadata validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
IMB11 committed Dec 17, 2024
1 parent a132663 commit b0ddf61
Show file tree
Hide file tree
Showing 32 changed files with 55 additions and 295 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ dependencies {
include implementation("org.mineskin:java-client:1.2.4-SNAPSHOT")
include implementation("commons-validator:commons-validator:1.7")

include implementation("com.drewnoakes:metadata-extractor:2.19.0")

modRuntimeOnly("me.djtheredstoner:DevAuth-fabric:1.2.0")
}

Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ plugins {

stonecutter {
create(rootProject) {
versions '1.21.3', '1.21', '1.20.1'
versions '1.21.3', '1.21'
vcsVersion = '1.21.3'
}
}
25 changes: 14 additions & 11 deletions src/main/java/com/mineblock11/skinshuffle/SkinShuffle.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
package com.mineblock11.skinshuffle;

import com.mineblock11.skinshuffle.compat.CompatLoader;
import com.mineblock11.skinshuffle.networking.HandshakePayload;
import com.mineblock11.skinshuffle.networking.RefreshPlayerListEntryPayload;
import com.mineblock11.skinshuffle.networking.ServerSkinHandling;
import com.mineblock11.skinshuffle.networking.SkinRefreshPayload;
import com.mineblock11.skinshuffle.util.SkinCacheRegistry;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.util.Identifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -34,20 +39,18 @@ public class SkinShuffle implements ModInitializer {

@Override
public void onInitialize() {
/*? if >=1.20.5 {*/
net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry.playC2S().register(
com.mineblock11.skinshuffle.networking.SkinRefreshPayload.PACKET_ID,
com.mineblock11.skinshuffle.networking.SkinRefreshPayload.PACKET_CODEC
PayloadTypeRegistry.playC2S().register(
SkinRefreshPayload.PACKET_ID,
SkinRefreshPayload.PACKET_CODEC
);
net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry.playS2C().register(
com.mineblock11.skinshuffle.networking.HandshakePayload.PACKET_ID,
net.minecraft.network.codec.PacketCodec.unit(com.mineblock11.skinshuffle.networking.HandshakePayload.INSTANCE)
PayloadTypeRegistry.playS2C().register(
HandshakePayload.PACKET_ID,
PacketCodec.unit(HandshakePayload.INSTANCE)
);
net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry.playS2C().register(
com.mineblock11.skinshuffle.networking.RefreshPlayerListEntryPayload.PACKET_ID,
com.mineblock11.skinshuffle.networking.RefreshPlayerListEntryPayload.PACKET_CODEC
PayloadTypeRegistry.playS2C().register(
RefreshPlayerListEntryPayload.PACKET_ID,
RefreshPlayerListEntryPayload.PACKET_CODEC
);
/*?}*/

ensureDataDir();
SkinCacheRegistry.initialize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@ public static void savePresets() {
JsonArray array = new JsonArray();
for (SkinPreset loadedPreset : loadedPresets) {
DataResult<JsonElement> dataResult = SkinPreset.CODEC.encodeStart(JsonOps.INSTANCE, loadedPreset);
/*? <1.20.5 {*/
/*array.add(dataResult.getOrThrow(false, SkinShuffle.LOGGER::error));
*//*?} else {*/
array.add(dataResult.result().orElseThrow(() -> new RuntimeException("Failed to encode skin preset.")));
/*?}*/
}
presetFile.add("loadedPresets", array);

Expand Down Expand Up @@ -130,11 +126,7 @@ public static void loadPresets() {
JsonArray array = presetFile.get("loadedPresets").getAsJsonArray();
for (JsonElement jsonElement : array) {
DataResult<Pair<SkinPreset, JsonElement>> dataResult = SkinPreset.CODEC.decode(JsonOps.INSTANCE, jsonElement);
/*? <1.20.5 {*/
/*Pair<SkinPreset, JsonElement> pair = dataResult.getOrThrow(false, SkinShuffle.LOGGER::error);
*//*?} else {*/
Pair<SkinPreset, JsonElement> pair = dataResult.result().orElseThrow(() -> new RuntimeException("Failed to decode skin preset."));
/*?}*/
SkinPreset preset = pair.getFirst();
loadedPresets.add(preset);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@
import java.util.Optional;

public abstract class CarouselScreen extends SpruceScreen {
//? if >=1.21.2 {
public static final double scaleFactor = 1.0D;
//?}

public final Screen parent;
public final CarouselView viewType;
public final CarouselView nextViewType;
Expand Down Expand Up @@ -174,11 +171,7 @@ public void render(DrawContext graphics, int mouseX, int mouseY, float delta) {
var cardAreaWidth = getCardWidth() + getCardGap();

// BG stuff
/*? if <1.20.4 {*/
/*this.renderBackground(graphics);
*//*?} else {*/
this.renderBackground(graphics, mouseX, mouseY, delta);
/*?}*/

graphics.fill(0, this.textRenderer.fontHeight * 3, this.width, this.height - (this.textRenderer.fontHeight * 3), 0x7F000000);
graphics.fillGradient(0, (int) (this.textRenderer.fontHeight * 2.75), this.width, this.textRenderer.fontHeight * 3, 0x00000000, 0x7F000000);
Expand Down Expand Up @@ -288,13 +281,8 @@ public void snapCarousel() {
setCardIndex(cardIndex);
}

/*? if <1.20.4 {*/
/*@Override
public boolean mouseScrolled(double mouseX, double mouseY, double verticalAmount) {
*//*?} else {*/
@Override
public boolean mouseScrolled(double mouseX, double mouseY, double hozAmount, double verticalAmount) {
/*?}*/
var sign = SkinShuffleConfig.get().invertCarouselScroll ? -1 : 1;
scrollCarousel(-verticalAmount / 4 * SkinShuffleConfig.get().carouselScrollSensitivity * sign, false);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ public static ArrayList<ClickableWidget> createCarouselWidgets(Screen screen) {
int x = screen.width / 2 + 104 + 25;

if(screen instanceof GameMenuScreen gameMenuScreen) {
//? <1.21 {
/*if(!gameMenuScreen.showMenu)
*///?} else {
if(!gameMenuScreen.shouldShowMenu())
//?}
return new ArrayList<>();

if(!SkinShuffleConfig.get().displayInPauseMenu) return widgets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

package com.mineblock11.skinshuffle.client.gui;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.png.PngMetadataReader;
import com.drew.metadata.Metadata;
import com.mineblock11.skinshuffle.SkinShuffle;
import com.mineblock11.skinshuffle.client.SkinShuffleClient;import com.mineblock11.skinshuffle.client.config.SkinPresetManager;
import com.mineblock11.skinshuffle.client.config.SkinShuffleConfig;
Expand All @@ -34,6 +37,7 @@
import net.minecraft.screen.ScreenTexts;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.PngMetadata;
import net.minecraft.util.Util;
import org.apache.commons.validator.routines.UrlValidator;

Expand Down Expand Up @@ -170,9 +174,23 @@ private boolean isValidPngFilePath(String pathStr) {

// Check if the file exists, follows symlinks, and is a regular file
if (Files.exists(path) && Files.isRegularFile(path)) {
// Check if the file has a .png extension (case insensitive)
// Check if the file has a .png extension (case-insensitive)
String fileName = path.getFileName().toString().toLowerCase();
return fileName.endsWith(".png");

if (!fileName.endsWith(".png")) {
return false;
}

// Validate the png file's metadata.
try {
PngMetadata metadata = PngMetadata.fromStream(Files.newInputStream(path));

// Width must be 64x64 or 64x32, and must be a png file.
int width = metadata.width(), height = metadata.height();
return (width == 64 && (height == 64 || height == 32));
} catch (Exception ignored) {
return false;
}
}

return false;
Expand Down Expand Up @@ -208,15 +226,9 @@ public boolean validate() {
}
}
case RESOURCE_LOCATION -> {
/*? if <1.21 {*/
/*if (Identifier.isValid(widget.getText())) {
return client.getResourceManager().getResource(new Identifier(widget.getText())).isPresent();
} else return false;
*//*?} else {*/
if (Identifier.validate(widget.getText()).isSuccess()) {
return client.getResourceManager().getResource(Identifier.tryParse(widget.getText())).isPresent();
} else return false;
/*?}*/
}
case USERNAME -> {
return isValidUsername(widget.getText());
Expand Down Expand Up @@ -306,13 +318,6 @@ public void render(DrawContext graphics, int mouseX, int mouseY, float delta) {
this.exitButton.active = !this.preset.equals(this.originalPreset);
}

/*? if <1.20.6 {*/
/*public void renderBackgroundTexture(DrawContext context) {
// If we don't explicitly have this, the background color will be slightly off from the tab color.
context.drawTexture(net.minecraft.client.gui.screen.world.CreateWorldScreen.LIGHT_DIRT_BACKGROUND_TEXTURE, 0, 0, 0, 0.0F, 0.0F, this.width, this.height, 32, 32);
}
*//*?}*/

private float getEntityRotation() {
return (float) GlfwUtil.getTime() * 35f;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,13 @@ public static void drawEntity(MatrixStack matrices, int x, int y, int size, floa
}

private static void setupModelViewStack() {
//? if >=1.20.5 {
Matrix4fStack modelViewStack = RenderSystem.getModelViewStack();
modelViewStack.pushMatrix();
modelViewStack.translate(0.0f, 0.0f, 1000.0f);

//? if <1.21.2 {
/*RenderSystem.applyModelViewMatrix();
*///?}

//?} else {
/*MatrixStack modelViewStack = RenderSystem.getModelViewStack();
modelViewStack.push();
modelViewStack.translate(0.0, 0.0, 1000.0);
RenderSystem.applyModelViewMatrix();
*///?}
}

private static void setupMatrices(MatrixStack matrices, int x, int y, int size, Quaternionf entityRotation) {
Expand Down Expand Up @@ -103,20 +96,10 @@ private static void renderEntity(MatrixStack matrices, float yaw, float pitch, S
vertexConsumers.getBuffer(RenderLayer.getEntityTranslucent(skin.getTexture())),
LightmapTextureManager.MAX_LIGHT_COORDINATE,
OverlayTexture.DEFAULT_UV,
//? if >=1.21 {
0xFFFFFFFF
//?} else {
/*1f, 1f, 1f, 1f
*///?}
);

//? if =1.20.1 {
/*MinecraftClient client = MinecraftClient.getInstance();
var hasCape = client.getSkinProvider().getTextures(client.getSession().getProfile()).containsKey(MinecraftProfileTexture.Type.CAPE);
if (hasCape && SkinShuffleConfig.get().showCapeInPreview) {
*///?} else {
if (skin.getSkinTextures().capeTexture() != null && SkinShuffleConfig.get().showCapeInPreview) {
//?}
matrices.push();
matrices.translate(0.0F, 0.0F, 0.2F);

Expand Down Expand Up @@ -146,19 +129,12 @@ private static void cleanupMatrices(MatrixStack matrices) {
}

private static void cleanupModelViewStack() {
//? if >=1.20.5 {
Matrix4fStack modelViewStack = RenderSystem.getModelViewStack();
modelViewStack.popMatrix();

//? if <1.21.2 {
/*RenderSystem.applyModelViewMatrix();
*///?}

//?} else {
/*MatrixStack modelViewStack = RenderSystem.getModelViewStack();
modelViewStack.pop();
RenderSystem.applyModelViewMatrix();
*///?}
}

public static class NoEntityPlayerModel extends PlayerEntityModel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,9 @@ public IconButtonWidget(int x, int y, int width, int height, int iconU, int icon
this.iconHeight = iconHeight;
}

/*? if <1.20.4 {*/
/*@Override
public void renderButton(DrawContext context, int mouseX, int mouseY, float delta) {
super.renderButton(context, mouseX, mouseY, delta);
*//*?} else {*/
@Override
protected void renderWidget(DrawContext context, int mouseX, int mouseY, float delta) {
super.renderWidget(context, mouseX, mouseY, delta);
/*?}*/

// Function<Identifier, RenderLayer> renderLayers, Identifier sprite, int x, int y, float u, float v, int width, int height, int textureWidth, int textureHeight, int color
context.drawTexture(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,10 @@ private float getEntityRotation() {
return (float) ((GlfwUtil.getTime() - currentTime) * 35.0f);
}

/*? if <1.20.4 {*/
/*@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta);
*//*?} else {*/

@Override
public void renderWidget(DrawContext context, int mouseX, int mouseY, float delta) {
super.renderWidget(context, mouseX, mouseY, delta);
/*?}*/

if (selectedPreset != null) {
// Don't want to render the entity if the skin is still loading
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,9 @@ public WarningIndicatorButton(int x, int y, Screen parent) {
client.isInSingleplayer() ? I18n.translate("skinshuffle.reconnect.rejoin") : I18n.translate("skinshuffle.reconnect.reconnect"))).formatted(Formatting.RED, Formatting.BOLD)));
}

/*? if <1.20.4 {*/
/*@Override
public void renderButton(DrawContext context, int mouseX, int mouseY, float delta) {
super.renderButton(context, mouseX, mouseY, delta);
*//*?} else {*/@Override
@Override
public void renderWidget(DrawContext context, int mouseX, int mouseY, float delta) {
super.renderWidget(context, mouseX, mouseY, delta);
/*?}*/

context.drawTexture(
//? >=1.21.2 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@
import net.minecraft.client.MinecraftClient;
import net.minecraft.util.Identifier;

/*? if <1.20.4 {*/
/*import net.minecraft.client.util.Session;
*//*?} else {*/
import net.minecraft.client.util.SkinTextures;
import net.minecraft.client.session.Session;
/*?}*/

public class SkinPreset {
public static final Codec<SkinPreset> CODEC = RecordCodecBuilder.create(instance ->
Expand All @@ -55,25 +51,6 @@ public static SkinPreset generateDefaultPreset() {
Session session = client.getSession();
String name = session.getUsername();

/*? if <1.20.4 {*/
/*if(!NetworkingUtil.isLoggedIn()) {
Identifier skinTexture = client.getSkinProvider().loadSkin(session.getProfile());
Skin skin = new ResourceSkin(skinTexture, skinTexture.getPath().contains("/slim/") ? "slim" : "classic");
return new SkinPreset(skin, name);
} else {
var skinQueryResult = SkinAPIs.getPlayerSkinTexture(session.getUuid());
if(skinQueryResult.usesDefaultSkin()) {
Identifier skinTexture = client.getSkinProvider().loadSkin(session.getProfile());
Skin skin = new ResourceSkin(skinTexture, skinTexture.getPath().contains("/slim/") ? "slim" : "classic");
return new SkinPreset(skin, name);
}
return new SkinPreset(new UrlSkin(skinQueryResult.skinURL(), skinQueryResult.modelType()), name);
}
*//*?} else {*/
if(!NetworkingUtil.isLoggedIn()) {
Skin skin = new ResourceSkin(Identifier.of("minecraft:textures/entity/player/wide/steve.png"), "default");
return new SkinPreset(skin, name);
Expand All @@ -89,7 +66,6 @@ public static SkinPreset generateDefaultPreset() {

return new SkinPreset(new UrlSkin(skinQueryResult.skinURL(), skinQueryResult.modelType()), name);
}
/*?}*/
}

public Skin getSkin() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import java.util.concurrent.CompletableFuture;

public abstract class BackedSkin implements Skin {
public abstract class BackedSkin implements Skin, AutoCloseable {
// Keep track of how many instances exist for each texture id, so we can clean them up when they're no longer used
// It should be fine to use the same map for all subclasses, since the texture ids will be unique anyway
public static final Object2IntMap<Identifier> INSTANCE_COUNTS = new Object2IntOpenHashMap<>();
Expand Down Expand Up @@ -100,15 +100,10 @@ protected void setTexture(Identifier textureId) {

protected abstract @Nullable AbstractTexture loadTexture(Runnable completionCallback);

@SuppressWarnings("deprecation")
@Override
protected void finalize() throws Throwable {
try {
if (textureId != null) {
decrementInstanceCountAndCleanup(textureId);
}
} finally {
super.finalize();
public void close() {
if (textureId != null) {
decrementInstanceCountAndCleanup(textureId);
}
}

Expand Down
Loading

0 comments on commit b0ddf61

Please sign in to comment.