1
0
mirror of https://github.com/ncblakely/GiantsTools synced 2024-11-16 19:55:36 +01:00
GiantsTools/Giants.Launcher/GameSettings.cs

136 lines
6.7 KiB
C#
Raw Normal View History

2020-08-10 06:58:51 +02:00
using System;
using System.Collections.Generic;
using System.IO;
2020-08-10 06:58:51 +02:00
using System.Linq;
using System.Windows.Forms;
using Microsoft.Win32;
namespace Giants.Launcher
{
public static class GameSettings
2020-08-13 06:58:53 +02:00
{
// Constants
private const string RegistryKey = @"HKEY_CURRENT_USER\Software\PlanetMoon\Giants";
private const int OptionsVersion = 4;
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
private static readonly Dictionary<string, object> Settings = new Dictionary<string, object>();
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
// List of renderers compatible with the user's system.
public static List<RendererInfo> CompatibleRenderers { get; set; } = new List<RendererInfo>();
public static T Get<T>(string settingName)
{
return (T)Get(settingName);
}
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
public static object Get(string settingName)
{
if (Settings.ContainsKey(settingName))
{
2020-08-13 06:58:53 +02:00
return Settings[settingName];
}
2020-08-13 06:58:53 +02:00
else
{
2020-08-13 06:58:53 +02:00
return 0;
}
2020-08-13 06:58:53 +02:00
}
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
public static void Modify(string settingName, object settingValue)
{
Settings[settingName] = settingValue;
}
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
public static void SetDefaults(string gamePath)
{
// Set default settings:
Settings[RegistryKeys.Renderer] = "gg_dx9r.dll";
Settings[RegistryKeys.Antialiasing] = 0;
Settings[RegistryKeys.AnisotropicFiltering] = 0;
Settings[RegistryKeys.Windowed] = 1;
Settings[RegistryKeys.VerticalSync] = 1;
Settings[RegistryKeys.TripleBuffering] = 1;
Settings[RegistryKeys.NoAutoUpdate] = 0;
2020-08-10 06:58:51 +02:00
// Set the current desktop resolution
Settings[RegistryKeys.VideoWidth] = Screen.PrimaryScreen.Bounds.Width;
Settings[RegistryKeys.VideoHeight] = Screen.PrimaryScreen.Bounds.Height;
2020-08-13 06:58:53 +02:00
// Get a list of renderers compatible with the user's system
if (!CompatibleRenderers.Any())
2020-08-13 06:58:53 +02:00
{
CompatibleRenderers = RendererInterop.GetCompatibleRenderers(gamePath);
if (!CompatibleRenderers.Any())
2020-08-13 06:58:53 +02:00
{
MessageBox.Show(
text: Resources.ErrorNoRenderers,
caption: Resources.Error,
buttons: MessageBoxButtons.OK,
icon: MessageBoxIcon.Error);
return;
2020-08-13 06:58:53 +02:00
}
}
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
// Select the highest priority renderer
var bestRenderer = CompatibleRenderers.Max();
Settings[RegistryKeys.Renderer] = Path.GetFileName(bestRenderer.FilePath);
Settings[RegistryKeys.AnisotropicFiltering] = bestRenderer.MaxAnisotropy;
Settings[RegistryKeys.Antialiasing] = bestRenderer.MaxAntialiasing;
2020-08-13 06:58:53 +02:00
}
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
public static void Load(string gamePath)
{
SetDefaults(gamePath);
2020-08-10 06:58:51 +02:00
if ((int)Registry.GetValue(RegistryKey, RegistryKeys.GameOptionsVersion, 0) == OptionsVersion)
2020-08-13 06:58:53 +02:00
{
try
{
Settings[RegistryKeys.Renderer] = RegistryExtensions.GetValue(RegistryKey, RegistryKeys.Renderer, RegistryKeys.Renderer, typeof(string));
Settings[RegistryKeys.Antialiasing] = RegistryExtensions.GetValue(RegistryKey, RegistryKeys.Antialiasing, Settings[RegistryKeys.Antialiasing], typeof(int));
Settings[RegistryKeys.AnisotropicFiltering] = RegistryExtensions.GetValue(RegistryKey, RegistryKeys.AnisotropicFiltering, Settings[RegistryKeys.AnisotropicFiltering], typeof(int));
Settings[RegistryKeys.VideoWidth] = RegistryExtensions.GetValue(RegistryKey, RegistryKeys.VideoWidth, Settings[RegistryKeys.VideoWidth], typeof(int));
Settings[RegistryKeys.VideoHeight] = RegistryExtensions.GetValue(RegistryKey, RegistryKeys.VideoHeight, Settings[RegistryKeys.VideoHeight], typeof(int));
Settings[RegistryKeys.Windowed] = RegistryExtensions.GetValue(RegistryKey, RegistryKeys.Windowed, Settings[RegistryKeys.Windowed], typeof(int));
Settings[RegistryKeys.VerticalSync] = RegistryExtensions.GetValue(RegistryKey, RegistryKeys.VerticalSync, Settings[RegistryKeys.VerticalSync], typeof(int));
Settings[RegistryKeys.TripleBuffering] = RegistryExtensions.GetValue(RegistryKey, RegistryKeys.TripleBuffering, Settings[RegistryKeys.TripleBuffering], typeof(int));
Settings[RegistryKeys.NoAutoUpdate] = RegistryExtensions.GetValue(RegistryKey, RegistryKeys.NoAutoUpdate, Settings[RegistryKeys.NoAutoUpdate], typeof(int));
2020-08-13 06:58:53 +02:00
}
catch (Exception ex)
{
MessageBox.Show(
text: string.Format(Resources.ErrorSettingsLoad, ex.Message),
caption: Resources.Error,
buttons: MessageBoxButtons.OK,
icon: MessageBoxIcon.Error);
2020-08-13 06:58:53 +02:00
}
}
}
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
public static void Save()
{
try
{
Registry.SetValue(RegistryKey, RegistryKeys.GameOptionsVersion, OptionsVersion, RegistryValueKind.DWord);
Registry.SetValue(RegistryKey, RegistryKeys.Renderer, Settings[RegistryKeys.Renderer], RegistryValueKind.String);
Registry.SetValue(RegistryKey, RegistryKeys.Antialiasing, Settings[RegistryKeys.Antialiasing], RegistryValueKind.DWord);
Registry.SetValue(RegistryKey, RegistryKeys.AnisotropicFiltering, Settings[RegistryKeys.AnisotropicFiltering], RegistryValueKind.DWord);
Registry.SetValue(RegistryKey, RegistryKeys.VideoWidth, Settings[RegistryKeys.VideoWidth], RegistryValueKind.DWord);
Registry.SetValue(RegistryKey, RegistryKeys.VideoHeight, Settings[RegistryKeys.VideoHeight], RegistryValueKind.DWord);
Registry.SetValue(RegistryKey, RegistryKeys.Windowed, Settings[RegistryKeys.Windowed], RegistryValueKind.DWord);
Registry.SetValue(RegistryKey, RegistryKeys.VerticalSync, Settings[RegistryKeys.VerticalSync], RegistryValueKind.DWord);
Registry.SetValue(RegistryKey, RegistryKeys.TripleBuffering, Settings[RegistryKeys.TripleBuffering], RegistryValueKind.DWord);
Registry.SetValue(RegistryKey, RegistryKeys.NoAutoUpdate, Settings[RegistryKeys.NoAutoUpdate], RegistryValueKind.DWord);
2020-08-13 06:58:53 +02:00
}
catch (Exception ex)
{
MessageBox.Show(
text: string.Format(Resources.ErrorSettingsSave, ex.Message),
caption: Resources.Error,
buttons: MessageBoxButtons.OK,
icon: MessageBoxIcon.Error);
2020-08-13 06:58:53 +02:00
}
}
}
2020-08-10 06:58:51 +02:00
}