2020-08-10 06:58:51 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-08-10 09:22:33 +02:00
|
|
|
|
using System.IO;
|
2020-08-10 06:58:51 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using Microsoft.Win32;
|
|
|
|
|
|
|
|
|
|
namespace Giants.Launcher
|
|
|
|
|
{
|
2020-08-13 08:37:45 +02:00
|
|
|
|
public static class GameSettings
|
2020-08-13 06:58:53 +02:00
|
|
|
|
{
|
|
|
|
|
// Constants
|
2020-08-13 08:02:36 +02:00
|
|
|
|
private const string RegistryKey = @"HKEY_CURRENT_USER\Software\PlanetMoon\Giants";
|
2020-11-04 04:49:04 +01:00
|
|
|
|
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.
|
2020-08-13 08:37:45 +02:00
|
|
|
|
public static List<RendererInfo> CompatibleRenderers { get; set; } = new List<RendererInfo>();
|
|
|
|
|
|
2020-08-13 08:02:36 +02:00
|
|
|
|
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 08:02:36 +02:00
|
|
|
|
{
|
2020-08-13 06:58:53 +02:00
|
|
|
|
return Settings[settingName];
|
2020-08-13 08:02:36 +02:00
|
|
|
|
}
|
2020-08-13 06:58:53 +02:00
|
|
|
|
else
|
2020-08-13 08:02:36 +02:00
|
|
|
|
{
|
2020-08-13 06:58:53 +02:00
|
|
|
|
return 0;
|
2020-08-13 08:02:36 +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:
|
2020-09-14 09:11:32 +02:00
|
|
|
|
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
|
2020-08-13 08:37:45 +02:00
|
|
|
|
if (!CompatibleRenderers.Any())
|
2020-08-13 06:58:53 +02:00
|
|
|
|
{
|
|
|
|
|
CompatibleRenderers = RendererInterop.GetCompatibleRenderers(gamePath);
|
2020-08-13 08:37:45 +02:00
|
|
|
|
if (!CompatibleRenderers.Any())
|
2020-08-13 06:58:53 +02:00
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(
|
2020-08-13 08:02:36 +02:00
|
|
|
|
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
|
2020-08-13 08:02:36 +02:00
|
|
|
|
if (CompatibleRenderers.Any())
|
|
|
|
|
{
|
2020-09-14 09:11:32 +02:00
|
|
|
|
Settings[RegistryKeys.Renderer] = Path.GetFileName(CompatibleRenderers.Max().FilePath);
|
2020-08-13 08:02:36 +02:00
|
|
|
|
}
|
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:
|
2020-09-14 09:11:32 +02:00
|
|
|
|
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
|
|
|
|
|
2020-09-14 09:11:32 +02:00
|
|
|
|
if ((int)Registry.GetValue(RegistryKey, RegistryKeys.GameOptionsVersion, 0) == OptionsVersion)
|
2020-08-13 06:58:53 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-09-14 09:11:32 +02:00
|
|
|
|
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)
|
|
|
|
|
{
|
2020-08-13 08:02:36 +02:00
|
|
|
|
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
|
|
|
|
|
{
|
2020-09-14 09:11:32 +02:00
|
|
|
|
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)
|
|
|
|
|
{
|
2020-08-13 08:02:36 +02:00
|
|
|
|
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
|
|
|
|
}
|