Exemple #1
0
        /// <summary>
        /// Stores data like assets and item templates. Defaults to an in-memory cache, but can be implemented as writing to disk by the platform
        /// </summary>
        // public IDataCache DataCache { get; set; } = new MemoryDataCache();
        // public Templates Templates { get; private set; }

        internal Session(ILoginProvider loginProvider, AccessToken accessToken, GeoCoordinate geoCoordinate, Signature.Types.DeviceInfo deviceInfo = null)
        {
            if (!ValidLoginProviders.Contains(loginProvider.ProviderId))
            {
                throw new ArgumentException($"LoginProvider ID must be one of the following: {string.Join(", ", ValidLoginProviders)}");
            }

            HttpClient    = new PokemonHttpClient();
            DeviceInfo    = deviceInfo ?? DeviceInfoHelper.GetRandomIosDevice();
            AccessToken   = accessToken;
            LoginProvider = loginProvider;
            // Player = new Player(geoCoordinate);
            //Map = new Map(this);
            //RpcClient = new RpcClient(this);
            //_heartbeat = new HeartbeatDispatcher(this);
        }
Exemple #2
0
        //public void Load(JObject jsonObj)
        //{
        //    try
        //    {
        //        var input = jsonObj.ToString(Formatting.None, new StringEnumConverter { CamelCaseText = true });
        //        var settings = new JsonSerializerSettings();
        //        settings.Converters.Add(new StringEnumConverter { CamelCaseText = true });
        //        JsonConvert.PopulateObject(input, this, settings);
        //        Save(_filePath);
        //    }
        //    catch (JsonReaderException exception)
        //    {
        //            Logger.Write("JSON Exception: " + exception.Message, LogLevel.Error);
        //    }
        //}

        public void Load(string configFile, string schemaFile, int schemaVersion, bool validate = false)
        {
            try
            {
                _filePath = configFile;

                if (File.Exists(_filePath))
                {
                    // if the file exists, load the settings
                    var input = File.ReadAllText(_filePath, Encoding.UTF8);

                    if (validate)
                    {
                        var jsonObj = JObject.Parse(input);

                        // Migrate before validation.
                        MigrateSettings(schemaVersion, jsonObj, configFile, schemaFile);

                        // validate Json using JsonSchema
                        Logger.Write("Validating auth.json...");
                        IList <ValidationError> errors = null;
                        bool valid;
                        try
                        {
                            valid = jsonObj.IsValid(JsonSchema, out errors);
                        }
                        catch (JSchemaException ex)
                        {
                            if (ex.Message.Contains("commercial licence") || ex.Message.Contains("free-quota"))
                            {
                                Logger.Write(
                                    "auth.json: " + ex.Message);
                                valid = false;
                            }
                            else
                            {
                                throw;
                            }
                        }
                        if (!valid)
                        {
                            if (errors != null)
                            {
                                foreach (var error in errors)
                                {
                                    Logger.Write(
                                        "auth.json [Line: " + error.LineNumber + ", Position: " + error.LinePosition + "]: " +
                                        error.Path + " " +
                                        error.Message, LogLevel.Error);
                                }
                            }

                            Logger.Write("Fix auth.json and restart NecroBot or press a key to ignore and continue...",
                                         LogLevel.Warning);
                            Console.ReadKey();
                        }

                        // Now we know it's valid so update input with the migrated version.
                        input = jsonObj.ToString();
                    }

                    var settings = new JsonSerializerSettings();
                    settings.Converters.Add(new StringEnumConverter {
                        CamelCaseText = true
                    });
                    JsonConvert.PopulateObject(input, this, settings);
                }
                // Do some post-load logic to determine what device info to be using - if 'custom' is set we just take what's in the file without question
                if (DeviceConfig.DevicePlatform.Equals("ios", StringComparison.InvariantCultureIgnoreCase))
                {
                    // iOS
                    if (DeviceConfig.DevicePackageName.Equals("random", StringComparison.InvariantCultureIgnoreCase))
                    {
                        var randomAppleDeviceInfo = DeviceInfoHelper.GetRandomIosDevice();
                        SetDevInfoByDeviceInfo(randomAppleDeviceInfo);

                        // After generating iOS settings, automatically set the package name to "custom", so that we don't regenerate settings every time we start.
                        DeviceConfig.DevicePackageName = "custom";
                    }
                }
                else
                {
                    // We cannot emulate Android at the moment, so if we got here, then regenerate the settings with random iOS device.

                    /*
                     * // Android
                     * if (!DeviceConfig.DevicePackageName.Equals("random", StringComparison.InvariantCultureIgnoreCase) &&
                     *  !DeviceConfig.DevicePackageName.Equals("custom", StringComparison.InvariantCultureIgnoreCase))
                     * {
                     *  // User requested a specific device package, check to see if it exists and if so, set it up - otherwise fall-back to random package
                     *  var keepDevId = DeviceConfig.DeviceId;
                     *  SetDevInfoByKey();
                     *  DeviceConfig.DeviceId = keepDevId;
                     * }
                     * if (DeviceConfig.DevicePackageName.Equals("random", StringComparison.InvariantCultureIgnoreCase))
                     * {
                     *  // Random is set, so pick a random device package and set it up - it will get saved to disk below and re-used in subsequent sessions
                     *  var rnd = new Random();
                     *  var rndIdx = rnd.Next(0, DeviceInfoHelper.AndroidDeviceInfoSets.Keys.Count - 1);
                     *  DeviceConfig.DevicePackageName = DeviceInfoHelper.AndroidDeviceInfoSets.Keys.ToArray()[rndIdx];
                     *  SetDevInfoByKey();
                     * }
                     */
                    DeviceConfig.DevicePlatform    = "ios";
                    DeviceConfig.DevicePackageName = "custom";

                    var randomAppleDeviceInfo = DeviceInfoHelper.GetRandomIosDevice();
                    SetDevInfoByDeviceInfo(randomAppleDeviceInfo);

                    // Clear out the android fields.
                    DeviceConfig.AndroidBoardName      = null;
                    DeviceConfig.AndroidBootloader     = null;
                    DeviceConfig.DeviceModelIdentifier = null;
                    DeviceConfig.FirmwareTags          = null;
                    DeviceConfig.FirmwareFingerprint   = null;
                }

                if (string.IsNullOrEmpty(DeviceConfig.DeviceId) || DeviceConfig.DeviceId == "8525f5d8201f78b5")
                {
                    // Changed to random hex as full alphabet letters could have been flagged
                    // iOS device ids are 16 bytes (32 chars long)
                    DeviceConfig.DeviceId = RandomString(32, "0123456789abcdef");
                }

                Save(_filePath);
            }
            catch (JsonReaderException exception)
            {
                if (exception.Message.Contains("Unexpected character") && exception.Message.Contains("Username"))
                {
                    Logger.Write("JSON Exception: You need to properly configure your Username using quotations.",
                                 LogLevel.Error);
                }
                else if (exception.Message.Contains("Unexpected character") &&
                         exception.Message.Contains("Password"))
                {
                    Logger.Write(
                        "JSON Exception: You need to properly configure your Password using quotations.",
                        LogLevel.Error);
                }
                else
                {
                    Logger.Write("JSON Exception: " + exception.Message, LogLevel.Error);
                }
            }
        }
Exemple #3
0
        //public void Load(JObject jsonObj)
        //{
        //    try
        //    {
        //        var input = jsonObj.ToString(Formatting.None, new StringEnumConverter { CamelCaseText = true });
        //        var settings = new JsonSerializerSettings();
        //        settings.Converters.Add(new StringEnumConverter { CamelCaseText = true });
        //        JsonConvert.PopulateObject(input, this, settings);
        //        Save(_filePath);
        //    }
        //    catch (JsonReaderException exception)
        //    {
        //            Logger.Write("JSON Exception: " + exception.Message, LogLevel.Error);
        //    }
        //}

        public void Load(string path, bool boolSkipSave = false, bool validate = false)
        {
            try
            {
                _filePath = path;

                if (File.Exists(_filePath))
                {
                    // if the file exists, load the settings
                    var input = File.ReadAllText(_filePath, Encoding.UTF8);

                    if (validate)
                    {
                        // validate Json using JsonSchema
                        Logger.Write("Validating auth.json...");
                        var jsonObj = JObject.Parse(input);
                        IList <ValidationError> errors = null;
                        bool valid;
                        try
                        {
                            valid = jsonObj.IsValid(JsonSchema, out errors);
                        }
                        catch (JSchemaException ex)
                        {
                            if (ex.Message.Contains("commercial licence") || ex.Message.Contains("free-quota"))
                            {
                                Logger.Write(
                                    "auth.json: " + ex.Message);
                                valid = false;
                            }
                            else
                            {
                                throw;
                            }
                        }
                        if (!valid)
                        {
                            if (errors != null)
                            {
                                foreach (var error in errors)
                                {
                                    Logger.Write(
                                        "auth.json [Line: " + error.LineNumber + ", Position: " + error.LinePosition + "]: " +
                                        error.Path + " " +
                                        error.Message, LogLevel.Error);
                                }
                            }

                            Logger.Write("Fix auth.json and restart NecroBot or press a key to ignore and continue...",
                                         LogLevel.Warning);
                            Console.ReadKey();
                        }
                    }

                    var settings = new JsonSerializerSettings();
                    settings.Converters.Add(new StringEnumConverter {
                        CamelCaseText = true
                    });
                    JsonConvert.PopulateObject(input, this, settings);
                }
                // Do some post-load logic to determine what device info to be using - if 'custom' is set we just take what's in the file without question
                if (DeviceConfig.DevicePlatform.Equals("ios", StringComparison.InvariantCultureIgnoreCase))
                {
                    // iOS
                    if (DeviceConfig.DevicePackageName.Equals("random", StringComparison.InvariantCultureIgnoreCase))
                    {
                        var randomAppleDeviceInfo = DeviceInfoHelper.GetRandomIosDevice();
                        SetDevInfoByDeviceInfo(randomAppleDeviceInfo);
                    }
                }
                else
                {
                    // Android
                    if (!DeviceConfig.DevicePackageName.Equals("random", StringComparison.InvariantCultureIgnoreCase) &&
                        !DeviceConfig.DevicePackageName.Equals("custom", StringComparison.InvariantCultureIgnoreCase))
                    {
                        // User requested a specific device package, check to see if it exists and if so, set it up - otherwise fall-back to random package
                        var keepDevId = DeviceConfig.DeviceId;
                        SetDevInfoByKey();
                        DeviceConfig.DeviceId = keepDevId;
                    }
                    if (DeviceConfig.DevicePackageName.Equals("random", StringComparison.InvariantCultureIgnoreCase))
                    {
                        // Random is set, so pick a random device package and set it up - it will get saved to disk below and re-used in subsequent sessions
                        var rnd    = new Random();
                        var rndIdx = rnd.Next(0, DeviceInfoHelper.AndroidDeviceInfoSets.Keys.Count - 1);
                        DeviceConfig.DevicePackageName = DeviceInfoHelper.AndroidDeviceInfoSets.Keys.ToArray()[rndIdx];
                        SetDevInfoByKey();
                    }
                }
                if (string.IsNullOrEmpty(DeviceConfig.DeviceId) || DeviceConfig.DeviceId == "8525f5d8201f78b5")
                {
                    DeviceConfig.DeviceId = RandomString(16, "0123456789abcdef");
                }
                // changed to random hex as full alphabet letters could have been flagged

                // Jurann: Note that some device IDs I saw when adding devices had smaller numbers, only 12 or 14 chars instead of 16 - probably not important but noted here anyway

                if (!boolSkipSave)
                {
                    Save(_filePath);
                }
            }
            catch (JsonReaderException exception)
            {
                if (exception.Message.Contains("Unexpected character") && exception.Message.Contains("PtcUsername"))
                {
                    Logger.Write("JSON Exception: You need to properly configure your PtcUsername using quotations.",
                                 LogLevel.Error);
                }
                else if (exception.Message.Contains("Unexpected character") && exception.Message.Contains("PtcPassword"))
                {
                    Logger.Write(
                        "JSON Exception: You need to properly configure your PtcPassword using quotations.",
                        LogLevel.Error);
                }
                else if (exception.Message.Contains("Unexpected character") &&
                         exception.Message.Contains("GoogleUsername"))
                {
                    Logger.Write(
                        "JSON Exception: You need to properly configure your GoogleUsername using quotations.",
                        LogLevel.Error);
                }
                else if (exception.Message.Contains("Unexpected character") &&
                         exception.Message.Contains("GooglePassword"))
                {
                    Logger.Write(
                        "JSON Exception: You need to properly configure your GooglePassword using quotations.",
                        LogLevel.Error);
                }
                else
                {
                    Logger.Write("JSON Exception: " + exception.Message, LogLevel.Error);
                }
            }
        }