From cae5c71af92e1ec1c372eb658fc0f1263be823f1 Mon Sep 17 00:00:00 2001 From: Christian Treffs Date: Mon, 21 Oct 2024 13:54:11 +0200 Subject: [PATCH] Lint fix --- .../FirebladeECS/ComponentIdentifier.swift | 4 +- .../EntityIdentifierGenerator.swift | 3 +- Sources/FirebladeECS/Nexus+Codable.swift | 2 +- .../FirebladeECS/Nexus+Serialization.swift | 187 +++++++++--------- Sources/FirebladeECS/Version.swift | 27 +-- 5 files changed, 114 insertions(+), 109 deletions(-) diff --git a/Sources/FirebladeECS/ComponentIdentifier.swift b/Sources/FirebladeECS/ComponentIdentifier.swift index 9bf8905..09ee15f 100644 --- a/Sources/FirebladeECS/ComponentIdentifier.swift +++ b/Sources/FirebladeECS/ComponentIdentifier.swift @@ -23,12 +23,12 @@ extension ComponentIdentifier { } typealias StableId = UInt64 - internal static func makeStableTypeHash(component: Component) -> StableId { + static func makeStableTypeHash(component: Component) -> StableId { let componentTypeString = String(describing: type(of: component)) return StringHashing.singer_djb2(componentTypeString) } - internal static func makeStableInstanceHash(component: Component, entityId: EntityIdentifier) -> StableId { + static func makeStableInstanceHash(component: Component, entityId: EntityIdentifier) -> StableId { let componentTypeString = String(describing: type(of: component)) + String(entityId.id) return StringHashing.singer_djb2(componentTypeString) } diff --git a/Sources/FirebladeECS/EntityIdentifierGenerator.swift b/Sources/FirebladeECS/EntityIdentifierGenerator.swift index 9d5f6bc..d92a725 100644 --- a/Sources/FirebladeECS/EntityIdentifierGenerator.swift +++ b/Sources/FirebladeECS/EntityIdentifierGenerator.swift @@ -101,4 +101,5 @@ public struct LinearIncrementingEntityIdGenerator: EntityIdentifierGenerator { storage.markUnused(entityId: entityId) } } -extension LinearIncrementingEntityIdGenerator.Storage: Codable { } + +extension LinearIncrementingEntityIdGenerator.Storage: Codable {} diff --git a/Sources/FirebladeECS/Nexus+Codable.swift b/Sources/FirebladeECS/Nexus+Codable.swift index 8034f3b..f00c83b 100644 --- a/Sources/FirebladeECS/Nexus+Codable.swift +++ b/Sources/FirebladeECS/Nexus+Codable.swift @@ -7,7 +7,7 @@ extension Nexus: Encodable { public func encode(to encoder: Encoder) throws { - let serializedNexus = try self.serialize() + let serializedNexus = try serialize() var container = encoder.singleValueContainer() try container.encode(serializedNexus) } diff --git a/Sources/FirebladeECS/Nexus+Serialization.swift b/Sources/FirebladeECS/Nexus+Serialization.swift index ee0f180..c9073b4 100644 --- a/Sources/FirebladeECS/Nexus+Serialization.swift +++ b/Sources/FirebladeECS/Nexus+Serialization.swift @@ -6,119 +6,122 @@ // #if canImport(Foundation) -import struct Foundation.Data - -extension Nexus { - final func serialize() throws -> SNexus { - var componentInstances: [ComponentIdentifier.StableId: SComponent] = [:] - var entityComponentsMap: [EntityIdentifier: Set] = [:] - - for entitId in self.componentIdsByEntity.keys { - entityComponentsMap[entitId] = [] - let componentIds = self.get(components: entitId) ?? [] - - for componentId in componentIds { - let component = self.get(unsafe: componentId, for: entitId) - let componentStableInstanceHash = ComponentIdentifier.makeStableInstanceHash(component: component, entityId: entitId) - componentInstances[componentStableInstanceHash] = SComponent.component(component) - entityComponentsMap[entitId]?.insert(componentStableInstanceHash) + import struct Foundation.Data + + extension Nexus { + final func serialize() throws -> SNexus { + var componentInstances: [ComponentIdentifier.StableId: SComponent] = [:] + var entityComponentsMap: [EntityIdentifier: Set] = [:] + + for entitId in componentIdsByEntity.keys { + entityComponentsMap[entitId] = [] + let componentIds = self.get(components: entitId) ?? [] + + for componentId in componentIds { + let component = self.get(unsafe: componentId, for: entitId) + let componentStableInstanceHash = ComponentIdentifier.makeStableInstanceHash(component: component, entityId: entitId) + componentInstances[componentStableInstanceHash] = SComponent.component(component) + entityComponentsMap[entitId]?.insert(componentStableInstanceHash) + } } - } - - return SNexus(version: version, - entities: entityComponentsMap, - components: componentInstances) - } - final func deserialize(from sNexus: SNexus, into nexus: Nexus) throws { - for freeId in sNexus.entities.map(\.key).reversed() { - nexus.entityIdGenerator.markUnused(entityId: freeId) + return SNexus(version: version, + entities: entityComponentsMap, + components: componentInstances) } - for componentSet in sNexus.entities.values { - let entity = self.createEntity() - for sCompId in componentSet { - guard let sComp = sNexus.components[sCompId] else { - throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "Could not find component instance for \(sCompId).")) - } + final func deserialize(from sNexus: SNexus, into nexus: Nexus) throws { + for freeId in sNexus.entities.map(\.key).reversed() { + nexus.entityIdGenerator.markUnused(entityId: freeId) + } - switch sComp { - case let .component(comp): - entity.assign(comp) + for componentSet in sNexus.entities.values { + let entity = createEntity() + for sCompId in componentSet { + guard let sComp = sNexus.components[sCompId] else { + throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "Could not find component instance for \(sCompId).")) + } + + switch sComp { + case let .component(comp): + entity.assign(comp) + } } } } } -} -extension EntityIdentifier: Encodable { - public func encode(to encoder: Encoder) throws { - var container = encoder.singleValueContainer() - try container.encode(id) + extension EntityIdentifier: Encodable { + public func encode(to encoder: Encoder) throws { + var container = encoder.singleValueContainer() + try container.encode(id) + } + } + + extension EntityIdentifier: Decodable { + public init(from decoder: Decoder) throws { + let container = try decoder.singleValueContainer() + let id = try container.decode(UInt32.self) + self.init(id) + } + } + + struct SNexus { + let version: Version + let entities: [EntityIdentifier: Set] + let components: [ComponentIdentifier.StableId: SComponent] } -} -extension EntityIdentifier: Decodable { - public init(from decoder: Decoder) throws { - let container = try decoder.singleValueContainer() - let id = try container.decode(UInt32.self) - self.init(id) + + extension SNexus: Encodable {} + extension SNexus: Decodable {} + + protocol ComponentEncoding { + static func encode(component: Component, to encoder: Encoder) throws + } + + protocol ComponentDecoding { + static func decode(from decoder: Decoder) throws -> Component } -} - -internal struct SNexus { - let version: Version - let entities: [EntityIdentifier: Set] - let components: [ComponentIdentifier.StableId: SComponent] -} -extension SNexus: Encodable { } -extension SNexus: Decodable { } - -protocol ComponentEncoding { - static func encode(component: Component, to encoder: Encoder) throws -} - -protocol ComponentDecoding { - static func decode(from decoder: Decoder) throws -> Component -} -typealias ComponentCodable = ComponentEncoding & ComponentDecoding - -extension SNexus: ComponentEncoding { - static func encode(component: Component, to encoder: Encoder) throws { - var container = encoder.singleValueContainer() - let bytes = withUnsafeBytes(of: component) { - Data(bytes: $0.baseAddress!, count: MemoryLayout.stride(ofValue: component)) + + typealias ComponentCodable = ComponentDecoding & ComponentEncoding + + extension SNexus: ComponentEncoding { + static func encode(component: Component, to encoder: Encoder) throws { + var container = encoder.singleValueContainer() + let bytes = withUnsafeBytes(of: component) { + Data(bytes: $0.baseAddress!, count: MemoryLayout.stride(ofValue: component)) + } + try container.encode(bytes) } - try container.encode(bytes) } -} - -extension SNexus: ComponentDecoding { - static func decode(from decoder: Decoder) throws -> Component { - let container = try decoder.singleValueContainer() - let instanceData = try container.decode(Data.self) - return instanceData.withUnsafeBytes { - $0.baseAddress!.load(as: Component.self) + + extension SNexus: ComponentDecoding { + static func decode(from decoder: Decoder) throws -> Component { + let container = try decoder.singleValueContainer() + let instanceData = try container.decode(Data.self) + return instanceData.withUnsafeBytes { + $0.baseAddress!.load(as: Component.self) + } } } -} -enum SComponent { - case component(Component) -} + enum SComponent { + case component(Component) + } -extension SComponent: Encodable { - public func encode(to encoder: Encoder) throws { - switch self { - case let .component(comp): - try CodingStrategy.encode(component: comp, to: encoder) + extension SComponent: Encodable { + public func encode(to encoder: Encoder) throws { + switch self { + case let .component(comp): + try CodingStrategy.encode(component: comp, to: encoder) + } } } -} -extension SComponent: Decodable { - public init(from decoder: Decoder) throws { - self = .component(try CodingStrategy.decode(from: decoder)) + extension SComponent: Decodable { + public init(from decoder: Decoder) throws { + self = try .component(CodingStrategy.decode(from: decoder)) + } } -} #endif diff --git a/Sources/FirebladeECS/Version.swift b/Sources/FirebladeECS/Version.swift index 47f5dc2..f7f8a83 100644 --- a/Sources/FirebladeECS/Version.swift +++ b/Sources/FirebladeECS/Version.swift @@ -45,22 +45,24 @@ public struct Version { guard requiredComponents.count == 3 else { return nil } - self.major = requiredComponents[0] - self.minor = requiredComponents[1] - self.patch = requiredComponents[2] + major = requiredComponents[0] + minor = requiredComponents[1] + patch = requiredComponents[2] func identifiers(start: String.Index?, end: String.Index) -> [String] { - guard let start = start else { return [] } - let identifiers = versionString[versionString.index(after: start).. Bool { major == other.major && minor == other.minor && patch == other.patch @@ -89,11 +91,11 @@ extension Version: Comparable { return lhsComparators.lexicographicallyPrecedes(rhsComparators) } - guard lhs.prereleaseIdentifiers.count > 0 else { + guard !lhs.prereleaseIdentifiers.isEmpty else { return false // Non-prerelease lhs >= potentially prerelease rhs } - guard rhs.prereleaseIdentifiers.count > 0 else { + guard !rhs.prereleaseIdentifiers.isEmpty else { return true // Prerelease lhs < non-prerelease rhs } @@ -111,7 +113,6 @@ extension Version: Comparable { case let (string1 as String, string2 as String): return string1 < string2 case (is Int, is String): return true // Int prereleases < String prereleases case (is String, is Int): return false - default: return false }