Ejemplo n.º 1
0
        internal async Task Login(AuthResult auth)
        {
            if (!Init.IsCompleted)
            {
                await Init;
            }
            if (auth.Content.Status != "LOGIN")
            {
                throw new ArgumentException("Invalid auth credentials");
            }

            var context = Riot.Services.Service.RegisterObjects();

            rtmp = new RtmpClient(new Uri("rtmps://" + Region.Current.MainServer + ":2099"), context, ObjectEncoding.Amf3);
            rtmp.MessageReceived += Rtmp_MessageReceived;
            rtmp.Disconnected    += Rtmp_Disconnected;
            await rtmp.ConnectAsync();

            chat.Connect(auth);

            LoginSession session = null;

            do
            {
                try {
                    var creds = auth.GetAuthCredentials();
                    creds.ClientVersion = clientVersion;
                    session             = await LoginService.Login(creds);
                } catch (InvocationException x) when(x.RootCause is ClientVersionMismatchException)
                {
                    var inner = (ClientVersionMismatchException)x.RootCause;

                    clientVersion = (string)inner.SubstitutionArguments[1];
                    Log("Patch (DDragon outdated): " + clientVersion);
                }
            } while (session == null);

            string bc = $"bc-{session.AccountSummary.AccountId}";
            string gn = $"gn-{session.AccountSummary.AccountId}";
            string cn = $"cn-{session.AccountSummary.AccountId}";
            await Task.WhenAll(
                rtmp.SubscribeAsync("my-rtmps", "messagingDestination", "bc", bc),
                rtmp.SubscribeAsync("my-rtmps", "messagingDestination", gn, gn),
                rtmp.SubscribeAsync("my-rtmps", "messagingDestination", cn, cn)
                );

            await rtmp.LoginAsync(auth.Username.ToLower(), session.Token);

            string state = await AccountService.GetAccountState();

            if (state != "ENABLED")
            {
                Log(state);
                throw new AuthenticationException("Not Enabled: " + state);
            }

            this.LoginQueue   = auth.Content;
            this.loginSession = session;
            this.Me           = await summoner.Connect(session);

            try {
                Authed?.Invoke(this, EventArgs.Empty);
            } catch (Exception x) {
                Log("Caught exception while dispatching auth: " + x);
            }

            BackEndServer.Async("/kappa/defer/auth", new JSONObject());

            new Thread(HeartBeatLoop)
            {
                IsBackground = true
            }.Start();
        }