-
Notifications
You must be signed in to change notification settings - Fork 1
/
SoundConverter.cs
55 lines (49 loc) · 1.93 KB
/
SoundConverter.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using ValveKeyValue;
namespace KVSurfaceUpdater
{
internal class SoundConverter : Converter
{
public bool FolderPerSoundFile = true;
public override async Task ConvertAsync(string targetPath)
{
string outputFolder = Path.Combine(targetPath, "data", "sound");
if (!Directory.Exists(outputFolder))
{
Directory.CreateDirectory(outputFolder);
}
await base.ConvertAsync(targetPath);
}
protected override bool ShouldConvertFile(string filename)
{
return Regex.Match(filename, @"game_sounds.*\.txt").Success;
}
protected override async Task<bool> ProcessFile(string filePath, string outputFolder)
{
var soundKV = OpenKVFile(filePath);
List<KVObject> allSounds = ListAllObjects(soundKV);
foreach(KVObject sound in allSounds)
{
await Task.Run(() =>
{
var kv3Sound = new KV3Sound(sound);
string newName = $"{kv3Sound.Name.ToLower().Replace("/", "_")}.sound";
string newSoundFolder = FolderPerSoundFile ? Path.Combine(outputFolder, "sound", Path.GetFileNameWithoutExtension(filePath)) : Path.Combine(outputFolder, "sound");
string newSoundFilePath = Path.Combine(newSoundFolder, newName);
if (!Directory.Exists(newSoundFolder))
{
Directory.CreateDirectory(newSoundFolder);
}
File.WriteAllText(newSoundFilePath, kv3Sound.ToString());
Console.WriteLine($"Made sound file for '{kv3Sound.Name}'.");
});
}
return true;
}
}
}