function randUrl(e){return e+"?rand="+Math.floor(1e4*Math.random())}function filename(e){return e.substring(e.lastIndexOf("/")+1)}function getPathUrl(e){var t=/^debug\//.test(e)?"/"+e:baseURIHttp+e;return t}function getRemoteFile(e){var t=new XMLHttpRequest;t.open("GET",randUrl("/"+e),!1);t.send(null);if(4==t.readyState&&200==t.status)return t.responseText;throw"download faile:"+e}function getRemoteFile_async(e,t){var n=new XMLHttpRequest;n.open("GET",randUrl("/"+e),!0);n.onreadystatechange=function(){if(4==n.readyState){n.onreadystatechange=function(){};200==n.status?t(e,n.responseText):t(e,null)}};n.send(null)}function initToLoad(e){for(var t={items:{}},n=0,a=0;a<e.length;a++)if(!t.items[e[a]]){t.items[e[a]]={status:"toload"};n++}t.total=t.remaining=n;return t}function preloadFiles(e,t){function n(e,n){if(null==n)a.items[e].status="failed";else{a.items[e].status="loaded";a.items[e].content=n}0==--a.remaining&&t(a)}if(e&&0!=e.length){var a=initToLoad(e);for(var r in a.items)getRemoteFile_async(r,function(e,t){n(e,t)})}else t({items:{}})}function outdateMsg(e){return e+" is using old format.please deploy it force update"}function getReleaseFiles(e,t){if(!e.modrelease&&!e.webapprelease){var n=outdateMsg(t);self.postMessage(n);throw n}return e.modrelease?e.webapprelease?e.modrelease.concat(e.webapprelease):e.modrelease:e.webapprelease}function getReleaseMts(e,t){if(!e.modrelmt&&!e.webapprelmt){var n=outdateMsg(t);self.postMessage(n);throw n}return e.modrelmt?e.webapprelmt?e.modrelmt.concat(e.webapprelmt):e.modrelmt:e.webapprelmt}function PROJ(e){return e+"/proj.config"}function SLN(e){return e+"/sln.config"}function makeMap(e,t){var n={};if(!e)return n;for(var a=getReleaseFiles(e,t),r=getReleaseMts(e,t),s=a.length,o=0;s>o;o++)n[a[o]]=r[o];return n}function updateProj(e,t,n){n?updateProjByConfig(e,n,t):getRemoteFile_async(PROJ(t),function(a,r){n=JSON.parse(r);updateProjByConfig(e,n,t)})}function updateProjByConfig(e,t,n,a){if(e&&t&&e.mt==t.mt)a&&a({status:"exist"});else{var r=makeMap(e,n),s=makeMap(t,n),o=[];for(var i in s)r[i]!=s[i]&&o.push(n+"/"+i);preloadFiles(o,function(r){var s=[];for(var o in r.items)s.push({filename:o.substr(n.length+1),content:r.items[o].content});self.postMessage({cmd:"updateReady",path:n,oldConfig:e,newConfig:t,type:"proj",updateFiles:s});a&&a({status:"loaded"})})}}function getProjConfigFromSlnConfig(e,t){if(!e||!e.projs)return null;for(var n=e.projs,a=n.length,r=filename(t),s=0;a>s;s++)if(n[s].path==r)return n[s];return null}function updateSln(e,t,n){getRemoteFile_async(SLN(t),function(a,r){function s(){i&&self.postMessage({cmd:"updateReady",path:t,type:"sln",oldConfig:e,newConfig:o})}if(null!=r){var o=JSON.parse(r);if(!e||e.mt!=o.mt){var i=!0;if("all"==n){n=[];for(var l=0;l<o.projs.length;l++)n.push(t+"/"+o.projs[l].path)}var f=initToLoad(n);for(var u in f.items){var p=getProjConfigFromSlnConfig(e,u),d=getProjConfigFromSlnConfig(o,u);d?updateProjByConfig(p,d,u,function(e){"failed"==e.status&&(i=!1);0==--f.remaining&&s()}):f.remaining--}0==f.remaining&&s()}}})}function map2respath(e,t){return"/"==t[0]?t.substr(1):e+"/"+t}function requestResFile(e){var t=new XMLHttpRequest;t.open("GET",randUrl("/"+e),!1);t.send(null);return 4==t.readyState&&200==t.status}function updateMnft(e,t,n){var a,r,s,o=t?JSON.parse(t):{},i=n?JSON.parse(n):{};for(a in i)if(!o[a]){s=map2respath(e,a);r=requestResFile(s);r&&(o[a]=i[a])}for(a in o)if(i[a]){if(i[a]!=o[a]){s=map2respath(e,a);r=requestResFile(s);r&&(o[a]=i[a])}}else delete i[a];self.postMessage({cmd:"loadMnftReady",path:e,mnft:JSON.stringify(o)})}var baseURIHttp;self.onmessage=function(e){var t=e.data;if("update"==t.cmd){baseURIHttp=t.baseURIHttp;try{"proj"==t.type?updateProj(t.oldConfig,t.path,t.newConfig):"sln"==t.type&&updateSln(t.oldConfig,t.path,t.usedProjs)}catch(n){self.postMessage("exception:"+JSON.stringify(n))}}else if("loadMnft"==t.cmd){baseURIHttp=t.baseURIHttp;try{updateMnft(t.path,t.oldMnft,t.newMnft)}catch(n){self.postMessage("exception:"+JSON.stringify(n))}}else self.postMessage("error command:"+t)};