Respack loader now loads any valid xml, not just named ones.

master
William Toohey 9 years ago
parent ad71da904f
commit 48ac80fe5e
  1. 106
      src/js/ResourcePack.js

@ -44,9 +44,7 @@ function Respack() {
this.downloaded = -1; this.downloaded = -1;
this.enabled = true; this.enabled = true;
this._songXMLPromise = null; this._xmlQueue = [];
this._imageXMLPromise = null;
this._infoXMLPromise = null;
this.totalFiles = -1; this.totalFiles = -1;
@ -169,9 +167,7 @@ Respack.prototype.parseZip = function(zip) {
return this.parseXML(); return this.parseXML();
}).then(() => { }).then(() => {
// Cleanup // Cleanup
this._songXMLPromise = null; this._xmlQueue = [];
this._imageXMLPromise = null;
this._infoXMLPromise = null;
console.log("Loaded", this.name, "successfully with", this.songs.length, console.log("Loaded", this.name, "successfully with", this.songs.length,
"songs and", this.images.length, "images."); "songs and", this.images.length, "images.");
}); });
@ -186,19 +182,8 @@ Respack.prototype.parseFile = function(file) {
this.imageQueue.push(this.parseImage(file)); this.imageQueue.push(this.parseImage(file));
this.filesToLoad++; this.filesToLoad++;
} }
else { else if(name.toLowerCase().endsWith(".xml")){
switch(name.toLowerCase()) { this._xmlQueue.push(this.loadXML(file));
case "songs.xml":
this._songXMLPromise = this.loadXML(file);
break;
case "images.xml":
this._imageXMLPromise = this.loadXML(file);
break;
case "info.xml":
this._infoXMLPromise = this.loadXML(file);
break;
default:
}
} }
}; };
@ -358,42 +343,35 @@ Respack.prototype.loadXML = function(file) {
//XML parser will complain about a bare '&', but some respacks use &amp //XML parser will complain about a bare '&', but some respacks use &amp
text = text.replace(/&/g, '&'); text = text.replace(/&/g, '&');
text = text.replace(/&/g, '&'); text = text.replace(/&/g, '&');
resolve(text); let parser = new DOMParser();
let dom = parser.parseFromString(text, "text/xml");
resolve(dom);
}); });
}); });
}; };
Respack.prototype.parseXML = function() { Respack.prototype.parseXML = function() {
let p = Promise.resolve(); for(let i = 0; i < this._xmlQueue.length; i++) {
// info xml? this._xmlQueue[i] = this._xmlQueue[i].then(dom => {
if(this._infoXMLPromise) { switch(dom.documentElement.nodeName) {
p = p.then(() => { case "songs":
return this._infoXMLPromise; if(this.songs.length > 0)
}).then(text => { this.parseSongFile(dom);
this.parseInfoFile(text); break;
}); case "info":
} if(this.images.length > 0)
// song xml and songs exist? this.parseInfoFile(dom);
if (this.songs.length > 0) { break;
if(this._songXMLPromise) { case "images":
p = p.then(() => { this.parseImageFile(dom);
return this._songXMLPromise; break;
}).then(text => { default:
this.parseSongFile(text); console.log("XML found with no songs, images or info");
}); break;
} else { }
console.log("!!!", "Got songs but no songs.xml!");
}
}
// images xml and images exist?
if (this.images.length > 0 && this._imageXMLPromise) {
p = p.then(() => {
return this._imageXMLPromise;
}).then(text => {
this.parseImageFile(text);
}); });
} }
return p; return Promise.all(this._xmlQueue);
}; };
// Save some chars // Save some chars
@ -402,18 +380,11 @@ Element.prototype.getTag = function(tag, def) {
return t ? t.textContent : (def ? def : null); return t ? t.textContent : (def ? def : null);
}; };
Respack.prototype.parseSongFile = function(text) { Respack.prototype.parseSongFile = function(dom) {
debug(" - Parsing songFile"); debug(" - Parsing songFile");
let oParser = new DOMParser();
let oDOM = oParser.parseFromString(text, "text/xml");
if(oDOM.documentElement.nodeName !== "songs"){
console.log("songs.xml error, corrupt file?");
return;
}
let newSongs = []; let newSongs = [];
let el = oDOM.documentElement.firstElementChild; let el = dom.documentElement.firstElementChild;
for(; el; el = el.nextElementSibling) { for(; el; el = el.nextElementSibling) {
let song = this.getSong(el.attributes[0].value); let song = this.getSong(el.attributes[0].value);
if(song) { if(song) {
@ -467,16 +438,10 @@ Respack.prototype.parseSongFile = function(text) {
this.songs = newSongs; this.songs = newSongs;
}; };
Respack.prototype.parseInfoFile = function(text) { Respack.prototype.parseInfoFile = function(dom) {
debug(" - Parsing infoFile"); debug(" - Parsing infoFile");
let oParser = new DOMParser(); let info = dom.documentElement;
let oDOM = oParser.parseFromString(text, "text/xml");
let info = oDOM.documentElement;
if(info.nodeName !== "info"){
console.log("info.xml error, corrupt file?");
return;
}
// self reference strings to avoid changing strings twice in future // self reference strings to avoid changing strings twice in future
this.name = info.getTag("name", this.name); this.name = info.getTag("name", this.name);
@ -485,18 +450,11 @@ Respack.prototype.parseInfoFile = function(text) {
this.link = info.getTag("link", this.link); this.link = info.getTag("link", this.link);
}; };
Respack.prototype.parseImageFile = function(text) { Respack.prototype.parseImageFile = function(dom) {
debug(" - Parsing imagefile"); debug(" - Parsing imagefile");
let oParser = new DOMParser();
let oDOM = oParser.parseFromString(text, "text/xml");
if(oDOM.documentElement.nodeName !== "images"){
console.log("images.xml error, corrupt file?");
return;
}
let newImages = []; let newImages = [];
let el = oDOM.documentElement.firstElementChild; let el = dom.documentElement.firstElementChild;
for(; el; el = el.nextElementSibling) { for(; el; el = el.nextElementSibling) {
let image = this.getImage(el.attributes[0].value); let image = this.getImage(el.attributes[0].value);
if(image) { if(image) {

Loading…
Cancel
Save