From 6d78a4dc0ed20fe78bbcef97e25db11bcf8cc64f Mon Sep 17 00:00:00 2001 From: Nick Blakely Date: Wed, 12 Aug 2020 23:37:45 -0700 Subject: [PATCH] More cleanup. Only show renderer file name if multiple entries exist. --- Giants.Launcher/GameSettings.cs | 10 +-- Giants.Launcher/Giants.Launcher.csproj | 3 +- Giants.Launcher/LauncherForm.cs | 2 +- Giants.Launcher/Native/Capabilities.cs | 60 -------------- Giants.Launcher/Native/NativeMethods.cs | 3 - Giants.Launcher/Native/RendererInterop.cs | 8 +- Giants.Launcher/OptionsForm.cs | 80 +++++++++--------- Giants.Launcher/Program.cs | 1 - .../Renderer/RenderInfoExtensions.cs | 28 +++++++ Giants.Launcher/Renderer/RendererInfo.cs | 81 +++++++++++++++++++ 10 files changed, 163 insertions(+), 113 deletions(-) delete mode 100644 Giants.Launcher/Native/Capabilities.cs create mode 100644 Giants.Launcher/Renderer/RenderInfoExtensions.cs create mode 100644 Giants.Launcher/Renderer/RendererInfo.cs diff --git a/Giants.Launcher/GameSettings.cs b/Giants.Launcher/GameSettings.cs index 16be3aa..7caf465 100644 --- a/Giants.Launcher/GameSettings.cs +++ b/Giants.Launcher/GameSettings.cs @@ -7,7 +7,7 @@ using Microsoft.Win32; namespace Giants.Launcher { - static class GameSettings + public static class GameSettings { // Constants private const string RegistryKey = @"HKEY_CURRENT_USER\Software\PlanetMoon\Giants"; @@ -16,8 +16,8 @@ namespace Giants.Launcher private static readonly Dictionary Settings = new Dictionary(); // List of renderers compatible with the user's system. - public static List CompatibleRenderers; - + public static List CompatibleRenderers { get; set; } = new List(); + public static T Get(string settingName) { return (T)Get(settingName); @@ -54,10 +54,10 @@ namespace Giants.Launcher Settings[SettingKeys.NoAutoUpdate] = 0; // Get a list of renderers compatible with the user's system - if (CompatibleRenderers == null) + if (!CompatibleRenderers.Any()) { CompatibleRenderers = RendererInterop.GetCompatibleRenderers(gamePath); - if (CompatibleRenderers.Count == 0) + if (!CompatibleRenderers.Any()) { MessageBox.Show( text: Resources.ErrorNoRenderers, diff --git a/Giants.Launcher/Giants.Launcher.csproj b/Giants.Launcher/Giants.Launcher.csproj index 175a552..4a5c40e 100644 --- a/Giants.Launcher/Giants.Launcher.csproj +++ b/Giants.Launcher/Giants.Launcher.csproj @@ -77,7 +77,8 @@ - + + diff --git a/Giants.Launcher/LauncherForm.cs b/Giants.Launcher/LauncherForm.cs index 28572a6..2344dec 100644 --- a/Giants.Launcher/LauncherForm.cs +++ b/Giants.Launcher/LauncherForm.cs @@ -234,7 +234,7 @@ namespace Giants.Launcher UpdateInfo info = (UpdateInfo)e.UserState; this.txtProgress.Visible = true; - this.txtProgress.Text = string.Format(Resources.DownloadProgress, e.ProgressPercentage, (info.FileSize / 1024) / 1024); + this.txtProgress.Text = string.Format(Resources.DownloadProgress, e.ProgressPercentage, info.FileSize / 1024 / 1024); } } } diff --git a/Giants.Launcher/Native/Capabilities.cs b/Giants.Launcher/Native/Capabilities.cs deleted file mode 100644 index 94849b6..0000000 --- a/Giants.Launcher/Native/Capabilities.cs +++ /dev/null @@ -1,60 +0,0 @@ -using System; -using System.IO; - -namespace Giants.Launcher -{ - partial class RendererInterop - { - public class Capabilities : IComparable - { - [Flags] - public enum RendererFlag - { - LowBitDepthAllowed = 0x1, - - // Multisampling support flags: - MSAA2x = 0x2, - MSAA4x = 0x4, - MSAA8x = 0x8, - MSAA16x = 0x10, - - // Other options: - VSync = 0x20, - TripleBuffer = 0x40, - }; - - public Capabilities(string filePath, ref RendererInterop.GFXCapabilityInfo gfxCaps) - { - this.FilePath = filePath; - this.FileName = Path.GetFileName(filePath); - this.MaxAnisotropy = gfxCaps.maxAnisotropy; - this.Flags = (RendererFlag)gfxCaps.flags; - this.Priority = gfxCaps.priority; - this.Name = gfxCaps.rendererName; - } - - public override string ToString() - { - return string.Format("{0} ({1})", this.Name, Path.GetFileName(this.FilePath)); - } - - public int CompareTo(object obj) - { - if (obj == null) return 1; - - Capabilities other = obj as Capabilities; - if (other != null) - return this.Priority.CompareTo(other.Priority); - else - throw new ArgumentException(); - } - - public string FilePath { get; private set; } - public string FileName { get; private set; } - public int MaxAnisotropy { get; private set; } - public RendererFlag Flags { get; private set; } - public int Priority { get; private set; } - public string Name { get; private set; } - } - } -} diff --git a/Giants.Launcher/Native/NativeMethods.cs b/Giants.Launcher/Native/NativeMethods.cs index ea11b21..4d92538 100644 --- a/Giants.Launcher/Native/NativeMethods.cs +++ b/Giants.Launcher/Native/NativeMethods.cs @@ -29,9 +29,6 @@ namespace Giants.Launcher [DllImport("user32.dll")] public static extern bool EnumDisplaySettings( string deviceName, int modeNum, ref DEVMODE devMode); - const int ENUM_CURRENT_SETTINGS = -1; - - const int ENUM_REGISTRY_SETTINGS = -2; [StructLayout(LayoutKind.Sequential)] public struct DEVMODE diff --git a/Giants.Launcher/Native/RendererInterop.cs b/Giants.Launcher/Native/RendererInterop.cs index 0fa9748..698f9b3 100644 --- a/Giants.Launcher/Native/RendererInterop.cs +++ b/Giants.Launcher/Native/RendererInterop.cs @@ -6,7 +6,7 @@ using System.Windows.Forms; namespace Giants.Launcher { - partial class RendererInterop + public class RendererInterop { #pragma warning disable 649 public struct GFXCapabilityInfo @@ -46,11 +46,11 @@ namespace Giants.Launcher } - public static List GetCompatibleRenderers(string gamePath) + public static List GetCompatibleRenderers(string gamePath) { var dir = new DirectoryInfo( Path.GetDirectoryName(gamePath)); - var capabilities = new List(); + var capabilities = new List(); // Search current directory for compatible renderers: foreach (FileInfo file in dir.GetFiles("gg_*.dll")) @@ -62,7 +62,7 @@ namespace Giants.Launcher string path = Path.Combine(file.DirectoryName, file.Name); if (GetRendererCapabilities(path, ref interopCaps)) { - Capabilities caps = new Capabilities(path, ref interopCaps); + RendererInfo caps = new RendererInfo(path, ref interopCaps); capabilities.Add(caps); } } diff --git a/Giants.Launcher/OptionsForm.cs b/Giants.Launcher/OptionsForm.cs index d7c6f0f..6f7d8d2 100644 --- a/Giants.Launcher/OptionsForm.cs +++ b/Giants.Launcher/OptionsForm.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Windows.Forms; namespace Giants.Launcher @@ -26,10 +27,14 @@ namespace Giants.Launcher private void SetOptions() { this.cmbRenderer.Items.Clear(); - this.cmbRenderer.Items.AddRange(GameSettings.CompatibleRenderers.ToArray()); + this.cmbRenderer.Items.AddRange( + GameSettings.CompatibleRenderers + .Disambiguate() + .ToList() + .ToArray()); - RendererInterop.Capabilities renderer = GameSettings.CompatibleRenderers.Find( - r => StringComparer.OrdinalIgnoreCase.Compare(Path.GetFileName(r.FilePath), GameSettings.Get("Renderer")) == 0); + RendererInfo renderer = GameSettings.CompatibleRenderers.Find( + r => StringComparer.OrdinalIgnoreCase.Compare(Path.GetFileName(r.FilePath), GameSettings.Get(SettingKeys.Renderer)) == 0); if (renderer != null) { @@ -41,22 +46,22 @@ namespace Giants.Launcher this.cmbRenderer.SelectedItem = renderer; } - var Resolutions = (List)this.cmbResolution.DataSource; - this.cmbResolution.SelectedItem = Resolutions.Find(r => r.Width == (int)GameSettings.Get("VideoWidth") && r.Height == (int)GameSettings.Get("VideoHeight")); + var resolutions = (List)this.cmbResolution.DataSource; + this.cmbResolution.SelectedItem = resolutions.Find(r => r.Width == GameSettings.Get(SettingKeys.VideoWidth) && r.Height == GameSettings.Get(SettingKeys.VideoHeight)); if (this.cmbResolution.SelectedItem == null) this.cmbResolution.SelectedIndex = 0; var AntialiasingOptions = (List>)this.cmbAntialiasing.DataSource; - this.cmbAntialiasing.SelectedItem = AntialiasingOptions.Find(o => o.Value == (int)GameSettings.Get("Antialiasing")); + this.cmbAntialiasing.SelectedItem = AntialiasingOptions.Find(o => o.Value == GameSettings.Get(SettingKeys.Antialiasing)); if (this.cmbAntialiasing.SelectedItem == null) this.cmbAntialiasing.SelectedIndex = 0; var AnisotropyOptions = (List>)this.cmbAnisotropy.DataSource; - this.cmbAnisotropy.SelectedItem = AnisotropyOptions.Find(o => o.Value == (int)GameSettings.Get("AnisotropicFiltering")); + this.cmbAnisotropy.SelectedItem = AnisotropyOptions.Find(o => o.Value == GameSettings.Get(SettingKeys.AnisotropicFiltering)); if (this.cmbAnisotropy.SelectedItem == null) this.cmbAnisotropy.SelectedIndex = 0; - this.chkUpdates.Checked = ((int)GameSettings.Get("NoAutoUpdate") == 1 ? false : true); + this.chkUpdates.Checked = GameSettings.Get(SettingKeys.NoAutoUpdate) != 1; } private void PopulateAntialiasing() @@ -64,22 +69,22 @@ namespace Giants.Launcher var antialiasingOptions = new List>(); antialiasingOptions.Add(new KeyValuePair(Resources.OptionNone, 0)); - var renderer = (RendererInterop.Capabilities)this.cmbRenderer.SelectedItem; + var renderer = (RendererInfo)this.cmbRenderer.SelectedItem; if (renderer != null) { - if (renderer.Flags.HasFlag(RendererInterop.Capabilities.RendererFlag.MSAA2x)) + if (renderer.Flags.HasFlag(RendererInfo.RendererFlag.MSAA2x)) { antialiasingOptions.Add(new KeyValuePair(string.Format(Resources.OptionSamples, 2), 2)); } - if (renderer.Flags.HasFlag(RendererInterop.Capabilities.RendererFlag.MSAA4x)) + if (renderer.Flags.HasFlag(RendererInfo.RendererFlag.MSAA4x)) { antialiasingOptions.Add(new KeyValuePair(string.Format(Resources.OptionSamples, 4), 4)); } - if (renderer.Flags.HasFlag(RendererInterop.Capabilities.RendererFlag.MSAA8x)) + if (renderer.Flags.HasFlag(RendererInfo.RendererFlag.MSAA8x)) { antialiasingOptions.Add(new KeyValuePair(string.Format(Resources.OptionSamples, 8), 8)); } - if (renderer.Flags.HasFlag(RendererInterop.Capabilities.RendererFlag.MSAA16x)) + if (renderer.Flags.HasFlag(RendererInfo.RendererFlag.MSAA16x)) { antialiasingOptions.Add(new KeyValuePair(string.Format(Resources.OptionSamples, 16), 16)); } @@ -110,18 +115,17 @@ namespace Giants.Launcher private void PopulateAnisotropy() { - List> AnisotropyOptions = new List>(); + var anisotropyOptions = new List>(); + anisotropyOptions.Add(new KeyValuePair(Resources.OptionNone, 0)); - AnisotropyOptions.Add(new KeyValuePair(Resources.OptionNone, 0)); - - var renderer = (RendererInterop.Capabilities)this.cmbRenderer.SelectedItem; + var renderer = (RendererInfo)this.cmbRenderer.SelectedItem; if (renderer != null) { for (int i = 2; i <= renderer.MaxAnisotropy; i++) { if (!this.IsPowerOfTwo(i)) continue; - AnisotropyOptions.Add(new KeyValuePair(string.Format(Resources.OptionSamples, i), i)); + anisotropyOptions.Add(new KeyValuePair(string.Format(Resources.OptionSamples, i), i)); } } @@ -132,7 +136,7 @@ namespace Giants.Launcher currentValue = (int)this.cmbAnisotropy.SelectedValue; } - this.cmbAnisotropy.DataSource = AnisotropyOptions; + this.cmbAnisotropy.DataSource = anisotropyOptions; this.cmbAnisotropy.DisplayMember = "Key"; this.cmbAnisotropy.ValueMember = "Value"; @@ -168,10 +172,10 @@ namespace Giants.Launcher this.PopulateAntialiasing(); this.PopulateAnisotropy(); - bool windowed = ((int)GameSettings.Get("Windowed") == 1 ? true : false); + bool windowed = GameSettings.Get(SettingKeys.Windowed) == 1; if (windowed) { - bool borderless = (int)GameSettings.Get("BorderlessWindow") == 1 ? true : false; + bool borderless = GameSettings.Get(SettingKeys.BorderlessWindow) == 1; if (borderless) this.cmbMode.SelectedIndex = 2; else @@ -180,55 +184,55 @@ namespace Giants.Launcher else this.cmbMode.SelectedIndex = 0; - var renderer = (RendererInterop.Capabilities)this.cmbRenderer.SelectedItem; + var renderer = (RendererInfo)this.cmbRenderer.SelectedItem; - if ((renderer.Flags & RendererInterop.Capabilities.RendererFlag.VSync) != RendererInterop.Capabilities.RendererFlag.VSync) + if ((renderer.Flags & RendererInfo.RendererFlag.VSync) != RendererInfo.RendererFlag.VSync) { this.chkVSync.Checked = false; this.chkVSync.Enabled = false; } else { - this.chkVSync.Checked = ((int)GameSettings.Get("VerticalSync") == 1 ? true : false); + this.chkVSync.Checked = GameSettings.Get(SettingKeys.VerticalSync) == 1; this.chkVSync.Enabled = true; } - if ((renderer.Flags & RendererInterop.Capabilities.RendererFlag.TripleBuffer) != RendererInterop.Capabilities.RendererFlag.TripleBuffer) + if ((renderer.Flags & RendererInfo.RendererFlag.TripleBuffer) != RendererInfo.RendererFlag.TripleBuffer) { this.chkTripleBuffering.Checked = false; this.chkTripleBuffering.Enabled = false; } else { - this.chkTripleBuffering.Checked = ((int)GameSettings.Get("TripleBuffering") == 1 ? true : false); + this.chkTripleBuffering.Checked = GameSettings.Get(SettingKeys.TripleBuffering) == 1; this.chkTripleBuffering.Enabled = true; } } private void btnOK_Click(object sender, EventArgs e) { - var renderer = this.cmbRenderer.SelectedItem as RendererInterop.Capabilities; + var renderer = this.cmbRenderer.SelectedItem as RendererInfo; if (renderer != null) { - GameSettings.Modify("Renderer", renderer.FileName); + GameSettings.Modify(SettingKeys.Renderer, renderer.FileName); } var resolution = (ScreenResolution)this.cmbResolution.SelectedItem; if (resolution != null) { - GameSettings.Modify("VideoWidth", resolution.Width); - GameSettings.Modify("VideoHeight", resolution.Height); + GameSettings.Modify(SettingKeys.VideoWidth, resolution.Width); + GameSettings.Modify(SettingKeys.VideoHeight, resolution.Height); } - GameSettings.Modify("Antialiasing", this.cmbAntialiasing.SelectedValue); - GameSettings.Modify("AnisotropicFiltering", this.cmbAnisotropy.SelectedValue); - bool windowed = ((WindowType)this.cmbMode.SelectedIndex == WindowType.Windowed || (WindowType)this.cmbMode.SelectedIndex == WindowType.Borderless); - GameSettings.Modify("Windowed", (windowed == true ? 1 : 0)); + GameSettings.Modify(SettingKeys.Antialiasing, this.cmbAntialiasing.SelectedValue); + GameSettings.Modify(SettingKeys.AnisotropicFiltering, this.cmbAnisotropy.SelectedValue); + bool windowed = (WindowType)this.cmbMode.SelectedIndex == WindowType.Windowed || (WindowType)this.cmbMode.SelectedIndex == WindowType.Borderless; + GameSettings.Modify(SettingKeys.Windowed, windowed == true ? 1 : 0); bool borderless = (WindowType)this.cmbMode.SelectedIndex == WindowType.Borderless; - GameSettings.Modify("BorderlessWindow", borderless == true ? 1 : 0); - GameSettings.Modify("VerticalSync", (this.chkVSync.Checked == true ? 1 : 0)); - GameSettings.Modify("TripleBuffering", (this.chkTripleBuffering.Checked == true ? 1 : 0)); - GameSettings.Modify("NoAutoUpdate", (this.chkUpdates.Checked == false ? 1 : 0)); + GameSettings.Modify(SettingKeys.BorderlessWindow, borderless == true ? 1 : 0); + GameSettings.Modify(SettingKeys.VerticalSync, this.chkVSync.Checked == true ? 1 : 0); + GameSettings.Modify(SettingKeys.TripleBuffering, this.chkTripleBuffering.Checked == true ? 1 : 0); + GameSettings.Modify(SettingKeys.NoAutoUpdate, this.chkUpdates.Checked == false ? 1 : 0); GameSettings.Save(); diff --git a/Giants.Launcher/Program.cs b/Giants.Launcher/Program.cs index 0ed8c81..31efb5a 100644 --- a/Giants.Launcher/Program.cs +++ b/Giants.Launcher/Program.cs @@ -37,7 +37,6 @@ namespace Giants.Launcher Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Form form; try { diff --git a/Giants.Launcher/Renderer/RenderInfoExtensions.cs b/Giants.Launcher/Renderer/RenderInfoExtensions.cs new file mode 100644 index 0000000..9b0262e --- /dev/null +++ b/Giants.Launcher/Renderer/RenderInfoExtensions.cs @@ -0,0 +1,28 @@ +namespace Giants.Launcher +{ + using System.Collections.Generic; + using System.IO; + using System.Linq; + + public static class RenderInfoExtensions + { + /// + /// Disambiguates renderers with the same name by adding the file name. + /// + public static IList Disambiguate(this IList rendererInfos) + { + foreach (var group in rendererInfos.GroupBy(x => x.Name)) + { + if (group.Count() > 1) + { + foreach (var rendererInfo in group) + { + rendererInfo.Name = $"{rendererInfo.Name} ({Path.GetFileName(rendererInfo.FilePath)})"; + } + } + } + + return rendererInfos; + } + } +} diff --git a/Giants.Launcher/Renderer/RendererInfo.cs b/Giants.Launcher/Renderer/RendererInfo.cs new file mode 100644 index 0000000..7f31410 --- /dev/null +++ b/Giants.Launcher/Renderer/RendererInfo.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace Giants.Launcher +{ + public class RendererInfo : IComparable + { + [Flags] + public enum RendererFlag + { + LowBitDepthAllowed = 0x1, + + // Multisampling support flags: + MSAA2x = 0x2, + MSAA4x = 0x4, + MSAA8x = 0x8, + MSAA16x = 0x10, + + // Other options: + VSync = 0x20, + TripleBuffer = 0x40, + }; + + public RendererInfo(string filePath, ref RendererInterop.GFXCapabilityInfo gfxCaps) + { + this.FilePath = filePath; + this.FileName = Path.GetFileName(filePath); + this.MaxAnisotropy = gfxCaps.maxAnisotropy; + this.Flags = (RendererFlag)gfxCaps.flags; + this.Priority = gfxCaps.priority; + this.Name = gfxCaps.rendererName; + } + + public override string ToString() + { + return this.Name; + } + + public int CompareTo(object obj) + { + if (obj == null) return 1; + + RendererInfo other = obj as RendererInfo; + if (other != null) + return this.Priority.CompareTo(other.Priority); + else + throw new ArgumentException(); + } + + public override bool Equals(object obj) + { + return obj is RendererInfo info && + this.FilePath == info.FilePath && + this.FileName == info.FileName && + this.MaxAnisotropy == info.MaxAnisotropy && + this.Flags == info.Flags && + this.Priority == info.Priority && + this.Name == info.Name; + } + + public override int GetHashCode() + { + int hashCode = 300496696; + hashCode = hashCode * -1521134295 + EqualityComparer.Default.GetHashCode(this.FilePath); + hashCode = hashCode * -1521134295 + EqualityComparer.Default.GetHashCode(this.FileName); + hashCode = hashCode * -1521134295 + this.MaxAnisotropy.GetHashCode(); + hashCode = hashCode * -1521134295 + this.Flags.GetHashCode(); + hashCode = hashCode * -1521134295 + this.Priority.GetHashCode(); + hashCode = hashCode * -1521134295 + EqualityComparer.Default.GetHashCode(this.Name); + return hashCode; + } + + public string FilePath { get; private set; } + public string FileName { get; private set; } + public int MaxAnisotropy { get; private set; } + public RendererFlag Flags { get; private set; } + public int Priority { get; private set; } + public string Name { get; set; } + } +}