This commit is contained in:
Christian Treffs 2024-10-21 13:54:11 +02:00
parent 041440c40d
commit cae5c71af9
No known key found for this signature in database
GPG Key ID: 49A4B4B460BE3ED4
5 changed files with 119 additions and 114 deletions

View File

@ -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)
}

View File

@ -101,4 +101,5 @@ public struct LinearIncrementingEntityIdGenerator: EntityIdentifierGenerator {
storage.markUnused(entityId: entityId)
}
}
extension LinearIncrementingEntityIdGenerator.Storage: Codable {}

View File

@ -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)
}

View File

@ -13,7 +13,7 @@ extension Nexus {
var componentInstances: [ComponentIdentifier.StableId: SComponent<SNexus>] = [:]
var entityComponentsMap: [EntityIdentifier: Set<ComponentIdentifier.StableId>] = [:]
for entitId in self.componentIdsByEntity.keys {
for entitId in componentIdsByEntity.keys {
entityComponentsMap[entitId] = []
let componentIds = self.get(components: entitId) ?? []
@ -36,7 +36,7 @@ extension Nexus {
}
for componentSet in sNexus.entities.values {
let entity = self.createEntity()
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)."))
@ -57,6 +57,7 @@ extension EntityIdentifier: Encodable {
try container.encode(id)
}
}
extension EntityIdentifier: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
@ -65,11 +66,12 @@ extension EntityIdentifier: Decodable {
}
}
internal struct SNexus {
struct SNexus {
let version: Version
let entities: [EntityIdentifier: Set<ComponentIdentifier.StableId>]
let components: [ComponentIdentifier.StableId: SComponent<SNexus>]
}
extension SNexus: Encodable {}
extension SNexus: Decodable {}
@ -80,7 +82,8 @@ protocol ComponentEncoding {
protocol ComponentDecoding {
static func decode(from decoder: Decoder) throws -> Component
}
typealias ComponentCodable = ComponentEncoding & ComponentDecoding
typealias ComponentCodable = ComponentDecoding & ComponentEncoding
extension SNexus: ComponentEncoding {
static func encode(component: Component, to encoder: Encoder) throws {
@ -117,7 +120,7 @@ extension SComponent: Encodable {
extension SComponent: Decodable {
public init(from decoder: Decoder) throws {
self = .component(try CodingStrategy.decode(from: decoder))
self = try .component(CodingStrategy.decode(from: decoder))
}
}

View File

@ -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 [] }
guard let start else { return [] }
let identifiers = versionString[versionString.index(after: start) ..< end]
return identifiers.split(separator: ".").map(String.init)
}
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 {
@ -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
}