Skip to content

Commit c1f1eea

Browse files
committed
Refactor BinaryEncoding for collections
1 parent 342d6cc commit c1f1eea

File tree

4 files changed

+84
-69
lines changed

4 files changed

+84
-69
lines changed

Sources/GateEngine/Resources/Geometry/Raw/RawGeometry.swift

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -294,26 +294,20 @@ extension Array where Element == RawGeometry {
294294

295295
extension RawGeometry: BinaryCodable {
296296
public func encode(into data: inout ContiguousArray<UInt8>, version: BinaryCodableVersion) throws {
297-
try Float.encodeArray(positions, into: &data, version: version)
298-
try uvSets.count.encode(into: &data, version: version)
299-
for uvSet in uvSets {
300-
try Float.encodeArray(uvSet, into: &data, version: version)
301-
}
302-
try Float.encodeArray(normals, into: &data, version: version)
303-
try Float.encodeArray(tangents, into: &data, version: version)
304-
try Float.encodeArray(colors, into: &data, version: version)
305-
try UInt16.encodeArray(indices, into: &data, version: version)
297+
try positions.encode(into: &data, version: version)
298+
try uvSets.encode(into: &data, version: version)
299+
try normals.encode(into: &data, version: version)
300+
try tangents.encode(into: &data, version: version)
301+
try colors.encode(into: &data, version: version)
302+
try indices.encode(into: &data, version: version)
306303
}
307304

308305
public init(decoding data: UnsafeRawBufferPointer, at offset: inout Int, version: BinaryCodableVersion) throws {
309-
self.positions = try Float.decodeArray(data, offset: &offset, version: version)
310-
let uvSetsCount = try Int(decoding: data, at: &offset, version: version)
311-
self.uvSets = try (0..<uvSetsCount).map({_ in
312-
try Float.decodeArray(data, offset: &offset, version: version)
313-
})
314-
self.normals = try Float.decodeArray(data, offset: &offset, version: version)
315-
self.tangents = try Float.decodeArray(data, offset: &offset, version: version)
316-
self.colors = try Float.decodeArray(data, offset: &offset, version: version)
317-
self.indices = try UInt16.decodeArray(data, offset: &offset, version: version)
306+
self.positions = try .init(decoding: data, at: &offset, version: version)
307+
self.uvSets = try .init(decoding: data, at: &offset, version: version)
308+
self.normals = try .init(decoding: data, at: &offset, version: version)
309+
self.tangents = try .init(decoding: data, at: &offset, version: version)
310+
self.colors = try .init(decoding: data, at: &offset, version: version)
311+
self.indices = try .init(decoding: data, at: &offset, version: version)
318312
}
319313
}

Sources/GateEngine/Resources/Skinning/Raw/RawSkeletalAnimation.swift

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,13 @@ public struct RawSkeletalAnimation: BinaryCodable {
2222
public func encode(into data: inout ContiguousArray<UInt8>, version: BinaryCodableVersion) throws {
2323
try self.name.encode(into: &data, version: version)
2424
try self.duration.encode(into: &data, version: version)
25-
26-
let keys = Array(self.animations.keys)
27-
let values = keys.map({animations[$0]!})
28-
try RawSkeleton.RawJoint.ID.encodeArray(keys, into: &data, version: version)
29-
try Self.JointAnimation.encodeArray(values, into: &data, version: version)
25+
try self.animations.encode(into: &data, version: version)
3026
}
3127

3228
public init(decoding data: UnsafeRawBufferPointer, at offset: inout Int, version: BinaryCodableVersion) throws {
3329
self.name = try String(decoding: data, at: &offset, version: version)
3430
self.duration = try Float(decoding: data, at: &offset, version: version)
35-
36-
let keys = try RawSkeleton.RawJoint.ID.decodeArray(data, offset: &offset, version: version)
37-
let values = try Self.JointAnimation.decodeArray(data, offset: &offset, version: version)
38-
self.animations = Dictionary(uniqueKeysWithValues: zip(keys, values))
31+
self.animations = try .init(decoding: data, at: &offset, version: version)
3932
}
4033
}
4134

@@ -120,16 +113,16 @@ public extension RawSkeletalAnimation {
120113
}
121114

122115
func encode(into data: inout ContiguousArray<UInt8>, version: BinaryCodableVersion) throws {
123-
try Float.encodeArray(times, into: &data, version: version)
116+
try times.encode(into: &data, version: version)
124117
try interpolation.encode(into: &data, version: version)
125-
try Position3.encodeArray(positions, into: &data, version: version)
118+
try positions.encode(into: &data, version: version)
126119
try bind.encode(into: &data, version: version)
127120
}
128121

129122
init(decoding data: UnsafeRawBufferPointer, at offset: inout Int, version: BinaryCodableVersion) throws {
130-
self.times = try Float.decodeArray(data, offset: &offset, version: version)
123+
self.times = try Array<Float>(decoding: data, at: &offset, version: version)
131124
self.interpolation = try Interpolation(decoding: data, at: &offset, version: version)
132-
self.positions = try Position3.decodeArray(data, offset: &offset, version: version)
125+
self.positions = try Array<Position3>(decoding: data, at: &offset, version: version)
133126
self.bind = try Position3(decoding: data, at: &offset, version: version)
134127
}
135128
}
@@ -151,16 +144,16 @@ public extension RawSkeletalAnimation {
151144
}
152145

153146
func encode(into data: inout ContiguousArray<UInt8>, version: BinaryCodableVersion) throws {
154-
try Float.encodeArray(times, into: &data, version: version)
147+
try times.encode(into: &data, version: version)
155148
try interpolation.encode(into: &data, version: version)
156-
try Quaternion.encodeArray(rotations, into: &data, version: version)
149+
try rotations.encode(into: &data, version: version)
157150
try bind.encode(into: &data, version: version)
158151
}
159152

160153
init(decoding data: UnsafeRawBufferPointer, at offset: inout Int, version: BinaryCodableVersion) throws {
161-
self.times = try Float.decodeArray(data, offset: &offset, version: version)
154+
self.times = try Array<Float>(decoding: data, at: &offset, version: version)
162155
self.interpolation = try Interpolation(decoding: data, at: &offset, version: version)
163-
self.rotations = try Quaternion.decodeArray(data, offset: &offset, version: version)
156+
self.rotations = try Array<Quaternion>(decoding: data, at: &offset, version: version)
164157
self.bind = try Quaternion(decoding: data, at: &offset, version: version)
165158
}
166159
}
@@ -179,22 +172,20 @@ public extension RawSkeletalAnimation {
179172
}
180173

181174
func encode(into data: inout ContiguousArray<UInt8>, version: BinaryCodableVersion) throws {
182-
try Float.encodeArray(times, into: &data, version: version)
175+
try times.encode(into: &data, version: version)
183176
try interpolation.encode(into: &data, version: version)
184-
try Size3.encodeArray(scales, into: &data, version: version)
177+
try scales.encode(into: &data, version: version)
185178
try bind.encode(into: &data, version: version)
186179
}
187180

188181
init(decoding data: UnsafeRawBufferPointer, at offset: inout Int, version: BinaryCodableVersion) throws {
189-
self.times = try Float.decodeArray(data, offset: &offset, version: version)
182+
self.times = try Array<Float>(decoding: data, at: &offset, version: version)
190183
self.interpolation = try Interpolation(decoding: data, at: &offset, version: version)
191-
self.scales = try Size3.decodeArray(data, offset: &offset, version: version)
184+
self.scales = try Array<Size3>(decoding: data, at: &offset, version: version)
192185
self.bind = try Size3(decoding: data, at: &offset, version: version)
193186
}
194187
}
195188

196-
197-
198189
public func encode(into data: inout ContiguousArray<UInt8>, version: BinaryCodableVersion) throws {
199190
try self.positionOutput.encode(into: &data, version: version)
200191
try self.rotationOutput.encode(into: &data, version: version)

Sources/GateEngine/Resources/Skinning/Raw/RawSkin.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ public struct RawSkin: Hashable, BinaryCodable {
3737

3838
extension RawSkin {
3939
public func encode(into data: inout ContiguousArray<UInt8>, version: BinaryCodableVersion) throws {
40-
try RawJoint.encodeArray(joints, into: &data, version: version)
41-
try UInt32.encodeArray(jointIndices, into: &data, version: version)
42-
try Float32.encodeArray(jointWeights, into: &data, version: version)
40+
try joints.encode(into: &data, version: version)
41+
try jointIndices.encode(into: &data, version: version)
42+
try jointWeights.encode(into: &data, version: version)
4343
try self.bindShape.encode(into: &data, version: version)
4444
}
4545

4646
public init(decoding data: UnsafeRawBufferPointer, at offset: inout Int, version: BinaryCodableVersion) throws {
47-
self.joints = try RawJoint.decodeArray(data, offset: &offset, version: version)
48-
self.jointIndices = try UInt32.decodeArray(data, offset: &offset, version: version)
49-
self.jointWeights = try Float32.decodeArray(data, offset: &offset, version: version)
47+
self.joints = try .init(decoding: data, at: &offset, version: version)
48+
self.jointIndices = try .init(decoding: data, at: &offset, version: version)
49+
self.jointWeights = try .init(decoding: data, at: &offset, version: version)
5050
self.bindShape = try Matrix4x4(decoding: data, at: &offset, version: version)
5151
}
5252
}

Sources/GateUtilities/BinaryCodable.swift

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,53 +60,81 @@ public extension BinaryCodable {
6060
data.append(contentsOf: bytes)
6161
}
6262
}
63+
6364
init(decoding data: UnsafeRawBufferPointer, at offset: inout Int, version: BinaryCodableVersion) throws {
6465
offset += MemoryLayout<Self>.alignment - (offset % MemoryLayout<Self>.alignment)
6566

6667
self = data.load(fromByteOffset: offset, as: Self.self)
6768
offset += MemoryLayout<Self>.size
6869
}
69-
70-
static func encodeArray(_ collection: Array<Self>, into data: inout ContiguousArray<UInt8>, version: BinaryCodableVersion) throws {
71-
try collection.count.encode(into: &data, version: version)
72-
for element in collection {
70+
}
71+
72+
extension Array: BinaryCodable where Element: BinaryCodable {}
73+
public extension Array where Element: BinaryCodable {
74+
func encode(into data: inout ContiguousArray<UInt8>, version: BinaryCodableVersion) throws {
75+
try self.count.encode(into: &data, version: version)
76+
for element in self {
7377
try element.encode(into: &data, version: version)
7478
}
7579
}
76-
static func decodeArray(_ data: UnsafeRawBufferPointer, offset: inout Int, version: BinaryCodableVersion) throws -> Array<Self> {
80+
81+
init(decoding data: UnsafeRawBufferPointer, at offset: inout Int, version: BinaryCodableVersion) throws {
7782
let count = try Int(decoding: data, at: &offset, version: version)
7883

79-
var collection: Array<Self> = []
84+
var collection: Array<Element> = []
8085
collection.reserveCapacity(count)
8186
for _ in 0 ..< count {
82-
let element = try Self(decoding: data, at: &offset, version: version)
87+
let element = try Element(decoding: data, at: &offset, version: version)
8388
collection.append(element)
8489
}
8590

86-
return collection
91+
self = collection
8792
}
88-
89-
static func encodeSet(_ collection: Set<Self>, into data: inout ContiguousArray<UInt8>, version: BinaryCodableVersion) throws where Self: Hashable {
90-
try collection.count.encode(into: &data, version: version)
91-
for element in collection {
93+
}
94+
95+
extension Set: BinaryCodable where Element: BinaryCodable {}
96+
public extension Set where Element: BinaryCodable {
97+
func encode(into data: inout ContiguousArray<UInt8>, version: BinaryCodableVersion) throws {
98+
try self.count.encode(into: &data, version: version)
99+
for element in self {
92100
try element.encode(into: &data, version: version)
93101
}
94102
}
95-
static func decodeSet(
96-
_ data: UnsafeRawBufferPointer,
97-
offset: inout Int,
98-
version: BinaryCodableVersion
99-
) throws -> Set<Self> where Self: Hashable {
103+
104+
init(decoding data: UnsafeRawBufferPointer, at offset: inout Int, version: BinaryCodableVersion) throws {
100105
let count = try Int(decoding: data, at: &offset, version: version)
101106

102-
var collection: Set<Self> = []
107+
var collection: Set<Element> = []
103108
collection.reserveCapacity(count)
104109
for _ in 0 ..< count {
105-
let element = try Self(decoding: data, at: &offset, version: version)
110+
let element = try Element(decoding: data, at: &offset, version: version)
106111
collection.insert(element)
107112
}
108113

109-
return collection
114+
self = collection
115+
}
116+
}
117+
118+
extension Dictionary: BinaryCodable where Key: BinaryCodable, Value: BinaryCodable {}
119+
public extension Dictionary where Key: BinaryCodable, Value: BinaryCodable {
120+
func encode(into data: inout ContiguousArray<UInt8>, version: BinaryCodableVersion) throws {
121+
try self.count.encode(into: &data, version: version)
122+
for (key, value) in self {
123+
try key.encode(into: &data, version: version)
124+
try value.encode(into: &data, version: version)
125+
}
126+
}
127+
128+
init(decoding data: UnsafeRawBufferPointer, at offset: inout Int, version: BinaryCodableVersion) throws {
129+
let count = try Int(decoding: data, at: &offset, version: version)
130+
var uniqueKeysWithValues: [(Key, Value)] = []
131+
uniqueKeysWithValues.reserveCapacity(count)
132+
for _ in 0 ..< count {
133+
let key = try Key(decoding: data, at: &offset, version: version)
134+
let value = try Value(decoding: data, at: &offset, version: version)
135+
uniqueKeysWithValues.append((key, value))
136+
}
137+
self.init(uniqueKeysWithValues: uniqueKeysWithValues)
110138
}
111139
}
112140

@@ -145,10 +173,11 @@ extension UInt: BinaryCodable {
145173
extension String: BinaryCodable {
146174
public func encode(into data: inout ContiguousArray<UInt8>, version: BinaryCodableVersion) throws {
147175
let cString = self.cString(using: .utf8)!
148-
try Int8.encodeArray(cString, into: &data, version: version)
176+
try cString.encode(into: &data, version: version)
149177
}
178+
150179
public init(decoding data: UnsafeRawBufferPointer, at offset: inout Int, version: BinaryCodableVersion) throws {
151-
let buffer = try Int8.decodeArray(data, offset: &offset, version: version)
180+
let buffer = try Array<Int8>(decoding: data, at: &offset, version: version)
152181
self.init(utf8String: buffer)!
153182
}
154183
}
@@ -157,6 +186,7 @@ public extension RawRepresentable where Self: BinaryCodable, RawValue: BinaryCod
157186
func encode(into data: inout ContiguousArray<UInt8>, version: BinaryCodableVersion) throws {
158187
try self.rawValue.encode(into: &data, version: version)
159188
}
189+
160190
init(decoding data: UnsafeRawBufferPointer, at offset: inout Int, version: BinaryCodableVersion) throws {
161191
let rawValue = try RawValue(decoding: data, at: &offset, version: version)
162192
self = .init(rawValue: rawValue)!

0 commit comments

Comments
 (0)