macOS: load device models from bundle resources

This commit is contained in:
Josh Palmer
2025-12-28 16:39:40 +01:00
parent 10eced9971
commit 0f7029583c

View File

@@ -104,8 +104,8 @@ enum DeviceModelCatalog {
}
private static func loadMapping(resourceName: String) -> [String: String] {
guard let url = Bundle.module.url(
forResource: resourceName,
guard let url = self.resourceURL(
resourceName: resourceName,
withExtension: "json",
subdirectory: "DeviceModels")
else {
@@ -121,6 +121,37 @@ enum DeviceModelCatalog {
}
}
private static func resourceURL(
resourceName: String,
withExtension ext: String,
subdirectory: String
) -> URL? {
let bundledSubdir = "Clawdis_Clawdis.bundle/\(subdirectory)"
let mainBundle = Bundle.main
if let url = mainBundle.url(forResource: resourceName, withExtension: ext, subdirectory: bundledSubdir)
?? mainBundle.url(forResource: resourceName, withExtension: ext, subdirectory: subdirectory)
{
return url
}
let fallbackBases = [
mainBundle.resourceURL,
mainBundle.bundleURL.appendingPathComponent("Contents/Resources"),
mainBundle.bundleURL.deletingLastPathComponent(),
].compactMap { $0 }
let fileName = "\(resourceName).\(ext)"
for base in fallbackBases {
let bundled = base.appendingPathComponent(bundledSubdir).appendingPathComponent(fileName)
if FileManager.default.fileExists(atPath: bundled.path) { return bundled }
let loose = base.appendingPathComponent(subdirectory).appendingPathComponent(fileName)
if FileManager.default.fileExists(atPath: loose.path) { return loose }
}
return nil
}
private enum NameValue: Decodable {
case string(String)
case stringArray([String])