Advertisement
ezmash

Cache Manager (MV)

Nov 30th, 2015
1,686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // Cache Manager
  3. // by Shaz
  4. // Last Updated: 2015.10.30
  5. //=============================================================================
  6.  
  7. /*:
  8.  * @plugindesc Selectively clear the image cache for memory usage improvements
  9.  * @author Shaz
  10.  *
  11.  * @param Mobile only
  12.  * @desc Cache management only runs on mobile platforms (N to run on all platforms)
  13.  * @default Y
  14.  *
  15.  * @param --------------------------------------------
  16.  *
  17.  * @param Clear All
  18.  * @desc Clears all cached images except System (if Y, ignore remaining parameters)
  19.  * @default Y
  20.  *
  21.  * @param Clear Animations
  22.  * @desc Clear Animation images
  23.  * @default Y
  24.  *
  25.  * @param Clear Battlebacks
  26.  * @desc Clear Battleback images
  27.  * @default Y
  28.  *
  29.  * @param Clear Battlers
  30.  * @desc Clear Battler images
  31.  * @default Y
  32.  *
  33.  * @param Clear Characters
  34.  * @desc Clear Character images
  35.  * @default Y
  36.  *
  37.  * @param Clear Faces
  38.  * @desc Clear Face images
  39.  * @default Y
  40.  *
  41.  * @param Clear Parallaxes
  42.  * @desc Clear Parallax images
  43.  * @default Y
  44.  *
  45.  * @param Clear Pictures
  46.  * @desc Clear Picture images
  47.  * @default Y
  48.  *
  49.  * @param Clear System
  50.  * @desc Clear System images (not recommended)
  51.  * @default N
  52.  *
  53.  * @param Clear Tilesets
  54.  * @desc Clear Tileset images
  55.  * @default Y
  56.  *
  57.  * @param Clear Titles
  58.  * @desc Clear Title images
  59.  * @default Y
  60.  *
  61.  * @param Custom Images
  62.  * @desc List custom image paths to clear, separated by ; (img/fogs; img/busts)
  63.  * @default
  64.  *
  65.  * @help This plugin does not provide plugin commands.
  66.  *
  67.  * This plugin allows you to specify how often the image cache should be
  68.  * cleared, and what kind of images should be cleared.  This can be used
  69.  * to improve memory usage on low-memory platforms, but will also result
  70.  * in more frequent loading delays.
  71.  *
  72.  * If not using the 'clear all' default option, and you have plugins that
  73.  * use their own custom image paths, list those paths in the Custom Images
  74.  * parameter to clear any images cached from those locations.
  75.  *
  76.  */
  77.  
  78. (function() {
  79.   var params = PluginManager.parameters('CacheManager');
  80.   var mobile = (params['Mobile only'] || 'Y').toUpperCase().substring(0,1) === 'Y';
  81.   var clearAll = (params['Clear All'] || 'Y').toUpperCase().substring(0,1) === 'Y';
  82.   if (!clearAll) {
  83.     pathsToClear = [];
  84.     if ((params['Clear Animations'] || 'Y').toUpperCase().substring(0,1) === 'Y')
  85.       pathsToClear.push('img/animations');
  86.     if ((params['Clear Battlebacks'] || 'Y').toUpperCase().substring(0,1) === 'Y')
  87.       pathsToClear.push('img/battlebacks1', 'img/battlebacks2');
  88.     if ((params['Clear Battlers'] || 'Y').toUpperCase().substring(0,1) === 'Y')
  89.       pathsToClear.push('img/enemies', 'img/sv-actors', 'img/sv-enemies');
  90.     if ((params['Clear Characters'] || 'Y').toUpperCase().substring(0,1) === 'Y')
  91.       pathsToClear.push('img/characters');
  92.     if ((params['Clear Faces'] || 'Y').toUpperCase().substring(0,1) === 'Y')
  93.       pathsToClear.push('img/faces');
  94.     if ((params['Clear Parallaxes'] || 'Y').toUpperCase().substring(0,1) === 'Y')
  95.       pathsToClear.push('img/parallaxes');
  96.     if ((params['Clear Pictures'] || 'Y').toUpperCase().substring(0,1) === 'Y')
  97.       pathsToClear.push('img/pictures');
  98.     if ((params['Clear System'] || 'N').toUpperCase().substring(0,1) === 'Y')
  99.       pathsToClear.push('img/system');
  100.     if ((params['Clear Tilesets'] || 'Y').toUpperCase().substring(0,1) === 'Y')
  101.       pathsToClear.push('img/tilesets');
  102.     if ((params['Clear Titles'] || 'Y').toUpperCase().substring(0,1) === 'Y')
  103.       pathsToClear.push('img/titles1', 'img/titles2');
  104.     (params['Custom Images'] || '').split(';').forEach(function(path) { if (path) pathsToClear.push(path.trim()) }.bind(this));
  105.   }
  106.  
  107.   ImageManager.clear = function() {
  108.     for (image in this._cache) {
  109.       if (clearAll && image.indexOf('img/system') === -1)
  110.         this.removeFromCache(image);
  111.       else if (!clearAll && pathsToClear.some(function(path) { return image.indexOf(path) > -1}.bind(this)))
  112.         this.removeFromCache(image);
  113.     }
  114.   };
  115.  
  116.   ImageManager.removeFromCache = function(image) {
  117.     if (image !== 'null') {
  118.       if (this._cache.hasOwnProperty(image) && this._cache[image] instanceof Bitmap) {
  119.         if (this._cache[image].baseTexture) {
  120.           this._cache[image].baseTexture.destroy();
  121.         }
  122.       }
  123.       delete this._cache[image];
  124.     }
  125.   };
  126.  
  127.   var _Scene_Map_create = Scene_Map.prototype.create;
  128.   Scene_Map.prototype.create = function() {
  129.     _Scene_Map_create.call(this);
  130.     if (this._transfer && (!mobile || Utils.isMobileDevice())) {
  131.       ImageManager.clear();
  132.     }
  133.   };
  134. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement