Skip to content

Commit a6fcbcc

Browse files
committed
Add BinaryCodable
1 parent 231c324 commit a6fcbcc

21 files changed

+471
-9
lines changed

Sources/GameMath/2D Types/Direction2.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public extension Direction2 {
4545
extension Direction2: Equatable {}
4646
extension Direction2: Hashable {}
4747
extension Direction2: Codable {}
48+
extension Direction2: BinaryCodable {}
4849

4950
public extension Direction2 {
5051
@inlinable

Sources/GameMath/2D Types/Position2.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public struct Position2: Vector2, Sendable {
5151
extension Position2: Equatable {}
5252
extension Position2: Hashable {}
5353
extension Position2: Codable {}
54+
extension Position2: BinaryCodable {}
5455

5556
public extension Position2 {
5657
@inlinable

Sources/GameMath/2D Types/Size2.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ extension Size2: Equatable {}
4545
extension Size2: Hashable {}
4646
extension Size2: Comparable {}
4747
extension Size2: Codable {}
48+
extension Size2: BinaryCodable {}
4849

4950
//MARK: Vector2
5051
extension Size2 {

Sources/GameMath/2D Types/Transform2.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,17 @@ extension Transform2: Codable {
176176
self.scale = Size2(width: values[3], height: values[4])
177177
}
178178
}
179+
180+
extension Transform2: BinaryCodable {
181+
public func encode(into data: inout ContiguousArray<UInt8>, version: BinaryCodableVersion) throws {
182+
try self.position.encode(into: &data, version: version)
183+
try self.rotation.encode(into: &data, version: version)
184+
try self.scale.encode(into: &data, version: version)
185+
}
186+
public init(decoding data: UnsafeRawBufferPointer, at offset: inout Int, version: BinaryCodableVersion) throws {
187+
let position = try Position2(decoding: data, at: &offset, version: version)
188+
let rotation = try Degrees(decoding: data, at: &offset, version: version)
189+
let scale = try Size2(decoding: data, at: &offset, version: version)
190+
self.init(position: position, rotation: rotation, scale: scale)
191+
}
192+
}

Sources/GameMath/2D Types/Vector2.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ extension Vector2 {
432432
}
433433
}
434434

435-
extension Vector2 {
435+
extension Vector2 where Self: Codable {
436436
@inlinable
437437
public func encode(to encoder: any Encoder) throws {
438438
var container = encoder.singleValueContainer()
@@ -447,6 +447,19 @@ extension Vector2 {
447447
}
448448
}
449449

450+
extension Vector2 where Self: BinaryCodable {
451+
public func encode(into data: inout ContiguousArray<UInt8>, version: BinaryCodableVersion) throws {
452+
try self.x.encode(into: &data, version: version)
453+
try self.y.encode(into: &data, version: version)
454+
}
455+
public init(decoding data: UnsafeRawBufferPointer, at offset: inout Int, version: BinaryCodableVersion) throws {
456+
let x = try Float(decoding: data, at: &offset, version: version)
457+
let y = try Float(decoding: data, at: &offset, version: version)
458+
self.init(x, y)
459+
}
460+
}
461+
462+
450463
extension Vector2 {
451464
@inlinable
452465
public func valuesArray() -> [Float] {return [x, y]}

Sources/GameMath/3D Types/Direction3.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,4 @@ extension Direction3: Codable {
197197
self.init(values[0], values[1], values[2])
198198
}
199199
}
200+
extension Direction3: BinaryCodable {}

Sources/GameMath/3D Types/Matrix3x3.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,30 @@ extension Matrix3x3: Codable {
291291
self.k = try container.decode(Float.self)
292292
}
293293
}
294+
295+
extension Matrix3x3: BinaryCodable {
296+
public func encode(into data: inout ContiguousArray<UInt8>, version: BinaryCodableVersion) throws {
297+
try self.a.encode(into: &data, version: version)
298+
try self.b.encode(into: &data, version: version)
299+
try self.c.encode(into: &data, version: version)
300+
try self.e.encode(into: &data, version: version)
301+
try self.f.encode(into: &data, version: version)
302+
try self.g.encode(into: &data, version: version)
303+
try self.i.encode(into: &data, version: version)
304+
try self.j.encode(into: &data, version: version)
305+
try self.k.encode(into: &data, version: version)
306+
}
307+
308+
public init(decoding data: UnsafeRawBufferPointer, at offset: inout Int, version: BinaryCodableVersion) throws {
309+
let a = try Float(decoding: data, at: &offset, version: version)
310+
let b = try Float(decoding: data, at: &offset, version: version)
311+
let c = try Float(decoding: data, at: &offset, version: version)
312+
let e = try Float(decoding: data, at: &offset, version: version)
313+
let f = try Float(decoding: data, at: &offset, version: version)
314+
let g = try Float(decoding: data, at: &offset, version: version)
315+
let i = try Float(decoding: data, at: &offset, version: version)
316+
let j = try Float(decoding: data, at: &offset, version: version)
317+
let k = try Float(decoding: data, at: &offset, version: version)
318+
self.init(a, b, c, e, f, g, i, j, k)
319+
}
320+
}

Sources/GameMath/3D Types/Matrix4x4.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,3 +796,44 @@ extension Matrix4x4: Codable {
796796
self.init(storage)
797797
}
798798
}
799+
800+
extension Matrix4x4: BinaryCodable {
801+
public func encode(into data: inout ContiguousArray<UInt8>, version: BinaryCodableVersion) throws {
802+
try self.a.encode(into: &data, version: version)
803+
try self.b.encode(into: &data, version: version)
804+
try self.c.encode(into: &data, version: version)
805+
try self.d.encode(into: &data, version: version)
806+
try self.e.encode(into: &data, version: version)
807+
try self.f.encode(into: &data, version: version)
808+
try self.g.encode(into: &data, version: version)
809+
try self.h.encode(into: &data, version: version)
810+
try self.i.encode(into: &data, version: version)
811+
try self.j.encode(into: &data, version: version)
812+
try self.k.encode(into: &data, version: version)
813+
try self.l.encode(into: &data, version: version)
814+
try self.m.encode(into: &data, version: version)
815+
try self.n.encode(into: &data, version: version)
816+
try self.o.encode(into: &data, version: version)
817+
try self.p.encode(into: &data, version: version)
818+
}
819+
820+
public init(decoding data: UnsafeRawBufferPointer, at offset: inout Int, version: BinaryCodableVersion) throws {
821+
let a = try Float(decoding: data, at: &offset, version: version)
822+
let b = try Float(decoding: data, at: &offset, version: version)
823+
let c = try Float(decoding: data, at: &offset, version: version)
824+
let d = try Float(decoding: data, at: &offset, version: version)
825+
let e = try Float(decoding: data, at: &offset, version: version)
826+
let f = try Float(decoding: data, at: &offset, version: version)
827+
let g = try Float(decoding: data, at: &offset, version: version)
828+
let h = try Float(decoding: data, at: &offset, version: version)
829+
let i = try Float(decoding: data, at: &offset, version: version)
830+
let j = try Float(decoding: data, at: &offset, version: version)
831+
let k = try Float(decoding: data, at: &offset, version: version)
832+
let l = try Float(decoding: data, at: &offset, version: version)
833+
let m = try Float(decoding: data, at: &offset, version: version)
834+
let n = try Float(decoding: data, at: &offset, version: version)
835+
let o = try Float(decoding: data, at: &offset, version: version)
836+
let p = try Float(decoding: data, at: &offset, version: version)
837+
self.init(a: a, b: b, c: c, d: d, e: e, f: f, g: g, h: h, i: i, j: j, k: k, l: l, m: m, n: n, o: o, p: p)
838+
}
839+
}

Sources/GameMath/3D Types/Position3.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,4 @@ extension Position3: Codable {
192192
self.init(values[0], values[1], values[2])
193193
}
194194
}
195-
195+
extension Position3: BinaryCodable {}

Sources/GameMath/3D Types/Quaternion.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,3 +629,5 @@ extension Quaternion: Codable {
629629
self.w = values[3]
630630
}
631631
}
632+
633+
extension Quaternion: BinaryCodable {}

0 commit comments

Comments
 (0)