diff --git a/Sources/FirebladeECS/Nexus+Codable.swift b/Sources/FirebladeECS/Nexus+Codable.swift index 377f54c..8034f3b 100644 --- a/Sources/FirebladeECS/Nexus+Codable.swift +++ b/Sources/FirebladeECS/Nexus+Codable.swift @@ -15,8 +15,10 @@ extension Nexus: Encodable { extension Nexus: Decodable { public convenience init(from decoder: Decoder) throws { + self.init() + let container = try decoder.singleValueContainer() let sNexus = try container.decode(SNexus.self) - try self.init(from: sNexus) + try deserialize(from: sNexus, into: self) } } diff --git a/Sources/FirebladeECS/Nexus+Serialization.swift b/Sources/FirebladeECS/Nexus+Serialization.swift index c2434b2..93e2934 100644 --- a/Sources/FirebladeECS/Nexus+Serialization.swift +++ b/Sources/FirebladeECS/Nexus+Serialization.swift @@ -10,8 +10,6 @@ import struct Foundation.Data extension Nexus { final func serialize() throws -> SNexus { - let version = Version(major: 1, minor: 0, patch: 0) - var componentInstances: [ComponentIdentifier.StableId: SComponent] = [:] var entityComponentsMap: [EntityIdentifier: Set] = [:] @@ -32,21 +30,15 @@ extension Nexus { components: componentInstances) } - convenience init(from sNexus: SNexus) throws { - let entityIds = sNexus.entities.map { $0.key }.reversed() - - // FIXME: this does not respect the generator of the target nexus! - self.init(componentsByType: [:], - componentsByEntity: [:], - entityIdGenerator: DefaultEntityIdGenerator(startProviding: entityIds), - familyMembersByTraits: [:], - codingStrategy: DefaultCodingStrategy()) + final func deserialize(from sNexus: SNexus, into nexus: Nexus) throws { + for freeId in sNexus.entities.map { $0.key }.reversed() { + nexus.entityIdGenerator.markUnused(entityId: freeId) + } for componentSet in sNexus.entities.values { let entity = self.createEntity() for sCompId in componentSet { guard let sComp = sNexus.components[sCompId] else { - // FIXME: we want a dedicated error throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "Could not find component instance for \(sCompId).")) } diff --git a/Sources/FirebladeECS/Nexus.swift b/Sources/FirebladeECS/Nexus.swift index 8f7c7f3..a69a856 100644 --- a/Sources/FirebladeECS/Nexus.swift +++ b/Sources/FirebladeECS/Nexus.swift @@ -6,6 +6,11 @@ // public final class Nexus { + /// The version of this Nexus implementation. + /// + /// Used for serialization. + final let version = Version(0, 18, 0) + /// - Key: ComponentIdentifier aka component type. /// - Value: Array of component instances of same type (uniform). /// New component instances are appended.