Skip to content

Instantly share code, notes, and snippets.

@MarvNC
Last active March 21, 2024 06:06
Show Gist options
  • Save MarvNC/69968757ae8f1cc8808fab882861d66d to your computer and use it in GitHub Desktop.
Save MarvNC/69968757ae8f1cc8808fab882861d66d to your computer and use it in GitHub Desktop.

Revisions

  1. MarvNC revised this gist Mar 21, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion _readme.md
    Original file line number Diff line number Diff line change
    @@ -4,4 +4,4 @@
    - Run `dump.js` in the `script` folder
    - Scripts will be dumped in `script/out`

    **[Download scripts zip](https://gist.github.com/MarvNC/69968757ae8f1cc8808fab882861d66d/raw/6d2a6296775ef8a619f4d33cfd3338c83ef004cc/scripts.zip)**
    **[Download scripts zip](https://gist.github.com/MarvNC/69968757ae8f1cc8808fab882861d66d/raw/scripts.zip)**
  2. MarvNC revised this gist Mar 21, 2024. 1 changed file with 0 additions and 0 deletions.
    Binary file modified scripts.zip
    Binary file not shown.
  3. MarvNC revised this gist Mar 21, 2024. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion _readme.md
    Original file line number Diff line number Diff line change
    @@ -2,4 +2,6 @@

    - Use asar to extract files in `resources`
    - Run `dump.js` in the `script` folder
    - Scripts will be dumped in `script/out`
    - Scripts will be dumped in `script/out`

    **[Download scripts zip](https://gist.github.com/MarvNC/69968757ae8f1cc8808fab882861d66d/raw/6d2a6296775ef8a619f4d33cfd3338c83ef004cc/scripts.zip)**
  4. MarvNC revised this gist Mar 21, 2024. 1 changed file with 0 additions and 0 deletions.
    Binary file added scripts.zip
    Binary file not shown.
  5. MarvNC created this gist Mar 21, 2024.
    5 changes: 5 additions & 0 deletions _readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    # For 寄甡 (Symbiotic Love)

    - Use asar to extract files in `resources`
    - Run `dump.js` in the `script` folder
    - Scripts will be dumped in `script/out`
    72 changes: 72 additions & 0 deletions dump.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    const fs = require('fs').promises;
    const path = require('path');
    const outDir = './out';

    async function readAndParseJSON() {
    try {
    const files = await fs.readdir('./');

    for (const file of files) {
    const data = await fs.readFile(file, 'utf8');

    let jsonData;
    try {
    jsonData = JSON.parse(data);
    // console.log(`JSON data from ${file}:`, jsonData);
    } catch (error) {
    // console.error(`Error parsing JSON from ${file}:`, error);
    }

    const script = readPossibleScriptJSON(jsonData);
    // If script exists, write it as a txt file
    if (script) {
    let filename = `${script.id}${script.title ? `-${script.title}` : ''}`;
    // Append length to filename
    filename += `-${script.lines.length}`;
    // Replace invalid characters
    filename = filename.replace(/[\\/:*?"<>|]/g, '');
    // Create outDir if it doesn't exist
    try {
    await fs.access(outDir);
    } catch (error) {
    await fs.mkdir(outDir);
    }
    const filepath = path.join(outDir, `${filename}.txt`);
    await fs.writeFile(filepath, script.lines, 'utf8');
    console.log(`Script written to filepath: ${filepath}`);
    }
    }
    } catch (err) {
    console.error('Error reading directory:', err);
    }
    }

    readAndParseJSON();

    /**
    * Check if it's a script, return script text if so
    * @param {*} data
    */
    function readPossibleScriptJSON(data) {
    const lines = [];
    // Check if data is array
    if (!Array.isArray(data)) {
    return null;
    }
    const id = data[0][3].id;
    const title = data[0][3].title;
    for (const line of data) {
    if (line && line[3] && line[3].text) {
    lines.push(line[3].text);
    }
    }
    if (lines.length > 0) {
    return {
    id,
    title,
    lines: lines.join('\n'),
    };
    } else {
    return null;
    }
    }