Skip to content

Commit

Permalink
RTTI: Fix HZD's LocalizedSimpleSoundResource deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadelessFox committed Mar 24, 2024
1 parent aec2ec4 commit d16d9f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ public void read(@NotNull RTTITypeRegistry registry, @NotNull ByteBuffer buffer,
wave.set("BlockAlignment", buffer.getShort());
wave.set("FormatTag", buffer.getShort());

int shift = 0;
for (RTTIEnum.Constant language : getLanguages(registry)) {
if ((mask & (1 << shift)) != 0) {
dataSources.add(Entry.read(registry, buffer, language));
final List<RTTIEnum.Constant> languages = getSupportedLanguages(registry);
for (int i = 0; i < languages.size(); i++) {
if ((mask & (1 << i)) != 0) {
dataSources.add(Entry.read(registry, buffer, languages.get(i)));
}
shift += 1;
}

object.set("WaveData", wave);
Expand Down Expand Up @@ -80,7 +79,7 @@ public void write(@NotNull RTTITypeRegistry registry, @NotNull ByteBuffer buffer
private static int computeMask(@NotNull RTTITypeRegistry registry, RTTIObject[] dataSources) {
int mask = 0;

final List<RTTIEnum.Constant> supportedLanguages = getLanguages(registry);
final List<RTTIEnum.Constant> supportedLanguages = getSupportedLanguages(registry);
final List<RTTIEnum.Constant> usedLanguages = Arrays.stream(dataSources)
.map(RTTIObject::<Entry>cast)
.map(entry -> entry.language)
Expand Down Expand Up @@ -112,13 +111,13 @@ public Component[] components(@NotNull RTTITypeRegistry registry) {
}

@NotNull
private static List<RTTIEnum.Constant> getLanguages(@NotNull RTTITypeRegistry registry) {
private static List<RTTIEnum.Constant> getSupportedLanguages(@NotNull RTTITypeRegistry registry) {
return Arrays.stream(registry.<RTTIEnum>find("ELanguage").values())
.filter(language -> (getFlags(language) & 2) != 0)
.filter(language -> (getLanguageFlags(language) & 2) != 0)
.toList();
}

private static int getFlags(@NotNull RTTITypeEnum.Constant language) {
private static int getLanguageFlags(@NotNull RTTITypeEnum.Constant language) {
return switch (language.value()) {
// English
case 1 -> 7;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.*;

@MessageHandlerRegistration(message = "MsgReadBinary", types = {
@Type(name = "LocalizedSimpleSoundResource", game = GameType.HZD)
Expand Down Expand Up @@ -51,12 +48,11 @@ public void read(@NotNull RTTITypeRegistry registry, @NotNull ByteBuffer buffer,
wave.set("BlockAlignment", buffer.getShort());
wave.set("FormatTag", buffer.getShort());

int shift = 0;
for (RTTIEnum.Constant language : getLanguages(registry)) {
if ((mask & (1 << shift)) != 0) {
entries.add(Entry.read(registry, buffer, language, location));
final List<RTTIEnum.Constant> languages = getSupportedLanguages(registry);
for (int i = 0; i < languages.size(); i++) {
if ((mask & (1 << i)) != 0) {
entries.add(Entry.read(registry, buffer, languages.get(i), location));
}
shift += 1;
}

object.set("Location", location);
Expand All @@ -72,9 +68,9 @@ public void write(@NotNull RTTITypeRegistry registry, @NotNull ByteBuffer buffer

buffer.putInt(location.length);
buffer.put(location);
buffer.putShort((short) computeMask(registry, dataSources));
buffer.putShort((short) computeLanguageMask(registry, dataSources));
buffer.put((byte) 28);
buffer.put((byte) computeFlags(wave));
buffer.put((byte) computeWaveFlags(wave));
buffer.putShort(wave.i16("FrameSize"));
buffer.put((byte) wave.<RTTITypeEnum.Constant>get("Encoding").value());
buffer.put(wave.i8("ChannelCount"));
Expand Down Expand Up @@ -106,25 +102,18 @@ public Component[] components(@NotNull RTTITypeRegistry registry) {
};
}

@NotNull
private static List<RTTIEnum.Constant> getLanguages(@NotNull RTTITypeRegistry registry) {
return Arrays.stream(registry.<RTTIEnum>find("ELanguage").values())
.filter(language -> (getFlags(language) & 2) != 0)
.toList();
}

private static int computeFlags(@NotNull RTTIObject wave) {
private static int computeWaveFlags(@NotNull RTTIObject wave) {
int flags = 0;
flags |= wave.bool("IsStreaming") ? 1 : 0;
flags |= wave.bool("UseVBR") ? 2 : 0;
flags |= wave.<RTTITypeEnum.Constant>get("EncodingQuality").value() << 2;
return flags;
}

private static int computeMask(@NotNull RTTITypeRegistry registry, @NotNull RTTIObject[] dataSources) {
private static int computeLanguageMask(@NotNull RTTITypeRegistry registry, @NotNull RTTIObject[] dataSources) {
int mask = 0;

final List<RTTIEnum.Constant> supportedLanguages = getLanguages(registry);
final List<RTTIEnum.Constant> supportedLanguages = getSupportedLanguages(registry);
final List<RTTIEnum.Constant> usedLanguages = Arrays.stream(dataSources)
.map(RTTIObject::<Entry>cast)
.map(entry -> entry.language)
Expand All @@ -138,7 +127,15 @@ private static int computeMask(@NotNull RTTITypeRegistry registry, @NotNull RTTI
return mask;
}

private static int getFlags(@NotNull RTTITypeEnum.Constant language) {
@NotNull
private static List<RTTIEnum.Constant> getSupportedLanguages(@NotNull RTTITypeRegistry registry) {
return Arrays.stream(registry.<RTTIEnum>find("ELanguage").values())
.filter(language -> (getLanguageFlags(language) & 2) != 0)
.sorted(Comparator.comparingInt(RTTITypeEnum.Constant::value))
.toList();
}

private static int getLanguageFlags(@NotNull RTTITypeEnum.Constant language) {
// See sub_7FF6BFD65BD0
return switch (language.value()) {
case 1 -> 7;
Expand Down

0 comments on commit d16d9f8

Please sign in to comment.