Main.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // Copyright © 2020 osy. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. import Logging
  17. import TipKit
  18. let logger = Logger(label: "com.utmapp.UTM") { label in
  19. var utmLogger = UTMLoggingSwift(label: label)
  20. var stdOutLogger = StreamLogHandler.standardOutput(label: label)
  21. #if DEBUG
  22. utmLogger.logLevel = .debug
  23. stdOutLogger.logLevel = .debug
  24. #endif
  25. return MultiplexLogHandler([
  26. utmLogger,
  27. stdOutLogger
  28. ])
  29. }
  30. @main
  31. class Main {
  32. static var jitAvailable = true
  33. static func main() {
  34. #if (os(iOS) || os(visionOS)) && WITH_JIT
  35. // check if we have jailbreak
  36. if jb_spawn_ptrace_child(CommandLine.argc, CommandLine.unsafeArgv) {
  37. logger.info("JIT: ptrace() child spawn trick")
  38. } else if jb_has_jit_entitlement() {
  39. logger.info("JIT: found entitlement")
  40. } else if jb_has_cs_disabled() {
  41. logger.info("JIT: CS_KILL disabled")
  42. } else if jb_has_cs_execseg_allow_unsigned() {
  43. logger.info("JIT: CS_EXECSEG_ALLOW_UNSIGNED set")
  44. } else if jb_enable_ptrace_hack() {
  45. logger.info("JIT: ptrace() hack supported")
  46. } else {
  47. logger.info("JIT: ptrace() hack failed")
  48. jitAvailable = false
  49. }
  50. // raise memlimits on jailbroken devices
  51. if jb_increase_memlimit() {
  52. logger.info("MEM: successfully removed memory limits")
  53. }
  54. #endif
  55. // do patches
  56. UTMPatches.patchAll()
  57. #if os(iOS) || os(visionOS)
  58. // register defaults
  59. registerDefaultsFromSettingsBundle()
  60. // register tips
  61. if #available(iOS 17, macOS 14, *) {
  62. try? Tips.configure()
  63. }
  64. #endif
  65. UTMApp.main()
  66. }
  67. // https://stackoverflow.com/a/44675628
  68. static private func registerDefaultsFromSettingsBundle() {
  69. let userDefaults = UserDefaults.standard
  70. if let settingsURL = Bundle.main.url(forResource: "Root", withExtension: "plist", subdirectory: "Settings.bundle"),
  71. let settings = NSDictionary(contentsOf: settingsURL),
  72. let preferences = settings["PreferenceSpecifiers"] as? [NSDictionary] {
  73. var defaultsToRegister = [String: AnyObject]()
  74. for prefSpecification in preferences {
  75. if let key = prefSpecification["Key"] as? String,
  76. let value = prefSpecification["DefaultValue"] {
  77. defaultsToRegister[key] = value as AnyObject
  78. logger.debug("registerDefaultsFromSettingsBundle: (\(key), \(value)) \(type(of: value))")
  79. }
  80. }
  81. // register version numbers
  82. if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] {
  83. userDefaults.set(version, forKey: "LastBootedVersion")
  84. }
  85. if let build = Bundle.main.infoDictionary?["CFBundleVersion"] {
  86. userDefaults.set(build, forKey: "LastBootedBuild")
  87. }
  88. userDefaults.register(defaults: defaultsToRegister)
  89. } else {
  90. logger.debug("registerDefaultsFromSettingsBundle: Could not find Settings.bundle")
  91. }
  92. }
  93. }