-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathXMLSettings.cs
209 lines (172 loc) · 7.54 KB
/
XMLSettings.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
namespace AudioHotkeySoundboard
{
public class XMLSettings
{
readonly static SoundboardSettings DEFAULT_SOUNDBOARD_SETTINGS = new SoundboardSettings(new Keys[] { Keys.Pause }, new Keys[] { Keys.Oemtilde, Keys.LControlKey }, new LoadXMLFile[] { new LoadXMLFile(new Keys[] { }, "") }, false, true, false, "", "", "");
internal static SoundboardSettings soundboardSettings = new SoundboardSettings();
//saving XML files like this makes the XML messy, but it works
#region Keys and sounds settings
public class SoundHotkey
{
public Keys[] Keys;
public string[] SoundLocations;
public string SoundClips;
public SoundHotkey() { }
public SoundHotkey(Keys[] keys, string[] soundLocs)
{
Keys = keys;
SoundLocations = soundLocs;
SoundClips = Helper.fileNamesFromLocations(soundLocs);
}
}
[Serializable]
public class Settings
{
public SoundHotkey[] SoundHotkeys;
public Settings() { }
public Settings(SoundHotkey[] sh)
{
SoundHotkeys = sh;
}
}
#endregion
#region Soundboard settings
public class LoadXMLFile
{
public Keys[] Keys;
public string XMLLocation;
public LoadXMLFile() { }
public LoadXMLFile(Keys[] keys, string xmlLocation)
{
Keys = keys;
XMLLocation = xmlLocation;
}
}
[Serializable]
public class SoundboardSettings
{
public Keys[] StopSoundKeys, PlaySelectionKeys;
public LoadXMLFile[] LoadXMLFiles;
public bool MinimizeToTray, PlaySoundsOverEachOther, RememberGainControl;
public string LastPlaybackDevice, LastPlaybackDevice2, LastLoopbackDevice;
public bool GoEvenFurtherBeyond;
public int GainValue;
public SoundboardSettings() { }
public SoundboardSettings(Keys[] stopSoundKeys, Keys[] playSelectionKeys, LoadXMLFile[] loadXMLFiles, bool minimizeToTray, bool playSoundsOverEachOther, bool rememberGainControl, string lastPlaybackDevice, string lastPlaybackDevice2, string lastLoopbackDevice)
{
StopSoundKeys = stopSoundKeys;
PlaySelectionKeys = playSelectionKeys;
LoadXMLFiles = loadXMLFiles;
MinimizeToTray = minimizeToTray;
PlaySoundsOverEachOther = playSoundsOverEachOther;
RememberGainControl = rememberGainControl;
LastPlaybackDevice = lastPlaybackDevice;
LastPlaybackDevice2 = lastPlaybackDevice2;
LastLoopbackDevice = lastLoopbackDevice;
}
}
#endregion
internal static void WriteXML(object kl, string xmlLoc)
{
XmlSerializer serializer = new XmlSerializer(kl.GetType());
using (MemoryStream memStream = new MemoryStream())
{
using (StreamWriter stream = new StreamWriter(memStream, Encoding.Unicode))
{
var settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
using (var writer = XmlWriter.Create(stream, settings))
{
var emptyNamepsaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
serializer.Serialize(writer, kl, emptyNamepsaces);
int count = (int)memStream.Length;
byte[] arr = new byte[count];
memStream.Seek(0, SeekOrigin.Begin);
memStream.Read(arr, 0, count);
using (BinaryWriter binWriter = new BinaryWriter(File.Open(xmlLoc, FileMode.Create)))
{
binWriter.Write(arr);
}
}
}
}
}
internal static object ReadXML(Type type, string xmlLoc)
{
var serializer = new XmlSerializer(type);
using (var reader = XmlReader.Create(xmlLoc))
{
if (serializer.CanDeserialize(reader))
{
return serializer.Deserialize(reader);
}
else return null;
}
}
internal static void SaveSoundboardSettingsXML()
{
WriteXML(soundboardSettings, Path.GetDirectoryName(Application.ExecutablePath) + "\\AHS-settings.xml");
}
internal static void LoadSoundboardSettingsXML()
{
string filePath = Path.GetDirectoryName(Application.ExecutablePath) + "\\AHS-settings.xml";
string oldFilePath = Path.GetDirectoryName(Application.ExecutablePath) + "\\settings.xml";
if (!File.Exists(filePath)) { filePath = oldFilePath; } // try the old file path if the new one doesn't exist
if (File.Exists(filePath))
{
SoundboardSettings settings;
try
{
settings = (SoundboardSettings)ReadXML(typeof(SoundboardSettings), filePath);
}
catch
{
soundboardSettings = DEFAULT_SOUNDBOARD_SETTINGS;
return;
}
if (settings == null)
{
soundboardSettings = DEFAULT_SOUNDBOARD_SETTINGS;
return;
}
if (settings.StopSoundKeys == null) settings.StopSoundKeys = new Keys[] { Keys.Pause };
if (settings.PlaySelectionKeys == null) settings.PlaySelectionKeys = new Keys[] { Keys.Oemtilde, Keys.LControlKey };
if (settings.LoadXMLFiles == null) settings.LoadXMLFiles = new LoadXMLFile[] { };
if (settings.LastPlaybackDevice == null) settings.LastPlaybackDevice = "";
if (settings.LastPlaybackDevice2 == null) settings.LastPlaybackDevice2 = "";
if (settings.LastLoopbackDevice == null) settings.LastLoopbackDevice = "";
if (settings.RememberGainControl)
{
MainForm.Instance.SetGain(settings.GainValue);
if (settings.GoEvenFurtherBeyond)
{
MainForm.Instance.GoEvenFurtherBeyond(true);
}
}
soundboardSettings = settings;
}
else
{
WriteXML(DEFAULT_SOUNDBOARD_SETTINGS, filePath);
soundboardSettings = DEFAULT_SOUNDBOARD_SETTINGS;
for (int i = 0; i < WaveOut.DeviceCount; i++)
{
if (WaveOut.GetCapabilities(i).ProductName.Contains("VB-Cable Input") || WaveOut.GetCapabilities(i).ProductName.Contains("CABLE Input") || WaveOut.GetCapabilities(i).ProductName.Contains("VoiceMeeter Input"))
{
soundboardSettings.LastPlaybackDevice = WaveOut.GetCapabilities(i).ProductName;
WriteXML(soundboardSettings, filePath);
}
}
}
}
}
}