|
| 1 | +const { dialog, BrowserWindow, app } = require("electron"); |
| 2 | +const path = require("path"); |
| 3 | +const util = require("util"); |
| 4 | +const execFile = util.promisify(require("child_process").execFile); |
| 5 | + |
| 6 | +const Promise = require("bluebird"); |
| 7 | +const fs = Promise.promisifyAll(require("fs-extra")); |
| 8 | + |
| 9 | +let loadedFilePath = null; |
| 10 | +let images = []; |
| 11 | + |
| 12 | +const getResourcePath = () => { |
| 13 | + if (app.isPackaged) { |
| 14 | + return process.resourcesPath; |
| 15 | + } else { |
| 16 | + return "."; |
| 17 | + } |
| 18 | +}; |
| 19 | + |
| 20 | +const getFiles = async (path = "./") => { |
| 21 | + const entries = await fs.readdir(path, { withFileTypes: true }); |
| 22 | + const files = entries |
| 23 | + .filter((file) => !file.isDirectory()) |
| 24 | + .map((file) => ({ ...file, path: path + file.name })); |
| 25 | + |
| 26 | + const folders = entries.filter((folder) => folder.isDirectory()); |
| 27 | + for (const folder of folders) { |
| 28 | + files.push(...(await getFiles(`${path}/${folder.name}/`))); |
| 29 | + } |
| 30 | + return files; |
| 31 | +}; |
| 32 | + |
| 33 | +const loadRcc = async (filePath) => { |
| 34 | + const localPath = `${path.resolve(getResourcePath(), "rcc")}`; |
| 35 | + |
| 36 | + // clear previous images |
| 37 | + images = []; |
| 38 | + |
| 39 | + // delete res directory |
| 40 | + try { |
| 41 | + await fs.rmdir(`${localPath}/qresource`, { recursive: true }); |
| 42 | + } catch {} |
| 43 | + |
| 44 | + await fs.copyFile(filePath, `${localPath}/res.rcc`); |
| 45 | + |
| 46 | + const result = await execFile(`${localPath}/rcc.exe`, ["--reverse"], { |
| 47 | + cwd: `${localPath}/`, |
| 48 | + }); |
| 49 | + |
| 50 | + // get directory content |
| 51 | + const files = await getFiles(`${localPath}/qresource/res/res.rcc`); |
| 52 | + for (const file of files) { |
| 53 | + const ext = path.extname(file.path); |
| 54 | + images.push({ |
| 55 | + name: path.parse(file.name).name, |
| 56 | + path: path.relative(`${localPath}/qresource/res/res.rcc`, file.path), |
| 57 | + isImage: ext === ".png" || ext === ".jpg", |
| 58 | + data: Buffer.from(await fs.readFile(file.path, "binary"), "binary"), |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + // sort by name |
| 63 | + images.sort((a, b) => a.name.localeCompare(b.name)); |
| 64 | + |
| 65 | + // cleanup |
| 66 | + await fs.rmdir(`${localPath}/qresource`, { recursive: true }); |
| 67 | + await fs.rm(`${localPath}/res.rcc`); |
| 68 | + |
| 69 | + loadedFilePath = filePath; |
| 70 | + |
| 71 | + BrowserWindow.getAllWindows()[0].webContents.send("populate-list", images); |
| 72 | +}; |
| 73 | + |
| 74 | +const extractToPng = async (directoryPath) => { |
| 75 | + if (images.length === 0) { |
| 76 | + dialog.showErrorBox("Error", "Nothing to extract."); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + for (const image of images) { |
| 81 | + if (image.isImage) { |
| 82 | + await fs.outputFileAsync(`${directoryPath}/${image.path}`, image.data); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + dialog.showMessageBox(null, { |
| 87 | + message: `Png images extracted successfully. Extracted ${images.length} images.`, |
| 88 | + type: "info", |
| 89 | + }); |
| 90 | +}; |
| 91 | + |
| 92 | +const saveRcc = async (filePath) => { |
| 93 | + if (images.length === 0) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + filePath = filePath || loadedFilePath; |
| 98 | + |
| 99 | + const localPath = `${path.resolve(getResourcePath(), "rcc")}`; |
| 100 | + |
| 101 | + // create .qrc file |
| 102 | + let data = `<!DOCTYPE RCC><RCC version="1.0">\n<qresource>\n`; |
| 103 | + |
| 104 | + for (const image of images) { |
| 105 | + data += `<file>${image.path}</file>\n`; |
| 106 | + } |
| 107 | + |
| 108 | + data += `</qresource>\n</RCC>`; |
| 109 | + |
| 110 | + await fs.outputFileAsync(`${localPath}/res/res.qrc`, data); |
| 111 | + |
| 112 | + // dump images |
| 113 | + for (const image of images) { |
| 114 | + await fs.outputFileAsync(`${localPath}/res/${image.path}`, image.data); |
| 115 | + } |
| 116 | + |
| 117 | + const result = await execFile( |
| 118 | + `${localPath}/rcc.exe`, |
| 119 | + [ |
| 120 | + "--format-version", |
| 121 | + "1", |
| 122 | + "--binary", |
| 123 | + `./res/res.qrc`, |
| 124 | + "-o", |
| 125 | + "./res/res_output.rcc", |
| 126 | + ], |
| 127 | + { |
| 128 | + cwd: `${localPath}/`, |
| 129 | + } |
| 130 | + ); |
| 131 | + |
| 132 | + await fs.move("./rcc/res/res_output.rcc", `${filePath}`, { overwrite: true }); |
| 133 | + |
| 134 | + // cleanup |
| 135 | + await fs.rmdir(`${localPath}/res`, { recursive: true }); |
| 136 | + |
| 137 | + dialog.showMessageBox(null, { |
| 138 | + message: `Rcc saved successfully.`, |
| 139 | + type: "info", |
| 140 | + }); |
| 141 | +}; |
| 142 | + |
| 143 | +const replaceImage = async (index, filePath) => { |
| 144 | + const image = images[index]; |
| 145 | + if (!image) { |
| 146 | + return null; |
| 147 | + } |
| 148 | + |
| 149 | + image.data = Buffer.from(await fs.readFile(filePath, "binary"), "binary"); |
| 150 | + return image.data; |
| 151 | +}; |
| 152 | + |
| 153 | +const getImageByIndex = (index) => { |
| 154 | + return images[index]?.data; |
| 155 | +}; |
| 156 | + |
| 157 | +module.exports = { |
| 158 | + loadRcc, |
| 159 | + saveRcc, |
| 160 | + replaceImage, |
| 161 | + getImageByIndex, |
| 162 | + extractToPng, |
| 163 | +}; |
0 commit comments