-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Settings.cs
116 lines (101 loc) · 3.4 KB
/
Settings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Xml;
namespace OverlayControl
{
/// <summary>
/// Singleton class managing the application's user settings.
/// </summary>
public sealed class Settings
{
public static Settings Instance { get { return _lazy.Value; } }
#region Private variables
private static readonly Lazy<Settings> _lazy =
new Lazy<Settings>(() => new Settings());
private readonly XmlDocument _file = new XmlDocument();
private string _fileLocation;
private bool _resetScores = true;
private bool _catchScoresUp = true;
#endregion
#region Properties
public bool ResetScores
{
get => _resetScores;
set
{
_file.SelectSingleNode("/Settings/ResetScores").InnerText = value.ToString();
_file.Save(_fileLocation);
_resetScores = value;
}
}
public bool CatchScoresUp
{
get => _catchScoresUp;
set
{
_file.SelectSingleNode("/Settings/CatchScoresUp").InnerText = value.ToString();
_file.Save(_fileLocation);
_catchScoresUp = value;
}
}
/// <summary>
/// Boolean representing whether the settings have been fully loaded into the application. Only to be set to true within the main window.
/// </summary>
public bool AreLoaded { get; set; }
#endregion
/// <summary>
/// Private constructor for the Settings singleton class.
/// </summary>
private Settings()
{
AreLoaded = false;
}
#region Methods
/// <summary>
/// Loads the application's settings into memory by reading them from the XML configuration file.
/// </summary>
/// <param name="filename">The configuration file's location.</param>
/// <returns>Whether every setting was loaded properly.</returns>
public bool Load(string filename)
{
try
{
bool success = true;
// Load the file into memory
_file.Load(filename);
_fileLocation = filename;
// Load each setting from the file
if (!bool.TryParse(_file.SelectSingleNode("/Settings/ResetScores").InnerText, out _resetScores))
success = false;
if (bool.TryParse(_file.SelectSingleNode("/Settings/CatchScoresUp").InnerText, out _catchScoresUp))
success = false;
return success;
}
catch
{
// There was an error; return false
return false;
}
}
/// <summary>
/// Saves the application's settings into a file named config.txt.
/// </summary>
/// <returns>Whether the operation was successful or not.</returns>
public bool Save()
{
// Check if file is loaded first
if (AreLoaded)
try
{
_file.Save(_fileLocation);
return true;
}
catch
{
return false;
}
// If no file is loaded, return false
return false;
}
#endregion
}
}