Advertisement
Double_X

doublex rmmv preloaded resources v100b

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