Ejemplo n.º 1
0
        protected override void OnParsingSuccess(JObject @object)
        {
            LoginWebClientError errorCode = LoginWebClientError.None;
            string errorMessage           = string.Empty;

            foreach (var property in @object.Properties())
            {
                if (property.Name == "errorCode")
                {
                    errorCode = (LoginWebClientError)SafeInt(property.Value, 0);
                }
                else if (property.Name == "errorMessage")
                {
                    errorMessage = (string)property.Value;
                }
            }

            if (errorCode == LoginWebClientError.Technical)
            {
                onTechnicalError.Invoke(errorMessage);
                return;
            }
            else if (errorCode == LoginWebClientError.Login)
            {
                onLoginError.Invoke(errorMessage);
                return;
            }
            else if (errorCode == LoginWebClientError.Authentication)
            {
                onTokenError.Invoke(errorMessage);
                return;
            }
            else if (errorCode != LoginWebClientError.None)
            {
                InvokeTechnicalParsingError();
                return;
            }

            var sessionObject  = @object["session"] as JObject;
            var playDataObject = @object["playdata"] as JObject;

            if (sessionObject == null || playDataObject == null)
            {
                InvokeTechnicalParsingError();
                return;
            }

            var worldsArray     = playDataObject["worlds"] as JArray;
            var charactersArray = playDataObject["characters"] as JArray;

            if (sessionObject == null || playDataObject == null)
            {
                InvokeTechnicalParsingError();
                return;
            }

            JToken sessionKeyToken, lastLoginTimeToken, statusToken;

            if (!sessionObject.TryGetValue("sessionkey", out sessionKeyToken) ||
                !sessionObject.TryGetValue("lastlogintime", out lastLoginTimeToken) ||
                !sessionObject.TryGetValue("status", out statusToken))
            {
                InvokeTechnicalParsingError();
                return;
            }

            var playdata = new PlayData();

            playdata.Session = new PlayData.PlayDataSession {
                Key           = SafeString(sessionKeyToken),
                Status        = SafeString(statusToken),
                LastLoginTime = SafeUint(lastLoginTimeToken),
                PremiumUntil  = SafeUint(sessionObject.GetValue("premiumuntil")),
                IsPremium     = SafeBool(sessionObject.GetValue("ispremium")),
            };

            if (ClientVersion >= 1148)
            {
                playdata.Session.FpsTracking          = (bool)sessionObject.GetValue("fpstracking");
                playdata.Session.IsReturner           = (bool)sessionObject.GetValue("isreturner");
                playdata.Session.ReturnerNotification = (bool)sessionObject.GetValue("returnernotification");
                playdata.Session.ShowRewardNews       = (bool)sessionObject.GetValue("showrewardnews");
            }

            if (ClientVersion >= 1149 && BuildVersion >= 5921)
            {
                playdata.Session.OptionTracking = (bool)sessionObject.GetValue("optiontracking");
            }

            List <int> worldIds = new List <int>();

            foreach (var worldObject in worldsArray.Children <JObject>())
            {
                JToken idToken, nameToken, previewStateToken;
                if (!worldObject.TryGetValue("id", out idToken) ||
                    !worldObject.TryGetValue("name", out nameToken) ||
                    !worldObject.TryGetValue("previewstate", out previewStateToken))
                {
                    continue;
                }

                int worldId = SafeInt(idToken);
                if (worldIds.Contains(worldId))
                {
                    continue;
                }

                string externalAddress            = string.Empty,
                       externalAddressProtected   = string.Empty,
                       externalAddressUnprotected = string.Empty;

                int externalPort            = 0,
                    externalPortProtected   = 0,
                    externalPortUnprotected = 0;

                bool antiCheatProtection = true;

                if (ClientVersion >= 1148)
                {
                    JToken externalAddressProtectedToken, externalPortProtectedToken;
                    if (!worldObject.TryGetValue("externaladdressprotected", out externalAddressProtectedToken) ||
                        !worldObject.TryGetValue("externalportprotected", out externalPortProtectedToken))
                    {
                        continue;
                    }

                    externalAddressProtected = SafeString(externalAddressProtectedToken);
                    externalPortProtected    = SafeInt(externalPortProtectedToken);
                }

                if (ClientVersion >= 1149 && BuildVersion >= 5921)
                {
                    JToken externalAddressUnprotectedToken, externalPortUnprotectedToken;
                    if (!worldObject.TryGetValue("externaladdressunprotected", out externalAddressUnprotectedToken) ||
                        !worldObject.TryGetValue("externalportunprotected", out externalPortUnprotectedToken))
                    {
                        continue;
                    }


                    externalAddressUnprotected = SafeString(externalAddressUnprotectedToken);
                    externalPortUnprotected    = SafeInt(externalPortUnprotectedToken);
                }
                else
                {
                    JToken externalAddressToken, externalPortToken;
                    if (!worldObject.TryGetValue("externaladdress", out externalAddressToken) ||
                        !worldObject.TryGetValue("externalport", out externalPortToken))
                    {
                        continue;
                    }

                    externalAddress = SafeString(externalAddressToken);
                    externalPort    = SafeInt(externalPortToken);
                }

                if (ClientVersion >= 1148 && worldObject.TryGetValue("anticheatprotection", out JToken antiChearProtectionToken))
                {
                    antiCheatProtection = SafeBool(antiChearProtectionToken);
                }

                worldIds.Add(worldId);
                playdata.Worlds.Add(new PlayData.PlayDataWorld()
                {
                    Id                      = worldId,
                    PreviewState            = SafeInt(previewStateToken),
                    ExternalPort            = externalPort,
                    ExternalPortProtected   = externalPortProtected,
                    ExternalPortUnprotected = externalPortUnprotected,

                    Name                       = SafeString(nameToken),
                    ExternalAddress            = externalAddress,
                    ExternalAddressProtected   = externalAddressProtected,
                    ExternalAddressUnprotected = externalAddressUnprotected,
                    AntiCheatProtection        = antiCheatProtection,

                    // 12.00
                    PvpType = SafeInt(worldObject.GetValue("pvptype"), -1),

                    // 12.15
                    IsTournamentActive     = SafeBool(worldObject.GetValue("istournamentactive")),
                    IsTournamentWorld      = SafeBool(worldObject.GetValue("istournamentworld")),
                    RestrictStore          = SafeBool(worldObject.GetValue("restrictedstore")),
                    CurrentTournamentPhase = SafeInt(worldObject.GetValue("currenttournamentphase"), -1),

                    // 12.20
                    IsMainCharacter = SafeBool(worldObject.GetValue("ismaincharacter")),
                });
            }

            foreach (var characterObject in charactersArray.Children <JObject>())
            {
                JToken worldIdToken, nameToken;
                if (!characterObject.TryGetValue("worldid", out worldIdToken) ||
                    !characterObject.TryGetValue("name", out nameToken))
                {
                    continue;
                }

                int worldId = SafeInt(worldIdToken);
                if (!worldIds.Contains(worldId))
                {
                    continue;
                }

                var character = new PlayData.PlayDataCharacter()
                {
                    WorldId = worldId,
                    Name    = SafeString(nameToken),

                    // 11.00 (x?)
                    IsMale   = SafeBool(characterObject.GetValue("ismale")),
                    Tutorial = SafeBool(characterObject.GetValue("tutorial")),

                    // 12.00
                    Level       = SafeInt(characterObject.GetValue("level")),
                    OutfitId    = SafeInt(characterObject.GetValue("outfitid")),
                    HeadColor   = SafeInt(characterObject.GetValue("headcolor")),
                    TorsoColor  = SafeInt(characterObject.GetValue("torsocolor")),
                    LegsColor   = SafeInt(characterObject.GetValue("legscolor")),
                    DetailColor = SafeInt(characterObject.GetValue("detailcolor")),
                    AddonsFlags = SafeInt(characterObject.GetValue("addonsflags")),

                    Vocation = SafeString(characterObject.GetValue("vocation")),

                    IsHidden = SafeBool(characterObject.GetValue("ishidden")),

                    // 12.15
                    IsTournamentParticipant          = SafeBool(characterObject.GetValue("istournamentparticipant")),
                    RemainingDailyTournamentPlaytime = SafeInt(characterObject.GetValue("remainingdailytournamentplaytime")),
                };

                playdata.Characters.Add(character);
            }

            onLoginSuccess.Invoke(playdata);
        }
Ejemplo n.º 2
0
        private PlayData ReadPlayData(Internal.CommunicationStream stream)
        {
            var playData = new PlayData();

            if (OpenTibiaUnity.GameManager.ClientVersion >= 1010)
            {
                byte worldCount = stream.ReadUnsignedByte();
                for (int i = 0; i < worldCount; i++)
                {
                    int    id       = stream.ReadUnsignedByte();
                    string name     = stream.ReadString();
                    string hostname = stream.ReadString();
                    ushort port     = stream.ReadUnsignedShort();
                    bool   preview  = stream.ReadBoolean();

                    playData.Worlds.Add(new PlayData.PlayDataWorld
                    {
                        Id              = id,
                        Name            = name,
                        ExternalAddress = hostname,
                        ExternalPort    = port,
                        PreviewState    = preview ? 1 : 0
                    });
                }

                byte characterCount = stream.ReadUnsignedByte();
                for (int i = 0; i < characterCount; i++)
                {
                    var    worldId = stream.ReadUnsignedByte();
                    string name    = stream.ReadString();

                    playData.Characters.Add(new PlayData.PlayDataCharacter
                    {
                        WorldId = worldId,
                        Name    = name
                    });
                }
            }
            else
            {
                int characterCount = stream.ReadUnsignedByte();
                for (int i = 0; i < characterCount; i++)
                {
                    string characterName = stream.ReadString();
                    string worldName     = stream.ReadString();
                    uint   worldIpLong   = stream.ReadUnsignedInt();
                    ushort worldPort     = stream.ReadUnsignedShort();

                    int worldId = 0;
                    int index   = playData.Worlds.FindIndex(x => x.Name == worldName);
                    if (index == -1)
                    {
                        worldId = playData.Worlds.Count;
                        playData.Worlds.Add(new PlayData.PlayDataWorld
                        {
                            Id              = worldId,
                            Name            = worldName,
                            ExternalAddress = new System.Net.IPAddress((long)worldIpLong).ToString(),
                            ExternalPort    = worldPort
                        });
                    }
                    else
                    {
                        worldId = playData.Worlds[index].Id;
                    }

                    playData.Characters.Add(new PlayData.PlayDataCharacter
                    {
                        Name    = characterName,
                        WorldId = worldId
                    });
                }
            }

            uint now = (uint)System.DateTime.Now.Second;

            if (OpenTibiaUnity.GameManager.ClientVersion >= 1077)
            {
                stream.ReadUnsignedByte(); // todo map accountState to strings
                playData.Session.IsPremium       = stream.ReadBoolean();
                playData.Session.PremiumUntil    = stream.ReadUnsignedInt();
                playData.Session.InfinitePremium = playData.Session.IsPremium && playData.Session.PremiumUntil == 0;
            }
            else
            {
                uint premiumDays = stream.ReadUnsignedShort();
                playData.Session.IsPremium       = premiumDays > 0;
                playData.Session.PremiumUntil    = premiumDays > 0 ? (now + premiumDays * 86400U) : 0;
                playData.Session.InfinitePremium = premiumDays == ushort.MaxValue;
            }
            return(playData);
        }