public static Settings InitializeSettings()
        {
            var firstRun = !File.Exists("settings.json");

            var settings = firstRun ? new Settings() : Settings.Instance;

            if (settings.ShowUpgradeWarning && !firstRun)
            {
                MessageBoxManager.Cancel = "Exit";
                MessageBoxManager.OK     = "Continue";
                MessageBoxManager.Register();
                var choice = MessageBox.Show(
                    "WARNING!!\n\nThis launcher is NOT COMPATIBLE with the old 'settings.json' file.\nStop NOW and launch the old version to export a profile of your mods WITH GROUPS!\nOnce that is done, move the old 'settings.json' file to a SAFE PLACE and then proceed.\nAfter loading, import the profile you saved to recover groups.\n\nIf you are not ready to do this, click 'Exit' to leave with no changes.",
                    "WARNING!", MessageBoxButtons.OKCancel, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button2);
                if (choice == DialogResult.Cancel)
                {
                    Environment.Exit(0);
                }
                MessageBoxManager.Unregister();
            }
            settings.ShowUpgradeWarning = false;


            // Verify Game Path
            if (!Directory.Exists(settings.GamePath))
            {
                settings.GamePath = XCOM2.DetectGameDir();
            }

            if (settings.GamePath == "")
            {
                MessageBox.Show(@"Could not find XCOM 2 installation path. Please fill it manually in the settings.");
            }

            // Verify Mod Paths
            var pathsToEdit = settings.ModPaths.Where(m => !m.EndsWith("\\")).ToList();

            foreach (var modPath in pathsToEdit)
            {
                settings.ModPaths.Add(modPath + "\\");
                settings.ModPaths.Remove(modPath);
            }

            var oldPaths = settings.ModPaths.Where(modPath => !Directory.Exists(modPath)).ToList();

            foreach (var modPath in oldPaths)
            {
                settings.ModPaths.Remove(modPath);
            }

            foreach (var modPath in XCOM2.DetectModDirs())
            {
                if (!settings.ModPaths.Contains(modPath))
                {
                    if (!settings.ModPaths.Contains(modPath + "\\"))
                    {
                        settings.ModPaths.Add(modPath);
                    }
                }
            }


            if (settings.ModPaths.Count == 0)
            {
                MessageBox.Show(@"Could not find XCOM 2 mod directories. Please fill them in manually in the settings.");
            }

            if (settings.Mods.Entries.Count > 0)
            {
                // Verify categories
                var index = settings.Mods.Entries.Values.Max(c => c.Index);
                foreach (var cat in settings.Mods.Entries.Values.Where(c => c.Index == -1))
                {
                    cat.Index = ++index;
                }

                // Verify Mods
                foreach (var mod in settings.Mods.All)
                {
                    if (!settings.ModPaths.Any(mod.IsInModPath))
                    {
                        mod.State |= ModState.NotLoaded;
                    }
                    if (!Directory.Exists(mod.Path) || !File.Exists(mod.GetModInfoFile()))
                    {
                        mod.State |= ModState.NotInstalled;
                    }
                    // tags clean up
                    mod.Tags = mod.Tags.Where(t => settings.Tags.ContainsKey(t)).ToList();
                }

                var newlyBrokenMods = settings.Mods.All.Where(m => (m.State == ModState.NotLoaded || m.State == ModState.NotInstalled) && !m.isHidden).ToList();
                if (newlyBrokenMods.Count > 0)
                {
                    if (newlyBrokenMods.Count == 1)
                    {
                        FlexibleMessageBox.Show($"The mod '{newlyBrokenMods[0].Name}' no longer exists and has been hidden.");
                    }
                    else
                    {
                        FlexibleMessageBox.Show($"{newlyBrokenMods.Count} mods no longer exist and have been hidden:\r\n\r\n" + string.Join("\r\n", newlyBrokenMods.Select(m => m.Name)));
                    }

                    foreach (var m in newlyBrokenMods)
                    {
                        m.isHidden = true;
                    }
                    //settings.Mods.RemoveMod(m);
                }
            }

            // import mods
            settings.ImportMods();

            return(settings);
        }
Exemple #2
0
        public static Settings InitializeSettings()
        {
            var firstRun = !File.Exists("settings.json");

            Settings settings;

            if (firstRun)
            {
                settings = new Settings();
            }

            else
            {
                try
                {
                    settings = Settings.FromFile("settings.json");
                }
                catch (JsonSerializationException)
                {
                    MessageBox.Show("settings.json could not be read.\r\nPlease delete or rename that file and try again.");
                    return(null);
                }
            }

            // Verify Game Path
            if (!Directory.Exists(settings.GamePath))
            {
                settings.GamePath = XCOM2.DetectGameDir();
            }

            if (settings.GamePath == "")
            {
                MessageBox.Show("Could not find XCOM 2 installation path. Please fill it manually in the settings.");
            }

            // Verify Mod Paths
            var oldPaths = settings.ModPaths.Where(modPath => !Directory.Exists(modPath)).ToList();

            foreach (var modPath in oldPaths)
            {
                settings.ModPaths.Remove(modPath);
            }

            foreach (var modPath in XCOM2.DetectModDirs())
            {
                if (!settings.ModPaths.Contains(modPath))
                {
                    settings.ModPaths.Add(modPath);
                }
            }


            if (settings.ModPaths.Count == 0)
            {
                MessageBox.Show("Could not find XCOM 2 mod directories. Please fill them in manually in the settings.");
            }

            if (settings.Mods.Entries.Count > 0)
            {
                // Verify categories
                var index = settings.Mods.Entries.Values.Max(c => c.Index);
                foreach (var cat in settings.Mods.Entries.Values.Where(c => c.Index == -1))
                {
                    cat.Index = ++index;
                }

                // Verify Mods
                foreach (var mod in settings.Mods.All.Where(mod => !settings.ModPaths.Any(mod.IsInModPath)))
                {
                    mod.State |= ModState.NotLoaded;
                }

                var brokenMods = settings.Mods.All.Where(m => !Directory.Exists(m.Path) || !File.Exists(m.GetModInfoFile())).ToList();
                if (brokenMods.Count > 0)
                {
                    MessageBox.Show($"{brokenMods.Count} mods no longer exists and have been removed:\r\n\r\n" + string.Join("\r\n", brokenMods.Select(m => m.Name)));

                    foreach (var m in brokenMods)
                    {
                        settings.Mods.RemoveMod(m);
                    }
                }
            }

            // import mods
            settings.ImportMods();

            return(settings);
        }
        public static Settings InitializeSettings()
        {
            var firstRun = !File.Exists("settings.json");

            var settings = firstRun ? new Settings() : Settings.Instance;

            // Logic behind this:
            // If the field ShowUpgradeWarning doesn't exists in the loaded settings file; it will be initialized to its default value "true".
            // In that case, an old incompatible settings version is assumed and we report a warning.
            if (settings.ShowUpgradeWarning && !firstRun)
            {
                Log.Warn("Incompatible settings.json");

                MessageBoxManager.Cancel = "Exit";
                MessageBoxManager.OK     = "Continue";
                MessageBoxManager.Register();
                var choice = MessageBox.Show("This launcher version is NOT COMPATIBLE with the old 'settings.json' file.\n" +
                                             "Stop NOW and launch the old version to export a profile of your mods INCLUDING GROUPS!\n" +
                                             "Once that is done, move the old 'settings.json' file to a SAFE PLACE and then proceed.\n" +
                                             "After loading, import the profile you saved to recover groups.\n\n" +
                                             "If you are not ready to do this, click 'Exit' to leave with no changes.",
                                             "WARNING!", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);

                if (choice == DialogResult.Cancel)
                {
                    Environment.Exit(0);
                }

                Log.Warn("User ignored incompatibility");
                MessageBoxManager.Unregister();
            }

            settings.ShowUpgradeWarning = false;

            // Verify Game Path
            if (!Directory.Exists(settings.GamePath))
            {
                settings.GamePath = XCOM2.DetectGameDir();
            }

            if (settings.GamePath == "")
            {
                Log.Warn("Unable to detect XCOM 2 installation path");
                MessageBox.Show(@"Could not find XCOM 2 installation path. Please fill it manually in the settings.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            // Make sure, that all mod paths have a trailing backslash
            var pathsWithMissingTrailingBackSlash = settings.ModPaths.Where(m => !m.EndsWith(@"\")).ToList();

            for (var i = 0; i < pathsWithMissingTrailingBackSlash.Count; i++)
            {
                pathsWithMissingTrailingBackSlash[i] += @"\";
            }

            // Check and potentially add new mod paths from XCOM ini file.
            var modPathsFromIni = XCOM2.DetectModDirs();

            if (modPathsFromIni != null)
            {
                settings.ModPaths.AddRange(modPathsFromIni.Where(modPath => !settings.ModPaths.Contains(modPath)));
            }
            else
            {
                MessageBox.Show("Unable to read mod directories from 'XComEngine.ini'. See file 'AML.log' for details.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            // Remove obsolete mod paths
            settings.ModPaths.RemoveAll(modPath => !Directory.Exists(modPath));

            if (settings.ModPaths.Count == 0)
            {
                Log.Warn("No XCOM 2 mod directories configured");
                MessageBox.Show(@"Could not find XCOM 2 mod directories. Please fill them in manually in the settings.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            if (settings.Mods.Entries.Count > 0)
            {
                // Verify categories
                var index = settings.Mods.Entries.Values.Max(c => c.Index);
                foreach (var cat in settings.Mods.Entries.Values.Where(c => c.Index == -1))
                {
                    cat.Index = ++index;
                }

                // Verify Mods
                foreach (var mod in settings.Mods.All)
                {
                    if (!settings.ModPaths.Any(mod.IsInModPath))
                    {
                        Log.Warn($"The mod {mod.ID} is not located in any of the configured mod directories -> ModState.NotLoaded");
                        mod.AddState(ModState.NotLoaded);
                    }

                    if (!Directory.Exists(mod.Path))
                    {
                        Log.Warn($"The mod {mod.ID} is no longer available in the directory {mod.Path} -> ModState.NotInstalled");
                        mod.AddState(ModState.NotInstalled);
                    }
                    else if (!File.Exists(mod.GetModInfoFile()))
                    {
                        string newModInfo = settings.Mods.FindModInfo(mod.Path);
                        if (newModInfo != null)
                        {
                            mod.ID = Path.GetFileNameWithoutExtension(newModInfo);
                        }
                        else
                        {
                            Log.Warn($"The XComMod file for the mod {mod.ID} is missing -> ModState.NotInstalled");
                            mod.AddState(ModState.NotInstalled);
                        }
                    }

                    // tags clean up
                    mod.Tags = mod.Tags.Where(t => settings.Tags.ContainsKey(t.ToLower())).ToList();
                }

                var newlyBrokenMods = settings.Mods.All.Where(m => (m.State == ModState.NotLoaded || m.State == ModState.NotInstalled) && !m.isHidden).ToList();
                if (newlyBrokenMods.Count > 0)
                {
                    if (newlyBrokenMods.Count == 1)
                    {
                        FlexibleMessageBox.Show($"The mod '{newlyBrokenMods[0].Name}' no longer exists and has been hidden.");
                    }
                    else
                    {
                        FlexibleMessageBox.Show($"{newlyBrokenMods.Count} mods no longer exist and have been hidden:\r\n\r\n" + string.Join("\r\n", newlyBrokenMods.Select(m => m.Name)));
                    }

                    foreach (var m in newlyBrokenMods)
                    {
                        m.isHidden = true;
                    }
                }
            }

            // import mods
            settings.ImportMods();

            return(settings);
        }