|
| 1 | +// |
| 2 | +// Adapter.swift |
| 3 | +// Minecraft Silicon |
| 4 | +// |
| 5 | +// Created by Cole Feuer on 2021-12-25. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +class Adapter { |
| 11 | + ///Adapts the base Minecraft version to an Apple Silicon-compatible version, returning its version ID |
| 12 | + static func adaptVersion(versionID: String) async throws -> String { |
| 13 | + let versionDir = Paths.versionsDir.appendingPathComponent(versionID, isDirectory: true) |
| 14 | + |
| 15 | + //Make sure the version directory is actually a version directory |
| 16 | + let versionFileJSON = versionDir.appendingPathComponent("\(versionID).json") |
| 17 | + let versionFileJar = versionDir.appendingPathComponent("\(versionID).jar") |
| 18 | + guard FileManager.default.fileExists(atPath: versionFileJSON.path), |
| 19 | + FileManager.default.fileExists(atPath: versionFileJar.path) else { |
| 20 | + throw AdapterError("\(versionID) is not a valid Minecraft version") |
| 21 | + } |
| 22 | + |
| 23 | + LoggingHelper.main.info("Adapting Minecraft version \(versionID) for arm64") |
| 24 | + |
| 25 | + //Read the version file |
| 26 | + var jsonData = try JSONSerialization.jsonObject(with: Data(contentsOf: versionFileJSON), options: .mutableContainers) as! [String: Any] |
| 27 | + |
| 28 | + //Read the base version |
| 29 | + //let baseVersion = jsonData["assets"] as! String |
| 30 | + |
| 31 | + //Iterate over libraries |
| 32 | + for (i, var library) in (jsonData["libraries"] as! [[String: Any]]).enumerated() { |
| 33 | + //Match library rules |
| 34 | + if let ruleArray = library["rules"] as? [[String: Any]] { |
| 35 | + guard checkVersionRules(ruleArray) else { continue } |
| 36 | + } |
| 37 | + |
| 38 | + //Parse the library name |
| 39 | + let libraryInfo = JavaLibrary(string: library["name"] as! String) |
| 40 | + |
| 41 | + if libraryInfo.group == "org.lwjgl" { |
| 42 | + /** |
| 43 | + LWJGL provides Apple Silicon-compatible builds starting with version 3.3.0, |
| 44 | + so we'll swap out any references to LWJGL libraries to their official releases. |
| 45 | + */ |
| 46 | + LoggingHelper.main.debug("Checking library \(libraryInfo.id)...") |
| 47 | + |
| 48 | + let downloads = library["downloads"] as! [String: Any] |
| 49 | + |
| 50 | + do { |
| 51 | + //Get the official LWJGL file info |
| 52 | + let lwjglFileURL = "https://build.lwjgl.org/release/\(AdapterConstants.lwjglVersion)/bin/\(libraryInfo.id)/\(libraryInfo.id).jar" |
| 53 | + let (lwjglFileHash, lwjglFileSize) = try await analyzeLibraryURL(url: URL(string: lwjglFileURL)!) |
| 54 | + |
| 55 | + //Upgrade the common artifact |
| 56 | + var libraryArtifact = (downloads["artifact"] as! [String: Any]) |
| 57 | + libraryArtifact["sha1"] = lwjglFileHash |
| 58 | + libraryArtifact["size"] = lwjglFileSize |
| 59 | + libraryArtifact["url"] = lwjglFileURL |
| 60 | + |
| 61 | + try DictionaryHelper.update(dictionary: &library, at: ["downloads", "artifact"], with: libraryArtifact) |
| 62 | + } |
| 63 | + |
| 64 | + //Check for natives |
| 65 | + if let nativesID = (library["natives"] as? [String: String])?[AdapterConstants.launcherOSID] { |
| 66 | + LoggingHelper.main.debug("Checking library \(libraryInfo.id) natives...") |
| 67 | + |
| 68 | + //Get the official LWJGL file info |
| 69 | + let lwjglFileURL = "https://build.lwjgl.org/release/\(AdapterConstants.lwjglVersion)/bin/\(libraryInfo.id)/\(libraryInfo.id)-natives-\(AdapterConstants.lwjglOSID)-arm64.jar" |
| 70 | + let (lwjglFileHash, lwjglFileSize) = try await analyzeLibraryURL(url: URL(string: lwjglFileURL)!) |
| 71 | + |
| 72 | + //Update the library classifier |
| 73 | + var libraryClassifier = (downloads["classifiers"] as! [String: [String: Any]])[nativesID]! |
| 74 | + //let libraryPath = URL(fileURLWithPath: libraryClassifier["path"] as! String, isDirectory: false) |
| 75 | + //libraryClassifier["path"] = libraryPath.deletingLastPathComponent().appendingPathComponent(libraryPath.deletingPathExtension().lastPathComponent + Constants.suffixARM + "." + libraryPath.pathExtension).relativePath |
| 76 | + libraryClassifier["sha1"] = lwjglFileHash |
| 77 | + libraryClassifier["size"] = lwjglFileSize |
| 78 | + libraryClassifier["url"] = lwjglFileURL |
| 79 | + |
| 80 | + try DictionaryHelper.update(dictionary: &library, at: ["downloads", "classifiers", nativesID], with: libraryClassifier) |
| 81 | + } |
| 82 | + |
| 83 | + //Copy back to jsonData |
| 84 | + var jsonDataLibraries = jsonData["libraries"] as! [[String: Any]] |
| 85 | + jsonDataLibraries[i] = library |
| 86 | + jsonData["libraries"] = jsonDataLibraries |
| 87 | + } else if libraryInfo.group == "ca.weblite" && libraryInfo.id == "java-objc-bridge" { |
| 88 | + /** |
| 89 | + This is a library used make native Objective-C calls from Java. |
| 90 | + It has been updated to produce universal builds, but Minecraft uses an older version that doesn't support Apple Silicon. |
| 91 | + */ |
| 92 | + LoggingHelper.main.debug("Checking library \(libraryInfo.id)...") |
| 93 | + |
| 94 | + let downloads = library["downloads"] as! [String: Any] |
| 95 | + |
| 96 | + do { |
| 97 | + //Get the native file info |
| 98 | + let updatedFileURL = "https://repo1.maven.org/maven2/ca/weblite/java-objc-bridge/1.1/java-objc-bridge-1.1.jar" |
| 99 | + let (updatedFileHash, updatedFileSize) = try await analyzeLibraryURL(url: URL(string: updatedFileURL)!) |
| 100 | + |
| 101 | + //Upgrade the common artifact |
| 102 | + var libraryArtifact = (downloads["artifact"] as! [String: Any]) |
| 103 | + libraryArtifact["sha1"] = updatedFileHash |
| 104 | + libraryArtifact["size"] = updatedFileSize |
| 105 | + libraryArtifact["url"] = updatedFileURL |
| 106 | + |
| 107 | + try DictionaryHelper.update(dictionary: &library, at: ["downloads", "artifact"], with: libraryArtifact) |
| 108 | + } |
| 109 | + |
| 110 | + //Copy back to jsonData |
| 111 | + var jsonDataLibraries = jsonData["libraries"] as! [[String: Any]] |
| 112 | + jsonDataLibraries[i] = library |
| 113 | + jsonData["libraries"] = jsonDataLibraries |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + //Update version ID |
| 118 | + jsonData["id"] = (jsonData["id"] as! String) + Constants.suffixARM |
| 119 | + |
| 120 | + //Create the new the version directory |
| 121 | + let updatedVersionID = versionID + Constants.suffixARM |
| 122 | + let updatedVersionDir = Paths.versionsDir.appendingPathComponent(updatedVersionID) |
| 123 | + let updatedVersionFileJSON = updatedVersionDir.appendingPathComponent("\(updatedVersionID).json") |
| 124 | + let updatedVersionFileJar = updatedVersionDir.appendingPathComponent("\(updatedVersionID).jar") |
| 125 | + |
| 126 | + if !FileManager.default.fileExists(atPath: updatedVersionDir.path) { |
| 127 | + try FileManager.default.createDirectory(at: updatedVersionDir, withIntermediateDirectories: false, attributes: .none) |
| 128 | + } |
| 129 | + |
| 130 | + //Copy over the jar file |
| 131 | + try FileManager.default.copyItem(at: versionFileJar, to: updatedVersionFileJar) |
| 132 | + |
| 133 | + //Write the updated JSON file |
| 134 | + do { |
| 135 | + let outputStream = OutputStream(url: updatedVersionFileJSON, append: false)! |
| 136 | + outputStream.open() |
| 137 | + defer { outputStream.close() } |
| 138 | + |
| 139 | + var error: NSError? |
| 140 | + JSONSerialization.writeJSONObject(jsonData, to: outputStream, options: [.withoutEscapingSlashes], error: &error) |
| 141 | + |
| 142 | + if let error = error { |
| 143 | + throw error |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + LoggingHelper.main.info("Successfully created Minecraft version version \(updatedVersionID)") |
| 148 | + |
| 149 | + return updatedVersionID |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +struct AdapterError: Error { |
| 154 | + let message: String |
| 155 | + |
| 156 | + init(_ message: String) { |
| 157 | + self.message = message |
| 158 | + } |
| 159 | + |
| 160 | + public var errorDescription: String { message } |
| 161 | +} |
0 commit comments