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

385 adapter the taobaominigame ClearLRU procedure #17230

Open
wants to merge 8 commits into
base: v3.8.5
Choose a base branch
from
Open
28 changes: 20 additions & 8 deletions platforms/minigame/common/engine/cache-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,12 @@ var cacheManager = {
caches.sort(function (a, b) {
return a.lastTime - b.lastTime;
});
caches.length = Math.floor(caches.length / 3);
if (caches.length === 0) {
cleaning = false;
return;
// cache length above 3 then clear 1/3, or clear all caches
if (caches.length < 3) {
console.warn("Due to caching large files in the game, there is insufficient storage space. Now starting forced cleaning.");
}
else {
caches.length = Math.floor(caches.length / 3);
}
for (var i = 0, l = caches.length; i < l; i++) {
var cacheKey = cc.assetManager.utils.getUuidFromURL(caches[i].originUrl) + "@native";
Expand All @@ -199,8 +201,13 @@ var cacheManager = {
function deferredDelete () {
var item = caches.pop();
if (self._isZipFile(item.originUrl)) {
rmdirSync(item.url, true);
self._deleteFileCB();
if (self._isZipFile(path)) {
deleteFile(path, self._deleteFileCB.bind(self));
}
else {
rmdirSync(path, true);
self._deleteFileCB();
}
}
else {
deleteFile(item.url, self._deleteFileCB.bind(self));
Expand All @@ -222,8 +229,13 @@ var cacheManager = {
clearTimeout(writeCacheFileList);
this._write();
if (this._isZipFile(url)) {
rmdirSync(path, true);
this._deleteFileCB();
if (self._isZipFile(path)) {
deleteFile(path, self._deleteFileCB.bind(self));
}
else {
rmdirSync(path, true);
self._deleteFileCB();
}
}
else {
deleteFile(path, this._deleteFileCB.bind(this));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/****************************************************************************
Copyright (c) 2019 Xiamen Yaji Software Co., Ltd.
https://www.cocos.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of cache-manager software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in cache-manager License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
const { deleteFile, rmdirSync, unzip, isOutOfStorage } = require('../fs-utils');
(function(){
var unzipSuffix = 0;
if (!(cc && cc.assetManager && cc.assetManager.cacheManager)) {
return;
}

const cacheManager = cc.assetManager.cacheManager;
cacheManager.unzipAndCacheBundle = function (id, zipFilePath, cacheBundleRoot, onComplete) {
let time = Date.now().toString();
let targetPath = `${this.cacheDir}/${cacheBundleRoot}/${time}${unzipSuffix++}`;
let self = this;
unzip(zipFilePath, targetPath, function (err) {
if (err) {
rmdirSync(targetPath, true);
if (isOutOfStorage(err.message)) {
self.outOfStorage = true;
self.autoClear && self.clearLRU();
}
onComplete && onComplete(err);
return;
}
self.cachedFiles.add(id, { bundle: cacheBundleRoot, url: targetPath, lastTime: time });
self.writeCacheFile();
onComplete && onComplete(null, targetPath);
});
};

})();

20 changes: 13 additions & 7 deletions platforms/minigame/platforms/taobao-mini-game/wrapper/fs-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
THE SOFTWARE.
****************************************************************************/
const fs = my.getFileSystemManager ? my.getFileSystemManager() : null;
const outOfStorageRegExp = /the maximum size of the file storage/; // not exactly right
const outOfStorageRegExp = "10028"; // not exactly right

const fsUtils = {

fs,

isOutOfStorage (errMsg) {
return outOfStorageRegExp.test(errMsg);
isOutOfStorage (errorCode) {
return errorCode.toString() == outOfStorageRegExp;
},

getUserDataPath () {
Expand Down Expand Up @@ -69,8 +69,11 @@ const fsUtils = {
}
},
fail (res) {
if(fsUtils.isOutOfStorage(res.error)){
cc.assetManager.cacheManager.clearLRU();
}
console.warn(`Download file failed: path: ${remoteUrl} message: ${res.errorMessage}`);
onComplete && onComplete(new Error(res.errorMessage), null);
onComplete && onComplete(new Error(res.error), null);
},
};
if (header) options.header = header;
Expand All @@ -92,7 +95,7 @@ const fsUtils = {
},
fail (res) {
console.warn(`Copy file failed: path: ${srcPath} message: ${res.errorMessage}`);
onComplete && onComplete(new Error(res.errorMessage));
onComplete && onComplete(new Error(res.error));
},
});
},
Expand All @@ -106,8 +109,11 @@ const fsUtils = {
onComplete && onComplete(null);
},
fail (res) {
if(fsUtils.isOutOfStorage(res.error)){
cc.assetManager.cacheManager.clearLRU();
}
console.warn(`Write file failed: path: ${path} message: ${res.errorMessage}`);
onComplete && onComplete(new Error(res.errorMessage));
onComplete && onComplete(new Error(res.error));
},
});
},
Expand Down Expand Up @@ -257,7 +263,7 @@ const fsUtils = {
},
fail (res) {
console.warn(`unzip failed: path: ${zipFilePath} message: ${res.errorMessage}`);
onComplete && onComplete(new Error(`unzip failed: ${res.errorMessage}`));
onComplete && onComplete(new Error(res.error));
},
});
},
Expand Down
Loading