-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSourceSoundConverter.cpp
174 lines (144 loc) · 5.17 KB
/
SourceSoundConverter.cpp
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
// Copyright 2019-2020 jakers1403
// SourceSoundConverter.cpp : Converts a sounds folder into a vsndevts file
#include <iostream>
#include <fstream>
#include <filesystem>
#include "LuaIntegration\LuaIntegration.h"
namespace fs = std::filesystem;
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
bool hasNumberAtEnd(string toCheck)
{
int lengthOfString = toCheck.length();
return isdigit(toCheck.at(lengthOfString - 1));
}
string removeNumberAtEnd(string withNum)
{
while (hasNumberAtEnd(withNum))
{
withNum = withNum.substr(0, withNum.size() - 1);
}
return withNum;
}
bool isNameVariation(string name)
{
string noNumAtEnd = removeNumberAtEnd(name);
return noNumAtEnd.back() != '_' && hasNumberAtEnd(name);
}
std::string FMgt_RemoveExtension(const std::string& filename) {
size_t lastdot = filename.find_last_of(".");
if (lastdot == std::string::npos) return filename;
return filename.substr(0, lastdot);
}
int main(int argc, char* argv[])
{
cout << "Source Sound Converter By Jakes1403/Jakers1403\n";
LI_Init();
LI_ExecuteLua("config.lua");
bool useVariations = LI_GetBoolVarValue("useVariations");
bool usePerFileFunction = LI_GetBoolVarValue("perFileFunction");
string soundType = LI_GetStringVarValue("soundType");
string outputFileName = LI_GetStringVarValue("outputFileName");
string inputPath = LI_GetStringVarValue("inputPath");
float soundVolume = LI_GetFloatVarValue("volume");
float soundHRTF = LI_GetFloatVarValue("hrtf");
if (inputPath == "path_to_sounds_folder")
{
string throwaway;
cout << "Please edit your config.lua file to point to your sounds folder.\nPress any key to continue.";
getline(cin, throwaway);
return 0;
}
ofstream fileStream;
fileStream.open(outputFileName);
const std::filesystem::path path = inputPath;
fileStream << "<!-- kv3 encoding:text:version{e21c7f3c-8a33-41c5-9977-a76d3a32aa0d} format:generic:version{7412167c-06e9-4698-aff2-e63eb59037e7} -->\n{\n";
vector<string> doneVariations;
bool lastWasVariation = false;
bool lastWasNewVariation = false;
for (auto& p : fs::recursive_directory_iterator(inputPath))
{
string inGameName = FMgt_RemoveExtension(fs::relative(p.path(), inputPath).u8string());
string nameOfFile = inGameName;
bool isFolder = fs::is_directory(p.path());
std::replace(inGameName.begin(), inGameName.end(), '\\', '.');
std::replace(nameOfFile.begin(), nameOfFile.end(), '\\', '/');
bool foundDash = false;
bool foundSpace = false;
if (inGameName.find('-') != std::string::npos)
foundDash = true;
if (inGameName.find(' ') != std::string::npos)
foundSpace = true;
if (foundDash || foundSpace)
{
cout << nameOfFile << " has spaces or dashes. Skipping!\n";
}
string overrideSoundType = soundType;
float overideVolume = soundVolume;
float overideHRTF = soundHRTF;
if (usePerFileFunction)
{
LI_SetStringVarValue("currentFilePathNoExt", nameOfFile);
LI_SetStringVarValue("currentInGameName", inGameName);
LI_RunFunction("perFile");
overideVolume = LI_GetFloatVarValue("volume");
overideHRTF = LI_GetFloatVarValue("hrtf");
if (LI_GetBoolVarValue("overideInGameName"))
{
inGameName = LI_GetStringVarValue("inGameName");
}
}
if (useVariations)
{
bool isVariation = isNameVariation(inGameName);
fileStream << isVariation << lastWasVariation << lastWasNewVariation << "\n";
if (!isFolder && !foundDash && !foundSpace && isVariation)
{
lastWasVariation = true;
if (!(std::count(doneVariations.begin(), doneVariations.end(), removeNumberAtEnd(inGameName))))
{
lastWasNewVariation = true;
doneVariations.push_back(removeNumberAtEnd(inGameName));
fileStream << "\t" << removeNumberAtEnd(inGameName) << " = \n\t{\n\t\ttype = \"hlvr_default_3d\"\n\t\tvolume = 1.000000\n\t\tuse_hrtf = 1.000000\n\t\tvsnd_files =\n\t\t[\n";
fileStream << "\t\t\t\"sounds/" << nameOfFile << ".vsnd\",\n";
}
else
{
lastWasNewVariation = false;
fileStream << "\t\t\t\"sounds/" << nameOfFile << ".vsnd\",\n";
}
}
if (!isVariation && lastWasVariation && lastWasNewVariation)
{
fileStream << "\t\t]\n\t}\n";
lastWasVariation = false;
}
if (!isFolder && !foundDash && !foundSpace && !isVariation)
{
fileStream << "\t" << inGameName << " = \n\t{\n\t\ttype = \"" << overrideSoundType << "\"\n\t\tvolume = " << overideVolume << "\n\t\tuse_hrtf = " << overideHRTF << "\n\t\tvsnd_files =\n\t\t[\n\t\t\t\"sounds/" << nameOfFile << ".vsnd\"\n\t\t]\n\t}\n";
}
else
{
cout << "Generating for folder: " << nameOfFile << endl;
}
}
else
{
if (!isFolder && !foundDash && !foundSpace)
{
fileStream << "\t" << inGameName << " = \n\t{\n\t\ttype = \"" << overrideSoundType << "\"\n\t\tvolume = " << overideVolume << "\n\t\tuse_hrtf = " << overideHRTF << "\n\t\tvsnd_files =\n\t\t[\n\t\t\t\"sounds/" << nameOfFile << ".vsnd\"\n\t\t]\n\t}\n";
}
else
{
cout << "Generating for folder: " << nameOfFile << endl;
}
}
}
fileStream << "}\n";
fileStream.close();
}