const os = require('os')
const frida = require('frida')

class FridaUtil {
  static async getDevice(id) {
    let list = await frida.enumerateDevices()
    let dev = list.find(dev => dev.id == id && dev.type == 'tether')

    if (dev)
      return dev

    throw new DeviceNotFoundError(id)
  }

  static screenshot(id) {
    const tmp = os.tmpdir() + new Date().getTime() + '.png'
    return new Promise((resolve, reject) => {
      // TODO: configurable executable path
      childProc.spawn('idevicescreenshot', ['-u', id, tmp]).on('close', code => {
        if (code == 0)
          resolve(tmp)
        else
          reject(code)
      })
    })
  }

  static async info(id) {
    let [stdout, stderr] = await childProc.exec('ideviceinfo -x')
    return plist.parse(stdout)
  }
}


function serializeDevice(dev) {
  let { name, id, icon } = dev
  icon.pixels = icon.pixels.toJSON()
  return { name, id, icon }
}


module.exports = {
  FridaUtil,
  serializeDevice
}

@@ -850,42 +850,128 @@
 v%0A  
-icon.pixels = icon.pixels.toJSON()
+let %7B pixels, height, width, rowstride %7D = icon%0A  pixels = pixels.toJSON()%0A  icon = %7B width, height, rowstride, pixels %7D
 %0A  r

fix missing icon