-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.js
101 lines (76 loc) · 2.5 KB
/
lib.js
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
let mkdirp = require('mkdirp'); // create directories that don't exist
let fs = require('fs'); // filesystem reader.
let debug = require('debug')('HUDGEN:lib');
function copy(entry,dir) {
let { name, path, parentDir, fullParentDir } = entry;
if(name == 'hudanimations_tf.txt') debug('WARN: Using hudanimations_tf.txt is not recommended. Use manifest instead.');
mkdirp(`${__dirname}/src/${dir}/${parentDir}`, (err) => {
if(err) throw new Error(err);
fs.copyFileSync(`${__dirname}/src/custom/${path}`, `${__dirname}/src/${dir}/${path}`);
});
}
function parseIncludes(data) {
let lines = data.split('\n');
let includes = [];
lines.forEach(function(line) {
if(line.indexOf('#') == 0 ) {
includes.push(line);
}
})
return includes;
}
function addIncludes(includes,output) {
let data = '';
includes.forEach(function(include) {
data += include + '\n';
})
return data + output;
}
function validateVDF(data) {
data = data.replace(/(?:^\/{2}.+\n?)+/, ''); // strip comments
/*
For some reason if you have a VDF object, it's key value doesn't need quotes.
This screws up the parser here because it needs quotes, so we have to implement
our own check
object {
}
"object" {
}
are both somehow valid
*/
function checkForKeyQuotes(dataLine) {
// get a set of all quote combinations in the string
let quoteSets =
dataLine
.trim() // trim whitespace
.match(/(["'])(?:(?=(\\?))\2.)*?\1/) // regex to match quote sets
// becaue .match returns null instead of an empty array if none are found, do this
quoteSets = quoteSets || [];
let hasQuotes = !!quoteSets.length;
return hasQuotes;
}
let outputLines = [];
let dataLines =
data
.replace(/\t/g, '') // remove tabs
.split(/\r?\n|\r/g) // split by newline
for(let i = 0; i < dataLines.length; i++) {
let line = dataLines[i];
// if the current line is '{', check to see if the last line needs
// modification
if(line == '{') {
let headerHasQuotes = checkForKeyQuotes(dataLines[i-1]);
if(!headerHasQuotes) {
outputLines[i-1] = `"${dataLines[i-1]}"`;
}
}
outputLines.push(line);
}
return outputLines.join('\n');
}
module.exports = {
copy,
validateVDF,
parseIncludes,
addIncludes
}