Skip to content

Commit da5e2bc

Browse files
committed
Refactor ObjectAnimation3D importing
1 parent 128d5c3 commit da5e2bc

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

Sources/GateEngine/Resources/Animation/ObjectAnimation3D.swift

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Foundation
1717
}
1818

1919
@usableFromInline
20-
internal var backend: ObjectAnimation3DBackend {
20+
internal var backend: RawObjectAnimation3D {
2121
assert(state == .ready, "This resource is not ready to be used. Make sure it's state property is .ready before accessing!")
2222
return Game.unsafeShared.resourceManager.objectAnimation3DCache(for: cacheKey)!.objectAnimation3DBackend!
2323
}
@@ -445,7 +445,7 @@ extension ObjectAnimation3D {
445445
}
446446
}
447447

448-
public final class ObjectAnimation3DBackend {
448+
public struct RawObjectAnimation3D {
449449
let name: String
450450
let duration: Float
451451
let animation: ObjectAnimation3D.Animation
@@ -460,9 +460,7 @@ public final class ObjectAnimation3DBackend {
460460
// MARK: - Resource Manager
461461

462462
public protocol ObjectAnimation3DImporter: ResourceImporter {
463-
init()
464-
465-
func process(data: Data, baseURL: URL, options: ObjectAnimation3DImporterOptions) async throws -> ObjectAnimation3DBackend
463+
func loadObjectAnimation(options: ObjectAnimation3DImporterOptions) async throws(GateEngineError) -> RawObjectAnimation3D
466464
}
467465

468466
public struct ObjectAnimation3DImporterOptions: Equatable, Hashable, Sendable {
@@ -495,6 +493,13 @@ extension ResourceManager {
495493
}
496494
}
497495

496+
extension RawObjectAnimation3D {
497+
init(path: String, options: ObjectAnimation3DImporterOptions = .none) async throws {
498+
let importer: any ObjectAnimation3DImporter = try await Game.unsafeShared.resourceManager.objectAnimation3DImporterForPath(path)
499+
self = try await importer.loadObjectAnimation(options: options)
500+
}
501+
}
502+
498503
extension ResourceManager.Cache {
499504
@usableFromInline
500505
struct ObjectAnimation3DKey: Hashable, CustomStringConvertible, Sendable {
@@ -517,7 +522,7 @@ extension ResourceManager.Cache {
517522

518523
@usableFromInline
519524
final class ObjectAnimation3DCache: ResourceCache {
520-
@usableFromInline var objectAnimation3DBackend: ObjectAnimation3DBackend?
525+
@usableFromInline var objectAnimation3DBackend: RawObjectAnimation3D?
521526
var lastLoaded: Date
522527
var state: ResourceState
523528
var referenceCount: UInt
@@ -565,7 +570,7 @@ extension ResourceManager {
565570
cache.objectAnimation3Ds[key] = Cache.ObjectAnimation3DCache()
566571
Game.unsafeShared.resourceManager.incrementLoading(path: key.requestedPath)
567572
Task.detached {
568-
let backend = ObjectAnimation3DBackend(
573+
let backend = RawObjectAnimation3D(
569574
name: name,
570575
duration: duration,
571576
animation: animation
@@ -618,18 +623,11 @@ extension ResourceManager {
618623
let path = key.requestedPath
619624

620625
do {
621-
let importer: any ObjectAnimation3DImporter = try await Game.unsafeShared.resourceManager.objectAnimation3DImporterForPath(path)
622-
623-
let data = try await Platform.current.loadResource(from: path)
624-
let backend = try await importer.process(
625-
data: data,
626-
baseURL: URL(string: path)!.deletingLastPathComponent(),
627-
options: key.options
628-
)
626+
let rawObjectAnimation = try await RawObjectAnimation3D(path: key.requestedPath, options: key.options)
629627

630628
Task { @MainActor in
631629
if let cache = cache.objectAnimation3Ds[key] {
632-
cache.objectAnimation3DBackend = backend
630+
cache.objectAnimation3DBackend = rawObjectAnimation
633631
cache.state = .ready
634632
}else{
635633
Log.warn("Resource \"\(path)\" was deallocated before being " + (isFirstLoad ? "loaded." : "re-loaded."))

Sources/GateEngine/Resources/Import & Export/Importers/GLTransmissionFormat.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,10 +1134,8 @@ extension GLTransmissionFormat: SkeletalAnimationImporter {
11341134
}
11351135
}
11361136

1137-
extension GLTransmissionFormat: ObjectAnimation3DImporter {
1138-
public func process(data: Data, baseURL: URL, options: ObjectAnimation3DImporterOptions) async throws -> ObjectAnimation3DBackend {
1139-
let gltf = try gltf(from: data, baseURL: baseURL)
1140-
1137+
extension GLTransmissionFormat: ObjectAnimation3DImporter {
1138+
public func loadObjectAnimation(options: ObjectAnimation3DImporterOptions) async throws(GateEngineError) -> RawObjectAnimation3D {
11411139
guard let animation = animation(named: options.subobjectName, from: gltf) else {
11421140
throw GateEngineError.failedToDecode(
11431141
"Couldn't find animation: \"\(options.subobjectName!)\".\nAvailable Animations: \((gltf.animations ?? []).map({$0.name}))"
@@ -1232,7 +1230,7 @@ extension GLTransmissionFormat: ObjectAnimation3DImporter {
12321230
objectAnimation.rotationOutput.times = objectAnimation.rotationOutput.times.map({$0 - timeMin})
12331231
objectAnimation.scaleOutput.times = objectAnimation.scaleOutput.times.map({$0 - timeMin})
12341232

1235-
return ObjectAnimation3DBackend(name: animation.name, duration: timeMax, animation: objectAnimation)
1233+
return RawObjectAnimation3D(name: animation.name, duration: timeMax, animation: objectAnimation)
12361234
}
12371235
}
12381236

0 commit comments

Comments
 (0)