Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import io.github.kosmx.emotes.common.network.PacketConfig;
import io.github.kosmx.emotes.common.network.PacketTask;
import io.github.kosmx.emotes.common.tools.MathHelper;
import io.netty.buffer.ByteBuf;
import org.redlance.platformtools.webp.decoder.DecodedImage;
import org.redlance.platformtools.webp.decoder.PlatformWebPDecoder;
import org.redlance.platformtools.webp.encoder.PlatformWebPEncoder;

import java.nio.ByteBuffer;
import java.io.IOException;

public class EmoteIconPacket extends AbstractNetworkPacket{
@Override
Expand All @@ -14,32 +18,42 @@ public byte getID() {

@Override
public byte getVer() {
return 0x12;
return 0x13;
}

@Override
public void read(ByteBuf byteBuf, NetData config, byte version) {
public void read(ByteBuf byteBuf, NetData config, byte version) throws IOException {
int size = byteBuf.readInt();
if (size <= 0) return;
if (size == 0 || (size < 0 && version < 0x13)) return;

ByteBuffer buffer = ByteBuffer.allocate(size);
byteBuf.readBytes(buffer);
buffer.flip();
config.extraData.put("iconData", buffer.asReadOnlyBuffer());
byte[] iconData = MathHelper.readBytes(byteBuf, Math.abs(size));

if (size < 0) { // Negative size = WebP, positive size = PNG
config.extraData.put("iconData", PlatformWebPDecoder.INSTANCE.decode(iconData));
} else {
config.extraData.put("iconData", DecodedImage.fromPng(iconData));
}
}

@Override
public void write(ByteBuf byteBuf, NetData config, byte version) {
public void write(ByteBuf byteBuf, NetData config, byte version) throws IOException {
assert config.emoteData != null;

ByteBuffer iconData = config.emoteData.data().getBinary("iconData");
if (iconData == null || !iconData.hasRemaining()) {
DecodedImage iconData = config.emoteData.data().getImage("iconData");
if (iconData == null) {
byteBuf.writeInt(0);
return;
}

byteBuf.writeInt(iconData.remaining());
byteBuf.writeBytes(iconData.duplicate());
if (version >= 0x13 && PlatformWebPEncoder.INSTANCE.isAvailable()) {
byte[] webpData = PlatformWebPEncoder.INSTANCE.encodeLossless(iconData);
byteBuf.writeInt(-webpData.length);
byteBuf.writeBytes(webpData);
} else {
byte[] pngData = iconData.toPng();
byteBuf.writeInt(pngData.length);
byteBuf.writeBytes(pngData);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,13 @@

import io.netty.buffer.ByteBuf;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

public class MathHelper {
public static ByteBuffer readFromIStream(InputStream stream) throws IOException {
return ByteBuffer.wrap(stream.readAllBytes());
}

/**
* If {@link ByteBuffer} is wrapped, it is safe to get the array
* but if is direct manual read is required.
* @param byteBuffer get the bytes from
* @return the byte array
*/
public static byte[] safeGetBytesFromBuffer(ByteBuffer byteBuffer) {
if (byteBuffer.isDirect() || byteBuffer.isReadOnly()) {
byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes);
return bytes;
}
else return byteBuffer.array();
public static byte[] readBytes(ByteBuf buf) {
return readBytes(buf, buf.readableBytes());
}

public static byte[] readBytes(ByteBuf buf) {
byte[] bytes = new byte[buf.readableBytes()];
public static byte[] readBytes(ByteBuf buf, int size) {
byte[] bytes = new byte[size];
buf.readBytes(bytes);
return bytes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import com.zigythebird.playeranimcore.animation.Animation;
import com.zigythebird.playeranimcore.animation.ExtraAnimationData;
import io.github.kosmx.emotes.common.CommonData;
import io.github.kosmx.emotes.common.tools.MathHelper;
import io.github.kosmx.emotes.common.tools.UUIDMap;
import net.raphimc.noteblocklib.NoteBlockLib;
import net.raphimc.noteblocklib.format.SongFormat;
import net.raphimc.noteblocklib.model.song.Song;
import org.redlance.platformtools.webp.decoder.DecodedImage;

import java.io.File;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -70,10 +69,10 @@ public static Map<String, Animation> serializeExternalEmote(Path file, String fo
Path icon = file.getParent().resolve(baseFileName + ".png");
if (Files.isRegularFile(icon)) {
try (InputStream iconStream = Files.newInputStream(icon)) {
final ByteBuffer byteBuffer = MathHelper.readFromIStream(iconStream);
final DecodedImage image = DecodedImage.fromPng(iconStream);

for (Animation emote : emotes.values()) { // Avoid lambda
emote.data().put("iconData", byteBuffer);
emote.data().put("iconData", image);
}
} catch (Throwable th) {
CommonData.LOGGER.warn("Error while reading icon: {}", icon.getFileName(), th);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package io.github.kosmx.emotes.server.serializer;

import com.zigythebird.playeranimcore.animation.Animation;
import io.github.kosmx.emotes.common.tools.MathHelper;
import io.github.kosmx.emotes.server.serializer.type.IWriter;
import net.raphimc.noteblocklib.NoteBlockLib;
import net.raphimc.noteblocklib.model.song.Song;
import org.redlance.platformtools.webp.decoder.DecodedImage;

import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -30,12 +29,12 @@ public static void writeAnimationInFormat(Animation animation, Path exportDir, I
if (format.onlyEmoteFile()) {
String fileName = EmoteSerializer.getBaseName(file.getFileName().toString());

if (animation.data().getBinary("iconData") instanceof ByteBuffer iconData) {
if (animation.data().getImage("iconData") instanceof DecodedImage iconData) {
Path iconPath = exportDir.resolve(fileName + ".png");
if (Files.exists(iconPath)) throw new FileAlreadyExistsException(iconPath.toString());

try (OutputStream iconStream = Files.newOutputStream(iconPath)) {
iconStream.write(MathHelper.safeGetBytesFromBuffer(iconData));
iconStream.write(iconData.toPng());
iconStream.flush();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import com.zigythebird.playeranimcore.animation.Animation;
import io.github.kosmx.emotes.common.CommonData;
import io.github.kosmx.emotes.common.network.EmotePacket;
import io.github.kosmx.emotes.common.tools.MathHelper;
import io.github.kosmx.emotes.common.tools.UUIDMap;
import io.github.kosmx.emotes.server.config.Serializer;
import io.github.kosmx.emotes.server.serializer.type.*;

import io.github.kosmx.emotes.server.services.InstanceService;
import org.jetbrains.annotations.Nullable;
import org.redlance.common.services.ServiceUtils;
import org.redlance.platformtools.webp.decoder.DecodedImage;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -137,8 +137,8 @@ private static void serializeInternalJson(String name){
emote.data().put(EmoteSerializer.BUILT_IN_KEY, true);

InputStream iconStream = UniversalEmoteSerializer.class.getClassLoader().getResourceAsStream("assets/" + CommonData.MOD_ID + "/emotes/" + name + ".png");
if(iconStream != null) {
emote.data().put("iconData", MathHelper.readFromIStream(iconStream));
if (iconStream != null) {
emote.data().put("iconData", DecodedImage.fromPng(iconStream));
iconStream.close();
}
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ org.gradle.parallel=true
# Dependencies
java_version = 25
translationfallbacks_version = 1.2.0+mc.26.1-snapshot-10
playeranimlib_version = 1.2.1+mc.26.1
playeranimlib_version = 1.2.1+dev+mc.26.1
bendablecuboids_version = 2.0.1+mc.1.21.11
modmenu_version = 18.0.0-alpha.6
fabric_api_version = 0.144.0+26.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import io.github.kosmx.emotes.PlatformTools;
import io.github.kosmx.emotes.api.proxy.INetworkInstance;
import io.github.kosmx.emotes.common.CommonData;
import io.github.kosmx.emotes.common.tools.MathHelper;
import io.github.kosmx.emotes.common.tools.UUIDMap;
import io.github.kosmx.emotes.main.network.ClientEmotePlay;
import io.github.kosmx.emotes.mc.McUtils;
Expand All @@ -28,10 +27,11 @@
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.redlance.platformtools.webp.decoder.DecodedImage;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
Expand Down Expand Up @@ -117,27 +117,29 @@ public static void clearEmotes(INetworkInstance networkInstance) {
}

public @Nullable Identifier getIconIdentifier() {
if (this.emote.data().getBinary("iconData") instanceof ByteBuffer buff && this.iconIdentifier == null) {
registerIcon(buff);
try {
if (this.emote.data().getImage("iconData") instanceof DecodedImage image && this.iconIdentifier == null) {
registerIcon(image.toPng());
}
}catch (Throwable th) {
CommonData.LOGGER.warn("Can't open emote {} icon!", emote, th);
/*if (!PlatformTools.getConfig().neverRemoveBadIcon.get()) {
this.iconIdentifier = null;
this.emote.data().remove("iconData");
}*/
}
return this.iconIdentifier;
}

private void registerIcon(ByteBuffer buffer) {
private void registerIcon(byte[] buf) throws IOException {
RenderSystem.assertOnRenderThread();

try (InputStream stream = new ByteArrayInputStream(MathHelper.safeGetBytesFromBuffer((buffer)))) {
try (InputStream stream = new ByteArrayInputStream(buf)) {
this.iconIdentifier = McUtils.newIdentifier("icon" + hashCode());

Minecraft.getInstance().getTextureManager().register(this.iconIdentifier,
new DynamicTexture(this.iconIdentifier::toString, NativeImage.read(stream))
);
} catch (Throwable th) {
CommonData.LOGGER.warn("Can't open emote {} icon!", emote, th);
/*if (!PlatformTools.getConfig().neverRemoveBadIcon.get()) {
this.iconIdentifier = null;
this.emote.data().remove("iconData");
}*/
}
}

Expand Down
Loading