Advertisement
Wuppertaler93

ExportMessagesToText.js

Apr 13th, 2020
762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 5.40 KB | None | 0 0
  1. /*:
  2.  * @plugindesc Exports all of the game's messages into a single txt file.
  3.  * @author Mogwai "Jake Jilg"
  4.  *
  5.  * @help
  6.  *   Mogwai "Jake Jilg" Exporter
  7.  * ----------------------------------------------------------------------------
  8.  * Export Messages to Text version 0.3
  9.  * create a folder on your main hard drive and use the game test, a popup will appear.
  10.  * Enter your folder and the file there.
  11.  * Example: "Textlog/textlog.txt"
  12.  * ----------------------------------------------------------------------------
  13.  *
  14.  */
  15. var tempTextBody = "";
  16. var mapname = [];
  17. window.addEventListener("load", function(e) {
  18.     var messages = {};
  19.     var getJSON = function(file, callback, done) {
  20.         var xmlHttp = new XMLHttpRequest();
  21.         xmlHttp.open("GET", file, false);
  22.         xmlHttp.send(null);
  23.         if (xmlHttp.responseText.length > 0)
  24.             return callback(xmlHttp.responseText);
  25.         else
  26.             done();
  27.     }
  28.     if (confirm("Would you like to export all of the game messages to a txt file?")) {
  29.         var txtname = prompt("What would you like to name the txt file?", "messages");
  30.         // collect map JSONs
  31.         var x = 0;
  32.         getJSON("data/MapInfos.json", function(text) {
  33.             var json = JSON.parse(text);
  34.             for (var i = 0; i < json.length; i++) {
  35.                 if (json[i] === null) continue;
  36.                 window.mapname.push(json[i]["name"]);
  37.             }
  38.         }, function() {
  39.             alert("failed to read map infos");
  40.         });
  41.         var getMap = function() {
  42.             x++;
  43.             var num = x < 10 ? "00" + x : x < 100 ? "0" + x : "" + x;
  44.             getJSON("data/Map" + num + ".json", function(text) {
  45.                 // parse to the messages
  46.                 if (typeof text !== "undefined") {
  47.                     window.tempTextBody += '"' + window.mapname[x - 1] + '"' + "\r\n\r\n";
  48.                     var json = JSON.parse(text);
  49.                     for (var i = 0; i < json.events.length; i++) {
  50.                         if (json.events[i] === null) continue;
  51.                         var event = json.events[i];
  52.                         if (typeof event.name === "undefined")
  53.                             var name = "noName";
  54.                         else
  55.                             var name = event.name;
  56.                         var message = "";
  57.                         for (var ii = 0; ii < event.pages.length; ii++) {
  58.                             if (event.pages[ii] === null) continue;
  59.                             if (typeof event.pages[ii].list === "undefined") continue;
  60.                             var page = event.pages[ii];
  61.                             for (var iii = 0; iii < page.list.length; iii++) {
  62.                                 if (typeof page.list[iii] === "undefined")
  63.                                     continue;
  64.                                 if (typeof page.list[iii].parameters === "undefined" ||
  65.                                     typeof page.list[iii].parameters !== typeof [] ||
  66.                                     typeof page.list[iii].parameters === typeof [] &&
  67.                                     page.list[iii].parameters.length === 0 ||
  68.                                     page.list[iii].parameters[0].length === 0 ||
  69.                                     typeof page.list[iii].parameters === typeof [] &&
  70.                                     typeof page.list[iii].parameters[0] !== typeof "")
  71.                                     continue;
  72.                                 var command = page.list[iii];
  73.                                 message += "    " + command.parameters[0] + "\r\n";
  74.                             }
  75.                             message += "\r\n";
  76.                         }
  77.                         window.tempTextBody += name + "\r\n" + message;
  78.                         if (i + 1 < json.events.length)
  79.                             window.tempTextBody += "- - - - - - - - - - - - - - - - - - - - - - -\r\n";
  80.                     }
  81.                 }
  82.                 if (x < window.mapname.length) { // repeat fetching
  83.                     window.tempTextBody += "______________________________________________\r\n\r\n";
  84.                     getMap(); // recursive
  85.                 } else {
  86.                     // no more maps
  87.                     var fs = require('fs');
  88.                     var path = require('path');
  89.                     var fname = document.location.pathname.match(/^\/\w:\//)!==null? // windows path
  90.                         unescape(document.location.pathname.replace(/^\/|index\.html$/g, '')) + txtname + ".txt"
  91.                       :
  92.                       path.join(
  93.                         unescape(document.location.pathname.replace(/index\.html$/, '')),
  94.                         txtname + ".txt"); // mac path
  95.                     fs.writeFile(
  96.                         fname,
  97.                         window.tempTextBody,
  98.                         function(err) {
  99.                             if (err) {
  100.                                 alert(err);
  101.                                 return;
  102.                             }
  103.                             alert("Done!\r\nCheck for the game messages file named \r\n" +
  104.                                 fname
  105.                             );
  106.                         });
  107.                 }
  108.             }, function() {
  109.                 alert("Error loading map json.");
  110.             });
  111.         }
  112.         getMap(); // first run
  113.     }
  114. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement