1
0
mirror of https://github.com/ncblakely/GiantsTools synced 2024-11-21 21:55:38 +01:00

More cleanup. Only show renderer file name if multiple entries exist.

This commit is contained in:
Nick Blakely 2020-08-12 23:37:45 -07:00
parent 0052935950
commit 6d78a4dc0e
10 changed files with 163 additions and 113 deletions

View File

@ -7,7 +7,7 @@ using Microsoft.Win32;
namespace Giants.Launcher namespace Giants.Launcher
{ {
static class GameSettings public static class GameSettings
{ {
// Constants // Constants
private const string RegistryKey = @"HKEY_CURRENT_USER\Software\PlanetMoon\Giants"; private const string RegistryKey = @"HKEY_CURRENT_USER\Software\PlanetMoon\Giants";
@ -16,8 +16,8 @@ namespace Giants.Launcher
private static readonly Dictionary<string, object> Settings = new Dictionary<string, object>(); private static readonly Dictionary<string, object> Settings = new Dictionary<string, object>();
// List of renderers compatible with the user's system. // List of renderers compatible with the user's system.
public static List<RendererInterop.Capabilities> CompatibleRenderers; public static List<RendererInfo> CompatibleRenderers { get; set; } = new List<RendererInfo>();
public static T Get<T>(string settingName) public static T Get<T>(string settingName)
{ {
return (T)Get(settingName); return (T)Get(settingName);
@ -54,10 +54,10 @@ namespace Giants.Launcher
Settings[SettingKeys.NoAutoUpdate] = 0; Settings[SettingKeys.NoAutoUpdate] = 0;
// Get a list of renderers compatible with the user's system // Get a list of renderers compatible with the user's system
if (CompatibleRenderers == null) if (!CompatibleRenderers.Any())
{ {
CompatibleRenderers = RendererInterop.GetCompatibleRenderers(gamePath); CompatibleRenderers = RendererInterop.GetCompatibleRenderers(gamePath);
if (CompatibleRenderers.Count == 0) if (!CompatibleRenderers.Any())
{ {
MessageBox.Show( MessageBox.Show(
text: Resources.ErrorNoRenderers, text: Resources.ErrorNoRenderers,

View File

@ -77,7 +77,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="ApplicationNames.cs" /> <Compile Include="ApplicationNames.cs" />
<Compile Include="Native\Capabilities.cs" /> <Compile Include="Renderer\RendererInfo.cs" />
<Compile Include="Renderer\RenderInfoExtensions.cs" />
<Compile Include="SettingKeys.cs" /> <Compile Include="SettingKeys.cs" />
<Compile Include="Updater\ApplicationType.cs" /> <Compile Include="Updater\ApplicationType.cs" />
<Compile Include="VersionHelper.cs" /> <Compile Include="VersionHelper.cs" />

View File

@ -234,7 +234,7 @@ namespace Giants.Launcher
UpdateInfo info = (UpdateInfo)e.UserState; UpdateInfo info = (UpdateInfo)e.UserState;
this.txtProgress.Visible = true; 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);
} }
} }
} }

View File

@ -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; }
}
}
}

View File

@ -29,9 +29,6 @@ namespace Giants.Launcher
[DllImport("user32.dll")] [DllImport("user32.dll")]
public static extern bool EnumDisplaySettings( public static extern bool EnumDisplaySettings(
string deviceName, int modeNum, ref DEVMODE devMode); string deviceName, int modeNum, ref DEVMODE devMode);
const int ENUM_CURRENT_SETTINGS = -1;
const int ENUM_REGISTRY_SETTINGS = -2;
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public struct DEVMODE public struct DEVMODE

View File

@ -6,7 +6,7 @@ using System.Windows.Forms;
namespace Giants.Launcher namespace Giants.Launcher
{ {
partial class RendererInterop public class RendererInterop
{ {
#pragma warning disable 649 #pragma warning disable 649
public struct GFXCapabilityInfo public struct GFXCapabilityInfo
@ -46,11 +46,11 @@ namespace Giants.Launcher
} }
public static List<Capabilities> GetCompatibleRenderers(string gamePath) public static List<RendererInfo> GetCompatibleRenderers(string gamePath)
{ {
var dir = new DirectoryInfo( var dir = new DirectoryInfo(
Path.GetDirectoryName(gamePath)); Path.GetDirectoryName(gamePath));
var capabilities = new List<Capabilities>(); var capabilities = new List<RendererInfo>();
// Search current directory for compatible renderers: // Search current directory for compatible renderers:
foreach (FileInfo file in dir.GetFiles("gg_*.dll")) foreach (FileInfo file in dir.GetFiles("gg_*.dll"))
@ -62,7 +62,7 @@ namespace Giants.Launcher
string path = Path.Combine(file.DirectoryName, file.Name); string path = Path.Combine(file.DirectoryName, file.Name);
if (GetRendererCapabilities(path, ref interopCaps)) if (GetRendererCapabilities(path, ref interopCaps))
{ {
Capabilities caps = new Capabilities(path, ref interopCaps); RendererInfo caps = new RendererInfo(path, ref interopCaps);
capabilities.Add(caps); capabilities.Add(caps);
} }
} }

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Windows.Forms; using System.Windows.Forms;
namespace Giants.Launcher namespace Giants.Launcher
@ -26,10 +27,14 @@ namespace Giants.Launcher
private void SetOptions() private void SetOptions()
{ {
this.cmbRenderer.Items.Clear(); 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( RendererInfo renderer = GameSettings.CompatibleRenderers.Find(
r => StringComparer.OrdinalIgnoreCase.Compare(Path.GetFileName(r.FilePath), GameSettings.Get<string>("Renderer")) == 0); r => StringComparer.OrdinalIgnoreCase.Compare(Path.GetFileName(r.FilePath), GameSettings.Get<string>(SettingKeys.Renderer)) == 0);
if (renderer != null) if (renderer != null)
{ {
@ -41,22 +46,22 @@ namespace Giants.Launcher
this.cmbRenderer.SelectedItem = renderer; this.cmbRenderer.SelectedItem = renderer;
} }
var Resolutions = (List<ScreenResolution>)this.cmbResolution.DataSource; var resolutions = (List<ScreenResolution>)this.cmbResolution.DataSource;
this.cmbResolution.SelectedItem = Resolutions.Find(r => r.Width == (int)GameSettings.Get("VideoWidth") && r.Height == (int)GameSettings.Get("VideoHeight")); this.cmbResolution.SelectedItem = resolutions.Find(r => r.Width == GameSettings.Get<int>(SettingKeys.VideoWidth) && r.Height == GameSettings.Get<int>(SettingKeys.VideoHeight));
if (this.cmbResolution.SelectedItem == null) if (this.cmbResolution.SelectedItem == null)
this.cmbResolution.SelectedIndex = 0; this.cmbResolution.SelectedIndex = 0;
var AntialiasingOptions = (List<KeyValuePair<string, int>>)this.cmbAntialiasing.DataSource; var AntialiasingOptions = (List<KeyValuePair<string, int>>)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<int>(SettingKeys.Antialiasing));
if (this.cmbAntialiasing.SelectedItem == null) if (this.cmbAntialiasing.SelectedItem == null)
this.cmbAntialiasing.SelectedIndex = 0; this.cmbAntialiasing.SelectedIndex = 0;
var AnisotropyOptions = (List<KeyValuePair<string, int>>)this.cmbAnisotropy.DataSource; var AnisotropyOptions = (List<KeyValuePair<string, int>>)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<int>(SettingKeys.AnisotropicFiltering));
if (this.cmbAnisotropy.SelectedItem == null) if (this.cmbAnisotropy.SelectedItem == null)
this.cmbAnisotropy.SelectedIndex = 0; this.cmbAnisotropy.SelectedIndex = 0;
this.chkUpdates.Checked = ((int)GameSettings.Get("NoAutoUpdate") == 1 ? false : true); this.chkUpdates.Checked = GameSettings.Get<int>(SettingKeys.NoAutoUpdate) != 1;
} }
private void PopulateAntialiasing() private void PopulateAntialiasing()
@ -64,22 +69,22 @@ namespace Giants.Launcher
var antialiasingOptions = new List<KeyValuePair<string, int>>(); var antialiasingOptions = new List<KeyValuePair<string, int>>();
antialiasingOptions.Add(new KeyValuePair<string, int>(Resources.OptionNone, 0)); antialiasingOptions.Add(new KeyValuePair<string, int>(Resources.OptionNone, 0));
var renderer = (RendererInterop.Capabilities)this.cmbRenderer.SelectedItem; var renderer = (RendererInfo)this.cmbRenderer.SelectedItem;
if (renderer != null) if (renderer != null)
{ {
if (renderer.Flags.HasFlag(RendererInterop.Capabilities.RendererFlag.MSAA2x)) if (renderer.Flags.HasFlag(RendererInfo.RendererFlag.MSAA2x))
{ {
antialiasingOptions.Add(new KeyValuePair<string, int>(string.Format(Resources.OptionSamples, 2), 2)); antialiasingOptions.Add(new KeyValuePair<string, int>(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, int>(string.Format(Resources.OptionSamples, 4), 4)); antialiasingOptions.Add(new KeyValuePair<string, int>(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, int>(string.Format(Resources.OptionSamples, 8), 8)); antialiasingOptions.Add(new KeyValuePair<string, int>(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, int>(string.Format(Resources.OptionSamples, 16), 16)); antialiasingOptions.Add(new KeyValuePair<string, int>(string.Format(Resources.OptionSamples, 16), 16));
} }
@ -110,18 +115,17 @@ namespace Giants.Launcher
private void PopulateAnisotropy() private void PopulateAnisotropy()
{ {
List<KeyValuePair<string, int>> AnisotropyOptions = new List<KeyValuePair<string, int>>(); var anisotropyOptions = new List<KeyValuePair<string, int>>();
anisotropyOptions.Add(new KeyValuePair<string, int>(Resources.OptionNone, 0));
AnisotropyOptions.Add(new KeyValuePair<string, int>(Resources.OptionNone, 0)); var renderer = (RendererInfo)this.cmbRenderer.SelectedItem;
var renderer = (RendererInterop.Capabilities)this.cmbRenderer.SelectedItem;
if (renderer != null) if (renderer != null)
{ {
for (int i = 2; i <= renderer.MaxAnisotropy; i++) for (int i = 2; i <= renderer.MaxAnisotropy; i++)
{ {
if (!this.IsPowerOfTwo(i)) continue; if (!this.IsPowerOfTwo(i)) continue;
AnisotropyOptions.Add(new KeyValuePair<string,int>(string.Format(Resources.OptionSamples, i), i)); anisotropyOptions.Add(new KeyValuePair<string,int>(string.Format(Resources.OptionSamples, i), i));
} }
} }
@ -132,7 +136,7 @@ namespace Giants.Launcher
currentValue = (int)this.cmbAnisotropy.SelectedValue; currentValue = (int)this.cmbAnisotropy.SelectedValue;
} }
this.cmbAnisotropy.DataSource = AnisotropyOptions; this.cmbAnisotropy.DataSource = anisotropyOptions;
this.cmbAnisotropy.DisplayMember = "Key"; this.cmbAnisotropy.DisplayMember = "Key";
this.cmbAnisotropy.ValueMember = "Value"; this.cmbAnisotropy.ValueMember = "Value";
@ -168,10 +172,10 @@ namespace Giants.Launcher
this.PopulateAntialiasing(); this.PopulateAntialiasing();
this.PopulateAnisotropy(); this.PopulateAnisotropy();
bool windowed = ((int)GameSettings.Get("Windowed") == 1 ? true : false); bool windowed = GameSettings.Get<int>(SettingKeys.Windowed) == 1;
if (windowed) if (windowed)
{ {
bool borderless = (int)GameSettings.Get("BorderlessWindow") == 1 ? true : false; bool borderless = GameSettings.Get<int>(SettingKeys.BorderlessWindow) == 1;
if (borderless) if (borderless)
this.cmbMode.SelectedIndex = 2; this.cmbMode.SelectedIndex = 2;
else else
@ -180,55 +184,55 @@ namespace Giants.Launcher
else else
this.cmbMode.SelectedIndex = 0; 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.Checked = false;
this.chkVSync.Enabled = false; this.chkVSync.Enabled = false;
} }
else else
{ {
this.chkVSync.Checked = ((int)GameSettings.Get("VerticalSync") == 1 ? true : false); this.chkVSync.Checked = GameSettings.Get<int>(SettingKeys.VerticalSync) == 1;
this.chkVSync.Enabled = true; 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.Checked = false;
this.chkTripleBuffering.Enabled = false; this.chkTripleBuffering.Enabled = false;
} }
else else
{ {
this.chkTripleBuffering.Checked = ((int)GameSettings.Get("TripleBuffering") == 1 ? true : false); this.chkTripleBuffering.Checked = GameSettings.Get<int>(SettingKeys.TripleBuffering) == 1;
this.chkTripleBuffering.Enabled = true; this.chkTripleBuffering.Enabled = true;
} }
} }
private void btnOK_Click(object sender, EventArgs e) 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) if (renderer != null)
{ {
GameSettings.Modify("Renderer", renderer.FileName); GameSettings.Modify(SettingKeys.Renderer, renderer.FileName);
} }
var resolution = (ScreenResolution)this.cmbResolution.SelectedItem; var resolution = (ScreenResolution)this.cmbResolution.SelectedItem;
if (resolution != null) if (resolution != null)
{ {
GameSettings.Modify("VideoWidth", resolution.Width); GameSettings.Modify(SettingKeys.VideoWidth, resolution.Width);
GameSettings.Modify("VideoHeight", resolution.Height); GameSettings.Modify(SettingKeys.VideoHeight, resolution.Height);
} }
GameSettings.Modify("Antialiasing", this.cmbAntialiasing.SelectedValue); GameSettings.Modify(SettingKeys.Antialiasing, this.cmbAntialiasing.SelectedValue);
GameSettings.Modify("AnisotropicFiltering", this.cmbAnisotropy.SelectedValue); GameSettings.Modify(SettingKeys.AnisotropicFiltering, this.cmbAnisotropy.SelectedValue);
bool windowed = ((WindowType)this.cmbMode.SelectedIndex == WindowType.Windowed || (WindowType)this.cmbMode.SelectedIndex == WindowType.Borderless); bool windowed = (WindowType)this.cmbMode.SelectedIndex == WindowType.Windowed || (WindowType)this.cmbMode.SelectedIndex == WindowType.Borderless;
GameSettings.Modify("Windowed", (windowed == true ? 1 : 0)); GameSettings.Modify(SettingKeys.Windowed, windowed == true ? 1 : 0);
bool borderless = (WindowType)this.cmbMode.SelectedIndex == WindowType.Borderless; bool borderless = (WindowType)this.cmbMode.SelectedIndex == WindowType.Borderless;
GameSettings.Modify("BorderlessWindow", borderless == true ? 1 : 0); GameSettings.Modify(SettingKeys.BorderlessWindow, borderless == true ? 1 : 0);
GameSettings.Modify("VerticalSync", (this.chkVSync.Checked == true ? 1 : 0)); GameSettings.Modify(SettingKeys.VerticalSync, this.chkVSync.Checked == true ? 1 : 0);
GameSettings.Modify("TripleBuffering", (this.chkTripleBuffering.Checked == true ? 1 : 0)); GameSettings.Modify(SettingKeys.TripleBuffering, this.chkTripleBuffering.Checked == true ? 1 : 0);
GameSettings.Modify("NoAutoUpdate", (this.chkUpdates.Checked == false ? 1 : 0)); GameSettings.Modify(SettingKeys.NoAutoUpdate, this.chkUpdates.Checked == false ? 1 : 0);
GameSettings.Save(); GameSettings.Save();

View File

@ -37,7 +37,6 @@ namespace Giants.Launcher
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
Form form; Form form;
try try
{ {

View File

@ -0,0 +1,28 @@
namespace Giants.Launcher
{
using System.Collections.Generic;
using System.IO;
using System.Linq;
public static class RenderInfoExtensions
{
/// <summary>
/// Disambiguates renderers with the same name by adding the file name.
/// </summary>
public static IList<RendererInfo> Disambiguate(this IList<RendererInfo> 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;
}
}
}

View File

@ -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<string>.Default.GetHashCode(this.FilePath);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.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<string>.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; }
}
}