-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoundStorageXML.cs
92 lines (82 loc) · 2.87 KB
/
SoundStorageXML.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
using BasicTwitchSoundPlayer.Extensions;
using BasicTwitchSoundPlayer.Structs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml;
namespace BasicTwitchSoundPlayer.SoundStorage
{
class SoundStorageXML
{
private static readonly string varSounds = "Sounds";
private static readonly string varRequirement = "Requirement";
private static readonly string varDescription = "SoundDescription";
private static readonly string varDateAdded = "DateAdded";
public static List<Structs.SoundEntry> LoadSoundBase(string XmlPath)
{
XmlDocument doc = new XmlDocument();
XmlNode ROOTNODE = null;
List<SoundEntry> entries = new List<SoundEntry>();
if (!File.Exists(XmlPath))
{
ROOTNODE = doc.Sui_GetNode("Entries");
SaveSoundBase(XmlPath, entries);
}
else
{
doc.Load(XmlPath);
foreach (XmlNode sound in doc["Entries"])
{
var newSoundEntry = doc.GetQuickSoundEntry(sound);
if (newSoundEntry.GetIsProperEntry())
entries.Add(newSoundEntry);
}
}
return entries;
}
public static void SaveSoundBase(string XmlPath, List<Structs.SoundEntry> entries)
{
XmlDocument doc = new XmlDocument();
XmlNode ROOTNODE = doc.Sui_GetNode("Entries");
var parentDir = Directory.GetParent(XmlPath);
if (!parentDir.Exists)
parentDir.Create();
foreach (var entry in entries)
{
if (entry.GetIsProperEntry())
{
var ChildNode = ROOTNODE.Sui_GetNode(doc, entry.GetCommand());
ChildNode.Sui_SetAttributeValue(doc, varSounds, string.Join(";", entry.GetAllFiles()));
ChildNode.Sui_SetAttributeValue(doc, varRequirement, entry.GetRequirement().ToString());
ChildNode.Sui_SetAttributeValue(doc, varDescription, entry.GetDescription());
ChildNode.Sui_SetAttributeValue(doc, varDateAdded, entry.GetDateAdded().ToString());
}
}
doc.Save(XmlPath);
}
}
static class SoundEntriesExtansions
{
private static readonly string varSounds = "Sounds";
private static readonly string varRequirement = "Requirement";
private static readonly string varDescription = "SoundDescription";
private static readonly string varDateAdded = "DateAdded";
public static SoundEntry GetQuickSoundEntry(this XmlDocument xmlDoc, XmlNode node)
{
try
{
string tmpCommand = node.Name;
string[] tmpSounds = node.Sui_GetAttributeValue(xmlDoc, varSounds, "").Split(';');
TwitchRightsEnum tmpRequirement = node.Sui_GetAttributeValue(xmlDoc, varRequirement, TwitchRightsEnum.Disabled.ToString()).ToTwitchRights();
string tmpDescription = node.Sui_GetAttributeValue(xmlDoc, varDescription, "");
DateTime dateAdded = node.Sui_GetAttributeValue(xmlDoc, varDateAdded, DateTime.UtcNow.ToString()).ToDateTimeSafe();
return new SoundEntry(tmpCommand, tmpRequirement, tmpSounds, tmpDescription, dateAdded);
}
catch
{
return new SoundEntry();
}
}
}
}