1
0
mirror of https://github.com/ncblakely/GiantsTools synced 2024-06-29 01:01:45 +02:00
GiantsTools/Giants.Launcher/GameSettings.cs

140 lines
7.2 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
{
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 = 3;
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<RendererInterop.Capabilities> CompatibleRenderers;
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[SettingKeys.Renderer] = "gg_dx7r.dll";
Settings[SettingKeys.Antialiasing] = 0;
Settings[SettingKeys.AnisotropicFiltering] = 0;
Settings[SettingKeys.VideoDepth] = 32;
Settings[SettingKeys.Windowed] = 0;
Settings[SettingKeys.BorderlessWindow] = 0;
Settings[SettingKeys.VerticalSync] = 1;
Settings[SettingKeys.TripleBuffering] = 1;
Settings[SettingKeys.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 == null)
{
CompatibleRenderers = RendererInterop.GetCompatibleRenderers(gamePath);
if (CompatibleRenderers.Count == 0)
{
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[SettingKeys.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[SettingKeys.VideoWidth] = Screen.PrimaryScreen.Bounds.Width;
Settings[SettingKeys.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, SettingKeys.GameOptionsVersion, 0) == OptionsVersion)
2020-08-13 06:58:53 +02:00
{
try
{
Settings[SettingKeys.Renderer] = RegistryExtensions.GetValue(RegistryKey, SettingKeys.Renderer, SettingKeys.Renderer, typeof(string));
Settings[SettingKeys.Antialiasing] = RegistryExtensions.GetValue(RegistryKey, SettingKeys.Antialiasing, Settings[SettingKeys.Antialiasing], typeof(int));
Settings[SettingKeys.AnisotropicFiltering] = RegistryExtensions.GetValue(RegistryKey, SettingKeys.AnisotropicFiltering, Settings[SettingKeys.AnisotropicFiltering], typeof(int));
Settings[SettingKeys.VideoWidth] = RegistryExtensions.GetValue(RegistryKey, SettingKeys.VideoWidth, Settings[SettingKeys.VideoWidth], typeof(int));
Settings[SettingKeys.VideoHeight] = RegistryExtensions.GetValue(RegistryKey, SettingKeys.VideoHeight, Settings[SettingKeys.VideoHeight], typeof(int));
Settings[SettingKeys.VideoDepth] = RegistryExtensions.GetValue(RegistryKey, SettingKeys.VideoDepth, Settings[SettingKeys.VideoDepth], typeof(int));
Settings[SettingKeys.Windowed] = RegistryExtensions.GetValue(RegistryKey, SettingKeys.Windowed, Settings[SettingKeys.Windowed], typeof(int));
Settings[SettingKeys.BorderlessWindow] = RegistryExtensions.GetValue(RegistryKey, SettingKeys.BorderlessWindow, Settings[SettingKeys.BorderlessWindow], typeof(int));
Settings[SettingKeys.VerticalSync] = RegistryExtensions.GetValue(RegistryKey, SettingKeys.VerticalSync, Settings[SettingKeys.VerticalSync], typeof(int));
Settings[SettingKeys.TripleBuffering] = RegistryExtensions.GetValue(RegistryKey, SettingKeys.TripleBuffering, Settings[SettingKeys.TripleBuffering], typeof(int));
Settings[SettingKeys.NoAutoUpdate] = RegistryExtensions.GetValue(RegistryKey, SettingKeys.NoAutoUpdate, Settings[SettingKeys.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, SettingKeys.GameOptionsVersion, OptionsVersion, RegistryValueKind.DWord);
Registry.SetValue(RegistryKey, SettingKeys.Renderer, Settings[SettingKeys.Renderer], RegistryValueKind.String);
Registry.SetValue(RegistryKey, SettingKeys.Antialiasing, Settings[SettingKeys.Antialiasing], RegistryValueKind.DWord);
Registry.SetValue(RegistryKey, SettingKeys.AnisotropicFiltering, Settings[SettingKeys.AnisotropicFiltering], RegistryValueKind.DWord);
Registry.SetValue(RegistryKey, SettingKeys.VideoWidth, Settings[SettingKeys.VideoWidth], RegistryValueKind.DWord);
Registry.SetValue(RegistryKey, SettingKeys.VideoHeight, Settings[SettingKeys.VideoHeight], RegistryValueKind.DWord);
Registry.SetValue(RegistryKey, SettingKeys.VideoDepth, Settings[SettingKeys.VideoDepth], RegistryValueKind.DWord);
Registry.SetValue(RegistryKey, SettingKeys.Windowed, Settings[SettingKeys.Windowed], RegistryValueKind.DWord);
Registry.SetValue(RegistryKey, SettingKeys.BorderlessWindow, Settings[SettingKeys.BorderlessWindow], RegistryValueKind.DWord);
Registry.SetValue(RegistryKey, SettingKeys.VerticalSync, Settings[SettingKeys.VerticalSync], RegistryValueKind.DWord);
Registry.SetValue(RegistryKey, SettingKeys.TripleBuffering, Settings[SettingKeys.TripleBuffering], RegistryValueKind.DWord);
Registry.SetValue(RegistryKey, SettingKeys.NoAutoUpdate, Settings[SettingKeys.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
}