1
0
mirror of https://github.com/ncblakely/GiantsTools synced 2024-11-04 22:35:37 +01:00
GiantsTools/Giants.Launcher/GameSettings.cs

123 lines
6.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
{
static class GameSettings
2020-08-13 06:58:53 +02:00
{
// Constants
private const string REGISTRY_KEY = @"HKEY_CURRENT_USER\Software\PlanetMoon\Giants";
private const int OPTIONS_VERSION = 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.
static public List<RendererInterop.Capabilities> CompatibleRenderers;
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))
return Settings[settingName];
else
return 0;
2020-08-10 06:58:51 +02:00
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["Renderer"] = "gg_dx7r.dll";
Settings["Antialiasing"] = 0;
Settings["AnisotropicFiltering"] = 0;
Settings["VideoWidth"] = 640;
Settings["VideoHeight"] = 480;
Settings["VideoDepth"] = 32;
Settings["Windowed"] = 0;
Settings["BorderlessWindow"] = 0;
Settings["VerticalSync"] = 1;
Settings["TripleBuffering"] = 1;
Settings["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(
"Could not locate any renderers compatible with your system. " +
"The most compatible renderer has been selected, but you may experience difficulty running the game.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
// Select the highest priority renderer
if (CompatibleRenderers.Count > 0)
Settings["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["VideoWidth"] = Screen.PrimaryScreen.Bounds.Width;
Settings["VideoHeight"] = Screen.PrimaryScreen.Bounds.Height;
}
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
2020-08-13 06:58:53 +02:00
if ((int)Registry.GetValue(REGISTRY_KEY, "GameOptionsVersion", 0) == OPTIONS_VERSION)
{
try
{
Settings["Renderer"] = RegistryExtensions.GetValue(REGISTRY_KEY, "Renderer", Settings["Renderer"], typeof(string));
Settings["Antialiasing"] = RegistryExtensions.GetValue(REGISTRY_KEY, "Antialiasing", Settings["Antialiasing"], typeof(int));
Settings["AnisotropicFiltering"] = RegistryExtensions.GetValue(REGISTRY_KEY, "AnisotropicFiltering", Settings["AnisotropicFiltering"], typeof(int));
Settings["VideoWidth"] = RegistryExtensions.GetValue(REGISTRY_KEY, "VideoWidth", Settings["VideoWidth"], typeof(int));
Settings["VideoHeight"] = RegistryExtensions.GetValue(REGISTRY_KEY, "VideoHeight", Settings["VideoHeight"], typeof(int));
Settings["VideoDepth"] = RegistryExtensions.GetValue(REGISTRY_KEY, "VideoDepth", Settings["VideoDepth"], typeof(int));
Settings["Windowed"] = RegistryExtensions.GetValue(REGISTRY_KEY, "Windowed", Settings["Windowed"], typeof(int));
Settings["BorderlessWindow"] = RegistryExtensions.GetValue(REGISTRY_KEY, "BorderlessWindow", Settings["BorderlessWindow"], typeof(int));
Settings["VerticalSync"] = RegistryExtensions.GetValue(REGISTRY_KEY, "VerticalSync", Settings["VerticalSync"], typeof(int));
Settings["TripleBuffering"] = RegistryExtensions.GetValue(REGISTRY_KEY, "TripleBuffering", Settings["TripleBuffering"], typeof(int));
Settings["NoAutoUpdate"] = RegistryExtensions.GetValue(REGISTRY_KEY, "NoAutoUpdate", Settings["NoAutoUpdate"], typeof(int));
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Could not read game settings from registry!\n\nReason: {0}", ex.Message));
}
}
}
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
public static void Save()
{
try
{
Registry.SetValue(REGISTRY_KEY, "GameOptionsVersion", 3, RegistryValueKind.DWord);
Registry.SetValue(REGISTRY_KEY, "Renderer", Settings["Renderer"], RegistryValueKind.String);
Registry.SetValue(REGISTRY_KEY, "Antialiasing", Settings["Antialiasing"], RegistryValueKind.DWord);
Registry.SetValue(REGISTRY_KEY, "AnisotropicFiltering", Settings["AnisotropicFiltering"], RegistryValueKind.DWord);
Registry.SetValue(REGISTRY_KEY, "VideoWidth", Settings["VideoWidth"], RegistryValueKind.DWord);
Registry.SetValue(REGISTRY_KEY, "VideoHeight", Settings["VideoHeight"], RegistryValueKind.DWord);
Registry.SetValue(REGISTRY_KEY, "VideoDepth", Settings["VideoDepth"], RegistryValueKind.DWord);
Registry.SetValue(REGISTRY_KEY, "Windowed", Settings["Windowed"], RegistryValueKind.DWord);
Registry.SetValue(REGISTRY_KEY, "BorderlessWindow", Settings["BorderlessWindow"], RegistryValueKind.DWord);
Registry.SetValue(REGISTRY_KEY, "VerticalSync", Settings["VerticalSync"], RegistryValueKind.DWord);
Registry.SetValue(REGISTRY_KEY, "TripleBuffering", Settings["TripleBuffering"], RegistryValueKind.DWord);
Registry.SetValue(REGISTRY_KEY, "NoAutoUpdate", Settings["NoAutoUpdate"], RegistryValueKind.DWord);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Could not save game settings to registry!\n\nReason: {0}", ex.Message));
}
}
}
2020-08-10 06:58:51 +02:00
}