Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve clean unused assets #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions panel/panel.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<div id="warp" class="warp">
<header>
<div v-if='isDone == true'>
<i class="fa fa-refresh" aria-hidden="true" title="刷新" @click="refresh"></i>
<i class="fa fa-times-circle" aria-hidden="true" title="删除所有未使用资源" @click="onDeleteAllClick"></i>
<i class="fa fa-picture-o"> {{items.length}} </i>
</div>
<div v-else>刷新中...</div>
</header>

<section class="res">
Expand Down
245 changes: 66 additions & 179 deletions panel/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

const Fs = require('fs');
const FFs = require('fire-fs');
const Path = require('path');
const cp = require('child_process');

var PATH = {
Expand All @@ -17,6 +16,7 @@ var createVM = function (elem) {
el: elem,
data: {
resources: true,
isDone: false,
input: "",
items: [],
ignore: null,
Expand All @@ -30,9 +30,10 @@ var createVM = function (elem) {
methods: {

refresh() {
this.isDone = false;
let adb = Editor.assetdb;
let self = this;
let custIngnore = this.splitInput(this.input)
let custIngnore = this.splitInput(this.input);

this.items.length = 0;
this.items = [];
Expand All @@ -42,70 +43,92 @@ var createVM = function (elem) {
return;
}

let json = null;
if (obj.type != 'bitmap-font') {
json = FFs.readJsonSync(obj.path);
}
else {
json = FFs.readFileSync(obj.path, 'utf-8');
if (!obj.destPath) {
return;
}

let json = FFs.readFileSync(obj.destPath, 'utf-8');

results.forEach(function (result) {
if (result.url.indexOf('/default_') !== -1) {
for (let i = 0; i < results.length; ++i) {
let result = results[i];
if (result.hidden) {
result.contain = true;
return;
continue;
}

for (let i = 0; i < custIngnore.length; i++) {
if (result.url.indexOf(custIngnore[i]) !== -1) {
result.contain = true;
return;
continue;
}
}

if (
self.resources &&
result.url.indexOf('db://assets/resources') !== -1
) {
if (self.resources &&
result.url.indexOf('db://assets/resources') !== -1) {
result.contain = true;
return;
continue;
}

if (
(typeof json) === 'string' &&
self.searchBf(json, result.url)
) {
result.contain = true;
return;

if (!result.jsonObj) {
result.jsonObj = FFs.readJsonSync(result.destPath);
}

if (
json['__type__'] === 'cc.AnimationClip' &&
self.searchClip(json, result.uuid)
) {
result.contain = true;
return;
if (result.jsonObj && result.jsonObj.content) {
if (result.jsonObj.content.atlas) {
result.contain = true;
continue;
}
}

if (result.jsonObj && result.jsonObj.content) {
if (result.jsonObj.content.texture) {
if (json.indexOf(result.jsonObj.content.texture) > -1 && obj.type == 'spine') {
result.contain = true;
}
}
}

result.contain =
result.contain ?
true :
self.search(json, result.uuid);
});
let isContain = json.indexOf(result.uuid) > -1;
result.contain = result.contain ? true : isContain;
}
});

let excludeSpriteFramesTexture = [];
results.forEach(function (result) {
result.contain == true ? '' : self.items.push({
url: result.url,
uuid: result.uuid
});
if (result.contain) {
if (result.jsonObj) {
if (result.jsonObj.content) {
let atlasUuid = result.jsonObj.content.atlas;
let textureUuid =result.jsonObj.content.texture;
if (atlasUuid) {
if (excludeSpriteFramesTexture.indexOf(textureUuid) === -1) {
excludeSpriteFramesTexture.push(textureUuid);
}
}
}
}
}
});

results.forEach(function (result) {
if (!result.contain) {
if (result.jsonObj) {
let uuid = result.jsonObj.content.texture;
if (excludeSpriteFramesTexture.indexOf(uuid) !== -1) {
console.error('exclude texture atlas ' + result.url);
return;
}
}
self.items.push({url: result.url, uuid: result.uuid});
}
});

self.isDone = true;
};

adb.queryAssets(
null,
['scene', 'prefab', 'animation-clip', 'bitmap-font'],
['scene', 'prefab', 'animation-clip', 'bitmap-font', 'sprite-atlas', 'texture-packer', 'spine'],
function (err, objs) {
adb.queryAssets(
null,
Expand All @@ -114,143 +137,7 @@ var createVM = function (elem) {
callback(objs, results);
}
);
}
);
},

/**
* Recursive
* @argument {JSON | Array} json
* @argument {String} uuid
*/
search(json, uuid) {
let self = this;
if (json instanceof Array) {
for (let i = 0; i < json.length; i++) {
if (self.search(json[i], uuid)) {
return true;
}
}
}
else if (json instanceof Object) {
if (json['__type__'] === 'cc.Sprite' && json._spriteFrame) {
return json._spriteFrame.__uuid__ == uuid;
}
else if (json['__type__'] === 'cc.Button') {
return self.searchButton(json, uuid);
}
else if (json['__type__'] && json['__type__'].length > 20) {
if (Editor.Utils.UuidUtils.isUuid(
Editor.Utils.UuidUtils.decompressUuid(json['__type__'])
)) {
return self.searchScript(json, uuid);
}
}
}
},

searchButton(json, uuid) {
return (json.pressedSprite && json.pressedSprite.__uuid__ == uuid) ||
(json.hoverSprite && json.hoverSprite.__uuid__ == uuid) ||
(json._N$normalSprite && json._N$normalSprite.__uuid__ == uuid) ||
(json._N$disabledSprite && json._N$disabledSprite.__uuid__ == uuid);
},

/**
* Recursive
*
* Search for the script (cc.Class) in the scene file (.fire).
*
* @argument {JSON} json cc.Class
* @argument {String} uuid target.uuid
*/
searchScript(json, uuid) {
let result = [];

for (let i in json) {
if (json[i] && json[i].__uuid__ && json[i].__uuid__ == uuid) {
return true;
}
}

return false;
},

/**
* Recursive
*
* Search for the animation clip (cc.Animation).
*
* @argument {JSON} json cc.Animation
* @argument {String} uuid target.uuid
*/
searchClip(json, uuid) {
let self = this;
let spriteFrame = [];
let paths = this.getValue(json, 'paths');
if (paths) {
for (let i in paths) {
spriteFrame = this.getValue(paths[i], 'spriteFrame');
if (spriteFrame) {
for (let i = 0; i < spriteFrame.length; i++) {
if (spriteFrame[i] && spriteFrame[i].value && spriteFrame[i].value.__uuid__ && spriteFrame[i].value.__uuid__ === uuid) {
return true;
}
}
}
}
}
else {
spriteFrame = this.getValue(json, 'spriteFrame');
if (spriteFrame) {
for (let i = 0; i < spriteFrame.length; i++) {
if (spriteFrame[i].value && spriteFrame[i].value.__uuid__ === uuid) {
return true;
}
}
}
}

return false;
},

searchBf(str, url) {
let start = url.lastIndexOf('/') + 1;
// let end = url.lastIndexOf('.');
let textureName = url.slice(start, url.length);

if (str.indexOf(textureName) == -1) {
return false;
}

return true;
},

/**
* ..
* @param {JSON} json
* @param {String} key
* @param {Boolean} pan 泛查询开关,因为这样叫比较酷
*/
getValue(json, key, pan) {
key = key ? key : 'spriteFrame';
if (typeof json !== 'object') {
return null;
}

for (let i in json) {
if (i === key) {
return json[i];
}
else {
let value = this.getValue(json[i], key);
if (value) {
return value;
}
}

}
return null;
});
},

jumpRes(uuid) {
Expand Down Expand Up @@ -302,7 +189,7 @@ var createVM = function (elem) {
this.refresh();
}
else {
let index = items.findIndex(function (item, index, array) {
let index = items.findIndex(function (item, index) {
return self.getPicUrl(item.url) == url[0];
});
index == -1 ? '' : items.splice(index, 1);
Expand Down Expand Up @@ -333,4 +220,4 @@ Editor.Panel.extend({
'scene:ready'() {
}
}
});
});