Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sound map reading #792

Merged
merged 1 commit into from
Nov 22, 2024
Merged
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
35 changes: 35 additions & 0 deletions TRLevelControl/Build/TRSFXBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using TRLevelControl.Model;

namespace TRLevelControl.Build;

public static class TRSFXBuilder
{
public static List<short> ReadSoundMap(TRLevelReader reader)
{
List<short> map = new();
while (reader.PeekUInt() >= ushort.MaxValue)
{
map.Add(reader.ReadInt16());
}

return map;
}

public static TRDictionary<S, T> Build<S, T>(List<short> soundMap, List<T> soundDetails)
where S : Enum
where T : class
{
TRDictionary<S, T> result = new();
for (int i = 0; i < soundMap.Count; i++)
{
if (soundMap[i] == -1 || soundMap[i] >= soundDetails.Count)
{
continue;
}

result[(S)(object)(uint)i] = soundDetails[soundMap[i]];
}

return result;
}
}
13 changes: 2 additions & 11 deletions TRLevelControl/Control/TR1/TR1LevelControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,7 @@ private void WriteDemoData(TRLevelWriter writer)

private void ReadSoundEffects(TRLevelReader reader)
{
_level.SoundEffects = new();
short[] soundMap = reader.ReadInt16s(Enum.GetValues<TR1SFX>().Length);
List<short> soundMap = TRSFXBuilder.ReadSoundMap(reader);

uint numSoundDetails = reader.ReadUInt32();
List<TR1SoundEffect> sfx = new();
Expand Down Expand Up @@ -349,15 +348,7 @@ private void ReadSoundEffects(TRLevelReader reader)
}
}

for (int i = 0; i < soundMap.Length; i++)
{
if (soundMap[i] == -1)
{
continue;
}

_level.SoundEffects[(TR1SFX)i] = sfx[soundMap[i]];
}
_level.SoundEffects = TRSFXBuilder.Build<TR1SFX, TR1SoundEffect>(soundMap, sfx);
}

private void WriteSoundEffects(TRLevelWriter writer)
Expand Down
13 changes: 2 additions & 11 deletions TRLevelControl/Control/TR2/TR2LevelControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,7 @@ private void WriteDemoData(TRLevelWriter writer)

private void ReadSoundEffects(TRLevelReader reader)
{
_level.SoundEffects = new();
short[] soundMap = reader.ReadInt16s(Enum.GetValues<TR2SFX>().Length);
List<short> soundMap = TRSFXBuilder.ReadSoundMap(reader);

uint numSoundDetails = reader.ReadUInt32();
List<TR2SoundEffect> sfx = new();
Expand All @@ -335,15 +334,7 @@ private void ReadSoundEffects(TRLevelReader reader)
sfx[soundID].SampleID = sampleIndices[samplePointer];
}

for (int i = 0; i < soundMap.Length; i++)
{
if (soundMap[i] < 0 || soundMap[i] >= sfx.Count)
{
continue;
}

_level.SoundEffects[(TR2SFX)i] = sfx[soundMap[i]];
}
_level.SoundEffects = TRSFXBuilder.Build<TR2SFX, TR2SoundEffect>(soundMap, sfx);
}

private void WriteSoundEffects(TRLevelWriter writer)
Expand Down
13 changes: 2 additions & 11 deletions TRLevelControl/Control/TR3/TR3LevelControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,7 @@ private void WriteDemoData(TRLevelWriter writer)

private void ReadSoundEffects(TRLevelReader reader)
{
_level.SoundEffects = new();
short[] soundMap = reader.ReadInt16s(Enum.GetValues<TR3SFX>().Length);
List<short> soundMap = TRSFXBuilder.ReadSoundMap(reader);

uint numSoundDetails = reader.ReadUInt32();
List<TR3SoundEffect> sfx = new();
Expand Down Expand Up @@ -337,15 +336,7 @@ private void ReadSoundEffects(TRLevelReader reader)
sfx[soundID].SampleID = sampleIndices[samplePointer];
}

for (int i = 0; i < soundMap.Length; i++)
{
if (soundMap[i] < 0 || soundMap[i] >= sfx.Count)
{
continue;
}

_level.SoundEffects[(TR3SFX)i] = sfx[soundMap[i]];
}
_level.SoundEffects = TRSFXBuilder.Build<TR3SFX, TR3SoundEffect>(soundMap, sfx);
}

private void WriteSoundEffects(TRLevelWriter writer)
Expand Down
13 changes: 2 additions & 11 deletions TRLevelControl/Control/TR4/TR4LevelControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,7 @@ private static void WriteDemoData(TRLevelWriter writer)

private void ReadSoundEffects(TRLevelReader reader)
{
_level.SoundEffects = new();
short[] soundMap = reader.ReadInt16s(Enum.GetValues<TR4SFX>().Length);
List<short> soundMap = TRSFXBuilder.ReadSoundMap(reader);

uint numSoundDetails = reader.ReadUInt32();
List<TR4SoundEffect> sfx = new();
Expand All @@ -356,15 +355,7 @@ private void ReadSoundEffects(TRLevelReader reader)
uint[] sampleIndices = reader.ReadUInt32s(numSampleIndices);
_observer?.OnSampleIndicesRead(sampleIndices);

for (int i = 0; i < soundMap.Length; i++)
{
if (soundMap[i] < 0 || soundMap[i] >= sfx.Count)
{
continue;
}

_level.SoundEffects[(TR4SFX)i] = sfx[soundMap[i]];
}
_level.SoundEffects = TRSFXBuilder.Build<TR4SFX, TR4SoundEffect>(soundMap, sfx);
}

private void WriteSoundEffects(TRLevelWriter writer)
Expand Down
11 changes: 2 additions & 9 deletions TRLevelControl/Control/TR5/TR5LevelControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,7 @@ private static void WriteDemoData(TRLevelWriter writer)

private void ReadSoundEffects(TRLevelReader reader)
{
_level.SoundEffects = new();
short[] soundMap = reader.ReadInt16s(Enum.GetValues<TR5SFX>().Length);
List<short> soundMap = TRSFXBuilder.ReadSoundMap(reader);

uint numSoundDetails = reader.ReadUInt32();
List<TR4SoundEffect> sfx = new();
Expand All @@ -408,13 +407,7 @@ private void ReadSoundEffects(TRLevelReader reader)
uint[] sampleIndices = reader.ReadUInt32s(numSampleIndices);
_observer?.OnSampleIndicesRead(sampleIndices);

for (int i = 0; i < soundMap.Length; i++)
{
if (soundMap[i] == -1)
continue;

_level.SoundEffects[(TR5SFX)i] = sfx[soundMap[i]];
}
_level.SoundEffects = TRSFXBuilder.Build<TR5SFX, TR4SoundEffect>(soundMap, sfx);

reader.ReadBytes(6); // OxCD padding

Expand Down
7 changes: 7 additions & 0 deletions TRLevelControl/IO/TRLevelReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public TRLevelReader Inflate(TRChunkType chunkType)
return new(inflatedStream);
}

public uint PeekUInt()
{
uint value = ReadUInt32();
BaseStream.Position -= sizeof(uint);
return value;
}

public void ReadUntil(long position)
{
long distance = position - BaseStream.Position;
Expand Down