diff --git a/Packages/GachaKit/Sources/GachaKit/Backends/CDGachaMO/CDGachaMOSputnik.swift b/Packages/GachaKit/Sources/GachaKit/Backends/CDGachaMO/CDGachaMOSputnik.swift index 1e09e86dd..1ac19f407 100644 --- a/Packages/GachaKit/Sources/GachaKit/Backends/CDGachaMO/CDGachaMOSputnik.swift +++ b/Packages/GachaKit/Sources/GachaKit/Backends/CDGachaMO/CDGachaMOSputnik.swift @@ -136,7 +136,7 @@ extension [CDGachaMO4GI] { } public mutating func fixItemIDs(with givenLanguage: GachaLanguage? = nil) { - let needsItemIDFix = !filter { $0.itemId.isEmpty }.isEmpty + let needsItemIDFix = !filter(\.itemId.isNotInt).isEmpty guard !isEmpty, needsItemIDFix else { return } var languages: [HoYo.APILang] = [.langCHS] if mightHaveNonCHSLanguageTag, !possibleLanguages.isEmpty { @@ -180,7 +180,7 @@ extension [CDGachaMO4GI] { var newItemContainer = Self() // 君子协定:这里要求 UIGFGachaItem 的 itemID 必须是有效值,否则会出现灾难性的后果。 try forEach { currentItem in - guard Int(currentItem.itemId) != nil else { + guard currentItem.itemId.isInt else { throw GachaMeta.GMDBError.itemIDInvalid(name: currentItem.name, game: currentItem.game) } let lang = lang.sanitized(by: currentItem.game) diff --git a/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/OldModels/GIGF.swift b/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/OldModels/GIGF.swift index 1cba394c7..4f3b0e7fb 100644 --- a/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/OldModels/GIGF.swift +++ b/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/OldModels/GIGF.swift @@ -131,7 +131,7 @@ public struct GIGF: Decodable { public internal(set) var list: [GIGFGachaItem] public var needsItemIDFix: Bool { - !list.filter { $0.itemID.isEmpty }.isEmpty + !list.filter(\.itemID.isNotInt).isEmpty } // MARK: Internal @@ -296,19 +296,19 @@ extension GIGF { list.forEach { v2Item in var newItemID = v2Item.itemID var newName = v2Item.name - if newItemID.isEmpty { + if newItemID.isNotInt { if let newItemIDInt = revDB[v2Item.name] { newItemID = newItemIDInt.description } } - if !newItemID.isEmpty { + if newItemID.isInt { newName = GachaMeta.sharedDB.mainDB4GI.plainQueryForNames( itemID: newItemID, langID: self.info.lang.rawValue ) ?? v2Item.name } var maybeRankType: String? = v2Item.rankType?.rawValue.description ?? nil - if maybeRankType == nil, !newItemID.isEmpty, + if maybeRankType == nil, newItemID.isInt, let intMaybeRankType = mainDB.plainQueryForRarity(itemID: newItemID) { maybeRankType = intMaybeRankType.description } diff --git a/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/OldModels/SRGFv1.swift b/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/OldModels/SRGFv1.swift index 2a556e0ce..d900dd164 100644 --- a/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/OldModels/SRGFv1.swift +++ b/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/OldModels/SRGFv1.swift @@ -116,7 +116,7 @@ extension SRGFv1 { if Int(id) == nil { error = SRGFv1.makeDecodingError(CodingKeys.id) } self.itemID = try container.decode(String.self, forKey: .itemID) - if Int(itemID) == nil { error = SRGFv1.makeDecodingError(CodingKeys.itemID) } + if !itemID.isInt { error = SRGFv1.makeDecodingError(CodingKeys.itemID) } self.itemType = try container.decodeIfPresent(String.self, forKey: .itemType) if itemType?.isEmpty ?? false { error = SRGFv1.makeDecodingError(CodingKeys.itemType) } diff --git a/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/UIGFv4/UIGFv4_GachaItemGI.swift b/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/UIGFv4/UIGFv4_GachaItemGI.swift index a14a49e3e..028e01d49 100644 --- a/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/UIGFv4/UIGFv4_GachaItemGI.swift +++ b/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/UIGFv4/UIGFv4_GachaItemGI.swift @@ -47,7 +47,7 @@ extension UIGFv4 { if Int(id) == nil { error = UIGFv4.makeDecodingError(CodingKeys.id) } self.itemID = try container.decode(String.self, forKey: .itemID) - if Int(itemID) == nil { error = UIGFv4.makeDecodingError(CodingKeys.itemID) } + if !itemID.isInt { error = UIGFv4.makeDecodingError(CodingKeys.itemID) } self.itemType = try container.decodeIfPresent(String.self, forKey: .itemType) if itemType?.isEmpty ?? false { error = UIGFv4.makeDecodingError(CodingKeys.itemType) } diff --git a/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/UIGFv4/UIGFv4_GachaItemHSR.swift b/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/UIGFv4/UIGFv4_GachaItemHSR.swift index f92630c18..b0c092c9b 100644 --- a/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/UIGFv4/UIGFv4_GachaItemHSR.swift +++ b/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/UIGFv4/UIGFv4_GachaItemHSR.swift @@ -47,7 +47,7 @@ extension UIGFv4 { if Int(id) == nil { error = UIGFv4.makeDecodingError(CodingKeys.id) } self.itemID = try container.decode(String.self, forKey: .itemID) - if Int(itemID) == nil { error = UIGFv4.makeDecodingError(CodingKeys.itemID) } + if !itemID.isInt { error = UIGFv4.makeDecodingError(CodingKeys.itemID) } self.itemType = try container.decodeIfPresent(String.self, forKey: .itemType) if itemType?.isEmpty ?? false { error = UIGFv4.makeDecodingError(CodingKeys.itemType) } diff --git a/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/UIGFv4/UIGFv4_GachaItemZZZ.swift b/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/UIGFv4/UIGFv4_GachaItemZZZ.swift index 55d7e6329..b9d23b2d4 100644 --- a/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/UIGFv4/UIGFv4_GachaItemZZZ.swift +++ b/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/UIGFv4/UIGFv4_GachaItemZZZ.swift @@ -46,7 +46,7 @@ extension UIGFv4 { if Int(id) == nil { error = UIGFv4.makeDecodingError(CodingKeys.id) } self.itemID = try container.decode(String.self, forKey: .itemID) - if Int(itemID) == nil { error = UIGFv4.makeDecodingError(CodingKeys.itemID) } + if !itemID.isInt { error = UIGFv4.makeDecodingError(CodingKeys.itemID) } self.itemType = try container.decodeIfPresent(String.self, forKey: .itemType) if itemType?.isEmpty ?? false { error = UIGFv4.makeDecodingError(CodingKeys.itemType) } diff --git a/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/UIGFv4/UIGFv4_Protocols.swift b/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/UIGFv4/UIGFv4_Protocols.swift index 433ad74d8..ffccc5419 100644 --- a/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/UIGFv4/UIGFv4_Protocols.swift +++ b/Packages/GachaKit/Sources/GachaKit/Backends/GachaExchange/UIGFModels/UIGFv4/UIGFv4_Protocols.swift @@ -92,7 +92,7 @@ extension Array where Element: UIGFGachaItemProtocol { var newItemContainer = Self() // 君子协定:这里要求 UIGFGachaItem 的 itemID 必须是有效值,否则会出现灾难性的后果。 try forEach { currentItem in - guard Int(currentItem.itemID) != nil else { + guard currentItem.itemID.isInt else { throw GachaMeta.GMDBError.itemIDInvalid(name: currentItem.name ?? "", game: currentItem.game) } let lang = lang.sanitized(by: currentItem.game) diff --git a/Packages/GachaKit/Sources/GachaKit/Backends/GachaPersistence/ManagedObjects/PZGachaEntry_Translators.swift b/Packages/GachaKit/Sources/GachaKit/Backends/GachaPersistence/ManagedObjects/PZGachaEntry_Translators.swift index 88e34968d..f56a30eb3 100644 --- a/Packages/GachaKit/Sources/GachaKit/Backends/GachaPersistence/ManagedObjects/PZGachaEntry_Translators.swift +++ b/Packages/GachaKit/Sources/GachaKit/Backends/GachaPersistence/ManagedObjects/PZGachaEntry_Translators.swift @@ -24,7 +24,7 @@ extension GachaFetchModels.PageFetched.FetchedEntry { newEntry.time = time } - if fixItemIDs, game == .genshinImpact, itemID.isEmpty { + if fixItemIDs, game == .genshinImpact, itemID.isNotInt { var newItemID = GachaMeta.sharedDB.reverseQuery4GI(for: name) if newItemID == nil { try await GachaMeta.Sputnik.updateLocalGachaMetaDB(for: .genshinImpact) @@ -61,7 +61,7 @@ extension GachaFetchModels.PageFetched { extension PZGachaEntryProtocol { public mutating func fixItemID() async throws { - guard Pizza.SupportedGame(rawValue: game) == .genshinImpact, itemID.isEmpty else { return } + guard Pizza.SupportedGame(rawValue: game) == .genshinImpact, itemID.isNotInt else { return } var newItemID = GachaMeta.sharedDB.reverseQuery4GI(for: name) if newItemID == nil { try await GachaMeta.Sputnik.updateLocalGachaMetaDB(for: .genshinImpact) @@ -75,7 +75,7 @@ extension PZGachaEntryProtocol { public func asItemIDFixed() async throws -> Self { guard Pizza.SupportedGame(rawValue: game) == .genshinImpact else { return self } - guard itemID.isEmpty else { return self } + guard itemID.isNotInt else { return self } var result = self var newItemID = GachaMeta.sharedDB.reverseQuery4GI(for: name) if newItemID == nil { diff --git a/Packages/GachaKit/Sources/GachaKit/Frontends/_DebugViews/CDGachaMOItemDebugView.swift b/Packages/GachaKit/Sources/GachaKit/Frontends/_DebugViews/CDGachaMOItemDebugView.swift index d9dafb5ad..a13baf7aa 100644 --- a/Packages/GachaKit/Sources/GachaKit/Frontends/_DebugViews/CDGachaMOItemDebugView.swift +++ b/Packages/GachaKit/Sources/GachaKit/Frontends/_DebugViews/CDGachaMOItemDebugView.swift @@ -40,7 +40,7 @@ public struct CDGachaMODebugView: View { } ForEach(delegate.managedObjs, id: \.enumID) { gachaItemMO in let theEntry = gachaItemMO.asPZGachaEntrySendable.expressible - let isWrecked = theEntry.itemID.isEmpty + let isWrecked = theEntry.itemID.isNotInt GachaEntryBar(entry: theEntry, showDate: true, debug: true, debugMenu: true) .foregroundStyle(isWrecked ? Color.red : Color.primary) } diff --git a/Packages/GachaKit/Sources/GachaKit/GachaKit_OSImpl.swift b/Packages/GachaKit/Sources/GachaKit/GachaKit_OSImpl.swift index 4cbdb86b9..7048ee0f3 100644 --- a/Packages/GachaKit/Sources/GachaKit/GachaKit_OSImpl.swift +++ b/Packages/GachaKit/Sources/GachaKit/GachaKit_OSImpl.swift @@ -17,3 +17,13 @@ extension String.LocalizationValue { String(localized: self, bundle: .module) } } + +extension StringProtocol { + public var isInt: Bool { + Int(self) != nil + } + + public var isNotInt: Bool { + Int(self) == nil + } +}