Refine Nexus serialization

This commit is contained in:
Christian Treffs 2020-10-21 08:16:26 +02:00
parent 168e1a8598
commit dadfeb86b1
No known key found for this signature in database
GPG Key ID: 49A4B4B460BE3ED4
3 changed files with 12 additions and 13 deletions

View File

@ -15,8 +15,10 @@ extension Nexus: Encodable {
extension Nexus: Decodable { extension Nexus: Decodable {
public convenience init(from decoder: Decoder) throws { public convenience init(from decoder: Decoder) throws {
self.init()
let container = try decoder.singleValueContainer() let container = try decoder.singleValueContainer()
let sNexus = try container.decode(SNexus.self) let sNexus = try container.decode(SNexus.self)
try self.init(from: sNexus) try deserialize(from: sNexus, into: self)
} }
} }

View File

@ -10,8 +10,6 @@ import struct Foundation.Data
extension Nexus { extension Nexus {
final func serialize() throws -> SNexus { final func serialize() throws -> SNexus {
let version = Version(major: 1, minor: 0, patch: 0)
var componentInstances: [ComponentIdentifier.StableId: SComponent<SNexus>] = [:] var componentInstances: [ComponentIdentifier.StableId: SComponent<SNexus>] = [:]
var entityComponentsMap: [EntityIdentifier: Set<ComponentIdentifier.StableId>] = [:] var entityComponentsMap: [EntityIdentifier: Set<ComponentIdentifier.StableId>] = [:]
@ -32,21 +30,15 @@ extension Nexus {
components: componentInstances) components: componentInstances)
} }
convenience init(from sNexus: SNexus) throws { final func deserialize(from sNexus: SNexus, into nexus: Nexus) throws {
let entityIds = sNexus.entities.map { $0.key }.reversed() for freeId in sNexus.entities.map { $0.key }.reversed() {
nexus.entityIdGenerator.markUnused(entityId: freeId)
// FIXME: this does not respect the generator of the target nexus! }
self.init(componentsByType: [:],
componentsByEntity: [:],
entityIdGenerator: DefaultEntityIdGenerator(startProviding: entityIds),
familyMembersByTraits: [:],
codingStrategy: DefaultCodingStrategy())
for componentSet in sNexus.entities.values { for componentSet in sNexus.entities.values {
let entity = self.createEntity() let entity = self.createEntity()
for sCompId in componentSet { for sCompId in componentSet {
guard let sComp = sNexus.components[sCompId] else { 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).")) throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "Could not find component instance for \(sCompId)."))
} }

View File

@ -6,6 +6,11 @@
// //
public final class Nexus { 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. /// - Key: ComponentIdentifier aka component type.
/// - Value: Array of component instances of same type (uniform). /// - Value: Array of component instances of same type (uniform).
/// New component instances are appended. /// New component instances are appended.