Ejemplo n.º 1
0
        private static Session SetupTranslationCode(Session session, ITranslation translator, GlobalSettings settings)
        {
            Logger.Write(translator.GetTranslation(TranslationString.FirstStartLanguagePrompt, "Y", "N"), LogLevel.None);
            string strInput;

            bool boolBreak = false;

            while (!boolBreak)
            {
                strInput = Console.ReadLine().ToLower();

                switch (strInput)
                {
                case "y":
                    boolBreak = true;
                    break;

                case "n":
                    return(session);

                default:
                    Logger.Write(translator.GetTranslation(TranslationString.PromptError, "y", "n"), LogLevel.Error);
                    continue;
                }
            }

            Logger.Write(translator.GetTranslation(TranslationString.FirstStartLanguageCodePrompt));
            strInput = Console.ReadLine();

            settings.ConsoleConfig.TranslationLanguageCode = strInput;
            session    = new Session(new ClientSettings(settings), new LogicSettings(settings));
            translator = session.Translation;
            Logger.Write(translator.GetTranslation(TranslationString.FirstStartLanguageConfirm, strInput));

            return(session);
        }
Ejemplo n.º 2
0
 public ClientSettings(GlobalSettings settings, IElevationService elevationService)
 {
     _settings         = settings;
     _elevationService = elevationService;
 }
Ejemplo n.º 3
0
        //private JObject _jsonObject;
        //public JObject JsonObject
        //{
        //    get
        //    {
        //        if (_jsonObject == null)
        //            _jsonObject = JObject.FromObject(this);

        //        return _jsonObject;
        //    }
        //    set
        //    {
        //        _jsonObject = value;
        //    }
        //}


        //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);
        //        var configFile = Path.Combine(ProfileConfigPath, "config.json");
        //        this.Save(configFile);

        //    }
        //    catch (JsonReaderException exception)
        //    {
        //            Logger.Write("JSON Exception: " + exception.Message, LogLevel.Error);
        //    }
        //}

        public static GlobalSettings Load(string path, bool boolSkipSave = false, bool validate = true)
        {
            GlobalSettings settings;
            var            profilePath       = Path.Combine(Directory.GetCurrentDirectory(), path);
            var            profileConfigPath = Path.Combine(profilePath, "config");
            var            configFile        = Path.Combine(profileConfigPath, "config.json");
            var            shouldExit        = false;

            if (File.Exists(configFile))
            {
                try
                {
                    //if the file exists, load the settings
                    string input;
                    int    count = 0;
                    while (true)
                    {
                        try
                        {
                            input = File.ReadAllText(configFile, Encoding.UTF8);
                            if (!input.Contains("DeprecatedMoves"))
                            {
                                input = input.Replace("\"Moves\"", $"\"DeprecatedMoves\"");
                            }

                            break;
                        }
                        catch (Exception exception)
                        {
                            if (count > 10)
                            {
                                //sometimes we have to wait close to config.json for access
                                Logger.Write("configFile: " + exception.Message, LogLevel.Error);
                            }
                            count++;
                            Thread.Sleep(1000);
                        }
                    }

                    var jsonSettings = new JsonSerializerSettings();
                    jsonSettings.Converters.Add(new StringEnumConverter {
                        CamelCaseText = true
                    });
                    jsonSettings.ObjectCreationHandling = ObjectCreationHandling.Replace;
                    jsonSettings.DefaultValueHandling   = DefaultValueHandling.Populate;

                    try
                    {
                        // validate Json using JsonSchema
                        if (validate)
                        {
                            Logger.Write("Validating config.json...");
                            var jsonObj = JObject.Parse(input);
                            IList <ValidationError> errors;
                            var valid = jsonObj.IsValid(JsonSchema, out errors);
                            if (!valid)
                            {
                                foreach (var error in errors)
                                {
                                    Logger.Write(
                                        "config.json [Line: " + error.LineNumber + ", Position: " + error.LinePosition + "]: " +
                                        error.Path + " " +
                                        error.Message, LogLevel.Error);
                                }
                                Logger.Write("Fix config.json and restart NecroBot or press a key to ignore and continue...",
                                             LogLevel.Warning);
                                Console.ReadKey();
                            }
                        }

                        settings = JsonConvert.DeserializeObject <GlobalSettings>(input, jsonSettings);
                    }
                    catch (JsonSerializationException exception)
                    {
                        Logger.Write("JSON Exception: " + exception.Message, LogLevel.Error);
                        return(null);
                    }
                    catch (JsonReaderException exception)
                    {
                        Logger.Write("JSON Exception: " + exception.Message, LogLevel.Error);
                        return(null);
                    }

                    //This makes sure that existing config files dont get null values which lead to an exception
                    foreach (var filter in settings.PokemonsTransferFilter.Where(x => x.Value.KeepMinOperator == null))
                    {
                        filter.Value.KeepMinOperator = "or";
                    }
                    foreach (var filter in settings.PokemonsTransferFilter.Where(x => x.Value.Moves == null))
                    {
                        filter.Value.Moves = (filter.Value.DeprecatedMoves != null)
                            ? new List <List <PokemonMove> > {
                            filter.Value.DeprecatedMoves
                        }
                            : filter.Value.Moves ?? new List <List <PokemonMove> >();
                    }
                    foreach (var filter in settings.PokemonsTransferFilter.Where(x => x.Value.MovesOperator == null))
                    {
                        filter.Value.MovesOperator = "or";
                    }
                }
                catch (JsonReaderException exception)
                {
                    Logger.Write("JSON Exception: " + exception.Message, LogLevel.Error);
                    return(null);
                }
            }
            else
            {
                settings   = new GlobalSettings();
                shouldExit = true;
            }

            settings.ProfilePath       = profilePath;
            settings.ProfileConfigPath = profileConfigPath;
            settings.GeneralConfigPath = Path.Combine(Directory.GetCurrentDirectory(), "config");

            if (!boolSkipSave || !settings.UpdateConfig.AutoUpdate)
            {
                settings.Save(configFile);
                settings.Auth.Load(Path.Combine(profileConfigPath, "auth.json"), boolSkipSave, validate);
            }

            return(shouldExit ? null : settings);
        }
Ejemplo n.º 4
0
 private static void SaveFiles(GlobalSettings settings, string configFile)
 {
     settings.Save(configFile);
     settings.Auth.Load(Path.Combine(settings.ProfileConfigPath, "auth.json"));
 }
Ejemplo n.º 5
0
 public LogicSettings(GlobalSettings settings)
 {
     _settings = settings;
 }
Ejemplo n.º 6
0
        private static void SetupWalkingConfig(ITranslation translator, GlobalSettings settings)
        {
            Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupWalkingSpeedPrompt, "Y", "N"),
                         LogLevel.None);
            string strInput;

            var boolBreak = false;

            while (!boolBreak)
            {
                strInput = Console.ReadLine().ToLower();

                switch (strInput)
                {
                case "y":
                    boolBreak = true;
                    break;

                case "n":
                    return;

                default:
                    Logger.Write(translator.GetTranslation(TranslationString.PromptError, "y", "n"), LogLevel.Error);
                    continue;
                }
            }

            Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupWalkingSpeedKmHPrompt));
            strInput = Console.ReadLine();
            settings.LocationConfig.WalkingSpeedInKilometerPerHour = double.Parse(strInput);
            Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupWalkingSpeedKmHConfirm, strInput));
            Logger.Write(
                translator.GetTranslation(TranslationString.FirstStartSetupUseWalkingSpeedVariantPrompt, "Y", "N"),
                LogLevel.None);

            boolBreak = false;
            while (!boolBreak)
            {
                strInput = Console.ReadLine().ToLower();

                switch (strInput)
                {
                case "y":
                    boolBreak = true;
                    settings.LocationConfig.UseWalkingSpeedVariant = true;
                    break;

                case "n":
                    settings.LocationConfig.UseWalkingSpeedVariant = false;
                    return;

                default:
                    Logger.Write(translator.GetTranslation(TranslationString.PromptError, "y", "n"), LogLevel.Error);
                    continue;
                }
            }

            Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupWalkingSpeedVariantPrompt));
            strInput = Console.ReadLine();
            settings.LocationConfig.WalkingSpeedVariant = double.Parse(strInput);
            Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupWalkingSpeedVariantConfirm, strInput));
        }
Ejemplo n.º 7
0
        private static void SetupConfig(ITranslation translator, GlobalSettings settings)
        {
            Logger.Write(translator.GetTranslation(TranslationString.FirstStartDefaultLocationPrompt, "Y", "N"),
                         LogLevel.None);

            var boolBreak = false;

            while (!boolBreak)
            {
                var strInput = Console.ReadLine().ToLower();

                switch (strInput)
                {
                case "y":
                    boolBreak = true;
                    break;

                case "n":
                    Logger.Write(translator.GetTranslation(TranslationString.FirstStartDefaultLocationSet));
                    return;

                default:
                    // PROMPT ERROR \\
                    Logger.Write(translator.GetTranslation(TranslationString.PromptError, "y", "n"), LogLevel.Error);
                    continue;
                }
            }

            Logger.Write(translator.GetTranslation(TranslationString.FirstStartDefaultLocation), LogLevel.None);
            Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupDefaultLatLongPrompt));
            while (true)
            {
                try
                {
                    var strInput = Console.ReadLine();
                    var strSplit = strInput.Split(',');

                    if (strSplit.Length > 1)
                    {
                        var dblLat  = double.Parse(strSplit[0].Trim(' '));
                        var dblLong = double.Parse(strSplit[1].Trim(' '));

                        settings.LocationConfig.DefaultLatitude  = dblLat;
                        settings.LocationConfig.DefaultLongitude = dblLong;

                        Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupDefaultLatLongConfirm,
                                                               $"{dblLat}, {dblLong}"));
                    }
                    else
                    {
                        Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupDefaultLocationError,
                                                               $"{settings.LocationConfig.DefaultLatitude}, {settings.LocationConfig.DefaultLongitude}",
                                                               LogLevel.Error));
                        continue;
                    }

                    break;
                }
                catch (FormatException)
                {
                    Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupDefaultLocationError,
                                                           $"{settings.LocationConfig.DefaultLatitude}, {settings.LocationConfig.DefaultLongitude}",
                                                           LogLevel.Error));
                }
            }
        }
Ejemplo n.º 8
0
        private static void SetupProxyConfig(ITranslation translator, GlobalSettings settings)
        {
            Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupProxyPrompt, "Y", "N"),
                         LogLevel.None);
            string strInput;

            var boolBreak = false;

            while (!boolBreak)
            {
                strInput = Console.ReadLine().ToLower();

                switch (strInput)
                {
                case "y":
                    boolBreak = true;
                    break;

                case "n":
                    return;

                default:
                    Logger.Write(translator.GetTranslation(TranslationString.PromptError, "y", "n"), LogLevel.Error);
                    continue;
                }
            }

            settings.Auth.ProxyConfig.UseProxy = true;

            Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupProxyHostPrompt));
            strInput = Console.ReadLine();
            settings.Auth.ProxyConfig.UseProxyHost = strInput;
            Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupProxyHostConfirm, strInput));

            Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupProxyPortPrompt));
            strInput = Console.ReadLine();
            settings.Auth.ProxyConfig.UseProxyPort = strInput;
            Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupProxyPortConfirm, strInput));

            Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupProxyAuthPrompt, "Y", "N"),
                         LogLevel.None);

            boolBreak = false;
            while (!boolBreak)
            {
                strInput = Console.ReadLine().ToLower();

                switch (strInput)
                {
                case "y":
                    boolBreak = true;
                    break;

                case "n":
                    return;

                default:
                    Logger.Write(translator.GetTranslation(TranslationString.PromptError, "y", "n"), LogLevel.Error);
                    continue;
                }
            }

            settings.Auth.ProxyConfig.UseProxyAuthentication = true;

            Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupProxyUsernamePrompt));
            strInput = Console.ReadLine();
            settings.Auth.ProxyConfig.UseProxyUsername = strInput;
            Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupProxyUsernameConfirm, strInput));
            Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupProxyPasswordPrompt));
            strInput = Console.ReadLine();
            settings.Auth.ProxyConfig.UseProxyPassword = strInput;
            Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupProxyPasswordConfirm, strInput));
        }
Ejemplo n.º 9
0
        public static GlobalSettings Load(string path, bool boolSkipSave = false)
        {
            GlobalSettings settings          = null;
            var            profilePath       = Path.Combine(Directory.GetCurrentDirectory(), path);
            var            profileConfigPath = Path.Combine(profilePath, "config");
            var            configFile        = Path.Combine(profileConfigPath, "config.json");
            var            shouldExit        = false;

            if (File.Exists(configFile))
            {
                try
                {
                    //if the file exists, load the settings
                    string input = "";
                    int    count = 0;
                    while (true)
                    {
                        try
                        {
                            input = File.ReadAllText(configFile);
                            if (!input.Contains("DeprecatedMoves"))
                            {
                                input = input.Replace("\"Moves\"", $"\"DeprecatedMoves\"");
                            }

                            break;
                        }
                        catch (Exception exception)
                        {
                            if (count > 10)
                            {
                                //sometimes we have to wait close to config.json for access
                                Logger.Write("configFile: " + exception.Message, LogLevel.Error);
                            }
                            count++;
                            Thread.Sleep(1000);
                        }
                    }
                    ;

                    var jsonSettings = new JsonSerializerSettings();
                    jsonSettings.Converters.Add(new StringEnumConverter {
                        CamelCaseText = true
                    });
                    jsonSettings.ObjectCreationHandling = ObjectCreationHandling.Replace;
                    jsonSettings.DefaultValueHandling   = DefaultValueHandling.Populate;

                    try
                    {
                        settings = JsonConvert.DeserializeObject <GlobalSettings>(input, jsonSettings);
                    }
                    catch (Newtonsoft.Json.JsonSerializationException exception)
                    {
                        Logger.Write("JSON Exception: " + exception.Message, LogLevel.Error);
                        return(null);
                    }

                    //This makes sure that existing config files dont get null values which lead to an exception
                    foreach (var filter in settings.PokemonsTransferFilter.Where(x => x.Value.KeepMinOperator == null))
                    {
                        filter.Value.KeepMinOperator = "or";
                    }
                    foreach (var filter in settings.PokemonsTransferFilter.Where(x => x.Value.Moves == null))
                    {
                        filter.Value.Moves = (filter.Value.DeprecatedMoves != null)
                            ? new List <List <PokemonMove> > {
                            filter.Value.DeprecatedMoves
                        }
                            : filter.Value.Moves ?? new List <List <PokemonMove> >();
                    }
                    foreach (var filter in settings.PokemonsTransferFilter.Where(x => x.Value.MovesOperator == null))
                    {
                        filter.Value.MovesOperator = "or";
                    }
                }
                catch (JsonReaderException exception)
                {
                    Logger.Write("JSON Exception: " + exception.Message, LogLevel.Error);
                    return(null);
                }
            }
            else
            {
                settings   = new GlobalSettings();
                shouldExit = true;
            }

            settings.ProfilePath       = profilePath;
            settings.ProfileConfigPath = profileConfigPath;
            settings.GeneralConfigPath = Path.Combine(Directory.GetCurrentDirectory(), "config");

            if (!boolSkipSave || !settings.UpdateConfig.AutoUpdate)
            {
                settings.Save(configFile);
                settings.Auth.Load(Path.Combine(profileConfigPath, "auth.json"));
            }

            return(shouldExit ? null : settings);
        }
Ejemplo n.º 10
0
        private static Session SetupTranslationCode(Session session, ITranslation translator, GlobalSettings settings)
        {
            if (false == PromptForBoolean(translator, translator.GetTranslation(TranslationString.FirstStartLanguagePrompt, "Y", "N")))
            {
                return(session);
            }

            string strInput = PromptForString(translator, translator.GetTranslation(TranslationString.FirstStartLanguageCodePrompt));

            settings.ConsoleConfig.TranslationLanguageCode = strInput;
            session    = new Session(new ClientSettings(settings), new LogicSettings(settings));
            translator = session.Translation;
            Logger.Write(translator.GetTranslation(TranslationString.FirstStartLanguageConfirm, strInput));

            return(session);
        }
Ejemplo n.º 11
0
 public LogicSettings(GlobalSettings settings)
 {
     _settings = settings;
     _rand     = new Random();
 }
Ejemplo n.º 12
0
        //private JObject _jsonObject;
        //public JObject JsonObject
        //{
        //    get
        //    {
        //        if (_jsonObject == null)
        //            _jsonObject = JObject.FromObject(this);

        //        return _jsonObject;
        //    }
        //    set
        //    {
        //        _jsonObject = value;
        //    }
        //}


        //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);
        //        var configFile = Path.Combine(ProfileConfigPath, "config.json");
        //        this.Save(configFile);

        //    }
        //    catch (JsonReaderException exception)
        //    {
        //            Logger.Write("JSON Exception: " + exception.Message, LogLevel.Error);
        //    }
        //}

        public static GlobalSettings Load(string path, bool validate = false)
        {
            GlobalSettings settings;

            var profilePath       = "";
            var profileConfigPath = "";
            var configFile        = "";
            var schemaFile        = "";


            if (Path.IsPathRooted(path))
            {
                profileConfigPath = Path.GetDirectoryName(path);
                configFile        = path;
                schemaFile        = path.Replace(".json", ".schema.json");
            }
            else
            {
                profilePath       = Path.Combine(Directory.GetCurrentDirectory(), path);
                profileConfigPath = Path.Combine(profilePath, "config");
                configFile        = Path.Combine(profileConfigPath, "config.json");
                schemaFile        = Path.Combine(profileConfigPath, "config.schema.json");
            }
            var shouldExit = false;
            int schemaVersionBeforeUpgrade = 0;

            if (File.Exists(configFile))
            {
                try
                {
                    //if the file exists, load the settings
                    string input;
                    var    count = 0;
                    while (true)
                    {
                        try
                        {
                            input = File.ReadAllText(configFile, Encoding.UTF8);
                            if (!input.Contains("DeprecatedMoves"))
                            {
                                input = input.Replace("\"Moves\"", $"\"DeprecatedMoves\"");
                            }

                            break;
                        }
                        catch (Exception exception)
                        {
                            if (count > 10)
                            {
                                //sometimes we have to wait close to config.json for access
                                Logger.Write("configFile: " + exception.Message, LogLevel.Error);
                            }
                            count++;
                            Thread.Sleep(1000);
                        }
                    }

                    var jsonSettings = new JsonSerializerSettings();
                    jsonSettings.Converters.Add(new StringEnumConverter {
                        CamelCaseText = true
                    });
                    jsonSettings.ObjectCreationHandling = ObjectCreationHandling.Replace;
                    jsonSettings.DefaultValueHandling   = DefaultValueHandling.Populate;

                    try
                    {
                        // validate Json using JsonSchema
                        if (validate)
                        {
                            JObject jsonObj = JObject.Parse(input);

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

                            // Save the original schema version since we need to pass it to AuthSettings for migration.
                            schemaVersionBeforeUpgrade = (int)jsonObj["UpdateConfig"]["SchemaVersion"];

                            // After migration we need to update the schema version to the latest version.
                            jsonObj["UpdateConfig"]["SchemaVersion"] = UpdateConfig.CURRENT_SCHEMA_VERSION;

                            Logger.Write("Validating config.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(
                                        "config.json: " + ex.Message);
                                    valid = false;
                                }
                                else
                                {
                                    throw;
                                }
                            }
                            if (!valid)
                            {
                                if (errors != null)
                                {
                                    foreach (var error in errors)
                                    {
                                        Logger.Write(
                                            "config.json [Line: " + error.LineNumber + ", Position: " + error.LinePosition +
                                            "]: " + error.Path + " " + error.Message, LogLevel.Error);
                                    }
                                }

                                Logger.Write(
                                    "Fix config.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();
                        }

                        settings = JsonConvert.DeserializeObject <GlobalSettings>(input, jsonSettings);
                    }
                    catch (JsonSerializationException exception)
                    {
                        Logger.Write("JSON Exception: " + exception.Message, LogLevel.Error);
                        return(null);
                    }
                    catch (JsonReaderException exception)
                    {
                        Logger.Write("JSON Exception: " + exception.Message, LogLevel.Error);
                        return(null);
                    }

                    //This makes sure that existing config files dont get null values which lead to an exception
                    foreach (var filter in settings.PokemonsTransferFilter.Where(x => x.Value.KeepMinOperator == null))
                    {
                        filter.Value.KeepMinOperator = "or";
                    }
                    foreach (var filter in settings.PokemonsTransferFilter.Where(x => x.Value.Moves == null))
                    {
                        filter.Value.Moves = filter.Value.DeprecatedMoves != null
                            ? new List <List <PokemonMove> > {
                            filter.Value.DeprecatedMoves
                        }
                            : filter.Value.Moves ?? new List <List <PokemonMove> >();
                    }
                    foreach (var filter in settings.PokemonsTransferFilter.Where(x => x.Value.MovesOperator == null))
                    {
                        filter.Value.MovesOperator = "or";
                    }
                }
                catch (JsonReaderException exception)
                {
                    Logger.Write("JSON Exception: " + exception.Message, LogLevel.Error);
                    return(null);
                }
            }
            else
            {
                settings   = new GlobalSettings();
                shouldExit = true;
            }

            settings.ProfilePath       = profilePath;
            settings.ProfileConfigPath = profileConfigPath;
            settings.GeneralConfigPath = Path.Combine(Directory.GetCurrentDirectory(), "config");

            settings.Save(configFile);
            settings.Auth.Load(Path.Combine(profileConfigPath, "auth.json"),
                               Path.Combine(profileConfigPath, "auth.schema.json"), schemaVersionBeforeUpgrade, validate);

            return(shouldExit ? null : settings);
        }