1
0
mirror of https://github.com/ncblakely/GiantsTools synced 2024-12-05 02:53:08 +01:00
GiantsTools/Giants.Launcher/GameSettings.cs

140 lines
7.3 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_dx7r.dll";
Settings[RegistryKeys.Antialiasing] = 0;
Settings[RegistryKeys.AnisotropicFiltering] = 0;
Settings[RegistryKeys.VideoDepth] = 32;
Settings[RegistryKeys.Windowed] = 0;
Settings[RegistryKeys.BorderlessWindow] = 0;
Settings[RegistryKeys.VerticalSync] = 1;
Settings[RegistryKeys.TripleBuffering] = 1;
Settings[RegistryKeys.NoAutoUpdate] = 0;
2020-08-10 06:58:51 +02:00
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);
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
if (CompatibleRenderers.Any())
{
Settings[RegistryKeys.Renderer] = Path.GetFileName(CompatibleRenderers.Max().FilePath);
}
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
// Set the current desktop resolution, leaving bit depth at the default 32:
Settings[RegistryKeys.VideoWidth] = Screen.PrimaryScreen.Bounds.Width;
Settings[RegistryKeys.VideoHeight] = Screen.PrimaryScreen.Bounds.Height;
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.VideoDepth] = RegistryExtensions.GetValue(RegistryKey, RegistryKeys.VideoDepth, Settings[RegistryKeys.VideoDepth], typeof(int));
Settings[RegistryKeys.Windowed] = RegistryExtensions.GetValue(RegistryKey, RegistryKeys.Windowed, Settings[RegistryKeys.Windowed], typeof(int));
Settings[RegistryKeys.BorderlessWindow] = RegistryExtensions.GetValue(RegistryKey, RegistryKeys.BorderlessWindow, Settings[RegistryKeys.BorderlessWindow], 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.VideoDepth, Settings[RegistryKeys.VideoDepth], RegistryValueKind.DWord);
Registry.SetValue(RegistryKey, RegistryKeys.Windowed, Settings[RegistryKeys.Windowed], RegistryValueKind.DWord);
Registry.SetValue(RegistryKey, RegistryKeys.BorderlessWindow, Settings[RegistryKeys.BorderlessWindow], 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
}