using System;
using System.IO;
using System.Windows.Forms;
using System.Xml;
namespace TeslaConsole;
///
/// Machine-level console settings that belong to no single game — the Settings
/// menu toggles that used to live only in memory and reset on every restart.
/// Stored as XML in %ProgramData%\Tesla Console\console.settings, next to
/// RPDefaults.rpd / BTDefaults.btd / local.siteconfig.
///
/// The values themselves stay where they are used (PlasmaBitmaps owns
/// EnableCustomBitmaps); this class only moves them to and from disk, so the
/// original decompiled classes keep their shape. Load() is called once from
/// Program.Main and never from a static initializer: the differential test suite
/// drives PlasmaBitmaps directly, and must keep seeing the original defaults
/// rather than whatever this operator happens to have saved.
///
internal static class ConsoleSettings
{
private static readonly string sSettingsFilePath = Path.Combine(Program.GetCommonAppDataDirectory(), "console.settings");
///
/// Applies the saved settings. A missing file is the normal first-run case;
/// a corrupt one is ignored and rewritten by the next Save(), because losing
/// a menu toggle must never stop the console from starting.
///
public static void Load()
{
try
{
if (!File.Exists(sSettingsFilePath))
{
return;
}
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(sSettingsFilePath);
foreach (XmlNode childNode in xmlDocument.DocumentElement.ChildNodes)
{
switch (childNode.Name)
{
case "EnableCustomBitmaps":
{
if (bool.TryParse(childNode.InnerText, out var result))
{
PlasmaBitmaps.EnableCustomBitmaps = result;
}
break;
}
}
}
}
catch (Exception)
{
}
}
public static void Save()
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.AppendChild(xmlDocument.CreateElement("ConsoleSettings"));
xmlDocument.DocumentElement.AppendChild(xmlDocument.CreateElement("EnableCustomBitmaps")).InnerText = PlasmaBitmaps.EnableCustomBitmaps.ToString();
string directoryName = Path.GetDirectoryName(sSettingsFilePath);
if (!Directory.Exists(directoryName))
{
Directory.CreateDirectory(directoryName);
}
xmlDocument.Save(sSettingsFilePath);
}
catch (Exception)
{
MessageBox.Show("The console settings file could not be saved. This setting will only be remembered until the application is closed.", "Error Saving Console Settings!", MessageBoxButtons.OK);
}
}
}