noname/game/directory.js

137 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-04-22 17:59:02 +00:00
var fs=require('fs');
var path=require('path');
var exec = require('child_process').exec;
global.window=global;
require(__dirname+'/update.js');
2018-07-08 10:16:24 +00:00
require(__dirname+'/asset.js');
2018-04-22 17:59:02 +00:00
var updates=window.noname_update;
2018-07-08 10:16:24 +00:00
var assetlist='';
2018-04-22 17:59:02 +00:00
var skinlist='window.noname_skin_list={\n';
2018-07-08 10:16:24 +00:00
var entrylist=[];
2018-08-10 09:29:29 +00:00
var entrymap={};
2018-04-22 17:59:02 +00:00
var get = function(dir,callback){
fs.readdir(dir,function(err,list){
var shift=function(){
if(list.length){
var filename=list.shift();
var delay=false;
if(!/\.|~|_/.test(filename[0])){
var url=dir+'/'+filename;
var stat=fs.statSync(url);
if(stat.isFile()){
if(['.jpg','.png','.mp3','.ttf'].indexOf(path.extname(url))!=-1){
2018-07-08 10:16:24 +00:00
var assetentry=path.relative(path.dirname(__dirname),url);
assetlist+=',\n\t\''+assetentry+'\'';
entrylist.push(assetentry);
2018-04-22 17:59:02 +00:00
}
}
else if(stat.isDirectory()){
if(dir==path.dirname(__dirname)+'/image/skin'){
fs.readdir(url,function(err,list){
var num=0;
for(var i=0;i<list.length;i++){
var url2=url+'/'+list[i];
var stat=fs.statSync(url2);
if(stat.isFile()&&path.extname(url2)=='.jpg'){
num++;
}
}
skinlist+='\t'+filename+':'+num+',\n';
2018-08-10 09:29:29 +00:00
entrymap[filename]=num;
2018-04-22 17:59:02 +00:00
shift();
});
delay=true;
}
else{
get(url,shift);
delay=true;
}
}
}
if(!delay){
shift();
}
}
else{
callback();
}
}
shift();
});
};
2018-07-08 10:16:24 +00:00
2018-04-22 17:59:02 +00:00
get(path.dirname(__dirname),function(){
2018-07-08 10:16:24 +00:00
var diff=false;
if(window.noname_asset_list.length==entrylist.length+1){
for(var i=0;i<entrylist.length;i++){
if(entrylist[i]!=window.noname_asset_list[i+1]){
diff=true;
break;
}
}
2018-08-10 09:29:29 +00:00
if(!diff){
for(var i in entrymap){
if(window.noname_skin_list[i]!==entrymap[i]){
diff=true;
break;
}
}
for(var i in noname_skin_list){
if(window.noname_skin_list[i]!==entrymap[i]){
diff=true;
break;
}
}
}
2018-07-08 10:16:24 +00:00
}
else{
diff=true;
}
if(diff){
var assetversion='window.noname_asset_list=[\n\t\''+updates.version+'\'';
fs.writeFile('game/asset.js',assetversion+assetlist+'\n];\n'+skinlist.slice(0,skinlist.length-2)+'\n};','utf-8',function(){
console.log('udpated asset.js');
});
}
2018-04-22 17:59:02 +00:00
exec('git diff --name-only', (error, stdout, stderr) => {
var updatelist='window.noname_update={\n\tversion:\''+updates.version+'\',';
updatelist+='\n\tupdate:\''+(updates.update||'')+'\',';
var apply=function(name,list){
updatelist+='\n\t'+name+':[\n';
for(var i=0;i<list.length;i++){
updatelist+='\t\t\''+list[i]+'\'';
if(i<list.length-1){
updatelist+=',';
}
updatelist+='\n';
}
updatelist+='\t]';
};
if(updates.changeLog){
apply('changeLog',updates.changeLog);
updatelist+=',';
}
2018-06-12 07:06:21 +00:00
if(updates.players){
apply('players',updates.players);
updatelist+=',';
}
if(updates.cards){
apply('cards',updates.cards);
updatelist+=',';
}
2018-04-22 17:59:02 +00:00
var changes = stdout.split('\n');
for(var i=0;i<changes.length;i++){
var extname=path.extname(changes[i]);
2018-06-26 16:31:12 +00:00
if(!changes[i]||(extname!='.js'&&extname!='.css')||changes[i]=='game/update.js'){
2018-04-22 17:59:02 +00:00
changes.splice(i--,1);
}
}
apply('files',changes);
fs.writeFile('game/update.js',updatelist+'\n};','utf-8',function(){
console.log('updated update.js');
});
});
2018-06-12 07:06:21 +00:00
});