-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlutils.cs
101 lines (92 loc) · 3.21 KB
/
xmlutils.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
// Originally written by algernon for Find It 2.
using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections.Generic;
using UnityEngine;
namespace Propitize
{
/// <summary>
/// XML serialization/deserialization utilities class.
/// </summary>
internal static class XMLUtils
{
internal const string SettingsFileName = "PropitizedTrees.xml";
/// <summary>
/// Load settings from XML file.
/// </summary>
internal static void LoadSettings()
{
try
{
// Check to see if configuration file exists.
if (File.Exists(SettingsFileName))
{
// Read it.
using (StreamReader reader = new StreamReader(SettingsFileName))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(XMLSettingsFile));
if (!(xmlSerializer.Deserialize(reader) is XMLSettingsFile xmlSettingsFile))
{
Debug.Log("Propitize: Couldn't deserialize settings file");
}
}
}
else
{
Debug.Log("Propitize: No settings file found");
}
}
catch (Exception e)
{
Debug.Log("Propitize: Exception reading XML settings file");
Debug.LogException(e);
}
}
/// <summary>
/// Save settings to XML file.
/// </summary>
internal static void SaveSettings()
{
try
{
// Pretty straightforward. Serialisation is within GBRSettingsFile class.
using (StreamWriter writer = new StreamWriter(SettingsFileName))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(XMLSettingsFile));
xmlSerializer.Serialize(writer, new XMLSettingsFile());
}
}
catch (Exception e)
{
Debug.Log("Propitize: exception saving XML settings file");
Debug.LogException(e);
}
}
}
/// <summary>
/// Class to hold global mod settings.
/// </summary>
[XmlRoot(ElementName = "Propitize", Namespace = "", IsNullable = false)]
internal static class Settings
{
internal static List<PropitizedTreeEntry> PropitizedTreeEntries = new List<PropitizedTreeEntry>();
}
/// <summary>
/// Defines the XML settings file.
/// </summary>
[XmlRoot(ElementName = "Propitize", Namespace = "", IsNullable = false)]
public class XMLSettingsFile
{
[XmlArray("PropitizedTreesArray")]
[XmlArrayItem("PropitizedTreesEntries")]
public List<PropitizedTreeEntry> PropitizedTreeEntries { get => Settings.PropitizedTreeEntries; set => Settings.PropitizedTreeEntries = value; }
}
public class PropitizedTreeEntry
{
[XmlAttribute("Name")]
public string name = "";
public PropitizedTreeEntry() { }
public PropitizedTreeEntry(string newName) { name = newName; }
}
}