Skip to content

Commit

Permalink
Split up ConfigElement.SerialiseSimple into ConfigElement.SerialiseSi…
Browse files Browse the repository at this point in the history
…mple/SerialiseElements
  • Loading branch information
UnknownShadow200 committed Oct 20, 2021
1 parent 48a8cce commit b91eeaa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
33 changes: 22 additions & 11 deletions MCGalaxy/Config/ConfigElement.cs
Expand Up @@ -21,9 +21,10 @@
using System.Reflection;
using MCGalaxy.Config;

namespace MCGalaxy {

public struct ConfigElement {
namespace MCGalaxy
{
public struct ConfigElement
{
public ConfigAttribute Attrib;
public FieldInfo Field;

Expand All @@ -38,7 +39,8 @@ public struct ConfigElement {
List<ConfigElement> elems = new List<ConfigElement>();
FieldInfo[] fields = type.GetFields(flags);

for (int i = 0; i < fields.Length; i++) {
for (int i = 0; i < fields.Length; i++)
{
FieldInfo field = fields[i];
Attribute[] attributes = Attribute.GetCustomAttributes(field, typeof(ConfigAttribute));
if (attributes.Length == 0) continue;
Expand All @@ -58,7 +60,8 @@ public struct ConfigElement {
}

public static void Parse(ConfigElement[] elems, object instance, string k, string v) {
foreach (ConfigElement elem in elems) {
foreach (ConfigElement elem in elems)
{
if (!elem.Attrib.Name.CaselessEq(k)) continue;

elem.Field.SetValue(instance, elem.Attrib.Parse(v)); return;
Expand All @@ -68,7 +71,8 @@ public struct ConfigElement {
public static void Serialise(ConfigElement[] elements, StreamWriter dst, object instance) {
Dictionary<string, List<ConfigElement>> sections = new Dictionary<string, List<ConfigElement>>();

foreach (ConfigElement elem in elements) {
foreach (ConfigElement elem in elements)
{
List<ConfigElement> members;
if (!sections.TryGetValue(elem.Attrib.Section, out members)) {
members = new List<ConfigElement>();
Expand All @@ -78,9 +82,11 @@ public struct ConfigElement {
}

// group output by sections
foreach (var kvp in sections) {
foreach (var kvp in sections)
{
dst.WriteLine("# " + kvp.Key + " settings");
foreach (ConfigElement elem in kvp.Value) {
foreach (ConfigElement elem in kvp.Value)
{
dst.WriteLine(elem.Format(instance));
}
dst.WriteLine();
Expand All @@ -90,9 +96,14 @@ public struct ConfigElement {
public static void SerialiseSimple(ConfigElement[] elements, string path, object instance) {
using (StreamWriter w = new StreamWriter(path)) {
w.WriteLine("#Settings file");
foreach (ConfigElement elem in elements) {
w.WriteLine(elem.Format(instance));
}
SerialiseElements(elements, w, instance);
}
}

public static void SerialiseElements(ConfigElement[] elements, TextWriter w, object instance) {
foreach (ConfigElement elem in elements)
{
w.WriteLine(elem.Format(instance));
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions MCGalaxy/Player/Group/GroupProperties.cs
Expand Up @@ -109,9 +109,7 @@ public sealed class GroupProperties {

foreach (Group group in givenList) {
w.WriteLine("RankName = " + group.Name);
foreach (ConfigElement elem in cfg) {
w.WriteLine(elem.Format(group));
}
ConfigElement.SerialiseElements(cfg, w, group);
w.WriteLine();
}
}
Expand Down

0 comments on commit b91eeaa

Please sign in to comment.