@sie-js/vkp 1.0.6 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/dist/src/VkpParseError.d.ts +12 -0
- package/dist/src/VkpParseError.js +65 -0
- package/dist/src/index.d.ts +33 -0
- package/dist/src/index.js +156 -0
- package/dist/src/index.test.d.ts +1 -0
- package/dist/src/index.test.js +381 -0
- package/dist/src/lexer.d.ts +2 -0
- package/dist/src/lexer.js +23 -0
- package/dist/src/parser.d.ts +37 -0
- package/dist/src/parser.js +510 -0
- package/package.json +34 -25
- package/.editorconfig +0 -6
- package/.github/dependabot.yml +0 -11
- package/.github/workflows/ci.yml +0 -18
- package/.prettierrc.json +0 -21
- package/examples/get-bad-patches.js +0 -122
- package/jest.config.js +0 -4
- package/src/VkpParseError.js +0 -68
- package/src/index.js +0 -171
- package/src/index.test.js +0 -403
- package/src/lexer.js +0 -46
- package/src/parser.js +0 -515
package/.prettierrc.json
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"arrowParens": "always",
|
|
3
|
-
"bracketSameLine": false,
|
|
4
|
-
"bracketSpacing": true,
|
|
5
|
-
"semi": true,
|
|
6
|
-
"experimentalTernaries": false,
|
|
7
|
-
"singleQuote": false,
|
|
8
|
-
"jsxSingleQuote": false,
|
|
9
|
-
"quoteProps": "as-needed",
|
|
10
|
-
"trailingComma": "none",
|
|
11
|
-
"singleAttributePerLine": false,
|
|
12
|
-
"htmlWhitespaceSensitivity": "css",
|
|
13
|
-
"vueIndentScriptAndStyle": false,
|
|
14
|
-
"proseWrap": "preserve",
|
|
15
|
-
"insertPragma": false,
|
|
16
|
-
"printWidth": 200,
|
|
17
|
-
"requirePragma": false,
|
|
18
|
-
"tabWidth": 4,
|
|
19
|
-
"useTabs": true,
|
|
20
|
-
"embeddedLanguageFormatting": "auto"
|
|
21
|
-
}
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import iconv from 'iconv-lite';
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import path from 'path';
|
|
4
|
-
import { Blob } from "buffer";
|
|
5
|
-
import { globSync } from 'glob';
|
|
6
|
-
import { vkpParse, vkpDetectContent } from '../src/index.js';
|
|
7
|
-
import child_process from 'child_process';
|
|
8
|
-
|
|
9
|
-
const PATCHES_DIR = `${import.meta.dirname}/../../patches/patches`;
|
|
10
|
-
|
|
11
|
-
for (let file of readFiles(PATCHES_DIR)) {
|
|
12
|
-
if (!file.match(/\.vkp$/))
|
|
13
|
-
continue;
|
|
14
|
-
|
|
15
|
-
let patchText = iconv.decode(fs.readFileSync(`${PATCHES_DIR}/${file}`), 'windows1251').replace(/(\r\n|\n)/g, '\n');
|
|
16
|
-
let patchUrl = patchText.match(/Details: (https?:\/\/.*?)$/m)[1];
|
|
17
|
-
|
|
18
|
-
let detectedType = vkpDetectContent(patchText);
|
|
19
|
-
if (detectedType == "DOWNLOAD_STUB") {
|
|
20
|
-
let patchId = path.basename(file).split('-')[0];
|
|
21
|
-
|
|
22
|
-
let [additionalFile] = globSync(`${PATCHES_DIR}/*/${patchId}-*.{rar,zip}`);
|
|
23
|
-
if (!additionalFile) {
|
|
24
|
-
console.error(`${file} - is download stub, but additional file not found!`);
|
|
25
|
-
continue;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
let archive = await getFilesFromArchive(additionalFile);
|
|
29
|
-
|
|
30
|
-
let extractedPatches = [];
|
|
31
|
-
for (let entry of archive.lsarContents) {
|
|
32
|
-
if (entry.XADFileName.match(/\.vkp$/i)) {
|
|
33
|
-
patchText = (await extractFileFromArchive(additionalFile, entry.XADIndex)).toString('utf-8');
|
|
34
|
-
analyzePatch(patchUrl, additionalFile, entry.XADFileName, patchText);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
} else {
|
|
38
|
-
analyzePatch(patchUrl, file, null, patchText);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function analyzePatch(patchUrl, file, subfile, patchText) {
|
|
43
|
-
let location = subfile ? `${file} -> ${subfile}` : file;
|
|
44
|
-
|
|
45
|
-
let vkp = vkpParse(patchText, {
|
|
46
|
-
allowEmptyOldData: true,
|
|
47
|
-
allowPlaceholders: true,
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
if (vkp.warnings.length || vkp.errors.length) {
|
|
51
|
-
console.log(`[${location}](${patchUrl})`);
|
|
52
|
-
|
|
53
|
-
for (let warn of vkp.warnings) {
|
|
54
|
-
console.log(`Warning: ${warn.message}`);
|
|
55
|
-
console.log("```");
|
|
56
|
-
console.log(warn.codeFrame(patchText));
|
|
57
|
-
console.log("```");
|
|
58
|
-
console.log("");
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
for (let err of vkp.errors) {
|
|
62
|
-
console.log(`Error: ${err.message}`);
|
|
63
|
-
console.log("```");
|
|
64
|
-
console.log(err.codeFrame(patchText));
|
|
65
|
-
console.log("```");
|
|
66
|
-
console.log("");
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
console.log("");
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function readFiles(dir, base, files) {
|
|
74
|
-
base = base || "";
|
|
75
|
-
files = files || [];
|
|
76
|
-
fs.readdirSync(dir, {withFileTypes: true}).forEach((entry) => {
|
|
77
|
-
if (entry.isDirectory()) {
|
|
78
|
-
readFiles(dir + "/" + entry.name, base + entry.name + "/", files);
|
|
79
|
-
} else {
|
|
80
|
-
files.push(base + entry.name);
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
return files;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
async function getFilesFromArchive(file) {
|
|
87
|
-
return new Promise((resolve, reject) => {
|
|
88
|
-
let proc = child_process.spawn("lsar", ["-j", file], { encoding: 'utf-8' });
|
|
89
|
-
let json = "";
|
|
90
|
-
proc.stdout.on('data', (chunk) => json += chunk);
|
|
91
|
-
proc.on('error', (e) => reject(e));
|
|
92
|
-
proc.on('close', (status) => {
|
|
93
|
-
try {
|
|
94
|
-
if (status != 0)
|
|
95
|
-
throw new Error(`Invalid archive [status=${status}]: ${file}`);
|
|
96
|
-
resolve(JSON.parse(json));
|
|
97
|
-
} catch (e) {
|
|
98
|
-
reject(e);
|
|
99
|
-
}
|
|
100
|
-
proc = json = null;
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function extractFileFromArchive(file, index) {
|
|
106
|
-
return new Promise((resolve, reject) => {
|
|
107
|
-
let proc = child_process.spawn("unar", ["-i", "-o", "-", file, index]);
|
|
108
|
-
let buffer = [];
|
|
109
|
-
proc.stdout.on('data', (chunk) => buffer.push(chunk));
|
|
110
|
-
proc.on('error', (e) => reject(e));
|
|
111
|
-
proc.on('close', (status) => {
|
|
112
|
-
try {
|
|
113
|
-
if (status != 0)
|
|
114
|
-
throw new Error(`Invalid archive [status=${status}]: ${file}`);
|
|
115
|
-
resolve(Buffer.concat(buffer));
|
|
116
|
-
} catch (e) {
|
|
117
|
-
reject(e);
|
|
118
|
-
}
|
|
119
|
-
proc = buffer = null;
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
|
-
}
|
package/jest.config.js
DELETED
package/src/VkpParseError.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
export class VkpParseError extends Error {
|
|
2
|
-
loc;
|
|
3
|
-
hint;
|
|
4
|
-
constructor(message, loc, hint) {
|
|
5
|
-
super(`${message} at line ${loc.line} col ${loc.column}${hint ? "\n" + hint : ""}`);
|
|
6
|
-
this.name = "VkpParseError";
|
|
7
|
-
this.loc = loc;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
codeFrame(text) {
|
|
11
|
-
return codeFrame(text, this.loc.line, this.loc.column);
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export function codeFrame(text, lineNum, colNum) {
|
|
16
|
-
let lines = text.split(/\r\n|\n/);
|
|
17
|
-
let maxLineNumLen = lines.length.toString().length + 1;
|
|
18
|
-
let out = "";
|
|
19
|
-
let n = 1;
|
|
20
|
-
for (let line of lines) {
|
|
21
|
-
if (Math.abs(n - lineNum) > 3) {
|
|
22
|
-
n++;
|
|
23
|
-
continue;
|
|
24
|
-
}
|
|
25
|
-
out += `${n == lineNum ? '>' : ' '}${n.toString().padStart(maxLineNumLen, ' ')} | ${tab2spaces(line)}\n`;
|
|
26
|
-
if (n == lineNum) {
|
|
27
|
-
out += ` ${" ".repeat(maxLineNumLen, ' ')} | ${str2spaces(line.substr(0, colNum - 1))}^\n`;
|
|
28
|
-
}
|
|
29
|
-
n++;
|
|
30
|
-
}
|
|
31
|
-
return out;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function getLocByOffset(text, offset) {
|
|
35
|
-
let line = 0;
|
|
36
|
-
let column = 1;
|
|
37
|
-
for (let i = 0; i < text.length; i++) {
|
|
38
|
-
let c = text.charAt(i);
|
|
39
|
-
if (c == "\n" || (c == "\r" && text.charAt(i + 1) == "\n")) {
|
|
40
|
-
column = 1;
|
|
41
|
-
line++;
|
|
42
|
-
}
|
|
43
|
-
if (i == offset)
|
|
44
|
-
return { line, column };
|
|
45
|
-
}
|
|
46
|
-
return { line, column: 1 };
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function str2spaces(line) {
|
|
50
|
-
return tab2spaces(line).replace(/./g, ' ');
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function tab2spaces(line) {
|
|
54
|
-
let newStr = "";
|
|
55
|
-
let virtualSymbols = 0;
|
|
56
|
-
for (let i = 0; i < line.length; i++) {
|
|
57
|
-
let c = line.charAt(i);
|
|
58
|
-
if (c == "\t") {
|
|
59
|
-
let spacesCnt = 4 - virtualSymbols % 4;
|
|
60
|
-
newStr += " ".repeat(spacesCnt);
|
|
61
|
-
virtualSymbols += spacesCnt;
|
|
62
|
-
} else {
|
|
63
|
-
virtualSymbols++;
|
|
64
|
-
newStr += c;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return newStr;
|
|
68
|
-
}
|
package/src/index.js
DELETED
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
import iconv from 'iconv-lite';
|
|
2
|
-
import { vkpRawParser } from './parser.js';
|
|
3
|
-
import { VkpParseError } from './VkpParseError.js';
|
|
4
|
-
import RTFParser from 'rtf-parser';
|
|
5
|
-
|
|
6
|
-
const DEFAULT_PRAGMAS = {
|
|
7
|
-
warn_no_old_on_apply: true,
|
|
8
|
-
warn_if_new_exist_on_apply: true,
|
|
9
|
-
warn_if_old_exist_on_undo: true,
|
|
10
|
-
undo: true,
|
|
11
|
-
old_equal_ff: false,
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
function vkpParse(text, options) {
|
|
15
|
-
options = {
|
|
16
|
-
allowEmptyOldData: false,
|
|
17
|
-
allowPlaceholders: false,
|
|
18
|
-
...options
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
let vkp = {
|
|
22
|
-
ast: null,
|
|
23
|
-
valid: false,
|
|
24
|
-
writes: [],
|
|
25
|
-
warnings: [],
|
|
26
|
-
errors: [],
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
let pragmas = {...DEFAULT_PRAGMAS};
|
|
30
|
-
let pragma2loc = {};
|
|
31
|
-
let offsetCorrector;
|
|
32
|
-
|
|
33
|
-
vkpRawParser(text, {
|
|
34
|
-
onPragma(value, loc) {
|
|
35
|
-
if (value.pragma.action == "enable") {
|
|
36
|
-
if (pragmas[value.pragma.name]) {
|
|
37
|
-
vkp.warnings.push(new VkpParseError(`Useless "#pragma ${value.pragma.action} ${value.pragma.name}" has no effect`, loc, `You can safely remove this line.`));
|
|
38
|
-
} else {
|
|
39
|
-
pragmas[value.pragma.name] = true;
|
|
40
|
-
pragma2loc[value.pragma.name] = loc;
|
|
41
|
-
}
|
|
42
|
-
} else if (value.pragma.action == "disable") {
|
|
43
|
-
if (!pragmas[value.pragma.name]) {
|
|
44
|
-
vkp.warnings.push(new VkpParseError(`Useless "#pragma ${value.pragma.action} ${value.pragma.name}" has no effect`, loc, `You can safely remove this line.`));
|
|
45
|
-
} else {
|
|
46
|
-
pragma2loc[value.pragma.name] = loc;
|
|
47
|
-
pragmas[value.pragma.name] = false;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
},
|
|
51
|
-
onPatchData(data, loc) {
|
|
52
|
-
let oldData = data.old ? data.old.buffer : null;
|
|
53
|
-
let newData = data.new.buffer;
|
|
54
|
-
|
|
55
|
-
if (data.new.placeholders > 0) {
|
|
56
|
-
if (!options.allowPlaceholders)
|
|
57
|
-
vkp.errors.push(new VkpParseError(`Found placeholder instead of real patch data`, data.new.loc));
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (pragmas.old_equal_ff && !oldData)
|
|
61
|
-
oldData = Buffer.alloc(newData.length).fill(0xFF);
|
|
62
|
-
|
|
63
|
-
if (oldData && oldData.length < newData.length)
|
|
64
|
-
vkp.errors.push(new VkpParseError(`Old data (${oldData.length} bytes) is less than new data (${newData.length} bytes)`, data.old.loc));
|
|
65
|
-
|
|
66
|
-
if (pragmas.warn_no_old_on_apply && !oldData) {
|
|
67
|
-
if (!options.allowEmptyOldData)
|
|
68
|
-
vkp.warnings.push(new VkpParseError(`Old data is not specified`, data.new.loc, `Undo operation is impossible!`));
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
vkp.writes.push({
|
|
72
|
-
addr: (offsetCorrector ? offsetCorrector.value : 0) + data.address,
|
|
73
|
-
size: newData.length,
|
|
74
|
-
old: oldData,
|
|
75
|
-
new: newData,
|
|
76
|
-
loc,
|
|
77
|
-
pragmas: {...pragmas}
|
|
78
|
-
});
|
|
79
|
-
},
|
|
80
|
-
onOffset(value, loc) {
|
|
81
|
-
offsetCorrector = { value: value.offset, text: value.text, loc };
|
|
82
|
-
},
|
|
83
|
-
onWarning(e) {
|
|
84
|
-
vkp.warnings.push(e);
|
|
85
|
-
},
|
|
86
|
-
onError(e) {
|
|
87
|
-
vkp.errors.push(e);
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
let unsinishedPragmas = [];
|
|
92
|
-
for (let k in pragmas) {
|
|
93
|
-
if (pragmas[k] !== DEFAULT_PRAGMAS[k]) {
|
|
94
|
-
let cancel = pragmas[k] ? `#pragma disable ${k}` : `#pragma enable ${k}`;
|
|
95
|
-
vkp.warnings.push(new VkpParseError(`Uncanceled pragma "${k}"`, pragma2loc[k], `Please put "${cancel}" at the end of the patch.`));
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (offsetCorrector && offsetCorrector.value != 0)
|
|
100
|
-
vkp.warnings.push(new VkpParseError(`Uncanceled offset ${offsetCorrector.text}`, offsetCorrector.loc, `Please put "+0" at the end of the patch.`));
|
|
101
|
-
|
|
102
|
-
vkp.valid = (vkp.errors.length == 0);
|
|
103
|
-
|
|
104
|
-
return vkp;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function vkpDetectContent(text) {
|
|
108
|
-
if (text.indexOf('{\\rtf1') >= 0)
|
|
109
|
-
return "RTF";
|
|
110
|
-
let trimmedText = text.replace(/\/\*.*?\*\//gs, '').replace(/(\/\/|;|#).*?$/mg, '');
|
|
111
|
-
if (trimmedText.match(/^\s*(0x[a-f0-9]+|[a-f0-9]+)\s*:[^\\/]/mi))
|
|
112
|
-
return "PATCH";
|
|
113
|
-
if (text.match(/;!(к патчу прикреплён файл|There is a file attached to this patch), https?:\/\//i))
|
|
114
|
-
return "DOWNLOAD_STUB";
|
|
115
|
-
if (!trimmedText.trim().length)
|
|
116
|
-
return "EMPTY";
|
|
117
|
-
return "UNKNOWN";
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// CP1251 -> UTF-8 + CRLF -> LF (with RTF support)
|
|
121
|
-
async function vkpNormalizeWithRTF(text) {
|
|
122
|
-
if (!Buffer.isBuffer(text))
|
|
123
|
-
throw new Error(`Patch text is not Buffer!`);
|
|
124
|
-
|
|
125
|
-
if (text.indexOf('{\\rtf1') >= 0) {
|
|
126
|
-
// Strip RTF images
|
|
127
|
-
while (true) {
|
|
128
|
-
let pictureIndex = text.indexOf('{\\pict');
|
|
129
|
-
if (pictureIndex >= 0) {
|
|
130
|
-
let pictureEndIndex = text.indexOf('}', pictureIndex);
|
|
131
|
-
if (pictureIndex >= 0) {
|
|
132
|
-
text = Buffer.concat([ text.slice(0, pictureIndex), text.slice(pictureEndIndex + 1) ]);
|
|
133
|
-
continue;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
break;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
text = text.toString('utf-8').replace(/{\\pict.*?\}/gsi, ''); // remove pictures
|
|
140
|
-
let parsed = await new Promise((resolve, reject) => {
|
|
141
|
-
RTFParser.string(text, (err, doc) => {
|
|
142
|
-
if (err) {
|
|
143
|
-
reject(err);
|
|
144
|
-
} else {
|
|
145
|
-
resolve(doc);
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
let lines = [];
|
|
151
|
-
for (let p of parsed.content) {
|
|
152
|
-
lines.push(p.content.map((s) => s.value).join(''));
|
|
153
|
-
}
|
|
154
|
-
return lines.join('\n');
|
|
155
|
-
}
|
|
156
|
-
return iconv.decode(text, 'windows-1251').replace(/(\r\n|\n|\r)/g, "\n");
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// CP1251 -> UTF-8 + CRLF -> LF
|
|
160
|
-
function vkpNormalize(text) {
|
|
161
|
-
if (!Buffer.isBuffer(text))
|
|
162
|
-
throw new Error(`Patch text is not Buffer!`);
|
|
163
|
-
return iconv.decode(text, 'windows-1251').replace(/(\r\n|\n|\r)/g, "\n");
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// UTF-8 -> CP1251 + LF -> CRLF
|
|
167
|
-
function vkpCanonicalize(text) {
|
|
168
|
-
return iconv.encode(text.replace(/(\r\n|\n|\r)/g, "\r\n"), 'windows-1251');
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
export { vkpParse, vkpRawParser, vkpNormalize, vkpNormalizeWithRTF, vkpCanonicalize, vkpDetectContent, VkpParseError };
|