From d16d9f84e8213f24c5cc9ba886ca5b29562c27ec Mon Sep 17 00:00:00 2001 From: ShadelessFox Date: Sun, 24 Mar 2024 04:03:40 +0100 Subject: [PATCH] RTTI: Fix HZD's `LocalizedSimpleSoundResource` deserialization --- ...DSLocalizedSimpleSoundResourceHandler.java | 17 ++++---- ...ZDLocalizedSimpleSoundResourceHandler.java | 41 +++++++++---------- 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/modules/decima-model/src/main/java/com/shade/decima/model/rtti/messages/ds/DSLocalizedSimpleSoundResourceHandler.java b/modules/decima-model/src/main/java/com/shade/decima/model/rtti/messages/ds/DSLocalizedSimpleSoundResourceHandler.java index 1090c44c5..b41b0b2b6 100644 --- a/modules/decima-model/src/main/java/com/shade/decima/model/rtti/messages/ds/DSLocalizedSimpleSoundResourceHandler.java +++ b/modules/decima-model/src/main/java/com/shade/decima/model/rtti/messages/ds/DSLocalizedSimpleSoundResourceHandler.java @@ -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 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); @@ -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 supportedLanguages = getLanguages(registry); + final List supportedLanguages = getSupportedLanguages(registry); final List usedLanguages = Arrays.stream(dataSources) .map(RTTIObject::cast) .map(entry -> entry.language) @@ -112,13 +111,13 @@ public Component[] components(@NotNull RTTITypeRegistry registry) { } @NotNull - private static List getLanguages(@NotNull RTTITypeRegistry registry) { + private static List getSupportedLanguages(@NotNull RTTITypeRegistry registry) { return Arrays.stream(registry.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; diff --git a/modules/decima-model/src/main/java/com/shade/decima/model/rtti/messages/hzd/HZDLocalizedSimpleSoundResourceHandler.java b/modules/decima-model/src/main/java/com/shade/decima/model/rtti/messages/hzd/HZDLocalizedSimpleSoundResourceHandler.java index 8df9d7db0..30d6f7b2c 100644 --- a/modules/decima-model/src/main/java/com/shade/decima/model/rtti/messages/hzd/HZDLocalizedSimpleSoundResourceHandler.java +++ b/modules/decima-model/src/main/java/com/shade/decima/model/rtti/messages/hzd/HZDLocalizedSimpleSoundResourceHandler.java @@ -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) @@ -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 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); @@ -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.get("Encoding").value()); buffer.put(wave.i8("ChannelCount")); @@ -106,14 +102,7 @@ public Component[] components(@NotNull RTTITypeRegistry registry) { }; } - @NotNull - private static List getLanguages(@NotNull RTTITypeRegistry registry) { - return Arrays.stream(registry.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; @@ -121,10 +110,10 @@ private static int computeFlags(@NotNull RTTIObject wave) { 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 supportedLanguages = getLanguages(registry); + final List supportedLanguages = getSupportedLanguages(registry); final List usedLanguages = Arrays.stream(dataSources) .map(RTTIObject::cast) .map(entry -> entry.language) @@ -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 getSupportedLanguages(@NotNull RTTITypeRegistry registry) { + return Arrays.stream(registry.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;