Refine Nexus serialization
This commit is contained in:
parent
168e1a8598
commit
dadfeb86b1
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<SNexus>] = [:]
|
||||
var entityComponentsMap: [EntityIdentifier: Set<ComponentIdentifier.StableId>] = [:]
|
||||
|
||||
|
|
@ -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)."))
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Reference in New Issue