Advertisement
Zeriab

[RMMZ] MoarMaps helper

Aug 26th, 2020
1,109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // RPG Maker MZ - Moar maps helper
  3. // Last Updated: 2020.08.26
  4. //=============================================================================
  5. // Copyright 2020 Zeriab
  6. //
  7. // This software is provided 'as-is', without any express or implied
  8. // warranty. In no event will the authors be held liable for any damages
  9. // arising from the use of this software.
  10. //
  11. // Permission is granted to anyone to use this software for any purpose,
  12. // including commercial applications, and to alter it and redistribute it
  13. // freely, subject to the following restrictions:
  14. //
  15. // 1. The origin of this software must not be misrepresented; you must not
  16. //    claim that you wrote the original software. If you use this software
  17. //    in a product, an acknowledgement in the product documentation would be
  18. //    appreciated but is not required.
  19. // 2. Altered source versions must be plainly marked as such, and must not be
  20. //    misrepresented as being the original software.
  21. // 3. This notice may not be removed or altered from any source distribution.
  22. //=============================================================================
  23.  
  24. /*:
  25.  * @target MZ
  26.  * @plugindesc Compresses subfolder maps
  27.  * @author Zeriab
  28.  *
  29.  * @help Zeriab_MoarMapsHelper.js
  30.  *
  31.  * This developer plugin does the following:
  32.  *  1. Traverses all subfolders in the 'data' folder for any file of the
  33.  *     MapXXX.json and MapInfos.json name
  34.  *  2. All the discovered map files are compressed. Original maps are preserved.
  35.  *     I.e. 'data/church/Map002.json' => 'data/church/Map002.json.pak'
  36.  *  3. Sets its status in the plugin manager to Off
  37.  *
  38.  * Note: This plugin assumes it runs locally.
  39.  *
  40.  * @param CompressedFolder
  41.  * @type string
  42.  * @default pak
  43.  * @text Output folder in the 'data' folder
  44.  * @desc Please ensure this folder is the same as the one specified in the Zeriab_MoarMaps plugin
  45.  */
  46.  
  47. (() => {
  48.     'use strict';
  49.     const pluginName = "Zeriab_MoarMapsHelper";
  50.     const parameters = PluginManager.parameters(pluginName);
  51.     const compressedFolder = parameters.CompressedFolder || 'pak';
  52.  
  53.     class MapCompresser {
  54.         constructor() {
  55.             this.fs = require('fs');
  56.             this.path = require('path');
  57.             this.pluginsConfig = 'js/plugins.js';
  58.             this.regex = /(Map\d+.json)|(MapInfos.json)/i;
  59.             this.extension = ".pak";
  60.         }
  61.        
  62.         run() {
  63.             console.log("Running...")
  64.             this.findMapFilesStart('data');
  65.             const message = "Compression completed. Please disable the Zeriab_MoarMapsHelper in the plugin manager.";
  66.             main.printError("", message);
  67.         }
  68.  
  69.         getDirectories(path) {
  70.             // From https://stackoverflow.com/questions/18112204/get-all-directories-within-directory-nodejs
  71.             return this.fs.readdirSync(path, { withFileTypes: true })
  72.                 .filter(dirent => dirent.isDirectory())
  73.                 .map(dirent => dirent.name);
  74.         }
  75.  
  76.         isMapFile(name) {
  77.             return name.toUpperCase().endsWith('JSON') && name.match(this.regex) !== null;
  78.         }
  79.  
  80.         getMapFiles(path) {
  81.             return this.fs.readdirSync(path, { withFileTypes: true })
  82.                 .filter(dirent => dirent.isFile())
  83.                 .map(dirent => dirent.name)
  84.                 .filter(name => this.isMapFile(name));
  85.         }
  86.  
  87.         findMapFilesStart(sourcePath) {
  88.             const outputPath = this.path.join(sourcePath, compressedFolder);
  89.             this.findMapFiles(sourcePath, outputPath);
  90.         }
  91.  
  92.         findMapFiles(path, outputPath) {
  93.             const directories = this.getDirectories(path);
  94.             for (const directory of directories) {
  95.                 // Ignore special compressed folder
  96.                 if (directory === compressedFolder) {
  97.                     continue;
  98.                 }
  99.                 const subfolder = this.path.join(path, directory)
  100.                 const outputSubfolder = this.path.join(outputPath, directory)
  101.                 const mapFiles = this.getMapFiles(subfolder);
  102.                 this.compressSubfolder(subfolder, mapFiles, outputSubfolder);
  103.  
  104.                 // Handle nested folders
  105.                 this.findMapFiles(subfolder, outputSubfolder);
  106.             }
  107.         }
  108.  
  109.         compressSubfolder(subfolder, mapNames, outputSubfolder) {
  110.             if (!this.fs.existsSync(outputSubfolder)) {
  111.                 this.fs.mkdirSync(outputSubfolder, {recursive: true});
  112.             }
  113.             for (const mapName of mapNames) {
  114.                 const sourcePath = this.path.join(subfolder, mapName);
  115.                 const outputPath = this.path.join(outputSubfolder, mapName) + this.extension;
  116.                 this.compressFile(sourcePath, outputPath);
  117.             }
  118.         }
  119.  
  120.         compressFile(sourcePath, outputPath) {
  121.             const json = this.fs.readFileSync(sourcePath);
  122.             const zip = pako.deflate(json, { to: "string", level: 1 });
  123.             this.fs.writeFileSync(outputPath, zip);
  124.         }
  125.     }
  126.  
  127.     const compressor = new MapCompresser();
  128.     compressor.run();
  129. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement