From f9e1ef7ab583902452bc76aee0150477a6c8ccf4 Mon Sep 17 00:00:00 2001 From: DismissedLight <1686188646@qq.com> Date: Sun, 8 Oct 2023 22:26:40 +0800 Subject: [PATCH] fix #990 --- .../Service/GachaLog/UIGFImportService.cs | 40 ++++++++++++------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/src/Snap.Hutao/Snap.Hutao/Service/GachaLog/UIGFImportService.cs b/src/Snap.Hutao/Snap.Hutao/Service/GachaLog/UIGFImportService.cs index 93fd0ccf..806ed3e4 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/GachaLog/UIGFImportService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/GachaLog/UIGFImportService.cs @@ -27,7 +27,15 @@ internal sealed partial class UIGFImportService : IUIGFImportService { await taskContext.SwitchToBackgroundAsync(); - if (!metadataOptions.IsCurrentLocale(uigf.Info.Language)) + if (!uigf.IsCurrentVersionSupported(out UIGFVersion version)) + { + ThrowHelper.InvalidOperation(SH.ServiceUIGFImportUnsupportedVersion); + } + + // v2.3+ support any locale + // v2.2 only support matched locale + // v2.1 only support CHS + if (version is UIGFVersion.Major2Minor2OrLower && !metadataOptions.IsCurrentLocale(uigf.Info.Language)) { string message = SH.ServiceGachaUIGFImportLanguageNotMatch.Format(uigf.Info.Language, metadataOptions.LanguageCode); ThrowHelper.InvalidOperation(message, null); @@ -36,14 +44,13 @@ internal sealed partial class UIGFImportService : IUIGFImportService GachaArchiveOperation.GetOrAdd(gachaLogDbService, taskContext, uigf.Info.Uid, archives, out GachaArchive? archive); Guid archiveId = archive.InnerId; - _ = uigf.IsCurrentVersionSupported(out UIGFVersion version); - + List fullItems = new(); foreach (GachaConfigType queryType in GachaLog.QueryTypes) { long trimId = gachaLogDbService.GetOldestGachaItemIdByArchiveIdAndQueryType(archiveId, queryType); logger.LogInformation("Last Id to trim with: [{Id}]", trimId); - List currentTypeToAdd = version switch + List currentTypedList = version switch { UIGFVersion.Major2Minor3OrHigher => uigf.List .Where(i => i.UIGFGachaType == queryType && i.Id < trimId) @@ -55,20 +62,25 @@ internal sealed partial class UIGFImportService : IUIGFImportService .OrderByDescending(i => i.Id) .Select(i => GachaItem.From(archiveId, i, context.GetItemId(i))) .ToList(), - _ => throw ThrowHelper.InvalidOperation(SH.ServiceUIGFImportUnsupportedVersion), + _ => throw Must.NeverHappen(), }; - // 越早的记录手工导入的可能性越高 - // 错误率相对来说会更高 - // 因此从尾部开始查找 - if (currentTypeToAdd.LastOrDefault(item => item.ItemId is 0U) is { } item) - { - ThrowHelper.InvalidOperation(SH.ServiceGachaLogUIGFImportItemInvalidFormat.Format(item.Id)); - } - - await gachaLogDbService.AddGachaItemsAsync(currentTypeToAdd).ConfigureAwait(false); + ThrowIfContainsInvalidItem(currentTypedList); + fullItems.AddRange(currentTypedList); } + await gachaLogDbService.AddGachaItemsAsync(fullItems).ConfigureAwait(false); return archive; } + + private static void ThrowIfContainsInvalidItem(List currentTypeToAdd) + { + // 越早的记录手工导入的可能性越高 + // 错误率相对来说会更高 + // 因此从尾部开始查找 + if (currentTypeToAdd.LastOrDefault(item => item.ItemId is 0U) is { } item) + { + ThrowHelper.InvalidOperation(SH.ServiceGachaLogUIGFImportItemInvalidFormat.Format(item.Id)); + } + } } \ No newline at end of file