Skip to content

Commit 362f638

Browse files
committed
Refactor importing
1 parent 3b73a1b commit 362f638

File tree

12 files changed

+54
-44
lines changed

12 files changed

+54
-44
lines changed

Sources/GateEngine/GateEngine.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ extension Color {
5353

5454
public enum GateEngineError: Error, Equatable, Hashable, CustomStringConvertible {
5555
/// An error to represent any kind of error
56-
case custom(_ errorName: String, _ description: String)
56+
case custom(category: String, message: String)
5757

5858
case failedToLocate(resource: String, _ reason: String?)
5959
case failedToLoad(resource: String, _ reason: String?)
@@ -69,8 +69,8 @@ public enum GateEngineError: Error, Equatable, Hashable, CustomStringConvertible
6969

7070
public var description: String {
7171
switch self {
72-
case .custom(let errorName, let reason):
73-
return "\(errorName):\n\t" + reason.replacingOccurrences(of: "\n", with: "\n\t")
72+
case .custom(let category, let message):
73+
return "\(category):\n\t" + message.replacingOccurrences(of: "\n", with: "\n\t")
7474

7575
case .failedToLocate(let resource, let reason):
7676
var error = "FailedToLocate: \(resource)"
@@ -138,7 +138,7 @@ public enum GateEngineError: Error, Equatable, Hashable, CustomStringConvertible
138138

139139
extension GateEngineError: ExpressibleByStringLiteral {
140140
public init(stringLiteral value: StringLiteralType) {
141-
self = .custom("error", value)
141+
self = .custom(category: "error", message: value)
142142
}
143143
}
144144

Sources/GateEngine/Resources/Animation/ObjectAnimation3D.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ extension ResourceManager {
622622
throw GateEngineError.failedToLoad(resource: path, "Unknown file type.")
623623
}
624624
guard let importer: any ObjectAnimation3DImporter = Game.unsafeShared.resourceManager.importerForFileType(fileExtension) else {
625-
throw GateEngineError.failedToLoad(resource: path, "No importer for \(fileExtension).")
625+
throw GateEngineError.failedToLoad(resource: path, "No ObjectAnimation3DImporter for \(fileExtension).")
626626
}
627627

628628
let data = try await Platform.current.loadResource(from: path)

Sources/GateEngine/Resources/CollisionMesh/CollisionMesh.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ extension ResourceManager {
283283

284284
do {
285285
guard let importer: any CollisionMeshImporter = try await Game.unsafeShared.resourceManager.importerForFileType(path) else {
286-
throw GateEngineError.failedToLoad(resource: path, "No importer for \(URL(fileURLWithPath: path).pathExtension).")
286+
throw GateEngineError.failedToLoad(resource: path, "No CollisionMeshImporter for \(URL(fileURLWithPath: path).pathExtension).")
287287
}
288288

289289
let rawCollisionMesh = try await importer.loadCollisionMesh(options: key.collisionMeshOptions)

Sources/GateEngine/Resources/Geometry/Geometry.swift

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,33 +178,29 @@ extension ResourceManager {
178178
}
179179
}
180180

181-
func geometryImporterForPath(_ path: String) async throws -> (any GeometryImporter)? {
181+
func geometryImporterForPath(_ path: String) async throws(GateEngineError) -> any GeometryImporter {
182182
for type in self.importers.geometryImporters {
183183
if type.canProcessFile(path) {
184184
return try await self.importers.getImporter(path: path, type: type)
185185
}
186186
}
187-
return nil
187+
throw .custom(category: "\(Self.self)", message: "No GeometryImporter could be found for \(path)")
188188
}
189189
}
190190

191191
extension RawGeometry {
192192
@inlinable @_disfavoredOverload
193-
public init(_ path: GeoemetryPath, options: GeometryImporterOptions = .none) async throws {
193+
public init(_ path: GeoemetryPath, options: GeometryImporterOptions = .none) async throws(GateEngineError) {
194194
try await self.init(path: path.value, options: options)
195195
}
196-
public init(path: String, options: GeometryImporterOptions = .none) async throws {
197-
guard
198-
let importer: any GeometryImporter = try await Game.unsafeShared.resourceManager.geometryImporterForPath(path)
199-
else {
200-
throw GateEngineError.failedToLoad(resource: path, "No importer for \(URL(fileURLWithPath: path).pathExtension).")
201-
}
202-
196+
public init(path: String, options: GeometryImporterOptions = .none) async throws(GateEngineError) {
197+
let importer: any GeometryImporter
203198
do {
204-
self = try await importer.loadGeometry(options: options)
205-
} catch {
206-
throw GateEngineError(error)
199+
importer = try await Game.unsafeShared.resourceManager.geometryImporterForPath(path)
200+
}catch{
201+
throw .failedToLoad(resource: path, "No GeometryImporter for \(URL(fileURLWithPath: path).pathExtension).")
207202
}
203+
self = try await importer.loadGeometry(options: options)
208204
}
209205
}
210206

Sources/GateEngine/Resources/Geometry/Lines.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,18 @@ extension Lines: Equatable, Hashable {
6464

6565
extension RawLines {
6666
@inlinable @_disfavoredOverload
67-
public init(_ path: GeoemetryPath, options: GeometryImporterOptions = .none) async throws {
67+
public init(_ path: GeoemetryPath, options: GeometryImporterOptions = .none) async throws(GateEngineError) {
6868
try await self.init(path: path.value, options: options)
6969
}
70-
public init(path: String, options: GeometryImporterOptions = .none) async throws {
71-
guard let importer: any GeometryImporter = try await Game.unsafeShared.resourceManager.geometryImporterForPath(path) else {
72-
throw GateEngineError.failedToLoad(resource: path, "No importer for \(URL(fileURLWithPath: path).pathExtension).")
73-
}
74-
70+
public init(path: String, options: GeometryImporterOptions = .none) async throws(GateEngineError) {
71+
let importer: any GeometryImporter
7572
do {
76-
self = RawLines(wireframeFrom: try await importer.loadGeometry(options: options).generateTriangles())
77-
} catch {
78-
throw GateEngineError(error)
73+
importer = try await Game.unsafeShared.resourceManager.geometryImporterForPath(path)
74+
}catch{
75+
throw .failedToLoad(resource: path, "No GeometryImporter for \(URL(fileURLWithPath: path).pathExtension).")
7976
}
77+
let rawGeometry = try await importer.loadGeometry(options: options)
78+
self.init(wireframeFrom: rawGeometry.generateTriangles())
8079
}
8180
}
8281

Sources/GateEngine/Resources/Geometry/Points.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,18 @@ extension Points: Equatable, Hashable {
6464

6565
extension RawPoints {
6666
@inlinable @_disfavoredOverload
67-
public init(_ path: GeoemetryPath, options: GeometryImporterOptions = .none) async throws {
67+
public init(_ path: GeoemetryPath, options: GeometryImporterOptions = .none) async throws(GateEngineError) {
6868
try await self.init(path: path.value, options: options)
6969
}
70-
public init(path: String, options: GeometryImporterOptions = .none) async throws {
71-
guard let importer: any GeometryImporter = try await Game.unsafeShared.resourceManager.geometryImporterForPath(path) else {
72-
throw GateEngineError.failedToLoad(resource: path, "No importer for \(URL(fileURLWithPath: path).pathExtension).")
73-
}
74-
70+
public init(path: String, options: GeometryImporterOptions = .none) async throws(GateEngineError) {
71+
let importer: any GeometryImporter
7572
do {
76-
self = RawPoints(pointCloudFrom: try await importer.loadGeometry(options: options).generateTriangles())
77-
} catch {
78-
throw GateEngineError(error)
73+
importer = try await Game.unsafeShared.resourceManager.geometryImporterForPath(path)
74+
}catch{
75+
throw .failedToLoad(resource: path, "No GeometryImporter for \(URL(fileURLWithPath: path).pathExtension).")
7976
}
77+
let rawGeometry = try await importer.loadGeometry(options: options)
78+
self.init(pointCloudFrom: rawGeometry.generateTriangles())
8079
}
8180
}
8281

Sources/GateEngine/Resources/Skinning/SkeletalAnimation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ extension RawSkeletalAnimation {
366366
guard
367367
let importer: any SkeletalAnimationImporter = try await Game.unsafeShared.resourceManager.skeletalAnimationImporterForPath(path)
368368
else {
369-
throw GateEngineError.failedToLoad(resource: path, "No importer for \(URL(fileURLWithPath: path).pathExtension).")
369+
throw GateEngineError.failedToLoad(resource: path, "No SkeletalAnimationImporter for \(URL(fileURLWithPath: path).pathExtension).")
370370
}
371371

372372
do {

Sources/GateEngine/Resources/Skinning/Skeleton.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ final class SkeletonBackend {
126126

127127
init(rawSkeleton: RawSkeleton) {
128128
func createJoint(from rawJoint: RawSkeleton.RawJoint) -> Skeleton.Joint {
129-
var joint = Skeleton.Joint(rawJoint: rawJoint)
129+
let joint = Skeleton.Joint(rawJoint: rawJoint)
130130
joint.localTransform = rawJoint.localTransform
131131
for rawJoint in rawSkeleton.joints {
132132
if rawJoint.parent == joint.id {
@@ -581,7 +581,7 @@ extension RawSkeleton {
581581
guard
582582
let importer: any SkeletonImporter = try await Game.unsafeShared.resourceManager.skeletonImporterForPath(path)
583583
else {
584-
throw GateEngineError.failedToLoad(resource: path, "No importer for \(URL(fileURLWithPath: path).pathExtension).")
584+
throw GateEngineError.failedToLoad(resource: path, "No SkeletonImporter for \(URL(fileURLWithPath: path).pathExtension).")
585585
}
586586

587587
do {

Sources/GateEngine/Resources/Skinning/Skin.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ extension ResourceManager {
8484
extension RawSkin {
8585
public init(path: String, options: SkinImporterOptions = .none) async throws(GateEngineError) {
8686
guard let importer: any SkinImporter = try await Game.unsafeShared.resourceManager.skinImporterForPath(path) else {
87-
throw .failedToLoad(resource: path, "No importer for \(URL(fileURLWithPath: path).pathExtension).")
87+
throw .failedToLoad(resource: path, "No SkinImporter for \(URL(fileURLWithPath: path).pathExtension).")
8888
}
8989

9090
do {

Sources/GateEngine/Resources/Texture/Texture.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,23 @@ extension ResourceManager {
206206
return try await self.importers.getImporter(path: path, type: type)
207207
}
208208
}
209-
throw .custom("ResourceManager", "No TextureImporter could be found for \(path)")
209+
throw .custom(category: "\(Self.self)", message: "No TextureImporter could be found for \(path)")
210+
}
211+
}
212+
213+
extension RawTexture {
214+
@inlinable @_disfavoredOverload
215+
public init(_ path: TexturePath, options: TextureImporterOptions = .none) async throws {
216+
try await self.init(path: path.value, options: options)
217+
}
218+
public init(path: String, options: TextureImporterOptions = .none) async throws {
219+
let importer: any TextureImporter
220+
do {
221+
importer = try await Game.unsafeShared.resourceManager.textureImporterForPath(path)
222+
}catch{
223+
throw GateEngineError.failedToLoad(resource: path, "No TextureImporter for \(URL(fileURLWithPath: path).pathExtension).")
224+
}
225+
self = try await importer.loadTexture(options: options)
210226
}
211227
}
212228

0 commit comments

Comments
 (0)