Double_X

doublex rmmv preloaded resources v100b

Jul 7th, 2020 (edited)
166
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*============================================================================
  2.  *    ## Plugin Info
  3.  *----------------------------------------------------------------------------
  4.  *    # Plugin Name
  5.  *      DoubleX RMMV Preloaded Resources
  6.  *----------------------------------------------------------------------------
  7.  *    # Terms Of Use
  8.  *      1. Commercial use's always allowed and crediting me's always optional.
  9.  *      2. You shall keep this plugin's Plugin Info part's contents intact.
  10.  *      3. You shalln't claim that this plugin's written by anyone other than
  11.  *         DoubleX or my aliases. I always reserve the right to deny you from
  12.  *         using any of my plugins anymore if you've violated this.
  13.  *      4. If you repost this plugin directly(rather than just linking back),
  14.  *         you shall inform me of these direct repostings. I always reserve
  15.  *         the right to request you to edit those direct repostings.
  16.  *      5. CC BY 4.0, except those conflicting with any of the above, applies
  17.  *         to this plugin, unless you've my permissions not needing follow so.
  18.  *      6. I always reserve the right to deny you from using this plugin
  19.  *         anymore if you've violated any of the above.
  20.  *----------------------------------------------------------------------------
  21.  *    # Prerequisites
  22.  *      Abilities:
  23.  *      1. Nothing special for most ordinary cases
  24.  *         (No capability on Javascript ES5 experience but can still make
  25.  *         reasonable guesses on readable novice codes up to 100 LoC scale)
  26.  *      2. Little RMMV plugin development proficiency to fully utilize this
  27.  *         plugin in intended ways
  28.  *         (Elementary Javascript ES5 exposures being able to write beginner
  29.  *         codes up to 300LoC scale )
  30.  *----------------------------------------------------------------------------
  31.  *    # Links
  32.  *      This Plugin:
  33.  *      1. https://pastebin.com/qjqn9sE0
  34.  *      Posts:
  35.  *      1. https://forums.rpgmakerweb.com/index.php?threads/doublex-rmmv-preloaded-resources.123760/
  36.  *      2. https://www.rpgmakercentral.com/topic/42479-doublex-rmmv-preloaded-resources/
  37.  *      3. https://rpgmaker.net/scripts/789/
  38.  *      4. http://www.hbgames.org/forums/viewtopic.php?f=332&t=80223
  39.  *----------------------------------------------------------------------------
  40.  *    # Contributors
  41.  *      Authors:
  42.  *      1. DoubleX
  43.  *      Plugin Development Collaborators:
  44.  *      - None So Far
  45.  *      Bug Reporters:
  46.  *      - None So Far
  47.  *      Compatibility Issue Raisers:
  48.  *      - None So Far
  49.  *      Feature Requesters:
  50.  *      - None So Far
  51.  *----------------------------------------------------------------------------
  52.  *    # Changelog
  53.  *      v1.00b(GMT 0300 27-Mar-2020):
  54.  *      1. You no longer have to edit the value of
  55.  *         DoubleX_RMMZ.Preloaded_Resources_File when changing the plugin file
  56.  *         name
  57.  *      2. Fixed the crashes when preloading animations, images, etc, wthout
  58.  *         hues(such cases will be understood as having the default hue 0 only)
  59.  *      v1.00a(GMT 1500 7-Jul-2020):
  60.  *      1. 1st version of this plugin finished
  61.  *============================================================================*/
  62. /*~struct~PreloadedAnimation:
  63.  * @param filename
  64.  * @type file
  65.  * @dir img/animations/
  66.  * @desc The filename of the animation to be preloaded
  67.  * @param hues
  68.  * @type number[]
  69.  * @max 360
  70.  * @min 0
  71.  * @desc The hue of the animation to be preloaded
  72.  */
  73. /*~struct~PreloadedBattleBack1:
  74.  * @param filename
  75.  * @type file
  76.  * @dir img/battlebacks1/
  77.  * @desc The filename of the 1st battle back to be preloaded
  78.  * @param hues
  79.  * @type number[]
  80.  * @max 360
  81.  * @min 0
  82.  * @desc The hue of the 1st battle back to be preloaded
  83.  */
  84. /*~struct~PreloadedBattleBack2:
  85.  * @param filename
  86.  * @type file
  87.  * @dir img/battlebacks2/
  88.  * @desc The filename of the 2nd battle back to be preloaded
  89.  * @param hues
  90.  * @type number[]
  91.  * @max 360
  92.  * @min 0
  93.  * @desc The hue of the 2nd battle back to be preloaded
  94.  */
  95. /*~struct~PreloadedCharacter:
  96.  * @param filename
  97.  * @type file
  98.  * @dir img/characters/
  99.  * @desc The filename of the character to be preloaded
  100.  * @param hues
  101.  * @type number[]
  102.  * @max 360
  103.  * @min 0
  104.  * @desc The hue of the character to be preloaded
  105.  */
  106. /*~struct~PreloadedEnemy:
  107.  * @param filename
  108.  * @type file
  109.  * @dir img/enemies/
  110.  * @desc The filename of the enemy to be preloaded
  111.  * @param hues
  112.  * @type number[]
  113.  * @max 360
  114.  * @min 0
  115.  * @desc The hue of the enemy to be preloaded
  116.  */
  117. /*~struct~PreloadedFace:
  118.  * @param filename
  119.  * @type file
  120.  * @dir img/faces/
  121.  * @desc The filename of the face to be preloaded
  122.  * @param hues
  123.  * @type number[]
  124.  * @max 360
  125.  * @min 0
  126.  * @desc The hue of the face to be preloaded
  127.  */
  128. /*~struct~PreloadedParallax:
  129.  * @param filename
  130.  * @type file
  131.  * @dir img/parallaxes/
  132.  * @desc The filename of the parallax to be preloaded
  133.  * @param hues
  134.  * @type number[]
  135.  * @max 360
  136.  * @min 0
  137.  * @desc The hue of the parallax to be preloaded
  138.  */
  139. /*~struct~PreloadedPicture:
  140.  * @param filename
  141.  * @type file
  142.  * @dir img/pictures/
  143.  * @desc The filename of the picture to be preloaded
  144.  * @param hues
  145.  * @type number[]
  146.  * @max 360
  147.  * @min 0
  148.  * @desc The hue of the parallax to be preloaded
  149.  */
  150. /*~struct~PreloadedSVActor:
  151.  * @param filename
  152.  * @type file
  153.  * @dir img/sv_actors/
  154.  * @desc The filename of the sideview actor to be preloaded
  155.  * @param hues
  156.  * @type number[]
  157.  * @max 360
  158.  * @min 0
  159.  * @desc The hue of the sideview actor to be preloaded
  160.  */
  161. /*~struct~PreloadedSVEnemy:
  162.  * @param filename
  163.  * @type file
  164.  * @dir img/sv_enemies/
  165.  * @desc The filename of the sideview enemy to be preloaded
  166.  * @param hues
  167.  * @type number[]
  168.  * @max 360
  169.  * @min 0
  170.  * @desc The hue of the sideview enemy to be preloaded
  171.  */
  172. /*~struct~PreloadedSystem:
  173.  * @param filename
  174.  * @type file
  175.  * @dir img/system/
  176.  * @desc The filename of the system image to be preloaded
  177.  * @param hues
  178.  * @type number[]
  179.  * @max 360
  180.  * @min 0
  181.  * @desc The hue of the system image to be preloaded
  182.  */
  183. /*~struct~PreloadedTileset:
  184.  * @param filename
  185.  * @type file
  186.  * @dir img/tilesets/
  187.  * @desc The filename of the tileset to be preloaded
  188.  * @param hues
  189.  * @type number[]
  190.  * @max 360
  191.  * @min 0
  192.  * @desc The hue of the tileset to be preloaded
  193.  */
  194. /*~struct~PreloadedTitle1:
  195.  * @param filename
  196.  * @type file
  197.  * @dir img/titles1/
  198.  * @desc The filename of the 1st title to be preloaded
  199.  * @param hues
  200.  * @type number[]
  201.  * @max 360
  202.  * @min 0
  203.  * @desc The hue of the 1st title to be preloaded
  204.  */
  205. /*~struct~PreloadedTitle2:
  206.  * @param filename
  207.  * @type file
  208.  * @dir img/titles2/
  209.  * @desc The filename of the 2nd title to be preloaded
  210.  * @param hues
  211.  * @type number[]
  212.  * @max 360
  213.  * @min 0
  214.  * @desc The hue of the 2nd title to be preloaded
  215.  */
  216. /*~struct~PreloadedMiscImage:
  217.  * @param path
  218.  * @type file
  219.  * @dir img/
  220.  * @desc The filename of the misc image to be preloaded
  221.  * @param hues
  222.  * @type number[]
  223.  * @max 360
  224.  * @min 0
  225.  * @desc The hue of the misc image to be preloaded
  226.  * @param smooth
  227.  * @type boolean
  228.  * @desc The smooth of the misc image to be preloaded
  229.  */
  230. /*:
  231.  * @plugindesc (v1.00b)Lets you sets some audios/images to be loaded upon game start
  232.  * This should boost the FPS on phones noticeably if there's enough memory
  233.  * @author DoubleX
  234.  *
  235.  * @param IsPreloadAudio
  236.  * @type boolean
  237.  * @desc Sets whether the specified audios will be preloaded
  238.  * @default true
  239.  *
  240.  * @param preloadAudioMSInterval
  241.  * @parent IsPreloadAudio
  242.  * @type number
  243.  * @desc Sets the number of milliseconds to wait before loading the
  244.  * next audio included by the same parameter(0 means no wait)
  245.  * @default 0
  246.  *
  247.  * @param preloadedBGMs
  248.  * @parent IsPreloadAudio
  249.  * @type file[]
  250.  * @dir audio/bgm/
  251.  * @desc Sets the list of BGMs to be preloaded
  252.  * @default []
  253.  *
  254.  * @param preloadedBGSs
  255.  * @parent IsPreloadAudio
  256.  * @type file[]
  257.  * @dir audio/bgs/
  258.  * @desc Sets the list of BGSs to be preloaded
  259.  * @default []
  260.  *
  261.  * @param preloadedMEs
  262.  * @parent IsPreloadAudio
  263.  * @type file[]
  264.  * @dir audio/me/
  265.  * @desc Sets the list of MEs to be preloaded
  266.  * @default []
  267.  *
  268.  * @param preloadedSEs
  269.  * @parent IsPreloadAudio
  270.  * @type file[]
  271.  * @dir audio/se/
  272.  * @desc Sets the list of SEs to be preloaded
  273.  * @default []
  274.  *
  275.  * @param preloadedStaticSEs
  276.  * @parent IsPreloadAudio
  277.  * @type file[]
  278.  * @dir audio/se/
  279.  * @desc Sets the list of static SEs to be preloaded
  280.  * @default []
  281.  *
  282.  * @param preloadedMiscAudios
  283.  * @parent IsPreloadAudio
  284.  * @type file[]
  285.  * @dir audio/
  286.  * @desc Sets the list of other audio files to be preloaded
  287.  * @default []
  288.  *
  289.  * @param IsPreloadImage
  290.  * @type boolean
  291.  * @desc Sets whether the specified images will be preloaded
  292.  * @default true
  293.  *
  294.  * @param preloadImageMSInterval
  295.  * @parent IsPreloadImage
  296.  * @type number
  297.  * @desc Sets the number of milliseconds to wait before loading the
  298.  * next hue and image under the same parameter(0 means no wait)
  299.  * @default 0
  300.  *
  301.  * @param preloadedAnimations
  302.  * @parent IsPreloadImage
  303.  * @type struct<PreloadedAnimation>[]
  304.  * @desc Sets the list of animations to be preloaded
  305.  * @default []
  306.  *
  307.  * @param preloadedBattleBack1s
  308.  * @parent IsPreloadImage
  309.  * @type struct<PreloadedBattleBack1>[]
  310.  * @desc Sets the list of 1st battle backs to be preloaded
  311.  * @default []
  312.  *
  313.  * @param preloadedBattleBack2s
  314.  * @parent IsPreloadImage
  315.  * @type struct<PreloadedBattleBack2>[]
  316.  * @desc Sets the list of 2nd battle backs to be preloaded
  317.  * @default []
  318.  *
  319.  * @param preloadedCharacters
  320.  * @parent IsPreloadImage
  321.  * @type struct<PreloadedCharacter>[]
  322.  * @desc Sets the list of characters to be preloaded
  323.  * @default []
  324.  *
  325.  * @param preloadedEnemies
  326.  * @parent IsPreloadImage
  327.  * @type struct<PreloadedEnemy>[]
  328.  * @desc Sets the list of enemies to be preloaded
  329.  * @default []
  330.  *
  331.  * @param preloadedFaces
  332.  * @parent IsPreloadImage
  333.  * @type struct<PreloadedFace>[]
  334.  * @desc Sets the list of faces to be preloaded
  335.  * @default []
  336.  *
  337.  * @param preloadedParallaxes
  338.  * @parent IsPreloadImage
  339.  * @type struct<PreloadedParallax>[]
  340.  * @desc Sets the list of parallaxes to be preloaded
  341.  * @default []
  342.  *
  343.  * @param preloadedPictures
  344.  * @parent IsPreloadImage
  345.  * @type struct<PreloadedPicture>[]
  346.  * @desc Sets the list of pictures to be preloaded
  347.  * @default []
  348.  *
  349.  * @param preloadedSVActors
  350.  * @parent IsPreloadImage
  351.  * @type struct<PreloadedSVActor>[]
  352.  * @desc Sets the list of sideview actors to be preloaded
  353.  * @default []
  354.  *
  355.  * @param preloadedSVEnemies
  356.  * @parent IsPreloadImage
  357.  * @type struct<PreloadedSVEnemy>[]
  358.  * @desc Sets the list of sideview enemies to be preloaded
  359.  * @default []
  360.  *
  361.  * @param preloadedSystem
  362.  * @parent IsPreloadImage
  363.  * @type struct<PreloadedSystem>[]
  364.  * @desc Sets the list of system images to be preloaded
  365.  * @default []
  366.  *
  367.  * @param preloadedTilesets
  368.  * @parent IsPreloadImage
  369.  * @type struct<PreloadedTileset>[]
  370.  * @desc Sets the list of tilesets to be preloaded
  371.  * @default []
  372.  *
  373.  * @param preloadedTitles1
  374.  * @parent IsPreloadImage
  375.  * @type struct<PreloadedTitle1>[]
  376.  * @desc Sets the list of 1st titles to be preloaded
  377.  * @default []
  378.  *
  379.  * @param preloadedTitles2
  380.  * @parent IsPreloadImage
  381.  * @type struct<PreloadedTitle2>[]
  382.  * @desc Sets the list of 2nd titles to be preloaded
  383.  * @default []
  384.  *
  385.  * @param preloadedMiscImages
  386.  * @parent IsPreloadImage
  387.  * @type struct<PreloadedMiscImage>[]
  388.  * @desc Sets the list of other image files to be preloaded
  389.  * @default []
  390.  *
  391.  * @help
  392.  *============================================================================
  393.  * 1. You should only preload resources that are actually used or the game can
  394.  *    take an excessively and unnecessarily long time to start
  395.  * 2. You might have to test the values of preloadAudioMSInterval and
  396.  *    preloadImageMSInterval to have the optimal preload time for your project
  397.  * 3. Setting preloadAudioMSInterval/preloadImageMSInterval as 0 might block
  398.  *    the UI thread for too long and thus crashing/freezing the game in phones
  399.  * 4. You should consider not preloading resources that are only rarely used
  400.  *    in case the preloading times are still too long
  401.  * 5. Some plugins might use HTML5Audio instead of WebAudio, and preloading
  402.  *    audios using HTML5Audio is meaningless as HTML5Audio is a static class
  403.  *    having nothing to preload
  404.  * 6. You should compress the resources to be preoloaded for phones or the
  405.  *    extra memory consumption from preloading them can quickly crash/freeze
  406.  *    the game there
  407.  * 7. No identical resource should be duplicated in the same parameter or
  408.  *    across parameters(this plugin won't explicitly skip those duplicates as
  409.  *    simplifying the codes this way can actually reduce preload time)
  410.  * 8. If you want to keep the current parameter values in the plugin manager
  411.  *    upon using a newer version, you can do the following:
  412.  *    - Renames the newer version to be that of the older version
  413.  *    - Edits the value of DoubleX_RMMV.Preloaded_Resources_File to be the
  414.  *      filename of the older version, which must be done via opening this
  415.  *      plugin js file directly
  416.  * 9. (Advanced)By default, the images are cached upon their first uses, and
  417.  *    the cache is a Least Recently Used(LRU) cache. This plugin reserves all
  418.  *    preloaded images so the LRU will never release them unless they're
  419.  *    explicitly told to be released via a script call
  420.  * 10. (Advanced)To preload images in nested folders(like pictures/folderX),
  421.  *     you've to use preloadedMiscImages, and their paths must be entered via
  422.  *     raw texts, because the default RMMV plugin manager doesn't natively
  423.  *     support resources in nested folders, meaning that hacks are inevitable
  424.  *============================================================================
  425.  *    ## (Advanced)Script Call Info
  426.  *----------------------------------------------------------------------------
  427.  *    # Image manipulations
  428.  *      1. ImageManager.releasePreloadedFolderImg(param, filename, hue)
  429.  *         - Releases the image with the filename filename and hue hue
  430.  *           specified in parameter param
  431.  *         - This can be useful when an image becomes rarely used and/or the
  432.  *           preloaded images are consuming too much memory
  433.  *         - Please note that using this script call doesn't always remove the
  434.  *           image from the LRU cache instantly as it's still up to the LRU
  435.  *           cache to determine when to remove that image now that it can be
  436.  *           removed due to no longer being reserved
  437.  *         - param and filename are supposed to be String
  438.  *         - hue is supposed to be an integer from 0 to 360 inclusive
  439.  *         - The script call's supposed to be Idempotent
  440.  *      2. ImageManager.releasePreloadedMiscImg(path, hue, smooth)
  441.  *         - Releases the image with the path path, hue hue and smooth smooth
  442.  *         - This can be useful when an image becomes rarely used and/or the
  443.  *           preloaded images are consuming too much memory
  444.  *         - Please note that using this script call doesn't always remove the
  445.  *           image from the LRU cache instantly as it's still up to the LRU
  446.  *           cache to determine when to remove that image now that it can be
  447.  *           removed due to no longer being reserved
  448.  *         - path is supposed to be a String
  449.  *         - hue is supposed to be an integer from 0 to 360 inclusive
  450.  *         - smooth is supposed to be a Boolean
  451.  *         - The script call's supposed to be Idempotent
  452.  *    # Audio manipulations
  453.  *      1. AudioManager.invalidateCachedWebAudio(folder, name)
  454.  *         - Releases the audio with folder folder and filename name from the
  455.  *           audio cache
  456.  *         - This doesn't work for static SE audio loaded as WebAudio
  457.  *         - folder is supposed to be a String
  458.  *         - name is supposed to be a String
  459.  *         - The script call's supposed to be Idempotent
  460.  *============================================================================
  461.  */
  462.  
  463. var DoubleX_RMMV = DoubleX_RMMV || {};
  464. DoubleX_RMMV["Preloaded Resources"] = "v1.00b";
  465.  
  466. (function() {
  467.  
  468.     "use strict";
  469.  
  470.     var src = document.currentScript.src;
  471.     var name = src.split("/").slice(-1)[0].split(".")[0].replace(/%20/g, " ");
  472.     DoubleX_RMMV.Preloaded_Resources_File = name;
  473.  
  474. })();
  475.  
  476. /*============================================================================
  477.  *    ## Plugin Implementations
  478.  *       You need not edit this part as it's about how this plugin works
  479.  *----------------------------------------------------------------------------
  480.  *    # Plugin Support Info:
  481.  *      1. Prerequisites
  482.  *         - Basic knowledge on what Graphics, DataManager, ImageManager and
  483.  *           AudioManager do in general
  484.  *         - Some RMMV plugin development proficiency to fully comprehend this
  485.  *           plugin
  486.  *           (Basic knowledge on what RMMV plugin development does in general
  487.  *           with several easy, simple and small plugins written without
  488.  *           nontrivial bugs up to 1000 LoC scale but still being
  489.  *           inexperienced)
  490.  *      2. Parameter/Return value of type * means it might be of any type
  491.  *      3. Function signature with (**) means it might take any number of
  492.  *         parameters of any type
  493.  *----------------------------------------------------------------------------*/
  494.  
  495. DoubleX_RMMV.Preloaded_Resources = {};
  496.  
  497. /*----------------------------------------------------------------------------
  498.  *    # Edit class: Graphics
  499.  *      - Preloads all resources specified in parameters upon game start
  500.  *----------------------------------------------------------------------------*/
  501.  
  502. (function() {
  503.  
  504.     "use strict";
  505.  
  506.     // It's unlikely that the original version's needed
  507.     Graphics._paintUpperCanvas = function() { // v1.00a - v1.00a; Rewritten
  508.         this._clearUpperCanvas();
  509.         // It's unlikely that these codes will be edited
  510.         if (isPaintUpperCanvas.call(this)) paintUpperCanvas.call(this);
  511.         //
  512.     }; // Graphics._paintUpperCanvas
  513.     //
  514.  
  515.     var MIN_LOADING_COUNT = 20;
  516.  
  517.     /**
  518.      * Nullipotent
  519.      * @since v1.00a @version v1.00a
  520.      * @return {Boolean} The check result
  521.      */
  522.     function isPaintUpperCanvas() { // It's unlikely that it'll ever be reused
  523.         return this._loadingImage && this._loadingCount >= MIN_LOADING_COUNT;
  524.     } // isPaintUpperCanvas
  525.  
  526.     /**
  527.      * Idempotent
  528.      * @since v1.00a @version v1.00a
  529.      * @todo Stops redrawing the loading image every frame
  530.      */
  531.     function paintUpperCanvas() { // It's unlikely that it'll ever be reused
  532.         var context = this._upperCanvas.getContext("2d");
  533.         context.save();
  534.         var alpha = ((this._loadingCount - MIN_LOADING_COUNT) / 30).clamp(0, 1);
  535.         context.globalAlpha = alpha;
  536.         var dx = (this._width - this._loadingImage.width) / 2;
  537.         var dy = (this._height - this._loadingImage.height) / 2;
  538.         context.drawImage(this._loadingImage, dx, dy);
  539.         drawPreloadProgressBar.call(this, context, dx, dy);
  540.         context.restore();
  541.     } // paintUpperCanvas
  542.  
  543.     /**
  544.      * Idempotent
  545.      * @since v1.00a @version v1.00a
  546.      * @param {CanvasRenderingContext2D} context - The 2D canvas context
  547.      * @param {Int} dx - The starting x coordinate of the canvas drawing
  548.      * @param {Int} dy - The starting y coordinate of the canvas drawing
  549.      * @todo Breaks this excessively long function into several shorter pieces
  550.      */
  551.     function drawPreloadProgressBar(context, dx, dy) {
  552.         if (!this.isDrawPreloadProgressBar) return;
  553.         var num = DataManager.preloadResourceNum;
  554.         if (num <= 0) return;
  555.         var fillStyle = context.fillStyle;
  556.         context.fillStyle = "white";
  557.         var y = dy + this._loadingImage.height;
  558.         var width = this._loadingImage.width;
  559.         var height = this._loadingImage.height / 4;
  560.         context.fillRect(dx, y, width, height);
  561.         context.fillStyle = "green";
  562.         var progress = DataManager.preloadResourceProgress;
  563.         context.fillRect(dx, y, width * progress / num, height);
  564.         context.fillStyle = "blue";
  565.         var font = context.font;
  566.         context.font = "16px GameFont";
  567.         var text = "Preloaded " + progress + "/" + num + " specified resources";
  568.         var textX = dx + width / 2 - context.measureText(text).width / 2;
  569.         // The formalu of textY is derived from actual testings
  570.         var textY = y + height / 2 + 4, lineJoin = context.lineJoin;
  571.         //
  572.         context.lineJoin = "round";
  573.         context.strokeText(text, textX, textY, width);
  574.         context.fillText(text, textX, textY, width);
  575.         context.lineJoin = lineJoin;
  576.         context.font = font, context.fillStyle = fillStyle;
  577.     } // drawPreloadProgressBar
  578.  
  579. })(); // Graphics
  580.  
  581. /*----------------------------------------------------------------------------
  582.  *    # Edit class: DataManager
  583.  *      - Preloads all resources specified in parameters upon game start
  584.  *----------------------------------------------------------------------------*/
  585.  
  586. (function(PR) {
  587.  
  588.     "use strict";
  589.  
  590.     PR.DataManager = { orig: {}, new: {} };
  591.     var _DM = PR.DataManager.orig, _PR = PR.DataManager.new;
  592.  
  593.     _PR.BOOL_PARAM_RESULT = function(val) {
  594.         return val && (val === "true" || val !== "false");
  595.     }; // _PR.BOOL_PARAM_RESULT
  596.  
  597.     _PR._PARSE_IMG_PARAM = function(parsedVal, v, i) {
  598.         var obj = parsedVal[i] = JSON.parse(v);
  599.         var hues = JSON.parse(obj.hues || "[]");
  600.         hues.forEach(function(hue, j) { hues[j] = JSON.parse(hue); });
  601.         // Animations, images, etc without hues are treated as with hue 0 only
  602.         if (hues.length <= 0) hues.push(0);
  603.         //
  604.         // Smooth is either true or false so an array of smoothes' not needed
  605.         obj.hues = hues, obj.smooth = obj.smooth && JSON.parse(obj.smooth);
  606.         //
  607.     }; // _PR._PARSE_IMG_PARAM
  608.     _PR._PARSED_IMG_PARAM = function(parsedVal) {
  609.         // Not using declarative counterparts' minimize perload time and memory
  610.         parsedVal.forEach(function(v, i) {
  611.             _PR._PARSE_IMG_PARAM(parsedVal, v, i);
  612.         });
  613.         return parsedVal;
  614.         //
  615.     }; // _PR._PARSED_IMG_PARAM
  616.     _PR._PARSED_PARAM = function(param, val) {
  617.         var parsedVal = JSON.parse(val);
  618.         if (PR.ImageManager.new.PRELOAD_IMG_PARAMS.contains(param)) {
  619.             return _PR._PARSED_IMG_PARAM(parsedVal);
  620.         }
  621.         return parsedVal;
  622.     }; // _PR._PARSED_PARAM
  623.     _PR._PARSED_PARAMS = function(params) {
  624.         Object.keys(params).forEach(function(param) {
  625.             params[param] = _PR._PARSED_PARAM(param, params[param]);
  626.         });
  627.         return params;
  628.     }; // _PR._PARSED_PARAMS
  629.  
  630.     // Marks whether the parameters in the parent are all preloaded or skipped
  631.     DataManager._isParentParamPreloaded = { // New private variable
  632.         IsPreloadAudio: false,
  633.         IsPreloadImage: false
  634.     }; // DataManager._isParentParamPreloaded
  635.     //
  636.  
  637.     DataManager.preloadResourceNum = DataManager.preloadResourceProgress = 0;
  638.  
  639.     _PR._PARENT_PARAMS = Object.keys(DataManager._isParentParamPreloaded);
  640.  
  641.     _DM.loadDatabase = DataManager.loadDatabase;
  642.     _PR.loadDatabase = DataManager.loadDatabase = function() {
  643.     // v1.00a - v1.00a; Extended
  644.         _DM.loadDatabase.apply(this, arguments);
  645.         // Added to preload all specified audio and image resources as well
  646.         _PR._preload.call(this);
  647.         //
  648.     }; // DataManager.loadDatabase
  649.  
  650.     _DM.isDatabaseLoaded = DataManager.isDatabaseLoaded;
  651.     _PR.isDatabaseLoaded = DataManager.isDatabaseLoaded = function() {
  652.     // v1.00a - v1.00a; Extended
  653.         // Edited to start the game only after all resources are preloaded too
  654.         if (!_DM.isDatabaseLoaded.apply(this, arguments)) return false;
  655.         return this._areAllResourcesPreloaded;
  656.         //
  657.     }; // DataManager.isDatabaseLoaded
  658.  
  659.     /**
  660.      * Idempotent
  661.      * @interface @since v1.00a @version v1.00a
  662.      * @param {Param} parentParam - The parameter as the parent of other ones
  663.      */
  664.     DataManager.onFinishPreload = function(parentParam) {
  665.         console.info("All parameters under " + parentParam + " are preloaded.");
  666.         this._isParentParamPreloaded[parentParam] = true;
  667.         if (!_PR._areAllParentParamsPreloaded.call(this)) return;
  668.         _PR._onFinishPreload.call(this);
  669.     }; // DataManager.onFinishPreload
  670.  
  671.     /**
  672.      * The this pointer is DataManager
  673.      * Idempotent
  674.      * @since v1.00a @version v1.00a
  675.      */
  676.     _PR._preload = function() {
  677.         // Ensures this method will only be called once
  678.         if (this._isPreloadResourceRun) return;
  679.         Graphics.isDrawPreloadProgressBar = true;
  680.         _PR._preloadStartNow = Date.now();
  681.         _PR._preloadResources.call(this);
  682.         this._isPreloadResourceRun = true; // New private variable
  683.         //
  684.     }; // _PR._preload
  685.  
  686.     /**
  687.      * The this pointer is DataManager
  688.      * Idempotent
  689.      * @since v1.00a @version v1.00a
  690.      */
  691.     _PR._preloadResources = function() {
  692.         var params = _PR._parsedParams.call(this);
  693.         // They must be placed here to have the most accurate report
  694.         var elapsedMs = Date.now() - _PR._preloadStartNow;
  695.         console.info("Parameters parsing time: " + elapsedMs + " milliseconds");
  696.         //
  697.         ImageManager.preloadImgs(params);
  698.         AudioManager.preloadAudios(params);
  699.     }; // _PR._preloadResources
  700.  
  701.     /**
  702.      * The this pointer is DataManager
  703.      * Nullipotent
  704.      * @since v1.00a @version v1.00a
  705.      * @returns {{*}} The mapping of all parameter name-value pairs
  706.      */
  707.     _PR._parsedParams = function() {
  708.         // This method's called only once anyway so it won't hurt performance
  709.         var filename = DoubleX_RMMV.Preloaded_Resources_File;
  710.         var params = PluginManager.parameters(filename);
  711.         //
  712.         // Parsing the whole thing all at once's much faster than parsing later
  713.         return _PR._PARSED_PARAMS(JsonEx.makeDeepCopy(params));
  714.         // The original plugin parameter container shouldn't be ever edited
  715.     }; // _PR._parsedParams
  716.  
  717.     /**
  718.      * The this pointer is DataManager
  719.      * Nullipotent
  720.      * @since v1.00a @version v1.00a
  721.      * @returns {Boolean} The check result
  722.      */
  723.     _PR._areAllParentParamsPreloaded = function() {
  724.         return _PR._PARENT_PARAMS.every(_PR._isParentParamPreloaded, this);
  725.     }; // _PR._areAllParentParamsPreloaded
  726.  
  727.     /**
  728.      * The this pointer is DataManager
  729.      * Nullipotent
  730.      * @since v1.00a @version v1.00a
  731.      * @param {Param} parentParam - The parameter as the parent of other ones
  732.      * @returns {Boolean} The check result
  733.      */
  734.     _PR._isParentParamPreloaded = function(parentParam) {
  735.         return this._isParentParamPreloaded[parentParam];
  736.     }; // _PR._isParentParamPreloaded
  737.  
  738.     /**
  739.      * The this pointer is DataManager
  740.      * Idempotent
  741.      * @since v1.00a @version v1.00a
  742.      */
  743.     _PR._onFinishPreload = function() {
  744.         // It's better to be clear by using 2 variables even when 1 is suffice
  745.         this._areAllResourcesPreloaded = true;
  746.         Graphics.isDrawPreloadProgressBar = false;
  747.         //
  748.         var elapsedMs = Date.now() - _PR._preloadStartNow;
  749.         console.info("Preload time elapsed: " + elapsedMs + " milliseconds");
  750.     }; // _PR._onFinishPreload
  751.  
  752. })(DoubleX_RMMV.Preloaded_Resources); // DataManager
  753.  
  754. /*----------------------------------------------------------------------------
  755.  *    # Edit class: ImageManager
  756.  *      - Preloads all specified image resources upon game start
  757.  *----------------------------------------------------------------------------*/
  758.  
  759. (function(PR) {
  760.  
  761.     "use strict";
  762.  
  763.     PR.ImageManager = { orig: {}, new: {} };
  764.     var DM = PR.DataManager.new, _PR = PR.ImageManager.new;
  765.  
  766.     _PR._FOLDER_IMG_RESERVATION_ID = function(param, filename, hue) {
  767.         return JSON.stringify({ param: param, filename: filename, hue: hue });
  768.     }; // _PR._FOLDER_IMG_RESERVATION_ID
  769.     _PR._IMG_HUE_COUNT = function(imgs) {
  770.         return imgs.reduce(_PR._REDUCED_IMG_HUE_COUNT, 0);
  771.     }; // _PR._IMG_HUE_COUNT
  772.     _PR._IS_VALID_HUE = function(hue) {
  773.         return Number.isInteger(hue) && hue >= 0 && hue <= 360;
  774.     }; // _PR._IS_VALID_HUE
  775.     _PR._MISC_IMG_RESERVATION_ID = function(path, hue, smooth) {
  776.         return JSON.stringify({ path: path, hue: hue, smooth: smooth });
  777.     }; // _PR._MISC_IMG_RESERVATION_ID
  778.     _PR._REDUCED_IMG_HUE_COUNT = function(imgHueCount, img) {
  779.         return imgHueCount + img.hues.length;
  780.     }; // _PR._REDUCED_IMG_HUE_COUNT
  781.     _PR._SHOW_INVALID_IMG_HUE = function(name, hue) {
  782.         try { asdasdasdasdasdasd; /* Forcibly throws an error */ } catch (err) {
  783.             console.warn([
  784.                 "The hue of image " + name + " is " + hue + ".",
  785.                 "But hue must be an integer from 0 to 360 inclusive!",
  786.                 "The relevant stacktrace is as follows:",
  787.                 err.stack
  788.             ].join("\n"));
  789.         }
  790.     }; // _PR._SHOW_INVALID_IMG_HUE
  791.  
  792.     // Maps the preload image parameter to the ImageManager function to be used
  793.     _PR._PRELOAD_IMG_PARAM_FUNCS = {
  794.         preloadedAnimations: "reserveAnimation",
  795.         preloadedBattleBack1s: "reserveBattleback1",
  796.         preloadedBattleBack2s: "reserveBattleback2",
  797.         preloadedCharacters: "reserveCharacter",
  798.         preloadedEnemies: "reserveEnemy",
  799.         preloadedFaces: "reserveFace",
  800.         preloadedParallaxes: "reserveParallax",
  801.         preloadedPictures: "reservePicture",
  802.         preloadedSVActors: "reserveSvActor",
  803.         preloadedSVEnemies: "reserveSvEnemy",
  804.         preloadedSystem: "reserveSystem",
  805.         preloadedTilesets: "reserveTileset",
  806.         preloadedTitles1: "reserveTitle1",
  807.         preloadedTitles2: "reserveTitle2"
  808.     }; // _PR._PRELOAD_IMG_PARAM_FUNCS
  809.     //
  810.  
  811.     _PR.PRELOAD_IMG_PARAMS = Object.keys(_PR._PRELOAD_IMG_PARAM_FUNCS);
  812.     _PR.PRELOAD_IMG_PARAMS.push("preloadedMiscImages");
  813.  
  814.     // Marks which image parameters have finished preloading image files
  815.     ImageManager._isImgParamPreloaded = {}; // New private variable
  816.     //
  817.  
  818.     /**
  819.      * DON'T CALL THIS MANUALLY UNLESS YOU REALLY KNOW WHAT YOU'RE TRULY DOING
  820.      * Idempotent
  821.      * @interface @since v1.00a @version v1.00a
  822.      * @param {{String}} params - The mapping of all parameter name-value pairs
  823.      */
  824.     ImageManager.preloadImgs = function(params) {
  825.         if (DM.BOOL_PARAM_RESULT(params.IsPreloadImage)) {
  826.             return _PR._preloadImgs.call(this, params);
  827.         }
  828.         DataManager.onFinishPreload("IsPreloadImage");
  829.     }; // ImageManager.preloadImgs
  830.  
  831.     /**
  832.      * Idempotent
  833.      * @interface @since v1.00a @version v1.00a
  834.      * @param {Param} param - The name of the parameter of images in folder
  835.      * @param {String} filename - The name of the image to be preloaded
  836.      * @param {Hue} hue - The hue of the image to be preloaded
  837.      */
  838.     ImageManager.releasePreloadedFolderImg = function(param, filename, hue) {
  839.         var id = _PR._FOLDER_IMG_RESERVATION_ID(param, filename, hue);
  840.         this.releaseReservation(id);
  841.     }; // ImageManager.releasePreloadedFolderImg
  842.  
  843.     /**
  844.      * Idempotent
  845.      * @interface @since v1.00a @version v1.00a
  846.      * @param {String} path - The path of the image to be preloaded
  847.      * @param {Hue} hue - The hue of the image to be preloaded
  848.      * @param {Boolean} smooth - The smooth of the image to be preloaded
  849.      */
  850.     ImageManager.releasePreloadedMiscImg = function(path, hue, smooth) {
  851.         var reservationId = _PR._MISC_IMG_RESERVATION_ID(path, hue, smooth);
  852.         this.releaseReservation(reservationId);
  853.     }; // ImageManager.releasePreloadedMiscImg
  854.  
  855.     /**
  856.      * The this pointer is ImageManager
  857.      * Idempotent
  858.      * @since v1.00a @version v1.00a
  859.      * @param {{String}} params - The mapping of all parameter name-value pairs
  860.      */
  861.     _PR._preloadImgs = function(params) {
  862.         _PR._preloadImgsInFolders.call(this, params, interval);
  863.         var interval = params.preloadImageMSInterval;
  864.         _PR._preloadMiscImgs.call(this, params.preloadedMiscImages, interval);
  865.     }; // _PR._preloadImgs
  866.  
  867.     /**
  868.      * The this pointer is ImageManager
  869.      * Idempotent
  870.      * @since v1.00a @version v1.00a
  871.      * @param {{String}} params - The mapping of all parameter name-value pairs
  872.      */
  873.     _PR._preloadImgsInFolders = function(params) {
  874.         // This method's called only once anyway so it won't hurt performance
  875.         Object.keys(_PR._PRELOAD_IMG_PARAM_FUNCS).forEach(function(param) {
  876.             _PR._preloadImgsInFolder.call(this, params, param);
  877.         }, this);
  878.         // Not binding _preloadImgsInFolder is to minimize preload memory leaks
  879.     }; // _PR._preloadImgsInFolders
  880.  
  881.     /**
  882.      * The this pointer is ImageManager
  883.      * Idempotent
  884.      * @since v1.00a @version v1.00a
  885.      * @param {{String}} params - The mapping of all parameter name-value pairs
  886.      * @param {Param} param - The name of the parameter of images in folder
  887.      */
  888.     _PR._preloadImgsInFolder = function(params, param) {
  889.         var imgs = params[param], interval = params.preloadImageMSInterval;
  890.         // Not binding _preloadImgHues is to minimize preload memory leaks
  891.         var preloadHuesFunc = function(image, nextFunc) {
  892.             var self = ImageManager;
  893.             _PR._preloadImgHues.call(self, param, interval, image, nextFunc);
  894.         };
  895.         //
  896.         _PR._runPreloadImgQueue.call(this, imgs, param, preloadHuesFunc);
  897.     }; // _PR._preloadImgsInFolder
  898.  
  899.     /**
  900.      * The this pointer is ImageManager
  901.      * Idempotent
  902.      * @since v1.00a @version v1.00a
  903.      * @param {Param} param - The name of the parameter of images in folder
  904.      * @param {Number} interval - The number of milliseconds as interval
  905.      * @param {{String, [Hue]}} img - The image information to preload image
  906.      * @param {()} nextFunc - The function as the image preload queue
  907.      */
  908.     _PR._preloadImgHues = function(param, interval, img, nextFunc) {
  909.         // Not binding _callReserveImgFunc is to minimize preload memory leaks
  910.         var filename = img.filename, hues = img.hues, callFunc = function(hue) {
  911.             _PR._callReserveImgFunc.call(ImageManager, param, filename, hue);
  912.         };
  913.         //
  914.         _PR._runPreloadHueQueue.call(this, hues, nextFunc, callFunc, interval);
  915.     }; // _PR._preloadImgHues
  916.  
  917.     /**
  918.      * The this pointer is ImageManager
  919.      * Idempotent
  920.      * @since v1.00a @version v1.00a
  921.      * @param {Param} param - The name of the parameter of images in folder
  922.      * @param {String} filename - The name of the image to be preloaded
  923.      * @param {Hue} hue - The hue of the image to be preloaded
  924.      */
  925.     _PR._callReserveImgFunc = function(param, filename, hue) {
  926.         if (!_PR._IS_VALID_HUE(hue)) { // Users might input raw values directly
  927.             return _PR._SHOW_INVALID_IMG_HUE(filename, hue);
  928.         }
  929.         // Users might input raw values or empty images directly
  930.         if (filename) _PR._reserveImg.call(this, param, filename, hue);
  931.         //
  932.     }; // _PR._callReserveImgFunc
  933.  
  934.     /**
  935.      * The this pointer is ImageManager
  936.      * Idempotent
  937.      * @since v1.00a @version v1.00a
  938.      * @param {Param} param - The name of the parameter of images in folder
  939.      * @param {String} filename - The name of the image to be preloaded
  940.      * @param {Hue} hue - The hue of the image to be preloaded
  941.      */
  942.     _PR._reserveImg = function(param, filename, hue) {
  943.         var id = _PR._FOLDER_IMG_RESERVATION_ID(param, filename, hue);
  944.         // _PR._PRELOAD_IMG_PARAM_FUNCS[param] is the reserve image function
  945.         this[_PR._PRELOAD_IMG_PARAM_FUNCS[param]](filename, hue, id);
  946.         //
  947.     }; // _PR._reserveImg
  948.  
  949.     /**
  950.      * The this pointer is ImageManager
  951.      * Idempotent
  952.      * @since v1.00a @version v1.00a
  953.      * @param {[String]} imgs - The list of misc image file paths
  954.      * @param {Number} interval - The number of milliseconds as interval
  955.      */
  956.     _PR._preloadMiscImgs = function(imgs, interval) {
  957.         // Not binding _preloadMiscImgHues is to minimize preload memory leaks
  958.         var preloadHuesFunc = function(img, nextFunc) {
  959.             _PR._preloadMiscImgHues.call(ImageManager, interval, img, nextFunc);
  960.         };
  961.         //
  962.         var param = "preloadedMiscImages";
  963.         _PR._runPreloadImgQueue.call(this, imgs, param, preloadHuesFunc);
  964.     }; // _PR._preloadMiscImgs
  965.  
  966.     /**
  967.      * The this pointer is ImageManager
  968.      * Idempotent
  969.      * @since v1.00a @version v1.00a
  970.      * @param {[{String, [Hue], Boolean]}} imgs - image files to be preloaded
  971.      * @param {Param} param - The name of the parameter of images in folder
  972.      * @param {(**)} preloadFunc - The function preloading the image hues
  973.      */
  974.     _PR._runPreloadImgQueue = function(imgs, param, preloadHuesFunc) {
  975.         DataManager.preloadResourceNum += _PR._IMG_HUE_COUNT(imgs);
  976.         // Otherwise loading too many images all at once can be too slow
  977.         (function preload() {
  978.             if (imgs.length <= 0) {
  979.                 return _PR._onFinishPreloadImgs.call(ImageManager, param);
  980.             }
  981.             preloadHuesFunc(imgs.shift(), preload);
  982.         })();
  983.         /** @todo Thinks of a way to eliminate the inner function memory leak */
  984.     }; // _PR._runPreloadImgQueue
  985.  
  986.     /**
  987.      * The this pointer is ImageManager
  988.      * Idempotent
  989.      * @since v1.00a @version v1.00a
  990.      * @param {Param} param - The name of the parameter of images in folder
  991.      */
  992.     _PR._onFinishPreloadImgs = function(param) {
  993.         console.info("All images specified in " + param + " are preloaded.");
  994.         this._isImgParamPreloaded[param] = true;
  995.         if (!_PR._areAllImgParamsPreloaded.call(this)) return;
  996.         DataManager.onFinishPreload("IsPreloadImage");
  997.     }; // _PR._onFinishPreloadImgs
  998.  
  999.     /**
  1000.      * The this pointer is ImageManager
  1001.      * Idempotent
  1002.      * @since v1.00a @version v1.00a
  1003.      * @returns {Boolean} The check result
  1004.      */
  1005.     _PR._areAllImgParamsPreloaded = function() {
  1006.         return _PR.PRELOAD_IMG_PARAMS.every(_PR._isImgParamPreloaded, this);
  1007.     }; // _PR._areAllImgParamsPreloaded
  1008.  
  1009.     /**
  1010.      * The this pointer is ImageManager
  1011.      * Idempotent
  1012.      * @since v1.00a @version v1.00a
  1013.      * @param {Param} param - The name of the parameter of images in folder
  1014.      * @returns {Boolean} The check result
  1015.      */
  1016.     _PR._isImgParamPreloaded = function(param) {
  1017.         return this._isImgParamPreloaded[param];
  1018.     }; // _PR._isImgParamPreloaded
  1019.  
  1020.     /**
  1021.      * The this pointer is ImageManager
  1022.      * Idempotent
  1023.      * @since v1.00a @version v1.00a
  1024.      * @param {Number} interval - The number of milliseconds as interval
  1025.      * @param {{String, [Hue], Boolean}} img - Misc image file to be preloaded
  1026.      * @param {()} nextFunc - The function as the image preload queue
  1027.      */
  1028.     _PR._preloadMiscImgHues = function(interval, img, nextFunc) {
  1029.         // smooth is either true or false so an array of smoothes' not needed
  1030.         var path = img.path, smooth = img.smooth, hues = img.hues;
  1031.         //
  1032.         // Not binding _preloadMiscImg is to minimize preload memory leaks
  1033.         var preload = function(hue) {
  1034.             _PR._preloadMiscImg.call(ImageManager, path, smooth, hue);
  1035.         };
  1036.         //
  1037.         _PR._runPreloadHueQueue.call(this, hues, nextFunc, preload, interval);
  1038.     }; // _PR._preloadMiscImgHues
  1039.  
  1040.     /**
  1041.      * The this pointer is ImageManager
  1042.      * Idempotent
  1043.      * @since v1.00a @version v1.00a
  1044.      * @param {[Hue]} hues - The list of hues of the current image to preload
  1045.      * @param {()} nextFunc - The as the image preload queue
  1046.      * @param {(**)} preloadFunc - The function preloading the image hue
  1047.      * @param {Number} interval - The number of milliseconds as interval
  1048.      */
  1049.     _PR._runPreloadHueQueue = function(hues, nextFunc, preloadFunc, interval) {
  1050.         // Using inner function's more performant by avoiding repeated bindings
  1051.         var isSetTimeout = interval > 0; // It's better than redundant checkings
  1052.         (function preload() {
  1053.             if (hues.length <= 0) return nextFunc();
  1054.             // Extracting them into a function can hurt perload time and memory
  1055.             preloadFunc(hues.shift());
  1056.             DataManager.preloadResourceProgress++;
  1057.             isSetTimeout ? setTimeout(preload, interval) : preload();
  1058.             //
  1059.         })();
  1060.         /** @todo Thinks of a way to eliminate the inner function memory leak */
  1061.     }; // _PR._runPreloadHueQueue
  1062.  
  1063.     /**
  1064.      * The this pointer is ImageManager
  1065.      * Idempotent
  1066.      * @since v1.00a @version v1.00a
  1067.      * @param {String} path - The path of the image to be preloaded
  1068.      * @param {Boolean} smooth - The smooth of the image to be preloaded
  1069.      * @param {Hue} hue - The hue of the image to be preloaded
  1070.      */
  1071.     _PR._preloadMiscImg = function(path, smooth, hue) {
  1072.         if (!_PR._IS_VALID_HUE(hue)) { // Users might input raw values directly
  1073.             return _PR._SHOW_INVALID_IMG_HUE(path, hue);
  1074.         }
  1075.         _PR._preloadValidMiscImg.call(this, path, hue, smooth);
  1076.     }; // _PR._preloadMiscImg
  1077.  
  1078.     /**
  1079.      * The this pointer is ImageManager
  1080.      * Idempotent
  1081.      * @since v1.00a @version v1.00b
  1082.      * @param {String} path - The path of the image to be preloaded
  1083.      * @param {Hue} hue - The hue of the image to be preloaded
  1084.      * @param {Boolean} smooth - The smooth of the image to be preloaded
  1085.      */
  1086.     _PR._preloadValidMiscImg = function(path, hue, smooth) {
  1087.         // It's to support nested folders for misc images only
  1088.         var filename = path.replace(/.*\//gmi, "");
  1089.         var folder = "img/" + path.replace(new RegExp(filename + "$"), "");
  1090.         //
  1091.         // Users might input raw values or empty images directly
  1092.         if (!folder && filename) return; // Empty images are allowed
  1093.         //
  1094.         // Extracting them into a method would pass redundant variables
  1095.         var reservationId = _PR._MISC_IMG_RESERVATION_ID(path, hue, smooth);
  1096.         this.reserveBitmap(folder, filename, hue, smooth, reservationId);
  1097.         //
  1098.     }; // _PR._preloadValidMiscImg
  1099.  
  1100. })(DoubleX_RMMV.Preloaded_Resources); // ImageManager
  1101.  
  1102. /*----------------------------------------------------------------------------
  1103.  *    # Edit class: AudioManager
  1104.  *      - Caches all previously created WebAudios and preloads them all
  1105.  *----------------------------------------------------------------------------*/
  1106.  
  1107. (function(PR) {
  1108.  
  1109.     "use strict";
  1110.  
  1111.     PR.AudioManager = { orig: {}, new: {} };
  1112.     var DM = PR.DataManager.new;
  1113.     var _AM = PR.AudioManager.orig, _PR = PR.AudioManager.new;
  1114.  
  1115.     _PR._PRELOAD_AUDIO_PARAM_FOLDERS = {
  1116.         preloadedBGMs: "bgm",
  1117.         preloadedBGSs: "bgs",
  1118.         preloadedMEs: "me",
  1119.         preloadedSEs: "se"
  1120.     }; // _PR._PRELOAD_AUDIO_PARAM_FOLDERS
  1121.  
  1122.     _PR._PRELOAD_AUDIO_PARAMS = Object.keys(_PR._PRELOAD_AUDIO_PARAM_FOLDERS);
  1123.     _PR._PRELOAD_AUDIO_PARAMS.push("preloadedStaticSEs");
  1124.     _PR._PRELOAD_AUDIO_PARAMS.push("preloadedMiscAudios");
  1125.  
  1126.     // Marks which audio parameters have finished preloading audio files
  1127.     AudioManager._isAudioParamPreloaded = {}; // New private variable
  1128.     //
  1129.     // Stores the mapping from all urls to their preloaded web audios
  1130.     AudioManager._preloadedWebAudios = {}; // New private variable
  1131.     //
  1132.  
  1133.     _AM.createBuffer = AudioManager.createBuffer;
  1134.     _PR.createBuffer = AudioManager.createBuffer = function(folder, name) {
  1135.     // v1.00a - v1.00a; Rewritten
  1136.         // Rewritten to cache the already created WebAudio instead
  1137.         var url = _PR._bufferUrl.call(this, folder, name);
  1138.         if (_PR._isHTML5Audio.call(this, folder)) {
  1139.             return _PR._html5Audio.call(this, url);
  1140.         } else return _PR._webAudio.call(this, url);
  1141.         //
  1142.     }; // AudioManager.createBuffer
  1143.  
  1144.     /**
  1145.      * DON'T CALL THIS MANUALLY UNLESS YOU REALLY KNOW WHAT YOU'RE TRULY DOING
  1146.      * Idempotent
  1147.      * @interface @since v1.00a @version v1.00a
  1148.      * @param {{String}} params - The mapping of all parameter name-value pairs
  1149.      */
  1150.     AudioManager.preloadAudios = function(params) {
  1151.         if (DM.BOOL_PARAM_RESULT(params.IsPreloadAudio)) {
  1152.             return _PR._preloadAudios.call(this, params);
  1153.         }
  1154.         DataManager.onFinishPreload("IsPreloadAudio");
  1155.     }; // AudioManager.preloadAudios
  1156.  
  1157.     /**
  1158.      * The this pointer is AudioManager
  1159.      * Idempotent
  1160.      * @since v1.00a @version v1.00a
  1161.      * @param {String} folder - The name of the folder under the audio folder
  1162.      * @param {String}  name - The name of the audio to create its buffer
  1163.      */
  1164.     AudioManager.invalidateCachedWebAudio = function(folder, name) {
  1165.         var url = _PR._bufferUrl.call(this, folder, name);
  1166.         // Using isStaticSe might risk name collision for non se audios
  1167.         if (!this._staticBuffers.contains(this._preloadedWebAudios[url])) {
  1168.             delete this._preloadedWebAudios[url];
  1169.         }
  1170.         //
  1171.     }; // AudioManager.invalidateCachedWebAudio
  1172.  
  1173.     /**
  1174.      * The this pointer is AudioManager
  1175.      * Nullipotent
  1176.      * @since v1.00a @version v1.00a
  1177.      * @param {String} folder - The name of the folder under the audio folder
  1178.      * @param {String}  name - The name of the audio to create its buffer
  1179.      * @returns {String} The url of the audio buffer to be created
  1180.      */
  1181.     _PR._bufferUrl = function(folder, name) {
  1182.         var ext = this.audioFileExt();
  1183.         return this._path + folder + "/" + encodeURIComponent(name) + ext;
  1184.     }; // _PR._bufferUrl
  1185.  
  1186.     /**
  1187.      * The this pointer is AudioManager
  1188.      * Nullipotent
  1189.      * @since v1.00a @version v1.00a
  1190.      * @param {String} folder - The name of the folder under the audio folder
  1191.      * @returns {Boolean} The check result
  1192.      */
  1193.     _PR._isHTML5Audio = function(folder) {
  1194.         // shouldUseHtml5Audio is false now but might be changed by plugins
  1195.         return folder === "bgm" && this.shouldUseHtml5Audio();
  1196.         //
  1197.     }; // _PR._isHTML5Audio
  1198.  
  1199.     /**
  1200.      * The this pointer is AudioManager
  1201.      * Nullipotent
  1202.      * @since v1.00a @version v1.00a
  1203.      * @param {String} url - The url of the audio buffer to be created
  1204.      * @returns {Html5Audio} The Html5Audio as the audio buffer to be created
  1205.      */
  1206.     _PR._html5Audio = function(url) {
  1207.         Html5Audio.setup(this._blobUrl || url);
  1208.         return Html5Audio;
  1209.     }; // _PR._html5Audio
  1210.  
  1211.     /**
  1212.      * The this pointer is AudioManager
  1213.      * Idempotent
  1214.      * @since v1.00a @version v1.00a
  1215.      * @param {String} url - The url of the audio buffer to be created
  1216.      * @returns {WebAudio} The WebAudio as the audio buffer to be created
  1217.      */
  1218.     _PR._webAudio = function(url) {
  1219.         if (!this._preloadedWebAudios[url]) {
  1220.             this._preloadedWebAudios[url] = new WebAudio(url);
  1221.         }
  1222.         return this._preloadedWebAudios[url];
  1223.     }; // _PR._webAudio
  1224.  
  1225.     /**
  1226.      * The this pointer is AudioManager
  1227.      * Idempotent
  1228.      * @since v1.00a @version v1.00a
  1229.      * @param {{String}} params - The mapping of all parameter name-value pairs
  1230.      */
  1231.     _PR._preloadAudios = function(params) {
  1232.         _PR._preloadAudioInFolders.call(this, params);
  1233.          var interval = params.preloadAudioMSInterval;
  1234.         _PR._preloadStaticSEs.call(this, params.preloadedStaticSEs, interval);
  1235.         _PR._preloadMiscAudios.call(this, params.preloadedMiscAudios, interval);
  1236.     }; // _PR._preloadAudios
  1237.  
  1238.     /**
  1239.      * The this pointer is AudioManager
  1240.      * Idempotent
  1241.      * @since v1.00a @version v1.00a
  1242.      * @param {{String}} params - The mapping of all parameter name-value pairs
  1243.      */
  1244.     _PR._preloadAudioInFolders = function(params) {
  1245.         // This method's called only once anyway so it won't hurt performance
  1246.         Object.keys(_PR._PRELOAD_AUDIO_PARAM_FOLDERS).forEach(function(param) {
  1247.             _PR._preloadAudioInFolder.call(this, params, param);
  1248.         }, this);
  1249.         // Not binding _preloadAudioInFolder is to minimize preload memory leaks
  1250.     }; // _PR._preloadAudioInFolders
  1251.  
  1252.     /**
  1253.      * The this pointer is AudioManager
  1254.      * Idempotent
  1255.      * @since v1.00a @version v1.00a
  1256.      * @param {{String}} params - The mapping of all parameter name-value pairs
  1257.      * @param {Param} param - The name of the parameter of audios in folder
  1258.      */
  1259.     _PR._preloadAudioInFolder = function(params, param) {
  1260.         var folder = _PR._PRELOAD_AUDIO_PARAM_FOLDERS[param];
  1261.         // Not binding _preloadAudio is to minimize preload memory leaks
  1262.         var func = function(audio) {
  1263.             _PR._preloadAudio.call(AudioManager, folder, audio);
  1264.         }, audios = params[param];
  1265.         //
  1266.         var interval = params.preloadAudioMSInterval;
  1267.         _PR._runPreloadAudioQueue.call(this, audios, param, func, interval);
  1268.     }; // _PR._preloadAudioInFolder
  1269.  
  1270.     /**
  1271.      * The this pointer is AudioManager
  1272.      * Idempotent
  1273.      * @since v1.00a @version v1.00a
  1274.      * @param {[String]} audios - The list of misc audio file paths
  1275.      * @param {Number} interval - The number of milliseconds as interval
  1276.      */
  1277.     _PR._preloadStaticSEs = function(audios, interval) {
  1278.         // Not binding loadStaticSe is to minimize preload memory leaks
  1279.         var func = function(audio) { AudioManager.loadStaticSe(audio); };
  1280.         //
  1281.         var param = "preloadedStaticSEs";
  1282.         _PR._runPreloadAudioQueue.call(this, audios, param, func, interval);
  1283.     }; // _PR._preloadStaticSEs
  1284.  
  1285.     /**
  1286.      * The this pointer is AudioManager
  1287.      * Idempotent
  1288.      * @since v1.00a @version v1.00a
  1289.      * @param {[String]} audios - The list of misc audio file paths
  1290.      * @param {Number} interval - The number of milliseconds as interval
  1291.      */
  1292.     _PR._preloadMiscAudios = function(audios, interval) {
  1293.         // Not binding _preloadMiscAudio is to minimize preload memory leaks
  1294.         var func = function(audio) {
  1295.             _PR._preloadMiscAudio.call(AudioManager, audio);
  1296.         }, param = "preloadedMiscAudios";
  1297.         //
  1298.         _PR._runPreloadAudioQueue.call(this, audios, param, func, interval);
  1299.     }; // _PR._preloadMiscAudios
  1300.  
  1301.     /**
  1302.      * The this pointer is AudioManager
  1303.      * Idempotent
  1304.      * @since v1.00a @version v1.00a
  1305.      * @param {String} audios - The list of filenames of the audios to preload
  1306.      * @param {Param} param - The name of the parameter of audios in folder
  1307.      * @param {(**)} preloadFunc - The function preloading the audio file
  1308.      * @param {Number} interval - The number of milliseconds as interval
  1309.      */
  1310.     _PR._runPreloadAudioQueue = function(audios, param, preloadFunc, interval) {
  1311.         DataManager.preloadResourceNum += audios.length;
  1312.         // Using inner function's more performant by avoiding repeated bindings
  1313.         var isSetTimeout = interval > 0; // It's better than redundant checkings
  1314.         (function preload() {
  1315.             if (audios.length <= 0) {
  1316.                 return _PR._onFinishPreloadAudios.call(AudioManager, param);
  1317.             }
  1318.             // Extracting them into a function can hurt perload time and memory
  1319.             preloadFunc(audios.shift());
  1320.             DataManager.preloadResourceProgress++;
  1321.             isSetTimeout ? setTimeout(preload, interval) : preload();
  1322.             //
  1323.         })();
  1324.         /** @todo Thinks of a way to eliminate the inner function memory leak */
  1325.     }; // _PR._runPreloadAudioQueue
  1326.  
  1327.     /**
  1328.      * The this pointer is AudioManager
  1329.      * Idempotent
  1330.      * @since v1.00a @version v1.00a
  1331.      * @param {Param} param - The name of the parameter of audios in folder
  1332.      */
  1333.     _PR._onFinishPreloadAudios = function(param) {
  1334.         console.info("All audios specified in " + param + " are preloaded.");
  1335.         this._isAudioParamPreloaded[param] = true;
  1336.         if (!_PR._areAllAudioParamsPreloaded.call(this)) return;
  1337.         DataManager.onFinishPreload("IsPreloadAudio");
  1338.     }; // _PR._onFinishPreloadAudios
  1339.  
  1340.     /**
  1341.      * The this pointer is AudioManager
  1342.      * Idempotent
  1343.      * @since v1.00a @version v1.00a
  1344.      * @returns {Boolean} The check result
  1345.      */
  1346.     _PR._areAllAudioParamsPreloaded = function() {
  1347.         var preloadAudioParams = _PR._PRELOAD_AUDIO_PARAMS;
  1348.         return preloadAudioParams.every(_PR._isAudioParamPreloaded, this);
  1349.     }; // _PR._areAllAudioParamsPreloaded
  1350.  
  1351.     /**
  1352.      * The this pointer is AudioManager
  1353.      * Idempotent
  1354.      * @since v1.00a @version v1.00a
  1355.      * @param {Param} param - The name of the parameter of audios in folder
  1356.      * @returns {Boolean} The check result
  1357.      */
  1358.     _PR._isAudioParamPreloaded = function(param) {
  1359.         return this._isAudioParamPreloaded[param];
  1360.     }; // _PR._isAudioParamPreloaded
  1361.  
  1362.     /**
  1363.      * The this pointer is AudioManager
  1364.      * Idempotent
  1365.      * @since v1.00a @version v1.00a
  1366.      * @param {String} preloadedMiscAudio - The misc audio file to be preloaded
  1367.      */
  1368.     _PR._preloadMiscAudio = function(preloadedMiscAudio) {
  1369.         var args = preloadedMiscAudio.split("/");
  1370.         _PR._preloadAudio.call(this, args[0], args[1]);
  1371.     }; // _PR._preloadMiscAudio
  1372.  
  1373.     /**
  1374.      * The this pointer is AudioManager
  1375.      * Idempotent
  1376.      * @since v1.00a @version v1.00a
  1377.      * @param {String} folder - The name of the folder under the audio folder
  1378.      * @param {String} name - The name of the audio to be preloaded
  1379.      */
  1380.     _PR._preloadAudio = function(folder, name) {
  1381.         // Users might input raw values or empty audios directly
  1382.         if (folder && name) this.createBuffer(folder, name);
  1383.         //
  1384.     }; // _PR._preloadAudio
  1385.  
  1386. })(DoubleX_RMMV.Preloaded_Resources); // AudioManager
  1387.  
RAW Paste Data