-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Storing user preferences to XML and loading them on startup
- Loading branch information
1 parent
6945e53
commit 9094544
Showing
6 changed files
with
153 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Windows.Forms; | ||
|
||
namespace QuantumOfSolace | ||
{ | ||
public class UserPreferences | ||
{ | ||
private const string CONFIG_FILE = "QuantumOfSolaceTool.xml"; | ||
|
||
public bool FullScreenMode { get; set; } | ||
public bool NoBalancingMiniGame { get; set; } | ||
|
||
public float DesiredFOV { get; set; } | ||
public bool DesiredFOVHackEnabled { get; set; } | ||
|
||
public int DesiredFPS { get; set; } | ||
public bool DesiredFPSHackEnabled { get; set; } | ||
|
||
public Keys DesiredFPSToggleKey { get; set; } | ||
|
||
|
||
public UserPreferences() | ||
{ | ||
FullScreenMode = false; | ||
NoBalancingMiniGame = false; | ||
DesiredFOV = 80; | ||
DesiredFOVHackEnabled = false; | ||
DesiredFPS = 60; | ||
DesiredFPSHackEnabled = false; | ||
DesiredFPSToggleKey = Keys.F5; | ||
} | ||
|
||
public static UserPreferences Load() | ||
{ | ||
if (File.Exists(CONFIG_FILE)) | ||
{ | ||
var readFile = XMLSerialization.ReadFromXMLFile<UserPreferences>(CONFIG_FILE); | ||
return readFile; | ||
} | ||
else | ||
return new UserPreferences(); | ||
} | ||
|
||
public void Save() | ||
{ | ||
XMLSerialization.SaveObjectToXML<UserPreferences>(this, CONFIG_FILE); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.IO; | ||
using System.Xml.Serialization; | ||
|
||
namespace QuantumOfSolace | ||
{ | ||
public class XMLSerialization | ||
{ | ||
public static T ReadFromXMLFile<T>(string filePath) where T : new() | ||
{ | ||
StreamReader reader = null; | ||
try | ||
{ | ||
if (!File.Exists(filePath)) | ||
return new T(); | ||
|
||
XmlSerializer serializer = new XmlSerializer(typeof(T)); | ||
reader = new StreamReader(filePath); | ||
return (T)serializer.Deserialize(reader); | ||
} | ||
finally | ||
{ | ||
if (reader != null) | ||
reader.Close(); | ||
} | ||
} | ||
|
||
public static void SaveObjectToXML<T>(T objectToStore, string filePath) where T : new() | ||
{ | ||
if (!Directory.Exists(Directory.GetParent(filePath).FullName)) | ||
Directory.CreateDirectory(Directory.GetParent(filePath).FullName); | ||
|
||
StreamWriter writter = null; | ||
|
||
try | ||
{ | ||
XmlSerializer serializer = new XmlSerializer(typeof(T)); | ||
writter = new StreamWriter(filePath); | ||
serializer.Serialize(writter, objectToStore); | ||
} | ||
catch (Exception e) | ||
{ | ||
throw e; | ||
} | ||
finally | ||
{ | ||
if (writter != null) | ||
writter.Close(); | ||
} | ||
} | ||
} | ||
} |