-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
306 lines (267 loc) · 12.7 KB
/
Program.cs
File metadata and controls
306 lines (267 loc) · 12.7 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Globalization;
namespace AMP.src
{
class Program
{
[DllImport(@"add_ogg_comment.dll")]
public static extern int amp_AddOggComment([MarshalAs(UnmanagedType.LPStr)]string path, float mindist, float maxdist, float basevol, uint sndtype, float maxaidist);
public static Dictionary<string, string> ReadConfig()
{
var fileDirectory = Path.GetDirectoryName(AppContext.BaseDirectory);
var filePath = $"{fileDirectory}\\config.ini";
if (File.Exists(filePath) == false)
throw new Exception($"The Config.ini file is missing at {filePath}.");
var lines = File.ReadLines(filePath);
var dict = new Dictionary<string, string>();
foreach (var line in lines)
{
var split = line.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
if (split.Length != 2)
continue;
if (dict.ContainsKey(split[0].Trim()) == false)
dict.Add(split[0].Trim(), split[1].Trim());
}
var validationMessage = new StringBuilder();
if (dict.ContainsKey("GamedataDir") == false)
validationMessage.AppendLine("The GamedataDir value is empty or missing.");
if (dict.ContainsKey("FfmpegBinDir") == false)
validationMessage.AppendLine("The FfmpegBinDir value is empty or missing.");
if (dict.ContainsKey("InputDir") == false)
validationMessage.AppendLine("The InputDir value is empty or missing.");
if (dict.ContainsKey("GameSndType") == false)
validationMessage.AppendLine("The GameSndType value is empty or missing.");
if (dict.ContainsKey("BaseSndVol") == false)
validationMessage.AppendLine("The BaseSndVol value is empty or missing.");
if (dict.ContainsKey("MinDist") == false)
validationMessage.AppendLine("The MinDist value is empty or missing.");
if (dict.ContainsKey("MaxDist") == false)
validationMessage.AppendLine("The MaxDist value is empty or missing.");
if (dict.ContainsKey("MaxAIDist") == false)
validationMessage.AppendLine("The MaxAIDist value is empty or missing.");
if (dict.ContainsKey("OutputDir") == false)
dict.Add("OutputDir", $"{dict["GamedataDir"]}\\sounds");
Console.Write($"Missing OutputDir using default instead");
if (dict.ContainsKey("RemoveWav") == false)
dict.Add("RemoveWav", "false");
Console.Write($"Missing RemoveWav setting to false");
if (validationMessage.Length > 0)
{
validationMessage.AppendLine("Please run Config.exe to configure the application.");
throw new Exception(validationMessage.ToString());
}
return dict;
}
public static void ValidatePaths(Dictionary<string, string> config)
{
Console.Write("Validating Paths \n");
foreach (var kvp in config)
{
if (kvp.Key != "OutputDir" && Regex.IsMatch(kvp.Value, @"\w+:\\") && Directory.Exists(kvp.Value) == false)
throw new Exception($"The {kvp.Key} {kvp.Value} does not exist.\n");
}
}
static string[] GetSoundFiles(Dictionary<string, string> config)
{
var soundDir = $"{config["InputDir"]}";
Console.Write($"Getting sound files from {soundDir} \n");
var soundFiles = new List<string>();
string[] suffixes = new string[] {"*.mp3", "*.wav"};
foreach (var suffix in suffixes)
{
List<string> found_files = new List<string>(Directory.GetFiles(soundDir, suffix, SearchOption.AllDirectories));
soundFiles.AddRange(found_files);
}
return soundFiles.ToArray();
}
static string GetFfmpegExe(Dictionary<string, string> config)
{
var exeLocation = $"{config["FfmpegBinDir"]}\\ffmpeg.exe";
Console.Write($"Searching for ffmpeg exe at {exeLocation} \n");
var exeLocationFileInfo = new FileInfo(exeLocation);
if (exeLocationFileInfo.Exists)
{
Console.Write($"Found ffmpeg exe \n");
return exeLocation;
}
throw new Exception($"Could not find {exeLocation}. \n Please Install Ffmpeg or correct the 'FfmpegBinDir' value to ffmpeg's bin folder\n");
}
static async Task ConvertSoundFile(string ffmpegExe, string i_args, string o_args, string path, string outpath)
{
Console.Write($"Converting '{path}' to '{outpath}'\n");
var directory = Path.GetDirectoryName(outpath);
if (!Directory.Exists(directory))
{
Console.Write($"Output Directory: {directory} doesn't exist\n");
Directory.CreateDirectory(directory);
Console.Write($"Created Directory: {directory}\n");
}
Process FfmpegProcess = new Process();
FfmpegProcess.StartInfo.FileName = ffmpegExe;
FfmpegProcess.StartInfo.Arguments = $"{i_args} -i \"{path}\" {o_args} \"{outpath}\"";
FfmpegProcess.StartInfo.RedirectStandardError = true;
FfmpegProcess.StartInfo.RedirectStandardOutput = true;
FfmpegProcess.StartInfo.UseShellExecute = false;
FfmpegProcess.StartInfo.CreateNoWindow = false;
FfmpegProcess.EnableRaisingEvents = true;
FfmpegProcess.Start();
await Task.Run(() => {
Console.WriteLine(FfmpegProcess.StandardError.ReadToEnd());
});
await FfmpegProcess.WaitForExitAsync();
Console.Write($"Finished Converting {path} \n");
}
static float stringToFloat(string str)
{
return float.Parse(str, CultureInfo.InvariantCulture.NumberFormat);
}
static uint gameSndTypeToInt(string sndType)
{
var sndTypeTbl = new Dictionary<string, uint>();
sndTypeTbl.Add("world_ambient", 134217856);
sndTypeTbl.Add("object_exploding", 134217984);
sndTypeTbl.Add("object_colliding", 134218240);
sndTypeTbl.Add("object_breaking", 134218752);
sndTypeTbl.Add("anomaly_idle", 268437504);
sndTypeTbl.Add("npc_eating", 536875008);
sndTypeTbl.Add("npc_attacking", 536879104);
sndTypeTbl.Add("npc_talking", 536887296);
sndTypeTbl.Add("npc_step", 536903680);
sndTypeTbl.Add("npc_injuring", 536936448);
sndTypeTbl.Add("npc_dying", 537001984);
sndTypeTbl.Add("item_using", 1077936128);
sndTypeTbl.Add("item_taking", 1082130432);
sndTypeTbl.Add("item_hiding", 1090519040);
sndTypeTbl.Add("item_dropping", 1107296256);
sndTypeTbl.Add("item_picking_up", 1140850688);
sndTypeTbl.Add("weapon_recharging", 2147745792);
sndTypeTbl.Add("weapon_bullet_hit", 2148007936);
sndTypeTbl.Add("weapon_empty_clicking", 2148532224);
sndTypeTbl.Add("weapon_shooting", 2149580800);
sndTypeTbl.Add("undefined", 0);
return sndTypeTbl[sndType];
}
static void AddOggComment(Dictionary<string, string> config, string path)
{
if (!File.Exists(path))
{
throw new Exception($"Cannot add ogg comment, file not found at {path}");
}
float basevol = stringToFloat(config["BaseSndVol"]);
float mindist = stringToFloat(config["MinDist"]);
float maxdist = stringToFloat(config["MaxDist"]);
float maxaidist = stringToFloat(config["MaxAIDist"]);
uint sndtype = gameSndTypeToInt(config["GameSndType"]);
Console.Write($"Adding Ogg Comment: MinDist:{mindist}, MaxDist:{maxdist}, BaseVol:{basevol}, SndType:{sndtype}, MaxAIDist:{maxaidist} \n");
amp_AddOggComment(path, mindist, maxdist, basevol, sndtype, maxaidist);
Console.Write($"\nOgg Comment Added to {path}\n");
}
static void MoveFile(Dictionary<string, string> config, string srcFile)
{
string outDir = config["OutputDir"];
string destFile = $"{outDir}\\{Path.GetFileName(srcFile)}";
Console.Write($"Moving {srcFile} to {destFile}\n");
var moved = true;
while (moved == false)
{
try
{
if (!Directory.Exists(outDir))
{
Directory.CreateDirectory(outDir);
}
if (!File.Exists(destFile))
{
File.Move(srcFile, destFile);
}
}
catch (Exception e)
{
Console.WriteLine($"The process failed: {0}\n", e.ToString());
}
}
}
static void RemoveFile(string srcFile)
{
if (!File.Exists(srcFile))
{
throw new Exception($"Cannot delete {srcFile} as it doesn't exist.\n");
}
File.Delete(srcFile);
}
public static async Task ProcessSoundFiles(Dictionary<string, string> config, string[] soundFiles)
{
var tasks = soundFiles.Select(path => ProcessSoundFile(config, path));
await Task.WhenAll(tasks);
}
static async Task ProcessSoundFile(Dictionary<string, string> config, string path)
{
string ffmpegExe = GetFfmpegExe(config);
string outpath_dir = config["OutputDir"];
string input_dir = config["InputDir"];
string ext = Path.GetExtension(path);
string path_to_convert = path;
if (path.StartsWith(input_dir, StringComparison.CurrentCultureIgnoreCase))
{
path = $"{outpath_dir}\\{path.Substring(input_dir.Length).TrimStart(Path.DirectorySeparatorChar)}";
}
if (ext == ".mp3"){
string outpath_wav = $"{Path.GetDirectoryName(path)}\\{Path.GetFileNameWithoutExtension(path)}.wav";
string outpath_ogg = $"{Path.GetDirectoryName(path)}\\{Path.GetFileNameWithoutExtension(path)}.ogg";
if (File.Exists(outpath_ogg))
{
Console.Write($"Ogg File Already Exists at: {outpath_ogg}\n");
return;
}
await ConvertSoundFile(ffmpegExe, "-y -f mp3", "-acodec pcm_s16le -vn -f wav", path_to_convert, outpath_wav);
await ConvertSoundFile(ffmpegExe, "-y -f wav", "-acodec libvorbis -y -vn -ac 1 -ar 44100 -f ogg", outpath_wav, outpath_ogg);
AddOggComment(config, outpath_ogg);
if (bool.Parse(config["RemoveWav"]))
{
RemoveFile(outpath_wav);
}
}
else if (ext == ".wav")
{
string outpath_ogg = $"{Path.GetDirectoryName(path)}\\{Path.GetFileNameWithoutExtension(path_to_convert)}.ogg";
if (File.Exists(outpath_ogg))
{
Console.Write($"Ogg File Already Exists at: {outpath_ogg}\n");
return;
}
await ConvertSoundFile(ffmpegExe, "-y -f wav", "-acodec libvorbis -y -vn -ac 1 -ar 44100 -f ogg", path_to_convert, outpath_ogg);
AddOggComment(config, outpath_ogg);
if (bool.Parse(config["RemoveWav"]))
{
RemoveFile(path);
}
}
}
public static async Task Main(string[] args)
{
try
{
var config = ReadConfig();
ValidatePaths(config);
string [] soundFiles = GetSoundFiles(config);
await ProcessSoundFiles(config, soundFiles);
Console.Write("Finished Processing Sound Files");
}
catch (Exception e)
{
Console.WriteLine(e);
Console.ReadKey();
}
}
}
}