Advertisement
Zalerinian

ZE - File List

Nov 30th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Imported = Imported || {};
  2. var Zale = Zale || {};
  3. Zale.FileList = {};
  4.  
  5. /*:
  6.  * @plugindesc Creates and maintains a list of all filenames in the game project. This is a scripting utility. <pluginID ZE - File List>
  7.  * @author Zalerinian
  8.  * @version 1.0.0
  9.  * @date November 30th, 2015
  10.  *
  11.  * @param List File
  12.  * @desc The filename for the list. Default: data/Filemap.json
  13.  * @default data/Filemap.json
  14.  *
  15.  * @help
  16.  * ==============================================================================
  17.  *    Support
  18.  * ==============================================================================
  19.  *
  20.  * Have an issue, question, or suggestion? My preferred method of contact would
  21.  * be on the RMW forums, preferrably by posint in one of my topics, but if you
  22.  * really need to reach me directly, feel free to PM me, or send me an email
  23.  * using the address below.
  24.  *
  25.  * Author: Zalerinian
  26.  * email: support at razelon.com
  27.  *
  28.  * ==============================================================================
  29.  *    Help
  30.  * ==============================================================================
  31.  *
  32.  * This plugin maintains a list of files present in a project so that other
  33.  * plugins can easily identify all the files available to them. This plugin
  34.  * provides functions to search the list with regular expressions. The file list
  35.  * can be directly accessed via FileList._list
  36.  *
  37.  * There are 3 functions available to filter the results found in the file list.
  38.  *
  39.  * To just look at file names, use FileList.scan(regex).
  40.  *
  41.  * To just look at file paths, use FileList.scanPath(regex).
  42.  *
  43.  * To look at both the file path and file name, use FileList.scanWithPath(regex).
  44.  */
  45.  
  46. (function(){
  47.   if(Imported["MVCommons"]) {
  48.     var author = [{
  49.       email: "support@razelon.com",
  50.       name: "Zalerinian",
  51.       website: "http://www.razelon.com"
  52.     }];
  53.     PluginManager.register("ZE - File List", "1.0.0", PluginManager.getBasicPlugin("ZE - File List").description, author, "2015-11-30");
  54.   } else if(!Imported["zAPI - File Tools"]) {
  55.     throw new Error("ZE - File List requires zAPI - File Tools");
  56.   } else {
  57.     Imported["ZE - File List"] = "1.0.0";
  58.   }
  59. })();
  60.  
  61. function FileList() {
  62.   throw new Error("FileList is a static class!");
  63. }
  64.  
  65. (function($){
  66.   "use strict";
  67.   $._list      = null;
  68.   $._updating  = false;
  69.   $._pending   = 0;
  70.  
  71.   var params = $plugins.filter(function(plugin) { return plugin.description.contains('<pluginID ZE - File List>'); });
  72.   if(params.length === 0) {
  73.     console.warn("Couldn't find parameters for ZE - File List. Defaults will be used.");
  74.   } else {
  75.     params = params[0].parameters;
  76.   }
  77.   Zale.FileList.PARAMS   = params || {};
  78.   Zale.FileList.FILENAME = params["List File"] || "data/Filemap.json";
  79.  
  80.   /*
  81.    * FileList.updateList()
  82.    * @note
  83.    * Updates the file list if the game was run in local mode. If the
  84.    * game is not in local mode, it will request the file at the path
  85.    * speciied in the parameters.
  86.    */
  87.   $.updateList = function() {
  88.     if(StorageManager.isLocalMode()) {
  89.       this._list     = [];
  90.       this._updating = true;
  91.       StorageManager.readDir("", function(e, list){
  92.         for(var i = list.length - 1; i >= 0; i--){
  93.           this.processItem(list[i], "");
  94.         }
  95.       }.bind(this));
  96.     } else {
  97.       this.loadList();
  98.     }
  99.   }
  100.  
  101.   /*
  102.    * FileList.processItem(item, dir)
  103.    * @param {String} item The filename to check now.
  104.    * @param {String} dir The directory the current item is in.
  105.    * @note
  106.    * This function will check if the current item is a directory. If
  107.    * it is, it will add the current item to the dir variable, and
  108.    * recursively call the processItem function with the new dir
  109.    * to check all the items in that folder.
  110.    *
  111.    * The pending count is updated per item to keep track of how many
  112.    * items are left to process, so that we know when we can call the
  113.    * save function. It's really not the most reliable method to keep
  114.    * track of items, as there will be several instances where the
  115.    * count will be 0. However, it's difficult to measure when an
  116.    * asynchronous recursive function is completed running.
  117.    */
  118.   $.processItem = function(item, dir) {
  119.     if(StorageManager.isLocalMode()) {
  120.       ++this._pending;
  121.       StorageManager.isDirectory(dir + item, function(e, isdir) {
  122.         if(isdir) {
  123.           --this._pending
  124.           dir += item + "/";
  125.           StorageManager.readDir(dir, function(e, list){
  126.             if(e) {
  127.               throw e;
  128.             }
  129.             for(var i = list.length - 1; i >= 0; i--){
  130.               this.processItem(list[i], dir);
  131.             }
  132.           }.bind(this));
  133.         } else {
  134.           --this._pending;
  135.           this._list.push(dir + item);
  136.         }
  137.         if(this._pending === 0) {
  138.           this._updating = false;
  139.           this.saveList();
  140.         }
  141.       }.bind(this));
  142.     }
  143.   }
  144.  
  145.   /*
  146.    * FileList.saveList()
  147.    * @note
  148.    * Saves the _list variable via the File Tools API, ignoring EEXIST
  149.    * errors from the mkdir function.
  150.    */
  151.   $.saveList = function() {
  152.     StorageManager.saveCompressedFile(Zale.FileList.FILENAME, JSON.stringify(this._list), function(e){
  153.       if(e) {
  154.         throw e;
  155.       }
  156.     }, ["EEXIST"]);
  157.   }
  158.  
  159.   /*
  160.    * FileList.loadList()
  161.    * @note
  162.    * Loads the list from the web server, or from the mobile device,
  163.    * in order to get the list when not in local mode. This will work
  164.    * in local mode, but the upadteList function is recommended for
  165.    * local mode, as it will rebuild a new list, which will be the
  166.    * most up to date at that time.
  167.    */
  168.   $.loadList = function() {
  169.     StorageManager.loadCompressedFile(Zale.FileList.FILENAME, 'utf8', function(e, data) {
  170.       if(e) {
  171.         throw e;
  172.       }
  173.       this._list = JSON.parse(data);
  174.     }.bind(this));
  175.   }
  176.  
  177.   /*
  178.    * FileList.scan(regex)
  179.    * @param {RegExp} regex A regular expression to compare against filenames.
  180.    * @note
  181.    * Will return a list of files where the regex matches the filename
  182.    * only. This will not check the directory path.
  183.    * @return {Array} A list of files that match the regex.
  184.    */
  185.   $.scan = function(regex) {
  186.     if(!this._list) {
  187.       return [];
  188.     }
  189.     return this._list.filter(function(v) {
  190.       return v.replace(/.*[\/\\]/, '').match(regex) !== null;
  191.     });
  192.   }
  193.  
  194.   /*
  195.    * FileList.scanPath(regex)
  196.    * @param {RegExp} regex The regular expression to compare against the folders of each item.
  197.    * @note
  198.    * This wil loop through each item, comparing the given regex
  199.    * against the relative folder path to each file. This allows
  200.    * us to get all files in a specific folder, rather than those
  201.    * with a specific filename.
  202.    * @return {Array} A list of files where the path matched the regex.
  203.    */
  204.   $.scanPath = function(regex) {
  205.     if(!this._list) {
  206.       return [];
  207.     }
  208.     return this._list.filter(function(v) {
  209.       return v.replace(/\/[^\/]+?$/, '').match(regex) !== null;
  210.     });
  211.   }
  212.  
  213.   /*
  214.    * FileList.scanWithPath(regex)
  215.    * @param {RegExp} regex The regular expression to compare against the full relative path to each item.
  216.    * @note
  217.    * This function will compare the regex against each file,
  218.    * including both the filename and folder path. This is
  219.    * useful for finding specific files in specific folders
  220.    * @return {Array} A list of files where the full relative path matched the regex.
  221.    */
  222.   $.scanWithPath = function(regex) {
  223.     if(!this._list) {
  224.       return [];
  225.     }
  226.     return this._list.filter(function(v) {
  227.       return v.match(regex) !== null;
  228.     })
  229.   }
  230.  
  231.   $.regexEscape = function(str) {
  232.     return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
  233.   }
  234. })(FileList);
  235.  
  236. (function($) {
  237.   var SM_readdirSync = $.readDirSync;
  238.   $.readDirSync = function(dirPath) {
  239.     SM_readdirSync.call(this, dirPath);
  240.     if(!this.isLocalMode()) {
  241.       var ary = FileList.scanPath(new RegExp(dirPath));
  242.       return ary.filter(function(v) {
  243.         return v.replace(/.*[\/\\]/)
  244.       });
  245.     }
  246.   }
  247.  
  248.   var SM_readdir = $.readDir;
  249.   $.readDir = function(dirPath, callback) {
  250.     SM_readdir.call(this, dirPath, callback);
  251.     if(!this.isLocalMode()){
  252.       setTimeout(function() {
  253.         var ary = FileList.scanPath(new RegExp(dirPath));
  254.         callback.call(this, null, ary.filter(function(v) {
  255.           return v.replace(/.*[\/\\]/);
  256.         }));
  257.       }, 0);
  258.     }
  259.   }
  260. })(StorageManager);
  261.  
  262. // Update the list at startup. The list is updated asynchronously, so this does not slow down the startup time.
  263. FileList.updateList();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement