-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmain.cpp
More file actions
325 lines (239 loc) · 11.1 KB
/
main.cpp
File metadata and controls
325 lines (239 loc) · 11.1 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include <zstd.h>
#include <fstream>
#include <algorithm>
#include <thread>
#include <iostream>
#include <charconv>
#include <stack>
#include "compiledCode.hpp"
#include "scriptCompiler.hpp"
#include "scriptSerializer.hpp"
#include <mutex>
#include <queue>
#include <base64.h>
#include <json.hpp>
#include "luaHandler.hpp"
#include "logger.hpp"
std::queue<std::filesystem::path> tasks;
std::mutex taskMutex;
bool threadsShouldRun = true;
std::filesystem::path outputDir;
// Paths that should be considered the root of a P-drive, even if its not directly a drive letter
// Basically they just get stripped off the front
// rootPathMappings = {};
// inputPath = "P:/test/temp/thing.sqf"
// results in virtualPath = "\\test\\temp\\thing.sqf"
// rootPathMappings = { {"P:/test", "/"} }
// inputPath = "P:/test/temp/thing.sqf"
// results in virtualPath = "\\temp\\thing.sqf"
std::vector<std::pair<std::filesystem::path, std::filesystem::path>> rootPathMappings;
std::filesystem::path ApplyRootPathMapping(const std::filesystem::path& inputPath)
{
// Performance of this is meh, but I don't care rn
for (auto& it : rootPathMappings)
{
auto deltaPos = std::mismatch(it.first.begin(), it.first.end(), inputPath.begin(), inputPath.end());
// If the first delta, is the end of our mapping, that means the whole mapping matched
if (deltaPos.first != it.first.end())
continue;
auto resultPath = it.second / inputPath.lexically_relative(it.first);
return resultPath;
}
auto rootDir = inputPath.root_path();
auto pathRelative = inputPath.lexically_relative(rootDir);
return pathRelative;
}
void compileRecursive(std::filesystem::path inputDir) {
const std::filesystem::path ignoreGit(".git");
const std::filesystem::path ignoreSvn(".svn");
//recursively search for pboprefix
for (auto i = std::filesystem::recursive_directory_iterator(inputDir, std::filesystem::directory_options::follow_directory_symlink);
i != std::filesystem::recursive_directory_iterator();
++i) {
if (i->is_directory() && (i->path().filename() == ignoreGit || i->path().filename() == ignoreSvn)) {
i.disable_recursion_pending(); //Don't recurse into that directory
continue;
}
if (!i->is_regular_file()) continue;
if (i->path().extension() == ".sqf"sv) {
if (i->path().filename() == "fnc_zeusAttributes.sqf") continue; //Hard ignore for missing include file
//if (i->path().filename() != "fnc_viewdir.sqf") continue;
//if (i->path().filename() != "test.sqf") continue; //Hard ignore for missing include file
//if (i->path().string().find("keybinding") == std::string::npos) continue; //CBA trying to format a code piece
//if (i->path().filename().string().find("XEH_preStart") == std::string::npos) continue; //Hard ignore unit tests
tasks.emplace(i->path());
}
}
}
void processFile(ScriptCompiler& comp, std::filesystem::path path) {
try {
auto pathRelative = ApplyRootPathMapping(path);
auto outputPath = outputDir / pathRelative.parent_path() / (path.stem().string() + ".sqfc");
//if sqfc exists, check if the sqf file has been updated (is newer). if not, skip this sqf file
if (std::filesystem::exists(outputPath)) {
auto sqfcWriteTime = std::filesystem::last_write_time(outputPath);
auto sqfWriteTime = std::filesystem::last_write_time(path);
if (sqfWriteTime <= sqfcWriteTime) //sqf file is older than sqfc
return;
}
std::error_code ec;
std::filesystem::create_directories(outputPath.parent_path(), ec);
std::cout << "compile " << outputPath.generic_string() << "\n";
auto compiledData = comp.compileScript(path.generic_string(), ("\\" / pathRelative).generic_string());
if (compiledData.constants.empty()) return; // no code or failed to compile
std::stringstream output(std::stringstream::binary | std::stringstream::out);
//ScriptSerializer::compiledToBinaryCompressed(compiledData, output);
ScriptSerializer::compiledToBinary(compiledData, output);
auto data = output.str();
auto encoded = data; //base64_encode(data);
std::ofstream outputFile(outputPath, std::ofstream::binary);
outputFile.write(encoded.data(), encoded.length());
//ScriptSerializer::compiledToBinary(compiledData, output);
outputFile.flush();
//std::istringstream data2(data, std::istringstream::binary);
//auto res = ScriptSerializer::binaryToCompiledCompressed(data2);
//auto outputPath2 = path.parent_path() / (path.stem().string() + ".sqfa");
//std::ofstream output2(outputPath2, std::ofstream::binary);
//ScriptSerializer::compiledToHumanReadable(compiledData, output2);
//output2.flush();
} catch (std::domain_error& err) {
}
catch (std::runtime_error& err) {
}
}
void DecompressSQFC(std::filesystem::path inputPath, std::filesystem::path outputPath)
{
// To use this, also need to edit ScriptSerializer::compiledToBinary and disable compressed serialization
std::ifstream inputFile(inputPath, std::ifstream::binary);
auto compiledData = ScriptSerializer::binaryToCompiled(inputFile);
std::stringstream output(std::stringstream::binary | std::stringstream::out);
ScriptSerializer::compiledToBinary(compiledData, output);
auto data = output.str();
auto encoded = data; //base64_encode(data);
std::ofstream outputFile(outputPath, std::ofstream::binary);
outputFile.write(encoded.data(), encoded.length());
outputFile.flush();
}
int main(int argc, char* argv[]) {
if (std::filesystem::exists("sqfc.lua")) {
std::cout << "Using LUA for config" << "\n";
GLuaHandler.LoadFromFile("sqfc.lua");
return 0; //#TODO return real error state if any script failed
}
if (!std::filesystem::exists("sqfc.json")) {
std::cout << "Missing sqfc.json in current working directory" << "\n";
return 1;
}
std::ifstream inputFile("sqfc.json");
auto json = nlohmann::json::parse(inputFile);
std::vector<std::string> checkConfigKeys = { "excludeList", "inputDirs", "includePaths", "outputDir", "workerThreads"};
for (const std::string& key : checkConfigKeys) {
if (!json.contains(key)) {
std::cout << "Missing \"" << key << "\" in sqfc.json" << "\n";
return 1;
}
}
std::vector<std::string> excludeList = json["excludeList"].get<std::vector<std::string>>();
std::transform(excludeList.begin(), excludeList.end(), excludeList.begin(), [](std::string inp) {
std::transform(inp.begin(), inp.end(), inp.begin(), ::tolower);
return inp;
});
std::vector<std::filesystem::path> inputDirs;
for (const std::string& inputDir : json["inputDirs"].get<std::vector<std::string>>()) {
inputDirs.push_back(std::filesystem::path(inputDir));
}
std::vector<std::filesystem::path> includePaths;
for (const std::string& includePath : json["includePaths"].get<std::vector<std::string>>()) {
includePaths.push_back(std::filesystem::path(includePath));
}
outputDir = std::filesystem::path(json["outputDir"].get<std::string>());
int numberOfWorkerThreads = json["workerThreads"].get<int>();
if (!json["rootPathMappings"].is_null() && !json["rootPathMappings"].is_array())
{
std::cout << "sqfc.json error, rootPathMappings has to be array of arrays";
}
else
{
for (const auto& includePath : json["rootPathMappings"]) {
auto x = includePath.get<std::array<std::string, 2>>();
auto virtualPath = x[1];
// Strip leading slash if there is one, we add that back later
if (virtualPath.front() == '/' || virtualPath.front() == '\\')
virtualPath.erase(0, 1);
rootPathMappings.push_back({std::filesystem::path(x[0]).lexically_normal(), std::filesystem::path(virtualPath).lexically_normal()});
}
}
// Logging
if (auto loggingConfig = json["logging"]; !loggingConfig.is_null())
{
if (auto verboseCfg = loggingConfig["verbose"]; verboseCfg.is_boolean())
GLogger.m_EnableVerbose = verboseCfg;
}
// Setup workers
std::mutex workWait;
workWait.lock();
auto workerFunc = [&]() {
ScriptCompiler compiler(includePaths);
workWait.lock();
workWait.unlock();
while (threadsShouldRun) {
std::unique_lock<std::mutex> lock(taskMutex);
if (tasks.empty()) return;
const auto task(std::move(tasks.front()));
tasks.pop();
if (tasks.empty())
threadsShouldRun = false;
lock.unlock();
auto foundExclude = std::find_if(excludeList.begin(), excludeList.end(), [&task](const std::string& excludeItem)
{
auto taskString = task.string();
std::transform(taskString.begin(), taskString.end(), taskString.begin(), ::tolower);
return taskString.find(excludeItem) != std::string::npos;
});
if (foundExclude == excludeList.end())
processFile(compiler, task);
}
};
//compileRecursive("I:/ACE3/addons");
//compileRecursive("I:/CBA_A3/addons");
//compileRecursive("T:/x/");
//compileRecursive("T:/z/ace/");
//compileRecursive("T:/z/acex/");
//compileRecursive("T:/a3");
//compileRecursive("P:/test/");
for (std::filesystem::path &inputDir : inputDirs) {
compileRecursive(inputDir);
}
workWait.unlock();
std::vector<std::thread> workerThreads;
for (int i = 0; i < numberOfWorkerThreads; i++) {
workerThreads.push_back(std::thread(workerFunc));
}
workerFunc();
for (std::thread &thread : workerThreads) {
thread.join();
}
/*
auto compiledScript = compiler.compileScript("I:/ACE3/addons/advanced_ballistics/functions/fnc_readWeaponDataFromConfig.sqf");
std::ofstream hr("P:\\human.sqfa");
ScriptSerializer::compiledToHumanReadable(compiledScript, hr);
hr.close();
std::ofstream bin("P:\\binary.sqfc", std::ofstream::binary);
ScriptSerializer::compiledToBinary(compiledScript, bin);
bin.close();
std::ifstream bini("P:\\binary.sqfc", std::ifstream::binary);
auto compData = ScriptSerializer::binaryToCompiled(bini);
std::ofstream hr2("P:\\humanpostbin.sqfa");
ScriptSerializer::compiledToHumanReadable(compData, hr2);
hr2.close();
std::ofstream biCn("P:\\binaryCompressed.sqfc", std::ofstream::binary);
ScriptSerializer::compiledToBinaryCompressed(compiledScript, biCn);
biCn.close();
std::ifstream biniC("P:\\binaryCompressed.sqfc", std::ifstream::binary);
auto compDataC = ScriptSerializer::binaryToCompiledCompressed(biniC);
std::ofstream hr2C("P:\\humanpostbincompressed.sqfa");
ScriptSerializer::compiledToHumanReadable(compDataC, hr2C);
hr2C.close();
*/
return 0; //#TODO return real error state if any script failed
}