22
33import net .minecraft .client .gui .font .providers .UnihexProvider ;
44
5+ import java .lang .reflect .Array ;
6+
57/**
68 * Implements more compact storage for LineData contents.
79 *
810 * Credit for the idea of using flattened fields rather than a backing array goes to @AnAwesomGuy.
911 */
1012public class CompactUnihexContents {
13+ private static final boolean TEST_ROUNDTRIP = false ;
14+
1115 private static long extract8Bytes (byte [] arr , int off ) {
1216 long l = 0 ;
1317 for (int i = 0 ; i < 8 ; i ++) {
14- l |= ((long )arr [off + i ] << (i * 8 ));
18+ l |= ((( long )arr [off + i ] & 0xFF ) << (i * 8 ));
1519 }
1620 return l ;
1721 }
@@ -23,7 +27,7 @@ private static byte extractByte(long compressed, int off) {
2327 private static long extract4Shorts (short [] arr , int off ) {
2428 long l = 0 ;
2529 for (int i = 0 ; i < 4 ; i ++) {
26- l |= ((long )arr [off + i ] << (i * 16 ));
30+ l |= ((( long )arr [off + i ] & 0xFFFF ) << (i * 16 ));
2731 }
2832 return l ;
2933 }
@@ -32,13 +36,26 @@ private static short extractShort(long compressed, int off) {
3236 return (short )((compressed >> (off * 16 )) & 0xFFFF );
3337 }
3438
39+ private static void verifyRoundTrip (Object originalArray , UnihexProvider .LineData data , int shift ) {
40+ for (int i = 0 ; i < 16 ; i ++) {
41+ int val = Array .getInt (originalArray , i ) << shift ;
42+ int actualVal = data .line (i );
43+ if (val != actualVal ) {
44+ throw new AssertionError ("Value at index %d differs. Expected %08x, got %08x" .formatted (i , val , actualVal ));
45+ }
46+ }
47+ }
48+
3549 public static class Bytes implements UnihexProvider .LineData {
3650 private final long b0 ;
3751 private final long b8 ;
3852
3953 public Bytes (byte [] contents ) {
4054 this .b0 = extract8Bytes (contents , 0 );
4155 this .b8 = extract8Bytes (contents , 8 );
56+ if (TEST_ROUNDTRIP ) {
57+ verifyRoundTrip (contents , this , 24 );
58+ }
4259 }
4360
4461 @ Override
@@ -70,6 +87,9 @@ public Shorts(short[] contents) {
7087 this .b4 = extract4Shorts (contents , 4 );
7188 this .b8 = extract4Shorts (contents , 8 );
7289 this .b12 = extract4Shorts (contents , 12 );
90+ if (TEST_ROUNDTRIP ) {
91+ verifyRoundTrip (contents , this , 16 );
92+ }
7393 }
7494
7595 @ Override
0 commit comments