This repository has been archived by the owner on Jul 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSoundPackHelper.cs
150 lines (125 loc) · 4.8 KB
/
SoundPackHelper.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
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace Mechvibes.CSharp
{
internal static class SoundPackHelper
{
public static SoundPack LoadFromManifest(string JSONFile)
{
if (IsMultikeyPack(JSONFile) == false)
{
try
{
throw new Exception("Cannot load multi-key soundpack from a single-key manifest. Please provide a multi-key soundpack file.");
}
catch
{
MessageBox.Show("Cannot load multi-key soundpack from a single-key manifest. Please provide a multi-key soundpack file.", "Pack Specified Is Not Multi-Key",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
JObject packInfo = JObject.Parse(File.ReadAllText(JSONFile));
string name = packInfo.Value<string>("name");
JObject defines = packInfo.Value<JObject>("defines");
List<Keymap> keybinds = new List<Keymap>();
foreach (JProperty keybind in defines.Properties())
{
Key key = KeymapHelper.GetKeyFromManifest(int.Parse(keybind.Name));
string audio = Path.GetDirectoryName(JSONFile) + "\\" + keybind.Value;
keybinds.Add(new Keymap(key, audio));
}
return new SoundPack(name, keybinds);
}
public static SingleKeySoundPack LoadSingleKeyFromManifest(string JSONFile)
{
if (IsMultikeyPack(JSONFile) == true)
{
try
{
throw new Exception("Cannot load single-key soundpack from a single-key manifest. Please provide a multi-key soundpack file.");
}
catch
{
MessageBox.Show("Cannot load single-key soundpack from a single-key manifest. Please provide a multi-key soundpack file.", "Pack Specified Is Not Single-Key",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
JObject packInfo = JObject.Parse(File.ReadAllText(JSONFile));
string name = packInfo.Value<string>("name");
JObject defines = packInfo.Value<JObject>("defines");
List<(Key, AudioRange)> keybinds = new List<(Key, AudioRange)>();
foreach (JProperty keybind in defines.Properties())
{
Key key = KeymapHelper.GetKeyFromManifest(int.Parse(keybind.Name));
if (defines[keybind.Name] is JArray audioPoints)
{
AudioRange audioInfo = new AudioRange
{
Position = int.Parse(audioPoints[0].ToString()),
Duration = int.Parse(audioPoints[1].ToString()),
};
keybinds.Add((key, audioInfo));
}
}
return new SingleKeySoundPack(name, Path.GetDirectoryName(JSONFile) + "\\" + packInfo.Value<string>("sound"), keybinds);
}
public static void SaveToManifest(SoundPack SoundPack, string JSONFile)
{
List<string> packinfo = new List<string>
{
"{",
"\t\"id\": \"custom-sound-pack-1612728813651\",",
"\t\"name\": \"" + SoundPack.Name + "\",",
"\t\"key_define_type\": \"multi\",",
"\t\"includes_numpad\": \"" + SoundPack.IncludesNumPad.ToString().ToLower() + "\",",
"\t\"sound\": \"" + Path.GetFileName(SoundPack.Keybinds[0].AudioFile) + "\",",
"\t\"defines\": {",
};
foreach (Keymap keymap in SoundPack.Keybinds)
packinfo.Add("\t\t\"" + KeymapHelper.GetCodeFromKey(keymap.Keybind) + "\": \"" + Path.GetFileName(keymap.AudioFile) + "\",");
packinfo.Add("\t}");
packinfo.Add("}");
File.WriteAllLines(JSONFile, packinfo);
}
public static void SaveSingleKeyToManifest(SingleKeySoundPack SoundPack, string JSONFile)
{
List<string> packinfo = new List<string>
{
"{",
"\t\"id\": \"custom-sound-pack-1612728813651",
"\t\"name\": " + SoundPack.Name + "\",",
"\t\"key_define_type\": \"single\",",
"\t\"includes_numpad\": \"" + SoundPack.IncludesNumPad.ToString().ToLower() + "\",",
"\t\"sound\": \"" + Path.GetFileName(SoundPack.AudioFile) + "\",",
"\t\"defines\": {",
};
foreach ((Key, AudioRange) keybind in SoundPack.Keybinds)
{
packinfo.Add("\t\t" + KeymapHelper.GetCodeFromKey(keybind.Item1) + "\": [");
packinfo.Add("\t\t\t" + keybind.Item2.Position + ",");
packinfo.Add("\t\t\t" + keybind.Item2.Duration + ",");
packinfo.Add("\t\t]" + (keybind.Item1 == SoundPack.Keybinds.Last().Item1 ? "" : ","));
}
packinfo.Add("\t}");
packinfo.Add("}");
File.WriteAllLines(JSONFile, packinfo);
}
public static bool? IsMultikeyPack(string JSONFile)
{
JObject packInfo = JObject.Parse(File.ReadAllText(JSONFile));
bool specifiesKeyType = packInfo.ContainsKey("key_define_type");
bool specifiesMultikey = specifiesKeyType && (packInfo.Value<string>("key_define_type") == "multi");
if (!specifiesKeyType)
{
MessageBox.Show("The pack manifest (config.json) specified is not in a valid form because it does not specify if the soundpack it represents is a multi-key or single-key soundpack.", "Pack Manifest (config.json) Specified Is Invalid",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
return specifiesMultikey;
}
}
}