public async Task GenerateMonitorConfigAsync()
        {
            var sb = new StringBuilder();

            sb.AppendLine("_  = function(p) return p; end;");
            sb.AppendLine("name = _(\"monitor_config_DAL\");");
            sb.AppendLine("Description = _(\"Monitor-Config created by DCS Alternate Launcher\")");
            sb.AppendLine();
            sb.AppendLine("-- *************** Displays ***************");

            var screens            = Screen.AllScreens.OrderBy(s => s.DeviceName).ToArray();
            var screensIndexByName = new Dictionary <string, int>();
            var usedScreens        = new List <Screen>();

            sb.AppendLine("displays =");
            sb.AppendLine("{");

            var realBounds = new Rect();

            for (var i = 0; i < screens.Length; i++)
            {
                var screen = screens[i];
                realBounds.Union(screen.Bounds);
            }

            var xOffset = Math.Abs(Screen.PrimaryScreen.Bounds.X - realBounds.X);
            var yOffset = Math.Abs(Screen.PrimaryScreen.Bounds.Y - realBounds.Y);

            for (var i = 0; i < screens.Length; i++)
            {
                var screen = screens[i];

                screensIndexByName.Add(screen.DeviceName, i + 1);

                sb.AppendLine($"    [{i + 1}] = ");
                sb.AppendLine("    {");
                sb.AppendLine($"        -- {screen.DeviceName},");
                sb.AppendLine($"        x = {xOffset + screen.Bounds.X},");
                sb.AppendLine($"        y = {yOffset + screen.Bounds.Y},");
                sb.AppendLine($"        width = {screen.Bounds.Width},");
                sb.AppendLine($"        height = {screen.Bounds.Height}");
                sb.AppendLine("    },");
            }

            sb.AppendLine("}");
            sb.AppendLine();

            sb.AppendLine("Viewports =");
            sb.AppendLine("{");
            sb.AppendLine("    Center =");
            sb.AppendLine("    {");
            sb.AppendLine($"        x = {xOffset},");
            sb.AppendLine($"        y = {yOffset},");
            sb.AppendLine($"        width = {Screen.PrimaryScreen.Bounds.Width},");
            sb.AppendLine($"        height = {Screen.PrimaryScreen.Bounds.Height},");
            sb.AppendLine("        viewDx = 0,");
            sb.AppendLine("        viewDy = 0,");
            sb.AppendLine($"        aspect = {Screen.PrimaryScreen.Bounds.Width} / {Screen.PrimaryScreen.Bounds.Height},");
            sb.AppendLine("    },");
            sb.AppendLine("}");
            sb.AppendLine();
            sb.AppendLine("UIMainView = Viewports.Center");
            sb.AppendLine();

            usedScreens.Add(Screen.PrimaryScreen);

            var viewportTemplates = _profileSettingsService.GetViewportTemplates();
            var installedModules  = await _dcsWorldService.GetInstalledAircraftModulesAsync();

            realBounds = Screen.PrimaryScreen.Bounds;

            foreach (var template in viewportTemplates)
            {
                if (!IsValidViewports(template.Viewports.ToArray()))
                {
                    continue;
                }

                var module = installedModules.FirstOrDefault(m => m.ModuleId == template.ModuleId);

                if (module == null)
                {
                    Tracer.Warn($"Could not patch viewport for module {template.ModuleId} because the module is not installed.");
                    continue;
                }

                Tracker.Instance.SendEvent(AnalyticsCategories.Viewports, "generating_viewports", module.ModuleId);

                sb.AppendLine($"-- *************** {module.DisplayName} ***************");
                sb.AppendLine();
                sb.AppendLine();

                foreach (var viewport in template.Viewports)
                {
                    var screen         = screens.Single(s => s.DeviceName == viewport.MonitorId);
                    var displayIndex   = screensIndexByName[screen.DeviceName];
                    var originalWidth  = (double)viewport.OriginalDisplayWidth;
                    var originalHeight = (double)viewport.OriginalDisplayHeight;
                    var ratioX         = viewport.X / originalWidth;
                    var ratioY         = viewport.Y / originalHeight;
                    var ratioW         = viewport.Width / originalWidth;
                    var ratioH         = viewport.Height / originalHeight;

                    var x = (int)Math.Round(screen.Bounds.Width * ratioX, 0, MidpointRounding.AwayFromZero);
                    var y = (int)Math.Round(screen.Bounds.Height * ratioY, 0, MidpointRounding.AwayFromZero);
                    var w = (int)Math.Round(screen.Bounds.Width * ratioW, 0, MidpointRounding.AwayFromZero);
                    var h = (int)Math.Round(screen.Bounds.Height * ratioH, 0, MidpointRounding.AwayFromZero);

                    realBounds.Union(screen.Bounds);

                    sb.AppendLine($"--{viewport.RelativeInitFilePath}");
                    sb.AppendLine($"{template.ViewportPrefix}_{viewport.ViewportName} =");
                    sb.AppendLine("{");
                    sb.AppendLine($"    x = displays[{displayIndex}].x + {x},");
                    sb.AppendLine($"    y = displays[{displayIndex}].y + {y},");
                    sb.AppendLine($"    width = {w},");
                    sb.AppendLine($"    height = {h},");
                    sb.AppendLine("}");
                    sb.AppendLine();

                    if (!usedScreens.Contains(screen))
                    {
                        usedScreens.Add(screen);
                    }
                }

                sb.AppendLine();
                sb.AppendLine();
            }

            var contents = sb.ToString();
            var install  = _settingsService.SelectedInstall;
            var monitorConfigDirectory = Path.Combine(install.SavedGamesPath, "Config\\MonitorSetup");
            var monitorConfigPath      = Path.Combine(monitorConfigDirectory, "monitor_config_DAL.lua");

            if (!Directory.Exists(monitorConfigDirectory))
            {
                Directory.CreateDirectory(monitorConfigDirectory);
            }

            if (File.Exists(monitorConfigPath))
            {
                if (MessageBoxEx.Show("Monitor config already exists.  Are you sure you want to override it?", "Overwrite?", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    File.WriteAllText(monitorConfigPath, contents);
                }
            }
            else
            {
                File.WriteAllText(monitorConfigPath, contents);
            }

            if (MessageBoxEx.Show("Do you want DCS Alternative Launcher to adjust your Game Resolution and Monitor Config?", "Update DCS", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                using (var context = new OptionLuaContext(install))
                {
                    context.SetValue("graphics", "multiMonitorSetup", "monitor_config_DAL");
                    context.SetValue("graphics", "width", realBounds.Width);
                    context.SetValue("graphics", "height", realBounds.Height);
                    context.Save();
                }
            }
            else
            {
                MessageBoxEx.Show(@"Make sure you setup the proper Monitor config in DCS once it is started." + Environment.NewLine +
                                  @"To do this go to Options -> System -> Monitors and change the drop down to ""monitor_config_DAL""" + Environment.NewLine +
                                  $"Then make sure you change your monitor resolution to at least {realBounds.Width}x{realBounds.Height}.");
            }
        }
        public Task PatchViewportsAsync()
        {
            return(Task.Run(async() =>
            {
                var install = _settingsService.SelectedInstall;
                var viewportTemplates = _profileSettingsService.GetViewportTemplates();
                var modules = await GetInstalledAircraftModulesAsync();

                foreach (var template in viewportTemplates)
                {
                    var module = modules.FirstOrDefault(m => m.ModuleId == template.ModuleId);

                    if (module == null)
                    {
                        Tracer.Warn($"Could not patch viewport for module {template.ModuleId} because the module is not installed.");
                        return;
                    }

                    Tracker.Instance.SendEvent(AnalyticsCategories.Viewports, "patching_viewports", module.ModuleId);

                    foreach (var viewport in template.Viewports)
                    {
                        if (string.IsNullOrEmpty(viewport.RelativeInitFilePath))
                        {
                            continue;
                        }

                        if (!install.FileExists(viewport.RelativeInitFilePath))
                        {
                            Tracer.Warn($"Module {template.ModuleId}: Unable to patch viewport(s) [{viewport.ViewportName} in file {viewport.RelativeInitFilePath}.");
                            continue;
                        }

                        var contents = install.ReadAllText(viewport.RelativeInitFilePath);
                        var isChanged = false;

                        if (!contents.Contains("dofile(LockOn_Options.common_script_path..\"ViewportHandling.lua\")"))
                        {
                            Tracer.Info($"Adding ViewportHandling code to {viewport.RelativeInitFilePath}");
                            contents += Environment.NewLine + "dofile(LockOn_Options.common_script_path..\"ViewportHandling.lua\")" + Environment.NewLine;
                            isChanged = true;
                        }

                        var originalCode = $"try_find_assigned_viewport(\"{viewport.ViewportName}\")";

                        var code = $"try_find_assigned_viewport(\"{module.ViewportPrefix}_{viewport.ViewportName}\")";

                        if (!contents.Contains(code))
                        {
                            Tracer.Info($"Adding viewport name assignment code to {viewport.RelativeInitFilePath}");

                            if (contents.Contains(originalCode))
                            {
                                contents = contents.Replace(originalCode, code);
                            }
                            else
                            {
                                contents += Environment.NewLine + code + Environment.NewLine;
                            }

                            isChanged = true;
                        }

                        if (isChanged)
                        {
                            Tracer.Info($"Saving {viewport.RelativeInitFilePath}");
                            install.WriteAllText(viewport.RelativeInitFilePath, contents);
                        }
                    }
                }
            }));
        }