Crossmate/Models/PuzzleCatalog.swift (5534B)
1import Foundation
2
3
4
5
6
7struct PuzzleCatalog {
8 struct Entry: Identifiable {
9 let id: String
10 let title: String
11 let publisher: String?
12 let gridWidth: Int
13 let gridHeight: Int
14
15
16 let blockCells: [Bool]
17
18
19 let sourceURL: URL
20
21
22 func loadSource() throws -> String {
23 try String(contentsOf: sourceURL, encoding: .utf8)
24 }
25 }
26
27
28 struct PuzzleBundle: Identifiable {
29 let id: String
30 let name: String
31 let puzzles: [Entry]
32 }
33
34
35 private static let debugBundleID = "debug"
36
37
38
39
40 private static let allBundles: [PuzzleBundle] = loadAllBundles()
41
42
43
44 static func bundles(includeDebug: Bool = false) -> [PuzzleBundle] {
45 includeDebug ? allBundles : allBundles.filter { $0.id != debugBundleID }
46 }
47
48 static func source(
49 matchingResourceID resourceID: String?,
50 title: String?
51 ) -> Entry? {
52 let entries = allPuzzles()
53 if let resourceID {
54 return entries.first { $0.id == resourceID }
55 } else if let title {
56 return entries.first { $0.title == title }
57 } else {
58 return nil
59 }
60 }
61
62
63
64
65 static func resourceID(matching source: String) -> String? {
66 guard let title = try? XD.parse(source).title else { return nil }
67 return allPuzzles()
68 .filter { $0.title == title }
69 .first { (try? $0.loadSource()) == source }?
70 .id
71 }
72
73
74 private static func allPuzzles() -> [Entry] {
75 allBundles.flatMap(\.puzzles)
76 }
77
78
79
80 private struct Manifest: Decodable {
81 let version: Int
82 let bundleID: String
83 let name: String
84 let puzzles: [ManifestPuzzle]
85 }
86
87 private struct ManifestPuzzle: Decodable {
88 let id: String
89 let title: String
90 let publisher: String?
91 let gridWidth: Int
92 let gridHeight: Int
93 let blockMask: String
94 }
95
96 private static func loadAllBundles() -> [PuzzleBundle] {
97 guard let puzzlesURL = Bundle.main.resourceURL?
98 .appendingPathComponent("Puzzles", isDirectory: true) else {
99 return []
100 }
101 let directories = (try? FileManager.default.contentsOfDirectory(
102 at: puzzlesURL,
103 includingPropertiesForKeys: [.isDirectoryKey]
104 )) ?? []
105
106 return directories
107 .compactMap { url -> PuzzleBundle? in
108 guard (try? url.resourceValues(forKeys: [.isDirectoryKey]))?
109 .isDirectory == true else {
110 return nil
111 }
112 return bundle(in: url)
113 }
114 .sorted { $0.id.localizedStandardCompare($1.id) == .orderedAscending }
115 }
116
117
118
119
120 private static func bundle(in directoryURL: URL) -> PuzzleBundle? {
121 let manifestURL = directoryURL.appendingPathComponent("manifest.json")
122 guard let data = try? Data(contentsOf: manifestURL),
123 let manifest = try? JSONDecoder().decode(Manifest.self, from: data) else {
124 return nil
125 }
126 let entries = manifest.puzzles.map { puzzle in
127 Entry(
128 id: puzzle.id,
129 title: puzzle.title,
130 publisher: puzzle.publisher,
131 gridWidth: puzzle.gridWidth,
132 gridHeight: puzzle.gridHeight,
133 blockCells: puzzle.blockMask.map { $0 == "#" },
134 sourceURL: directoryURL.appendingPathComponent("\(puzzle.id).xd")
135 )
136 }
137 return PuzzleBundle(id: manifest.bundleID, name: manifest.name, puzzles: entries)
138 }
139}