UTMScriptable.swift 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // Copyright © 2023 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 Foundation
  17. protocol UTMScriptable: Equatable {}
  18. extension UTMScriptable {
  19. /// Run a script command asynchronously
  20. /// - Parameters:
  21. /// - command: Script command to run
  22. /// - body: What to do
  23. @MainActor
  24. func withScriptCommand<Result>(_ command: NSScriptCommand, body: @MainActor @escaping () async throws -> Result) {
  25. command.suspendExecution()
  26. // we need to run this in next event loop due to the need to return before calling resume
  27. DispatchQueue.main.async {
  28. Task {
  29. do {
  30. let result = try await body()
  31. await MainActor.run {
  32. if result is Void {
  33. command.resumeExecution(withResult: nil)
  34. } else {
  35. command.resumeExecution(withResult: result)
  36. }
  37. }
  38. } catch {
  39. await MainActor.run {
  40. command.scriptErrorNumber = errOSAGeneralError
  41. command.scriptErrorString = error.localizedDescription
  42. command.resumeExecution(withResult: nil)
  43. }
  44. }
  45. }
  46. }
  47. }
  48. /// Convert text to data either as a UTF-8 string or as binary encoded in base64
  49. /// - Parameters:
  50. /// - text: Text input
  51. /// - isBase64Encoded: If true, the data will be decoded from base64
  52. /// - Returns: Data or nil on error (or if text was nil)
  53. func dataFromText(_ text: String?, isBase64Encoded: Bool = false) -> Data? {
  54. if let text = text {
  55. if isBase64Encoded {
  56. return Data(base64Encoded: text)
  57. } else {
  58. return text.data(using: .utf8)
  59. }
  60. } else {
  61. return nil
  62. }
  63. }
  64. /// Convert data to either UTF-8 string or as binary encoded in base64
  65. /// - Parameters:
  66. /// - data: Data input
  67. /// - isBase64Encoded: If true, the text will be encoded to base64
  68. /// - Returns: Text or nil on error (or if data was nil)
  69. func textFromData(_ data: Data?, isBase64Encoded: Bool = false) -> String? {
  70. if let data = data {
  71. if isBase64Encoded {
  72. return data.base64EncodedString()
  73. } else {
  74. return String(data: data, encoding: .utf8)
  75. }
  76. } else {
  77. return nil
  78. }
  79. }
  80. }