Skip to content

Commit 072b50c

Browse files
committed
Fixed problems with MetronInfo.xml file not being loaded when some fields were missing
--- Improve null safety in MetronInfoProvider.cs - Updated ComicInfo properties to use null-conditional and null-coalescing operators for better handling of missing values. - Introduced a new method, ParseFormat, to encapsulate format logic, enhancing code robustness and readability.
1 parent ebca2e0 commit 072b50c

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

ComicRack.Engine/IO/Provider/XmlInfo/MetronInfoProvider.cs

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
using System.Text;
88
using System.Threading.Tasks;
99
using cYo.Common.Threading;
10+
using cYo.Common.Windows;
1011
using cYo.Common.Xml;
12+
using cYo.Projects.ComicRack.Engine.Drawing;
1113

1214
namespace cYo.Projects.ComicRack.Engine.IO.Provider.XmlInfo
1315
{
@@ -25,15 +27,15 @@ public override ComicInfo ToComicInfo(MetronInfo metronInfo)
2527
{
2628
ComicInfo comicInfo = new ComicInfo()
2729
{
28-
Publisher = metronInfo.Publisher.Name,
29-
Imprint = metronInfo.Publisher.Imprint.Value,
30+
Publisher = metronInfo.Publisher?.Name ?? string.Empty,
31+
Imprint = metronInfo.Publisher?.Imprint?.Value ?? string.Empty,
3032
Writer = string.Join(delimiter, metronInfo.Credits.SelectMany(c =>
3133
c.Roles.Where(r =>
3234
r.Value == RoleValues.Writer ||
3335
r.Value == RoleValues.Plot
3436
).Select(r => c.Creator.Value))),
35-
Penciller = string.Join(delimiter, metronInfo.Credits.SelectMany(c =>
36-
c.Roles.Where(r =>
37+
Penciller = string.Join(delimiter, metronInfo.Credits.SelectMany(c =>
38+
c.Roles.Where(r =>
3739
r.Value == RoleValues.Penciller
3840
).Select(r => c.Creator.Value))),
3941
Inker = string.Join(delimiter, metronInfo.Credits.SelectMany(c =>
@@ -45,47 +47,58 @@ public override ComicInfo ToComicInfo(MetronInfo metronInfo)
4547
c.Roles.Where(r =>
4648
r.Value.ToString().Contains("Color")
4749
).Select(r => c.Creator.Value))),
48-
Editor = string.Join(delimiter, metronInfo.Credits.SelectMany(c =>
50+
Editor = string.Join(delimiter, metronInfo.Credits.SelectMany(c =>
4951
c.Roles.Where(r =>
5052
r.Value.ToString().Contains("Editor")
5153
).Select(r => c.Creator.Value))),
5254
Translator = string.Join(delimiter, metronInfo.Credits.SelectMany(c =>
5355
c.Roles.Where(r =>
5456
r.Value.ToString().Contains("Translator")
5557
).Select(r => c.Creator.Value))),
56-
Letterer = string.Join(delimiter, metronInfo.Credits.SelectMany(c =>
58+
Letterer = string.Join(delimiter, metronInfo.Credits.SelectMany(c =>
5759
c.Roles.Where(r =>
5860
r.Value == RoleValues.Letterer
5961
).Select(r => c.Creator.Value))),
60-
CoverArtist = string.Join(delimiter, metronInfo.Credits.SelectMany(c =>
62+
CoverArtist = string.Join(delimiter, metronInfo.Credits.SelectMany(c =>
6163
c.Roles.Where(r =>
6264
r.Value == RoleValues.Cover
6365
).Select(r => c.Creator.Value))),
64-
Series = metronInfo.Series.Name,
65-
Number = metronInfo.Number,
66-
Count = metronInfo.Series.IssueCount,
67-
AlternateSeries = metronInfo.Arcs.FirstOrDefault()?.Name,
68-
AlternateNumber = metronInfo.Arcs.FirstOrDefault()?.Number.ToString(),
69-
StoryArc = metronInfo.Stories.FirstOrDefault()?.Value,
70-
Summary = metronInfo.Summary,
71-
Volume = metronInfo.Series?.Volume ?? -1,
72-
Year = metronInfo.CoverDate.Year,
73-
Month = metronInfo.CoverDate.Month,
74-
Day = metronInfo.CoverDate.Day,
75-
Notes = metronInfo.Notes,
66+
Series = metronInfo.Series?.Name ?? string.Empty,
67+
Number = metronInfo.Number ?? string.Empty,
68+
Count = metronInfo.Series?.IssueCountSpecified is null or false ? -1 : metronInfo.Series.IssueCount,
69+
AlternateSeries = metronInfo.Arcs?.FirstOrDefault()?.Name ?? string.Empty,
70+
AlternateNumber = metronInfo.Arcs?.FirstOrDefault()?.Number.ToString() ?? string.Empty,
71+
Title = metronInfo.Stories?.FirstOrDefault()?.Value ?? string.Empty, //Some files seem to set the Title as the 1st Story
72+
StoryArc = metronInfo.Stories?.Skip(1).FirstOrDefault()?.Value ?? string.Empty, //So we set the StoryArc as the 2nd Story
73+
Summary = metronInfo.Summary ?? string.Empty,
74+
Volume = metronInfo.Series is null ? -1 : metronInfo.Series.VolumeSpecified ? metronInfo.Series.Volume : int.TryParse(metronInfo.Series.StartYear, out int vol) ? vol : -1,// Use Volume, if not use StartYear
75+
Year = metronInfo.CoverDateSpecified ? metronInfo.CoverDate.Year : -1,
76+
Month = metronInfo.CoverDateSpecified ? metronInfo.CoverDate.Month : -1,
77+
Day = metronInfo.CoverDateSpecified ? metronInfo.CoverDate.Day : -1,
78+
Notes = metronInfo.Notes ?? string.Empty,
7679
Genre = string.Join(delimiter, metronInfo.Genres.Select(g => g.Value)),
77-
Web = metronInfo.UrLs.Where(u => u.Primary)?.FirstOrDefault()?.Value ?? metronInfo.UrLs.FirstOrDefault()?.Value,
78-
PageCount = metronInfo.PageCount,
79-
LanguageISO = metronInfo.Series.Lang,
80-
AgeRating = metronInfo.AgeRating.ToString(),
80+
Web = (metronInfo.UrLs.Where(u => u.Primary)?.FirstOrDefault()?.Value ?? metronInfo.UrLs.FirstOrDefault()?.Value) ?? string.Empty,
81+
//PageCount = metronInfo.PageCount, //PageCount should determined by the program unless it's a fileless book, because it can create problems when set to default values
82+
LanguageISO = metronInfo.Series?.Lang ?? string.Empty,
83+
AgeRating = metronInfo.AgeRating == AgeRatingType.Unknown ? string.Empty : LocalizeUtility.LocalizeEnum(typeof(AgeRatingType), (int)metronInfo.AgeRating),
8184
Characters = string.Join(delimiter, metronInfo.Characters.Select(c => c.Value)),
8285
Teams = string.Join(delimiter, metronInfo.Teams.Select(t => t.Value)),
8386
Locations = string.Join(delimiter, metronInfo.Locations.Select(t => t.Value)),
8487
Tags = string.Join(delimiter, metronInfo.Tags.Select(t => t.Value)),
88+
Format = metronInfo.Series?.FormatSpecified is null or false ? string.Empty : ParseFormat(metronInfo),
8589
};
8690

8791
return comicInfo;
8892
}
8993
}
94+
95+
private static string ParseFormat(MetronInfo metronInfo)
96+
{
97+
return (metronInfo.Series?.Format) switch
98+
{
99+
FormatType.TradePaperback => "TPB",
100+
_ => LocalizeUtility.LocalizeEnum(typeof(FormatType), (int)metronInfo.Series?.Format),
101+
};
102+
}
90103
}
91104
}

0 commit comments

Comments
 (0)