|
23 | 23 |
|
24 | 24 | import Foundation |
25 | 25 |
|
26 | | -struct Config { |
27 | | - |
28 | | - struct Keys { |
29 | | - static let projectName = "project_name" |
30 | | - static let bundleID = "bundle_id" |
31 | | - static let unityPath = "unity_path" |
32 | | - static let unityVersion = "unity_version" |
33 | | - static let unitySceneName = "unity_scene_name" |
| 26 | +struct Config: Decodable { |
| 27 | + |
| 28 | + enum ConfigKeys: String, CodingKey { |
| 29 | + case ios |
| 30 | + case unity |
34 | 31 | } |
35 | 32 |
|
| 33 | + let iOS: iOSConfig |
| 34 | + let unity: UnityConfig |
| 35 | + |
| 36 | + init(from decoder: Decoder) throws { |
| 37 | + let container = try decoder.container(keyedBy: ConfigKeys.self) |
| 38 | + self.iOS = try iOSConfig(from: container.superDecoder(forKey: .ios)) |
| 39 | + self.unity = try UnityConfig(from: container.superDecoder(forKey: .unity)) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +struct iOSConfig: Decodable { |
36 | 44 | let projectName: String |
37 | | - let bundleID: String |
38 | | - let unityPath: String |
39 | | - let unityVersion: String |
40 | | - let unitySceneName: String |
41 | | - let unityProjectPath: String |
42 | | - let iOSProjectPath: String |
| 45 | + let bundleId: String |
| 46 | + let projectPath: String |
43 | 47 |
|
44 | | - let relativeIOSBuildPath: String |
45 | | - let relativeUnityClassesPath: String |
46 | | - let relativeUnityLibrariesPath: String |
47 | | - let relativeUnityDataPath: String |
48 | | - |
49 | | - |
50 | | - init?(json: [String : String], currentPath: String) { |
51 | | - guard let projectName = json[Keys.projectName], |
52 | | - let bundleID = json[Keys.bundleID], !bundleID.isEmpty, |
53 | | - let unityPath = json[Config.Keys.unityPath], !unityPath.isEmpty, |
54 | | - let unityVersion = json[Config.Keys.unityVersion], !unityVersion.isEmpty, |
55 | | - let sceneName = json[Config.Keys.unitySceneName] else { |
56 | | - return nil |
| 48 | + private enum Keys: String, CodingKey { |
| 49 | + case projectName |
| 50 | + case bundleId |
| 51 | + case projectPath |
| 52 | + } |
| 53 | + |
| 54 | + init(from decoder: Decoder) throws { |
| 55 | + let container = try decoder.container(keyedBy: Keys.self) |
| 56 | + let projectName = try container.decode(String.self, forKey: .projectName) |
| 57 | + guard !projectName.isEmpty else { |
| 58 | + throw UBKitError.invalidConfigArgument(Keys.projectName.rawValue) |
| 59 | + } |
| 60 | + |
| 61 | + let bundleId = try container.decode(String.self, forKey: .bundleId) |
| 62 | + guard !bundleId.isEmpty else { |
| 63 | + throw UBKitError.invalidConfigArgument(Keys.bundleId.rawValue) |
57 | 64 | } |
58 | 65 |
|
59 | | - if projectName.isEmpty { |
60 | | - guard let folderName = currentPath.components(separatedBy: "/").last else { |
61 | | - return nil |
62 | | - } |
63 | | - self.projectName = folderName |
| 66 | + let projectPath: String |
| 67 | + if let path = try? container.decode(String.self, forKey: .projectPath), !path.isEmpty { |
| 68 | + projectPath = path |
64 | 69 | } else { |
65 | | - self.projectName = projectName |
| 70 | + projectPath = FileManager.default.currentDirectoryPath.appending("/iOS/") |
66 | 71 | } |
67 | 72 |
|
68 | | - if sceneName.isEmpty { |
69 | | - self.unitySceneName = self.projectName |
| 73 | + self.projectName = projectName |
| 74 | + self.bundleId = bundleId |
| 75 | + self.projectPath = projectPath |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +struct UnityConfig { |
| 80 | + let projectName: String |
| 81 | + let applicationPath: String |
| 82 | + let version: String |
| 83 | + let projectPath: String |
| 84 | + let sceneNames: [String] |
| 85 | + let relativeIOSBuildPath: String |
| 86 | + let relativeClassesPath: String |
| 87 | + let relativeLibrariesPath: String |
| 88 | + let relativeDataPath: String |
| 89 | + |
| 90 | + private enum Keys: String, CodingKey { |
| 91 | + case applicationPath |
| 92 | + case version |
| 93 | + case projectPath |
| 94 | + case projectName |
| 95 | + case sceneNames |
| 96 | + } |
| 97 | + |
| 98 | + init(from decoder: Decoder) throws { |
| 99 | + let container = try decoder.container(keyedBy: Keys.self) |
| 100 | + let projectName = try container.decode(String.self, forKey: .projectName) |
| 101 | + guard !projectName.isEmpty else { |
| 102 | + throw UBKitError.invalidConfigArgument(Keys.projectName.rawValue) |
| 103 | + } |
| 104 | + |
| 105 | + let appPath = try container.decode(String.self, forKey: .applicationPath) |
| 106 | + guard !appPath.isEmpty else { |
| 107 | + throw UBKitError.invalidConfigArgument(Keys.applicationPath.rawValue) |
| 108 | + } |
| 109 | + |
| 110 | + let version = try container.decode(String.self, forKey: .version) |
| 111 | + guard !version.isEmpty else { |
| 112 | + throw UBKitError.invalidConfigArgument(Keys.version.rawValue) |
| 113 | + } |
| 114 | + |
| 115 | + let projectPath: String |
| 116 | + if let path = try? container.decode(String.self, forKey: .projectPath), !path.isEmpty { |
| 117 | + projectPath = path |
70 | 118 | } else { |
71 | | - self.unitySceneName = sceneName |
| 119 | + projectPath = FileManager.default.currentDirectoryPath.appending("/Unity/") |
72 | 120 | } |
73 | 121 |
|
74 | | - self.bundleID = bundleID |
75 | | - self.unityPath = unityPath |
76 | | - self.unityVersion = unityVersion |
| 122 | + let sceneNames = try container.decode([String].self, forKey: .sceneNames) |
| 123 | + guard sceneNames.count > 0 else { |
| 124 | + throw UBKitError.invalidConfigArgument(Keys.sceneNames.rawValue) |
| 125 | + } |
77 | 126 |
|
78 | | - self.unityProjectPath = currentPath.appending("/Unity/") |
79 | | - self.iOSProjectPath = currentPath.appending("/iOS/") |
| 127 | + self.projectName = projectName |
| 128 | + self.applicationPath = appPath |
| 129 | + self.version = version |
| 130 | + self.projectPath = projectPath |
| 131 | + self.sceneNames = sceneNames |
80 | 132 |
|
81 | | - self.relativeIOSBuildPath = self.projectName.appending("/ios_build/") |
82 | | - self.relativeUnityClassesPath = self.projectName.appending("/ios_build/Classes/") |
83 | | - self.relativeUnityLibrariesPath = self.projectName.appending("/ios_build/Libraries/") |
84 | | - self.relativeUnityDataPath = self.projectName.appending("/ios_build/Data/") |
| 133 | + self.relativeIOSBuildPath = projectName.appending("/ios_build/") |
| 134 | + self.relativeClassesPath = self.relativeIOSBuildPath.appending("Classes/") |
| 135 | + self.relativeLibrariesPath = self.relativeIOSBuildPath.appending("Libraries/") |
| 136 | + self.relativeDataPath = self.relativeIOSBuildPath.appending("Data/") |
85 | 137 | } |
86 | 138 | } |
0 commit comments