forked from Maddoxkkm/dvpl_converter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dvpl.js
95 lines (88 loc) · 3.66 KB
/
dvpl.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
console.log(`# DVPL Converter 2.0 for Windows `)
console.log(`# Modified for the ForBlitz Team and Mod Creators! `)
console.log(`# RifsXD - Fuck you motherfucker =) `)
console.log(` `)
// Loads everything in
const fs = require('fs').promises
const dvpl = require('./dvpl_logic.js');
const path = require('path');
// Scrappy cli code
const realArgs = process.argv.slice(2);
if (realArgs.length === 0) {
throw '# No Mode selected. Try dvpl --help for advice.'
}
const optionalArgs = realArgs.slice(1);
let keeporingals = false;
let convertFiles = false; // FLAGS
optionalArgs.forEach(arg => {
if (arg.toLowerCase() === '-ko') keeporingals = true;
if (arg.toLowerCase() === '-f') convertFiles = true;
})
switch (realArgs[0].toLowerCase()) {
case '-compress':
case '-c':
case '-с':
case 'compress':
case 'c':
case 'с':
// compress
dvplRecursion(process.cwd(), keeporingals, true).then(number => {
console.log(` `)
console.log(`# Compression is complete. Compressed: ${number} files.`)
})
break;
case '-decompress':
case '-d':
case '-в':
case 'decompress':
case 'd':
case 'в':
dvplRecursion(process.cwd(), keeporingals, false).then(number => {
console.log(` `)
console.log(`# Unpacking is complete. Unpacked: ${number} files.`)
})
// decompress
break;
case '--help':
case '-help':
case '-h':
case 'help':
case 'h':
console.log(`
# dvpl [mode] [--keep-originals]
# -compress (c, c) : Compresses files into dvpl.
# -decompress (в, d) : Decompresses dvpl files into standard files.
# -help (h) : Outputs an auxiliary message.
# -ko (-ko) : Saves the original file.
`)
break;
default:
console.error('Incorrect mode selected. Use -help for information.')
}
// main code that does all the heavy lifting
async function dvplRecursion(originalsDir, keepOrignals = false, compression = false) {
const dirList = await fs.readdir(originalsDir + "/", { withFileTypes: true });
return await (dirList.map(async (dirItem) => {
const isDecompression = !compression && dirItem.isFile() && dirItem.name.endsWith('.dvpl')
const isCompression = compression && dirItem.isFile() && !dirItem.name.endsWith('.dvpl')
if (dirItem.isDirectory()) {
return await dvplRecursion(originalsDir + "/" + dirItem.name, keepOrignals, compression)
}
else if (isDecompression || isCompression) {
try {
const fileData = await fs.readFile(originalsDir + "/" + dirItem.name)
const processedBlock = isCompression ? dvpl.compressDVPl(fileData) : dvpl.decompressDVPL(fileData);
const newName = path.basename(dirItem.name, '.dvpl')
await fs.writeFile(originalsDir + "/" + (isCompression ? dirItem.name + ".dvpl" : newName), processedBlock)
console.log(`File ${originalsDir + "/" + dirItem.name} has been successfully ${isCompression ? "compressed" : "decompressed"} into ${originalsDir + "/" + isCompression ? newName : dirItem.name + ".dvpl"}`)
keepOrignals ? undefined : await fs.unlink(originalsDir + "/" + dirItem.name);
return 1
} catch (err) {
console.log(`File ${originalsDir + "/" + dirItem.name} Failed to convert due to ${err}`)
return 0
}
} else {
return 0
}
})).reduce(async (a, b) => await a + await b, Promise.resolve(0))
}