/// <summary>
        /// Loads settings from the specified file name.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <returns>The settings.</returns>
        public static Settings Load(string fileName = null)
        {
            Log.Debug(typeof(Settings), "Load", "Begin");

            try
            {
                if (fileName == null)
                {
                    fileName = FilePathName;
                }

                if (File.Exists(fileName))
                {
                    Log.Info(typeof(Settings), "Load", fileName);

                    using (FileStream file = File.OpenRead(fileName))
                    {
                        XmlSerializer ser = new XmlSerializer(typeof(ServiceDispatcherSettings));
                        ServiceDispatcherSettings cfg = ser.Deserialize(file) as ServiceDispatcherSettings;
                        if (cfg != null)
                        {
                            Log.Debug(typeof(Settings), "Load", "Loaded");

                            if (cfg.Version < 3)
                            {
                                if (cfg.Version < 2)
                                {
                                    cfg.DispatchHearsesByDistrict = cfg.DispatchByDistrict;
                                    cfg.DispatchHearsesByRange = cfg.DispatchByRange;
                                    cfg.DispatchGarbageTrucksByDistrict = cfg.DispatchByDistrict;
                                    cfg.DispatchGarbageTrucksByRange = cfg.DispatchByRange;
                                    cfg.DispatchAmbulancesByDistrict = cfg.DispatchByDistrict;
                                    cfg.DispatchAmbulancesByRange = cfg.DispatchByRange;
                                }

                                if (cfg.MinimumGarbageForDispatch >= 2000)
                                {
                                    cfg.MinimumGarbageForPatrol = 200;
                                }
                                else if (cfg.MinimumGarbageForDispatch >= 300)
                                {
                                    cfg.MinimumGarbageForPatrol = 150;
                                }
                                else if (cfg.MinimumGarbageForDispatch >= 100)
                                {
                                    cfg.MinimumGarbageForPatrol = 100;
                                }
                                else
                                {
                                    cfg.MinimumGarbageForPatrol = cfg.MinimumGarbageForDispatch;
                                }
                            }
                            Settings sets = new Settings(cfg);

                            Log.Debug(typeof(Settings), "Load", "End");
                            return sets;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(typeof(Settings), "Load", ex);
            }

            Log.Debug(typeof(Settings), "Load", "End");
            return new Settings();
        }
Esempio n. 2
0
        /// <summary>
        /// Creates the service group.
        /// </summary>
        /// <param name="helper">The helper.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="dispatcher">The dispatcher.</param>
        /// <param name="canService">If set to <c>true</c> this service can be enabled.</param>
        /// <returns>
        /// The group.
        /// </returns>
        private UIHelperBase CreateServiceGroup(UIHelperBase helper, Settings.StandardServiceSettings settings, Dispatcher dispatcher, bool canService)
        {
            try
            {
                UIHelperBase group = helper.AddGroup(settings.VehicleNamePlural);

                if (canService)
                {
                    group.AddCheckbox(
                        "Dispatch " + settings.VehicleNamePlural.ToLower(),
                        settings.DispatchVehicles,
                        value =>
                        {
                            try
                            {
                                if (settings.DispatchVehicles != value)
                                {
                                    settings.DispatchVehicles = value;
                                    Global.Settings.Save();
                                    Global.ReInitializeHandlers();
                                }
                            }
                            catch (Exception ex)
                            {
                                Log.Error(this, "CreateServiceGroup", ex, settings.VehicleNamePlural, "DispatchVehicles", value);
                            }
                        });
                }
                else
                {
                    UIComponent checkBox = group.AddCheckbox(
                        "Dispatch " + settings.VehicleNamePlural.ToLower(),
                        false,
                        value =>
                        {
                        }) as UIComponent;
                }

                group.AddCheckbox(
                    "Dispatch by district",
                    settings.DispatchByDistrict,
                    value =>
                    {
                        try
                        {
                            if (settings.DispatchByDistrict != value)
                            {
                                settings.DispatchByDistrict = value;
                                Global.BuildingUpdateNeeded = true;
                                Global.Settings.Save();
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Error(this, "CreateServiceGroup", ex, settings.VehicleNamePlural, "DispatchByDistrict", value);
                        }
                    });

                group.AddCheckbox(
                    "Dispatch by building range",
                    settings.DispatchByRange,
                    value =>
                    {
                        try
                        {
                            if (settings.DispatchByRange != value)
                            {
                                settings.DispatchByRange = value;
                                Global.BuildingUpdateNeeded = true;
                                Global.Settings.Save();
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Error(this, "CreateServiceGroup", ex, settings.VehicleNamePlural, "DispatchByRange", value);
                        }
                    });

                if (settings.CanRemoveFromGrid)
                {
                    group.AddCheckbox(
                        "Pass through " + settings.VehicleNamePlural.ToLower(),
                        settings.RemoveFromGrid,
                        value =>
                        {
                            try
                            {
                                if (settings.RemoveFromGrid != value)
                                {
                                    settings.RemoveFromGrid = value;
                                    Global.Settings.Save();
                                    Global.ReInitializeHandlers();
                                }
                            }
                            catch (Exception ex)
                            {
                                Log.Error(this, "CreateServiceGroup", ex, settings.VehicleNamePlural, "RemoveFromGrid", value);
                            }
                        });
                }

                if (settings.CanLimitOpportunisticCollection)
                {
                    if (settings.OpportunisticCollectionLimitDetour == Detours.Methods.None || Detours.CanDetour(settings.OpportunisticCollectionLimitDetour))
                    {
                        group.AddCheckbox(
                            "Prioritize assigned buildings",
                            settings.LimitOpportunisticCollection,
                            value =>
                            {
                                try
                                {
                                    if (settings.LimitOpportunisticCollection != value)
                                    {
                                        settings.LimitOpportunisticCollection = value;
                                        Detours.InitNeeded = true;
                                        Global.Settings.Save();
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Log.Error(this, "OnSettingsUI", ex, "GarbageGroup", "LimitOpportunisticGarbageCollection", value);
                                    Log.Error(this, "CreateServiceGroup", ex, settings.VehicleNamePlural, "LimitOpportunisticCollection");
                                }
                            });
                    }
                    else
                    {
                        UIComponent limitOpportunisticGarbageCollectionCheckBox = group.AddCheckbox(
                            "Prioritize assigned buildings",
                            false,
                            value =>
                            {
                            }) as UIComponent;
                        limitOpportunisticGarbageCollectionCheckBox.Disable();
                    }
                }

                group.AddDropdown(
                    "Send out spare " + settings.VehicleNamePlural.ToLower() + " when",
                    this.vehicleCreationOptions.OrderBy(vco => vco.Key).Select(vco => vco.Value).ToArray(),
                    (int)settings.CreateSpares,
                    value =>
                    {
                        try
                        {
                            if (Log.LogALot || Library.IsDebugBuild)
                            {
                                Log.Debug(this, "CreateServiceGroup", "Set", settings.VehicleNamePlural, "CreateSpares", value);
                            }

                            foreach (ServiceDispatcherSettings.SpareVehiclesCreation option in Enum.GetValues(typeof(ServiceDispatcherSettings.SpareVehiclesCreation)))
                            {
                                if ((byte)option == value)
                                {
                                    if (settings.CreateSpares != option)
                                    {
                                        if (Log.LogALot || Library.IsDebugBuild)
                                        {
                                            Log.Debug(this, "CreateServiceGroup", "Set", settings.VehicleNamePlural, "CreateSpares", value, option);
                                        }

                                        settings.CreateSpares = option;

                                        if (dispatcher != null)
                                        {
                                            try
                                            {
                                                dispatcher.ReInitialize();
                                            }
                                            catch (Exception ex)
                                            {
                                                Log.Error(this, "CreateServiceGroup", ex, settings.VehicleNamePlural, "CreateSpares", "ReInitializeDispatcher");
                                            }
                                        }

                                        Global.Settings.Save();
                                    }

                                    break;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Error(this, "CreateServiceGroup", ex, settings.VehicleNamePlural, "CreateSpares", value);
                        }
                    });

                group.AddDropdown(
                    settings.VehicleNameSingular + " dispatch strategy",
                    this.targetBuildingChecks.OrderBy(bco => bco.Key).Select(bco => bco.Value).ToArray(),
                    (int)settings.ChecksPreset,
                    value =>
                    {
                        try
                        {
                            if (Log.LogALot || Library.IsDebugBuild)
                            {
                                Log.Debug(this, "CreateServiceGroup", "Set", settings.VehicleNamePlural, "ChecksPreset", value);
                            }

                            foreach (ServiceDispatcherSettings.BuildingCheckOrder checks in Enum.GetValues(typeof(ServiceDispatcherSettings.BuildingCheckOrder)))
                            {
                                if ((byte)checks == value)
                                {
                                    if (settings.ChecksPreset != checks)
                                    {
                                        if (Log.LogALot || Library.IsDebugBuild)
                                        {
                                            Log.Debug(this, "CreateServiceGroup", "Set", settings.VehicleNamePlural, "ChecksPreset", value, checks);
                                        }

                                        try
                                        {
                                            settings.ChecksPreset = checks;
                                        }
                                        catch (Exception ex)
                                        {
                                            Log.Error(this, "OnSettingsUI", ex, "Set", "DeathChecksPreset", checks);
                                        }

                                        if (dispatcher != null)
                                        {
                                            try
                                            {
                                                dispatcher.ReInitialize();
                                            }
                                            catch (Exception ex)
                                            {
                                                Log.Error(this, "CreateServiceGroup", ex, settings.VehicleNamePlural, "ChecksPreset", "ReInitializeDispatcher");
                                            }
                                        }

                                        Global.Settings.Save();
                                    }

                                    break;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Error(this, "CreateServiceGroup", ex, settings.VehicleNamePlural, "ChecksPreset", value);
                        }
                    });

                if (settings.UseMinimumAmountForPatrol)
                {
                    group.AddExtendedSlider(
                        settings.MaterialName + " patrol limit",
                        1.0f,
                        5000.0f,
                        1.0f,
                        settings.MinimumAmountForPatrol,
                        false,
                        value =>
                        {
                            try
                            {
                                if (settings.MinimumAmountForPatrol != (ushort)value)
                                {
                                    settings.MinimumAmountForPatrol = (ushort)value;
                                    Global.BuildingUpdateNeeded = true;
                                    Global.Settings.Save();
                                }
                            }
                            catch (Exception ex)
                            {
                                Log.Error(this, "CreateServiceGroup", ex, settings.VehicleNamePlural, "MinimumAmountForPatrol", value);
                            }
                        });
                }

                if (settings.UseMinimumAmountForDispatch)
                {
                    group.AddExtendedSlider(
                        settings.MaterialName + " dispatch limit",
                        1.0f,
                        5000.0f,
                        1.0f,
                        settings.MinimumAmountForDispatch,
                        false,
                        value =>
                        {
                            try
                            {
                                if (settings.MinimumAmountForDispatch != (ushort)value)
                                {
                                    settings.MinimumAmountForDispatch = (ushort)value;
                                    Global.BuildingUpdateNeeded = true;
                                    Global.Settings.Save();
                                }
                            }
                            catch (Exception ex)
                            {
                                Log.Error(this, "CreateServiceGroup", ex, settings.VehicleNamePlural, "MinimumAmountForDispatch", value);
                            }
                        });
                }

                return group;
            }
            catch (Exception ex)
            {
                Log.Error(this, "CreateServiceGroup", ex, settings.EmptiableServiceBuildingNamePlural);
                return null;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Adds the hidden service controls.
        /// </summary>
        /// <param name="group">The group.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="canService">If set to <c>true</c> this service can be enabled.</param>
        private void AddHiddenServiceControls(UIHelperBase group, Settings.HiddenServiceSettings settings, bool canService)
        {
            if (canService)
            {
                group.AddCheckbox(
                    "Dispatch " + settings.VehicleNamePlural.ToLower(),
                    settings.DispatchVehicles,
                    value =>
                    {
                        try
                        {
                            if (settings.DispatchVehicles != value)
                            {
                                settings.DispatchVehicles = value;
                                Global.Settings.Save();
                                Global.ReInitializeHandlers();
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Error(this, "CreateWreckingRecoveryGroup", ex, settings.VehicleNamePlural, "DispatchVehicles", value);
                        }
                    });
            }
            else
            {
                UIComponent checkBox = group.AddCheckbox(
                    "Dispatch " + settings.VehicleNamePlural.ToLower(),
                    false,
                    value =>
                    {
                    }) as UIComponent;
                checkBox.Disable();
            }

            group.AddExtendedSlider(
                settings.VehicleNameSingular + " delay",
                0.0f,
                60.0f * 24.0f,
                0.01f,
                (float)settings.DelayMinutes,
                true,
                value =>
                {
                    try
                    {
                        if (settings.DelayMinutes != (double)value)
                        {
                            settings.DelayMinutes = (double)value;
                            Global.BuildingUpdateNeeded = true;
                            Global.Settings.Save();
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error(this, "CreateWreckingRecoveryGroup", ex, settings.VehicleNamePlural, "DelayMinutes", value);
                    }
                });
        }
Esempio n. 4
0
        /// <summary>
        /// Creates the group for service building that can be emptied.
        /// </summary>
        /// <param name="helper">The helper.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="canService">If set to <c>true</c> this service can be enabled.</param>
        /// <returns>
        /// The group.
        /// </returns>
        private UIHelperBase CreateEmptiableServiceBuildingGroup(UIHelperBase helper, Settings.StandardServiceSettings settings, bool canService)
        {
            try
            {
                UIHelperBase group = helper.AddGroup(settings.EmptiableServiceBuildingNamePlural);

                if (canService)
                {
                    group.AddCheckbox(
                        "Empty " + settings.EmptiableServiceBuildingNamePlural.ToLower() + " automatically",
                        settings.AutoEmpty,
                        value =>
                        {
                            try
                            {
                                if (settings.AutoEmpty != value)
                                {
                                    settings.AutoEmpty = value;
                                    Global.Settings.Save();
                                    Global.ReInitializeHandlers();
                                }
                            }
                            catch (Exception ex)
                            {
                                Log.Error(this, "CreateEmptiableServiceBuildingGroup", ex, settings.EmptiableServiceBuildingNamePlural, "AutoEmpty", value);
                            }
                        });
                }
                else
                {
                    UIComponent checkBox = group.AddCheckbox(
                        "Empty " + settings.EmptiableServiceBuildingNamePlural.ToLower() + " automatically",
                        false,
                        value =>
                        {
                        }) as UIComponent;
                    checkBox.Disable();
                }

                group.AddExtendedSlider(
                    "Start emptying at %",
                    0,
                    100,
                    1,
                    settings.AutoEmptyStartLevelPercent,
                    value =>
                    {
                        try
                        {
                            if (settings.AutoEmptyStartLevelPercent != (uint)value)
                            {
                                settings.AutoEmptyStartLevelPercent = (uint)value;
                                Global.BuildingUpdateNeeded = true;
                                Global.Settings.Save();
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Error(this, "CreateEmptiableServiceBuildingGroup", ex, settings.EmptiableServiceBuildingNamePlural, "AutoEmptyStartLevelPercent", value);
                        }
                    });

                group.AddExtendedSlider(
                    "Stop emptying at %",
                    0,
                    100,
                    1,
                    settings.AutoEmptyStopLevelPercent,
                    value =>
                    {
                        try
                        {
                            if (settings.AutoEmptyStopLevelPercent != (uint)value)
                            {
                                settings.AutoEmptyStopLevelPercent = (uint)value;
                                Global.BuildingUpdateNeeded = true;
                                Global.Settings.Save();
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Error(this, "CreateEmptiableServiceBuildingGroup", ex, settings.EmptiableServiceBuildingNamePlural, "AutoEmptyStopLevelPercent", value);
                        }
                    });

                return group;
            }
            catch (Exception ex)
            {
                Log.Error(this, "CreateEmptiableServiceBuildingGroup", ex, settings.EmptiableServiceBuildingNamePlural);
                return null;
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Initializes the settings.
        /// </summary>
        public static void InitializeSettings()
        {
            if (Settings == null)
            {
                try
                {
                    try
                    {
                        Settings = Settings.Load();
                    }
                    catch (Exception ex)
                    {
                        Log.Error(typeof(Global), "InitSettings", ex);
                        Settings = new Settings();
                    }

                    Settings.LogSettings();

                    if (Settings.LoadedVersion < Settings.Version)
                    {
                        Settings.Save();
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(typeof(Global), "InitSettings", ex);
                }
            }
        }