create static method

Future<Teemo> create({
  1. int retries = 5,
  2. int retryAfter = 5,
})

Asychronous constructor for Teemo. Attempts to connect to the LCU retries times, at retryAfter interval.

Teemo will attempt to connect to the LCU websocket, as well as creating an HTTPS rest client instance. Teemo currently only uses the command-line arguments, not the lockfile

Implementation

static Future<Teemo> create({int retries = 5, int retryAfter = 5}) async {
  String authKey = '';
  int port = -1;

  for (int retry = 0;
      (authKey == '' || port == -1) && (retry < retries || retries == -1);
      retry++) {
    if (Platform.isMacOS || Platform.isLinux) {
      //we can run ps aux
      ProcessResult psRes = await Process.run('ps', ['aux']);
      List<String> args = psRes.stdout.split(' ');

      for (int i = 0; i < args.length; i++) {
        List<String> keyVal = args[i].split('=');
        if (keyVal[0] == '--remoting-auth-token') {
          authKey = keyVal[1];
        } else if (keyVal[0] == '--app-port') {
          try {
            port = int.parse(keyVal[1]);
          } catch (error) {
            print('couldn\'t parse port to number');
            break;
          }
        }
        if (authKey != '' && port >= 0) break;
      }
    }
    if (authKey != '' && port >= 0) break;
    /* print('waiting, will retry'); */
    await new Future.delayed(Duration(seconds: retryAfter));
    /* print('retrying: (${retries - retry} retries remaining)'); */
  }

  if (authKey == '' || port == -1) print('something\'s gone wrong');

  print(authKey + ':' + port.toString());

  String cert =
      await rootBundle.loadString('packages/teemo/assets/riotgames.pem');
  print(cert);
  SecurityContext secCtx = SecurityContext();
  secCtx.setTrustedCertificatesBytes(utf8.encode(cert));

  Teemo teemo = Teemo._create(
      authKey,
      port,
      HttpClient(context: secCtx),
      await WebSocket.connect('wss://127.0.0.1:$port',
          headers: {
            'Authorization':
                'Basic ' + utf8.fuse(base64).encode('riot:$authKey')
          },
          customClient:
              HttpClient(context: secCtx) //TODO: try teemo.rest_client
          ));
  teemo._startWebsocketListener();
  return teemo;
}