Skip to content

Commit

Permalink
PCE: Fixed issues loading CD-ROM games that are split into multiple f…
Browse files Browse the repository at this point in the history
…iles and have pregaps defined for one or more tracks

The pregaps were not calculated properly when starting each subsequent file, causing the start position of subsequent files to be offset by an amount equal to the cumulative pregaps of all the previous tracks
  • Loading branch information
SourMesen committed Dec 6, 2024
1 parent 94d88ec commit d690e97
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions Core/Shared/CdReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ bool CdReader::LoadCue(VirtualFile& cueFile, DiscInfo& disc)
}
}

int sectorsInPrevFiles = 0;
uint32_t totalPregapLbaLength = 0;
for(size_t i = 0; i < files.size(); i++) {
VirtualFile physicalFile = files[i].Filename;
Expand All @@ -167,7 +166,7 @@ bool CdReader::LoadCue(VirtualFile& cueFile, DiscInfo& disc)
}

disc.Files.push_back(files[i].Filename);
int sectorsInFile = 0;
int startSector = i == 0 ? 0 : (disc.Tracks[disc.Tracks.size() - 1].LastSector + 1);
for(size_t j = 0; j < files[i].Tracks.size(); j++) {
CueTrackEntry entry = files[i].Tracks[j];
TrackInfo trk = {};
Expand All @@ -180,13 +179,13 @@ bool CdReader::LoadCue(VirtualFile& cueFile, DiscInfo& disc)
for(CueIndexEntry& idx : entry.Indexes) {
if(idx.Number == 0) {
trk.HasLeadIn = true;
trk.LeadInPosition = DiscPosition::FromLba(idx.Position.ToLba() + sectorsInPrevFiles);
trk.LeadInPosition = DiscPosition::FromLba(idx.Position.ToLba() + startSector);
} else if(idx.Number == 1) {
if(entry.PreGap.HasGap) {
trk.HasLeadIn = true;
trk.LeadInPosition = DiscPosition::FromLba(idx.Position.ToLba() + totalPregapLbaLength - entry.PreGap.Length.ToLba() + sectorsInPrevFiles);
trk.LeadInPosition = DiscPosition::FromLba(idx.Position.ToLba() + totalPregapLbaLength - entry.PreGap.Length.ToLba() + startSector);
}
trk.StartPosition = DiscPosition::FromLba(idx.Position.ToLba() + totalPregapLbaLength + sectorsInPrevFiles);
trk.StartPosition = DiscPosition::FromLba(idx.Position.ToLba() + totalPregapLbaLength + startSector);
startPos = idx.Position;
} else {
MessageManager::Log("[CUE] Unsupported index number: " + std::to_string(idx.Number));
Expand Down Expand Up @@ -216,8 +215,6 @@ bool CdReader::LoadCue(VirtualFile& cueFile, DiscInfo& disc)
prvTrk.LastSector = prvTrk.EndPosition.ToLba();
prvTrk.SectorCount = prvTrk.LastSector - prvTrk.FirstSector + 1;
prvTrk.Size = prvTrk.SectorCount * trk.GetSectorSize();
sectorsInFile += prvTrk.SectorCount;

currentFileOffset = prvTrk.FileOffset + prvTrk.Size;
}
}
Expand All @@ -237,8 +234,6 @@ bool CdReader::LoadCue(VirtualFile& cueFile, DiscInfo& disc)
lastTrk.SectorCount = lastTrk.Size / lastTrk.GetSectorSize();
lastTrk.EndPosition = DiscPosition::FromLba(lastTrk.FirstSector + lastTrk.SectorCount - 1);
lastTrk.LastSector = lastTrk.EndPosition.ToLba();
sectorsInFile += lastTrk.SectorCount;
sectorsInPrevFiles += sectorsInFile;
}

TrackInfo& discLastTrk = disc.Tracks[disc.Tracks.size() - 1];
Expand Down

0 comments on commit d690e97

Please sign in to comment.