function _Storage() { if (window.nodewebkit) { window.fs = require('fs'); this.initDirectory(); this.startLoggingChat = this.nwStartLoggingChat; this.stopLoggingChat = this.nwStopLoggingChat; this.logChat = this.nwLogChat; } } /********************************************************* * Teams *********************************************************/ /** * Teams are normally loaded from `localStorage`. * If the client isn't running on `play.pokemonshowdown.com`, though, * teams are received from `crossdomain.php` instead. */ _Storage.prototype.teams = null; _Storage.prototype.loadTeams = function() { this.teams = []; if (window.nodewebkit) { return; } if (window.localStorage) { var teamString = localStorage.getItem('showdown_teams'); if (teamString) this.teams = JSON.parse(teamString); app.trigger('init:loadteams'); } }; _Storage.prototype.saveTeams = function() { if (window.localStorage) { Storage.cantSave = false; try { localStorage.setItem('showdown_teams', JSON.stringify(this.teams)); } catch (e) { if (e.code === DOMException.QUOTA_EXCEEDED_ERR) { Storage.cantSave = true; } else { throw e; } } } }; _Storage.prototype.saveTeam = function() { this.saveTeams(); }; _Storage.prototype.deleteTeam = function() { this.saveTeams(); }; _Storage.prototype.saveAllTeams = function() { this.saveTeams(); }; /********************************************************* * Node-webkit *********************************************************/ _Storage.prototype.initDirectory = function() { var self = this; var dir = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE; if (!(dir.charAt(dir.length-1) in {'/':1, '\\':1})) dir += '/'; fs.stat(dir+'Documents', function(err, stats) { if (err || !stats.isDirectory()) { fs.stat(dir+'My Documents', function(err, stats) { if (err || !stats.isDirectory()) { self.documentsDir = dir; } else { self.documentsDir = dir+'My Documents/'; } self.initDirectory2(); }); } else { self.documentsDir = dir+'Documents/'; self.initDirectory2(); } }); }; _Storage.prototype.initDirectory2 = function() { var self = this; fs.mkdir(self.documentsDir+'My Games', function() { fs.mkdir(self.documentsDir+'My Games/Pokemon Showdown', function() { fs.stat(self.documentsDir+'My Games/Pokemon Showdown', function(err, stats) { if (err) return; if (stats.isDirectory()) { self.dir = self.documentsDir+'My Games/Pokemon Showdown/'; fs.mkdir(self.dir+'Logs', function() {}); fs.mkdir(self.dir+'Teams', function() {}); // load teams self.nwLoadTeams(); self.saveAllTeams = self.nwSaveAllTeams; self.saveTeam = self.nwSaveTeam; self.deleteTeam = self.nwDeleteTeam; // logging if (Tools.prefs('logchat')) self.startLoggingChat(); } }); }); }); }; _Storage.prototype.revealFolder = function() { gui.Shell.openItem(this.dir); }; // teams _Storage.prototype.nwLoadTeams = function() { var self = this; var localApp = window.app; fs.readdir(this.dir+'Teams', function(err, files) { if (err) return; self.teams = []; self.nwTeamsLeft = files.length; for (var i=0; i= 0) { format = line.substr(1, bracketIndex-1); line = $.trim(line.substr(bracketIndex+1)); } fs.readFile(this.dir+'Teams/'+filename, function(err, data) { if (!err) { self.teams.push({ filename: filename, name: line, format: format, team: TeambuilderRoom.parseText(''+data) }); self.nwTeamsLeft--; if (!self.nwTeamsLeft) { self.nwFinishedLoadingTeams(localApp); } } }); }; _Storage.prototype.nwFinishedLoadingTeams = function(app) { this.teams.sort(this.teamCompare); if (!app) app = window.app; if (app) app.trigger('init:loadteams'); }; _Storage.prototype.teamCompare = function(a, b) { if (a.name > b.name) return 1; if (a.name < b.name) return -1; return 0; }; _Storage.prototype.nwDeleteAllTeams = function(callback) { var self = this; fs.readdir(this.dir+'Teams', function(err, files) { if (err) return; self.nwTeamsLeft = files.length; if (!self.nwTeamsLeft) { callback(); return; } for (var i=0; i