Advertisement
Archeia

Save Testers

Jan 27th, 2017
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*:
  2.  * Save Testers
  3.  *
  4.  * @plugindesc Allows testers saving/loading their own paces in different folders.
  5.  * @author Dr.Yami and Archeia
  6.  *
  7.  * @help This plugin does not provide plugin commands.
  8.  * Create a TESTER_ID.txt on your desktop. Place a name inside it.
  9.  * If you place the Master Key (Nessiah by default), you will be able to access
  10.  * all your beta tester's save files.
  11.  *
  12.  * @param Master Key
  13.  * @desc Project master, uses to access testers savefiles.
  14.  * Default: Nessiah
  15.  * @default Nessiah
  16.  *
  17.  */
  18.  
  19. var DrYami = DrYami || {};
  20. DrYami.SaveTesters = {};
  21. DrYami.SaveTesters.Params = PluginManager.parameters('SaveTesters');
  22.  
  23. DrYami.SaveTesters.MasterKey = String(DrYami.SaveTesters.Params['Master Key']);
  24.  
  25. (function () {
  26.     if (!Utils.isNwjs()) return;
  27.     var fs = require('fs');
  28.     var keyFilename = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'] + '/Desktop/TESTER_ID.txt';
  29.  
  30.     StorageManager.testerKey = "";
  31.  
  32.     var _loadDatabase = DataManager.loadDatabase;
  33.     DataManager.loadDatabase = function () {
  34.         _loadDatabase.call(this);
  35.         StorageManager.testerKey = StorageManager.getTesterKey();
  36.         StorageManager.createSaveFolder();
  37.     };
  38.  
  39.     StorageManager.getTesterKey = function () {
  40.         if (!fs.existsSync(keyFilename)) {
  41.             this.generateTesterKey();
  42.         }
  43.         return String(fs.readFileSync(keyFilename));
  44.     };
  45.  
  46.     StorageManager.generateTesterKey = function () {
  47.         var key = this.generateRandomString();
  48.         while (fs.existsSync(this.localFileDirectoryPath() + key)) {
  49.             key = this.generateRandomString();
  50.         }
  51.         fs.writeFileSync(keyFilename, key);
  52.     };
  53.  
  54.     StorageManager.generateRandomString = function (length) {
  55.         length = length || 8;
  56.         var text = "";
  57.         var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  58.  
  59.         for (var i = 0; i < length; i++)
  60.             text += chars.charAt(Math.floor(Math.random() * chars.length));
  61.  
  62.         return text;
  63.     };
  64.  
  65.     StorageManager.localFilePath = function (savefileId) {
  66.         var name;
  67.         var key = StorageManager.testerKey;
  68.         if (savefileId < 0) {
  69.             name = 'config.rpgsave';
  70.         } else if (savefileId === 0) {
  71.             name = 'global.rpgsave';
  72.         } else {
  73.             name = 'file%1.rpgsave'.format(savefileId);
  74.         }
  75.         return this.localFileDirectoryPath() + key + '/' + name;
  76.     };
  77.  
  78.     StorageManager.createSaveFolder = function () {
  79.         var orgdirPath = this.localFileDirectoryPath();
  80.         var dirPath = this.localFileDirectoryPath() + StorageManager.testerKey;
  81.         if (!fs.existsSync(orgdirPath)) fs.mkdirSync(orgdirPath);
  82.         if (!fs.existsSync(dirPath)) fs.mkdirSync(dirPath);
  83.     };
  84.  
  85.     StorageManager.getAllTesterKeys = function () {
  86.         var srcpath = this.localFileDirectoryPath();
  87.         var path = require('path');
  88.         return fs.readdirSync(srcpath).filter(function (file) {
  89.             return fs.statSync(path.join(srcpath, file)).isDirectory();
  90.         });
  91.     };
  92.  
  93.     StorageManager.isMasterKey = function () {
  94.         return StorageManager.testerKey === DrYami.SaveTesters.MasterKey;
  95.     };
  96.  
  97.     function Window_SaveMasterKey() {
  98.         this.initialize.apply(this, arguments);
  99.     }
  100.  
  101.     Window_SaveMasterKey.prototype = Object.create(Window_Command.prototype);
  102.     Window_SaveMasterKey.prototype.constructor = Window_SaveMasterKey;
  103.  
  104.     Window_SaveMasterKey.prototype.initialize = function () {
  105.         Window_Command.prototype.initialize.call(this, 0, 0);
  106.         this.updatePlacement();
  107.     };
  108.  
  109.     Window_SaveMasterKey.prototype.windowWidth = function () {
  110.         return 320;
  111.     };
  112.  
  113.     Window_SaveMasterKey.prototype.updatePlacement = function () {
  114.         this.x = (Graphics.boxWidth - this.width) / 2;
  115.         this.y = (Graphics.boxHeight - this.height) / 2;
  116.     };
  117.  
  118.     Window_SaveMasterKey.prototype.makeCommandList = function () {
  119.         var keys = StorageManager.getAllTesterKeys();
  120.         for (var i = 0; i < keys.length; i++) {
  121.             var key = keys[i];
  122.             this.addCommand(key, key);
  123.         }
  124.     };
  125.  
  126.     var _Scene_Boot_start = Scene_Boot.prototype.start;
  127.     Scene_Boot.prototype.start = function () {
  128.         if (!DataManager.isBattleTest() && !DataManager.isEventTest()
  129.             && StorageManager.isMasterKey()) {
  130.             this.checkPlayerLocation();
  131.             DataManager.setupNewGame();
  132.             SceneManager.goto(Scene_SaveMaster);
  133.             this.updateDocumentTitle();
  134.             Window_TitleCommand.initCommandPosition();
  135.         } else {
  136.             _Scene_Boot_start.call(this);
  137.         }
  138.     };
  139.  
  140.     function Scene_SaveMaster() {
  141.         this.initialize.apply(this, arguments);
  142.     }
  143.  
  144.     Scene_SaveMaster.prototype = Object.create(Scene_Base.prototype);
  145.     Scene_SaveMaster.prototype.constructor = Scene_SaveMaster;
  146.  
  147.     Scene_SaveMaster.prototype.initialize = function () {
  148.         Scene_Base.prototype.initialize.call(this);
  149.     };
  150.  
  151.     Scene_SaveMaster.prototype.create = function () {
  152.         Scene_Base.prototype.create.call(this);
  153.         this.createWindowLayer();
  154.         this.createWindows();
  155.     };
  156.  
  157.     Scene_SaveMaster.prototype.createWindows = function () {
  158.         this._windowSaveMaster = new Window_SaveMasterKey();
  159.         this._windowSaveMaster.setHandler('ok', this._commandOk.bind(this));
  160.         this.addWindow(this._windowSaveMaster);
  161.     };
  162.  
  163.     Scene_SaveMaster.prototype._commandOk = function () {
  164.         var key = this._windowSaveMaster.currentSymbol();
  165.         StorageManager.testerKey = key;
  166.         SceneManager.goto(Scene_Title);
  167.     };
  168. } ());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement