fix(makhno): handle voices with different season counts

Previously, the code assumed all voices had the same number of seasons
as the first voice, which could cause errors or incorrect behavior when
voices had varying season counts. Now calculates the maximum seasons
across all voices and safely handles cases where a specific voice
doesn't have the requested season index.
This commit is contained in:
baliasnyifeliks 2026-02-04 19:22:24 +02:00
parent ac8bbe0318
commit c6cc802403

View File

@ -196,13 +196,16 @@ namespace Makhno
return OnError(); return OnError();
} }
int maxSeasons = playerData.Voices.Max(v => v.Seasons?.Count ?? 0);
if (maxSeasons <= 0)
return OnError();
if (season == -1) if (season == -1)
{ {
var firstVoice = playerData.Voices.First();
var season_tpl = new SeasonTpl(); var season_tpl = new SeasonTpl();
for (int i = 0; i < firstVoice.Seasons.Count; i++) for (int i = 0; i < maxSeasons; i++)
{ {
var seasonItem = firstVoice.Seasons[i]; var seasonItem = playerData.Voices.Select(v => v.Seasons.ElementAtOrDefault(i)).FirstOrDefault(s => s != null);
string seasonName = seasonItem.Title ?? $"Сезон {i + 1}"; string seasonName = seasonItem.Title ?? $"Сезон {i + 1}";
int seasonNumber = i + 1; int seasonNumber = i + 1;
string link = $"{host}/makhno?imdb_id={imdb_id}&title={HttpUtility.UrlEncode(title)}&original_title={HttpUtility.UrlEncode(original_title)}&year={year}&serial=1&season={seasonNumber}"; string link = $"{host}/makhno?imdb_id={imdb_id}&title={HttpUtility.UrlEncode(title)}&original_title={HttpUtility.UrlEncode(original_title)}&year={year}&serial=1&season={seasonNumber}";
@ -213,7 +216,7 @@ namespace Makhno
} }
int seasonIndex = season > 0 ? season - 1 : season; int seasonIndex = season > 0 ? season - 1 : season;
if (seasonIndex < 0 || seasonIndex >= playerData.Voices.First().Seasons.Count) if (seasonIndex < 0 || seasonIndex >= maxSeasons)
return OnError(); return OnError();
var voice_tpl = new VoiceTpl(); var voice_tpl = new VoiceTpl();
@ -229,7 +232,11 @@ namespace Makhno
{ {
var voice = playerData.Voices[i]; var voice = playerData.Voices[i];
string voiceName = voice.Name ?? $"Озвучка {i + 1}"; string voiceName = voice.Name ?? $"Озвучка {i + 1}";
int seasonNumber = seasonIndex + 1; int voiceSeasonIndex = GetSeasonIndexForVoice(voice, seasonIndex);
if (voiceSeasonIndex < 0)
continue;
int seasonNumber = voiceSeasonIndex + 1;
string voiceLink = $"{host}/makhno?imdb_id={imdb_id}&title={HttpUtility.UrlEncode(title)}&original_title={HttpUtility.UrlEncode(original_title)}&year={year}&serial=1&season={seasonNumber}&t={i}"; string voiceLink = $"{host}/makhno?imdb_id={imdb_id}&title={HttpUtility.UrlEncode(title)}&original_title={HttpUtility.UrlEncode(original_title)}&year={year}&serial=1&season={seasonNumber}&t={i}";
bool isActive = selectedVoice == i.ToString(); bool isActive = selectedVoice == i.ToString();
voice_tpl.Append(voiceName, isActive, voiceLink); voice_tpl.Append(voiceName, isActive, voiceLink);
@ -238,9 +245,10 @@ namespace Makhno
if (!string.IsNullOrEmpty(selectedVoice) && int.TryParse(selectedVoice, out int voiceIndex) && voiceIndex < playerData.Voices.Count) if (!string.IsNullOrEmpty(selectedVoice) && int.TryParse(selectedVoice, out int voiceIndex) && voiceIndex < playerData.Voices.Count)
{ {
var selectedVoiceData = playerData.Voices[voiceIndex]; var selectedVoiceData = playerData.Voices[voiceIndex];
if (seasonIndex < selectedVoiceData.Seasons.Count) int effectiveSeasonIndex = GetSeasonIndexForVoice(selectedVoiceData, seasonIndex);
if (effectiveSeasonIndex >= 0)
{ {
var selectedSeason = selectedVoiceData.Seasons[seasonIndex]; var selectedSeason = selectedVoiceData.Seasons[effectiveSeasonIndex];
var sortedEpisodes = selectedSeason.Episodes.OrderBy(e => ExtractEpisodeNumber(e.Title)).ToList(); var sortedEpisodes = selectedSeason.Episodes.OrderBy(e => ExtractEpisodeNumber(e.Title)).ToList();
for (int i = 0; i < sortedEpisodes.Count; i++) for (int i = 0; i < sortedEpisodes.Count; i++)
@ -249,7 +257,7 @@ namespace Makhno
if (!string.IsNullOrEmpty(episode.File)) if (!string.IsNullOrEmpty(episode.File))
{ {
string streamUrl = BuildStreamUrl(init, episode.File); string streamUrl = BuildStreamUrl(init, episode.File);
int seasonNumber = seasonIndex + 1; int seasonNumber = effectiveSeasonIndex + 1;
episode_tpl.Append( episode_tpl.Append(
episode.Title, episode.Title,
title ?? original_title, title ?? original_title,
@ -278,6 +286,17 @@ namespace Makhno
return match.Success ? int.Parse(match.Groups[1].Value) : 0; return match.Success ? int.Parse(match.Groups[1].Value) : 0;
} }
private int GetSeasonIndexForVoice(Voice voice, int requestedSeasonIndex)
{
if (voice?.Seasons == null || voice.Seasons.Count == 0)
return -1;
if (requestedSeasonIndex >= 0 && requestedSeasonIndex < voice.Seasons.Count)
return requestedSeasonIndex;
return 0;
}
private async Task<ResolveResult> ResolvePlaySource(string imdbId, string title, string originalTitle, int year, int serial, MakhnoInvoke invoke) private async Task<ResolveResult> ResolvePlaySource(string imdbId, string title, string originalTitle, int year, int serial, MakhnoInvoke invoke)
{ {
string playUrl = null; string playUrl = null;