Skip to content

Commit

Permalink
Lint fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ctreffs committed Oct 21, 2024
1 parent 041440c commit cae5c71
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 109 deletions.
4 changes: 2 additions & 2 deletions Sources/FirebladeECS/ComponentIdentifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Check warning on line 29 in Sources/FirebladeECS/ComponentIdentifier.swift

View check run for this annotation

Codecov / codecov/patch

Sources/FirebladeECS/ComponentIdentifier.swift#L26-L29

Added lines #L26 - L29 were not covered by tests

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)
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/FirebladeECS/EntityIdentifierGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,5 @@ public struct LinearIncrementingEntityIdGenerator: EntityIdentifierGenerator {
storage.markUnused(entityId: entityId)
}
}
extension LinearIncrementingEntityIdGenerator.Storage: Codable { }

extension LinearIncrementingEntityIdGenerator.Storage: Codable {}
2 changes: 1 addition & 1 deletion Sources/FirebladeECS/Nexus+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
187 changes: 95 additions & 92 deletions Sources/FirebladeECS/Nexus+Serialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,119 +6,122 @@
//

#if canImport(Foundation)
import struct Foundation.Data

extension Nexus {
final func serialize() throws -> SNexus {
var componentInstances: [ComponentIdentifier.StableId: SComponent<SNexus>] = [:]
var entityComponentsMap: [EntityIdentifier: Set<ComponentIdentifier.StableId>] = [:]

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<SNexus>] = [:]
var entityComponentsMap: [EntityIdentifier: Set<ComponentIdentifier.StableId>] = [:]

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)."))

Check warning on line 42 in Sources/FirebladeECS/Nexus+Serialization.swift

View check run for this annotation

Codecov / codecov/patch

Sources/FirebladeECS/Nexus+Serialization.swift#L42

Added line #L42 was not covered by tests
}

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<ComponentIdentifier.StableId>]
let components: [ComponentIdentifier.StableId: SComponent<SNexus>]
}
}
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<ComponentIdentifier.StableId>]
let components: [ComponentIdentifier.StableId: SComponent<SNexus>]
}
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<CodingStrategy: ComponentCodable> {
case component(Component)
}
enum SComponent<CodingStrategy: ComponentCodable> {
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
27 changes: 14 additions & 13 deletions Sources/FirebladeECS/Version.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)..<end]
guard let start else { return [] }
let identifiers = versionString[versionString.index(after: start) ..< end]
return identifiers.split(separator: ".").map(String.init)

Check warning on line 55 in Sources/FirebladeECS/Version.swift

View check run for this annotation

Codecov / codecov/patch

Sources/FirebladeECS/Version.swift#L54-L55

Added lines #L54 - L55 were not covered by tests
}

self.prereleaseIdentifiers = identifiers(
prereleaseIdentifiers = identifiers(
start: prereleaseStartIndex,
end: metadataStartIndex ?? versionString.endIndex)
self.buildMetadataIdentifiers = identifiers(
end: metadataStartIndex ?? versionString.endIndex
)
buildMetadataIdentifiers = identifiers(
start: metadataStartIndex,
end: versionString.endIndex)
end: versionString.endIndex
)
}

public var versionString: String {
Expand All @@ -75,7 +77,7 @@ public struct Version {
}
}

extension Version: Equatable { }
extension Version: Equatable {}
extension Version: Comparable {
func isEqualWithoutPrerelease(_ other: Version) -> Bool {
major == other.major && minor == other.minor && patch == other.patch
Expand All @@ -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
}

Expand All @@ -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
}
Expand Down

0 comments on commit cae5c71

Please sign in to comment.