@sie-js/vkp 1.0.0 → 1.0.2

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.
@@ -0,0 +1,122 @@
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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sie-js/vkp",
3
- "version": "1.0.0",
4
- "description": "",
3
+ "version": "1.0.2",
4
+ "description": "Parser and utils for the .VKP files format which is used in the V-Klay.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
7
7
  "scripts": {
@@ -18,5 +18,8 @@
18
18
  "devDependencies": {
19
19
  "glob": "^10.3.12",
20
20
  "jest": "^29.7.0"
21
+ },
22
+ "repository": {
23
+ "url": "git+https://github.com/siemens-mobile-hacks/node-sie-vkp.git"
21
24
  }
22
25
  }
package/src/index.js CHANGED
@@ -59,7 +59,7 @@ function vkpParse(text, options) {
59
59
  if (pragmas.old_equal_ff && !oldData)
60
60
  oldData = Buffer.alloc(newData.length).fill(0xFF);
61
61
 
62
- if (oldData&& oldData.length < newData.length)
62
+ if (oldData && oldData.length < newData.length)
63
63
  vkp.errors.push(new VkpParseError(`Old data (${oldData.length} bytes) is less than new data (${newData.length} bytes)`, data.old.loc));
64
64
 
65
65
  if (pragmas.warn_no_old_on_apply && !oldData) {
@@ -107,10 +107,10 @@ function vkpDetectContent(text) {
107
107
  let trimmedText = text.replace(/\/\*.*?\*\//gs, '').replace(/(\/\/|;|#).*?$/mg, '');
108
108
  if (trimmedText.match(/^\s*(0x[a-f0-9]+|[a-f0-9]+)\s*:[^\\/]/mi))
109
109
  return "PATCH";
110
+ if (text.match(/;!(к патчу прикреплён файл|There is a file attached to this patch), https?:\/\//i))
111
+ return "DOWNLOAD_STUB";
110
112
  if (!trimmedText.trim().length)
111
113
  return "EMPTY";
112
- if (text.match(/;!к патчу прикреплён файл, https?:\/\//i))
113
- return "DOWNLOAD_STUB";
114
114
  return "UNKNOWN";
115
115
  }
116
116