quakeball/Assets/Scripts/Options.cs

62 lines
2.3 KiB
C#
Raw Normal View History

2020-08-06 23:12:26 +02:00
using UnityEngine;
using System.IO;
using System;
using System.Xml.Serialization;
2020-08-02 02:52:38 +02:00
namespace NeonTea.Quakeball {
/// <summary>Container for user-selected options.</summary>
2020-08-06 23:12:26 +02:00
[XmlRootAttribute("Options")]
2020-08-02 02:52:38 +02:00
public class OptionsData {
2020-08-06 23:12:26 +02:00
// DO NOT REMOVE OR CHANGE EXISTING FIELDS AFTER RELEASE TO AVOID BREAKING EXISTING CONFIGURATIONS
// New fields are fine though, the defaults will be used.
2020-08-02 02:52:38 +02:00
public bool InvertVerticalLook = false;
2020-08-06 23:12:26 +02:00
public float LookSensitivity = 0.1f;
2020-08-02 02:52:38 +02:00
}
2020-08-06 23:12:26 +02:00
/// <summary>A collection of functions to access user-specific configuration.</summary>
public static class Options {
private static string OptionsDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Quakeball");
private static string OptionsPath = Path.Combine(OptionsDirectory, "Config.xml");
private static OptionsData Singleton = Load();
2020-08-02 02:52:38 +02:00
public static OptionsData Get() {
return Singleton;
}
2020-08-06 23:12:26 +02:00
2020-08-07 01:42:59 +02:00
public static void Set(OptionsData options) {
Singleton = options;
}
public static string GetDirectory() {
return OptionsDirectory;
}
2020-08-06 23:12:26 +02:00
public static void Save(OptionsData options) {
XmlSerializer Serializer = new XmlSerializer(typeof(OptionsData));
Directory.CreateDirectory(OptionsDirectory);
FileStream OptionsFile = File.Create(OptionsPath);
Serializer.Serialize(OptionsFile, options);
OptionsFile.Close();
}
2020-08-07 01:42:59 +02:00
public static OptionsData Load(bool nullOnNotFound = false) {
2020-08-06 23:12:26 +02:00
try {
XmlSerializer Serializer = new XmlSerializer(typeof(OptionsData));
FileStream OptionsFile = File.OpenRead(OptionsPath);
OptionsData Options = (OptionsData)Serializer.Deserialize(OptionsFile);
OptionsFile.Close();
return Options;
2020-08-07 01:42:59 +02:00
} catch (FileNotFoundException) {
if (nullOnNotFound) {
return null;
}
} catch (DirectoryNotFoundException) {
if (nullOnNotFound) {
return null;
}
2020-08-06 23:12:26 +02:00
}
2020-08-07 00:43:30 +02:00
return new OptionsData();
2020-08-06 23:12:26 +02:00
}
2020-08-02 02:52:38 +02:00
}
}