Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=============================================================================
- // RPG Maker MZ - Moar maps helper
- // Last Updated: 2020.08.26
- //=============================================================================
- // Copyright 2020 Zeriab
- //
- // This software is provided 'as-is', without any express or implied
- // warranty. In no event will the authors be held liable for any damages
- // arising from the use of this software.
- //
- // Permission is granted to anyone to use this software for any purpose,
- // including commercial applications, and to alter it and redistribute it
- // freely, subject to the following restrictions:
- //
- // 1. The origin of this software must not be misrepresented; you must not
- // claim that you wrote the original software. If you use this software
- // in a product, an acknowledgement in the product documentation would be
- // appreciated but is not required.
- // 2. Altered source versions must be plainly marked as such, and must not be
- // misrepresented as being the original software.
- // 3. This notice may not be removed or altered from any source distribution.
- //=============================================================================
- /*:
- * @target MZ
- * @plugindesc Compresses subfolder maps
- * @author Zeriab
- *
- * @help Zeriab_MoarMapsHelper.js
- *
- * This developer plugin does the following:
- * 1. Traverses all subfolders in the 'data' folder for any file of the
- * MapXXX.json and MapInfos.json name
- * 2. All the discovered map files are compressed. Original maps are preserved.
- * I.e. 'data/church/Map002.json' => 'data/church/Map002.json.pak'
- * 3. Sets its status in the plugin manager to Off
- *
- * Note: This plugin assumes it runs locally.
- *
- * @param CompressedFolder
- * @type string
- * @default pak
- * @text Output folder in the 'data' folder
- * @desc Please ensure this folder is the same as the one specified in the Zeriab_MoarMaps plugin
- */
- (() => {
- 'use strict';
- const pluginName = "Zeriab_MoarMapsHelper";
- const parameters = PluginManager.parameters(pluginName);
- const compressedFolder = parameters.CompressedFolder || 'pak';
- class MapCompresser {
- constructor() {
- this.fs = require('fs');
- this.path = require('path');
- this.pluginsConfig = 'js/plugins.js';
- this.regex = /(Map\d+.json)|(MapInfos.json)/i;
- this.extension = ".pak";
- }
- run() {
- console.log("Running...")
- this.findMapFilesStart('data');
- const message = "Compression completed. Please disable the Zeriab_MoarMapsHelper in the plugin manager.";
- main.printError("", message);
- }
- getDirectories(path) {
- // From https://stackoverflow.com/questions/18112204/get-all-directories-within-directory-nodejs
- return this.fs.readdirSync(path, { withFileTypes: true })
- .filter(dirent => dirent.isDirectory())
- .map(dirent => dirent.name);
- }
- isMapFile(name) {
- return name.toUpperCase().endsWith('JSON') && name.match(this.regex) !== null;
- }
- getMapFiles(path) {
- return this.fs.readdirSync(path, { withFileTypes: true })
- .filter(dirent => dirent.isFile())
- .map(dirent => dirent.name)
- .filter(name => this.isMapFile(name));
- }
- findMapFilesStart(sourcePath) {
- const outputPath = this.path.join(sourcePath, compressedFolder);
- this.findMapFiles(sourcePath, outputPath);
- }
- findMapFiles(path, outputPath) {
- const directories = this.getDirectories(path);
- for (const directory of directories) {
- // Ignore special compressed folder
- if (directory === compressedFolder) {
- continue;
- }
- const subfolder = this.path.join(path, directory)
- const outputSubfolder = this.path.join(outputPath, directory)
- const mapFiles = this.getMapFiles(subfolder);
- this.compressSubfolder(subfolder, mapFiles, outputSubfolder);
- // Handle nested folders
- this.findMapFiles(subfolder, outputSubfolder);
- }
- }
- compressSubfolder(subfolder, mapNames, outputSubfolder) {
- if (!this.fs.existsSync(outputSubfolder)) {
- this.fs.mkdirSync(outputSubfolder, {recursive: true});
- }
- for (const mapName of mapNames) {
- const sourcePath = this.path.join(subfolder, mapName);
- const outputPath = this.path.join(outputSubfolder, mapName) + this.extension;
- this.compressFile(sourcePath, outputPath);
- }
- }
- compressFile(sourcePath, outputPath) {
- const json = this.fs.readFileSync(sourcePath);
- const zip = pako.deflate(json, { to: "string", level: 1 });
- this.fs.writeFileSync(outputPath, zip);
- }
- }
- const compressor = new MapCompresser();
- compressor.run();
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement