mirror of
https://github.com/smallmain/cocos-enhance-kit.git
synced 2025-10-09 03:45:23 +00:00
[adapters] 增加资源管线的多线程支持
This commit is contained in:
144
adapters/common/cache-manager-proxy.js
Normal file
144
adapters/common/cache-manager-proxy.js
Normal file
@@ -0,0 +1,144 @@
|
||||
const { getUserDataPath, readJsonSync, makeDirSync, writeFileSync, copyFile, downloadFile, writeFile, deleteFile, rmdirSync, unzip, isOutOfStorage } = window.fsUtils;
|
||||
|
||||
var cacheManager = {
|
||||
|
||||
// 以下为单向属性,修改会同步到 Worker 中
|
||||
_cacheDir: 'gamecaches',
|
||||
get cacheDir() {
|
||||
return this._cacheDir;
|
||||
},
|
||||
set cacheDir(v) {
|
||||
worker.cacheManager.set_cacheDir([this._cacheDir = v]);
|
||||
},
|
||||
|
||||
_cachedFileName: 'cacheList.json',
|
||||
get cachedFileName() {
|
||||
return this._cachedFileName;
|
||||
},
|
||||
set cachedFileName(v) {
|
||||
worker.cacheManager.set_cachedFileName([this._cachedFileName = v]);
|
||||
},
|
||||
|
||||
_cacheEnabled: true,
|
||||
get cacheEnabled() {
|
||||
return this._cacheEnabled;
|
||||
},
|
||||
set cacheEnabled(v) {
|
||||
worker.cacheManager.set_cacheEnabled([this._cacheEnabled = v]);
|
||||
},
|
||||
|
||||
_autoClear: true,
|
||||
get autoClear() {
|
||||
return this._autoClear;
|
||||
},
|
||||
set autoClear(v) {
|
||||
worker.cacheManager.set_autoClear([this._autoClear = v]);
|
||||
},
|
||||
|
||||
_cacheInterval: 500,
|
||||
get cacheInterval() {
|
||||
return this._cacheInterval;
|
||||
},
|
||||
set cacheInterval(v) {
|
||||
worker.cacheManager.set_cacheInterval([this._cacheInterval = v]);
|
||||
},
|
||||
|
||||
_deleteInterval: 500,
|
||||
get deleteInterval() {
|
||||
return this._deleteInterval;
|
||||
},
|
||||
set deleteInterval(v) {
|
||||
worker.cacheManager.set_deleteInterval([this._deleteInterval = v]);
|
||||
},
|
||||
|
||||
// 以下属性未暴露,仅在 Worker 中保留
|
||||
// writeFileInterval: 2000,
|
||||
// outOfStorage: false,
|
||||
// tempFiles: null,
|
||||
// cacheQueue: {},
|
||||
|
||||
// 以下为只读属性,并且在变动时从 Worker 中同步,需注意 lastTime 可能不是最新的
|
||||
cachedFiles: null,
|
||||
|
||||
// 以下为只读属性
|
||||
version: '1.0',
|
||||
|
||||
// 增加 download 函数以在 Worker 执行下载逻辑
|
||||
download(url, func, options, onFileProgress, onComplete) {
|
||||
// 暂未实现 onFileProgress 回调
|
||||
worker.cacheManager.download(
|
||||
[url, options.reload, options.header, options.cacheEnabled, options.__cacheBundleRoot__],
|
||||
([errMsg, path]) => {
|
||||
if (errMsg) {
|
||||
onComplete(new Error(errMsg), null);
|
||||
return;
|
||||
}
|
||||
func(path, options, (err, data) => {
|
||||
if (err) {
|
||||
this.removeCache(url);
|
||||
}
|
||||
onComplete(err, data);
|
||||
});
|
||||
},
|
||||
);
|
||||
},
|
||||
|
||||
// 增加 handleZip 函数以在 Worker 执行处理逻辑
|
||||
handleZip(url, options, onComplete) {
|
||||
// 暂未实现 options.onFileProgress 回调
|
||||
worker.cacheManager.handleZip(
|
||||
[url, options.header, options.__cacheBundleRoot__],
|
||||
([errMsg, path]) => {
|
||||
if (errMsg) {
|
||||
onComplete(new Error(errMsg), null);
|
||||
} else {
|
||||
onComplete(null, path);
|
||||
}
|
||||
},
|
||||
);
|
||||
},
|
||||
|
||||
getCache(url) {
|
||||
return this.cachedFiles.has(url) ? this.cachedFiles.get(url).url : '';
|
||||
},
|
||||
|
||||
// getTemp 改为异步函数
|
||||
getTempAsync(url, callback) {
|
||||
worker.cacheManager.getTemp([url], ([url]) => {
|
||||
callback(url);
|
||||
});
|
||||
},
|
||||
|
||||
init() {
|
||||
this._cacheDir = getUserDataPath() + '/' + this.cacheDir;
|
||||
worker.cacheManager.init(null, ([cachedFiles]) => {
|
||||
this.cachedFiles = new cc.AssetManager.Cache(cachedFiles);
|
||||
});
|
||||
},
|
||||
|
||||
clearCache() {
|
||||
worker.cacheManager.clearCache(null, () => {
|
||||
this.cachedFiles.clear();
|
||||
});
|
||||
},
|
||||
|
||||
clearLRU() {
|
||||
worker.cacheManager.clearLRU(null, ([deletedFiles]) => {
|
||||
for (let i = 0, l = deletedFiles.length; i < l; i++) {
|
||||
this.cachedFiles.remove(deletedFiles[i]);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
removeCache(url) {
|
||||
worker.cacheManager.removeCache([url], () => {
|
||||
this.cachedFiles.remove(url);
|
||||
});
|
||||
},
|
||||
|
||||
makeBundleFolder(bundleName) {
|
||||
makeDirSync(this.cacheDir + '/' + bundleName, true);
|
||||
},
|
||||
};
|
||||
|
||||
cc.assetManager.cacheManager = module.exports = cacheManager;
|
@@ -1,4 +1,4 @@
|
||||
const cacheManager = require('../cache-manager');
|
||||
const cacheManager = CC_WORKER_ASSET_PIPELINE ? require('../cache-manager-proxy') : require('../cache-manager');
|
||||
const { fs, downloadFile, readText, readArrayBuffer, readJson, loadSubpackage, getUserDataPath, exists } = window.fsUtils;
|
||||
|
||||
const REGEX = /^https?:\/\/.*/;
|
||||
@@ -29,25 +29,29 @@ function downloadScript (url, options, onComplete) {
|
||||
}
|
||||
}
|
||||
|
||||
function handleZip (url, options, onComplete) {
|
||||
let cachedUnzip = cacheManager.cachedFiles.get(url);
|
||||
if (cachedUnzip) {
|
||||
cacheManager.updateLastTime(url);
|
||||
onComplete && onComplete(null, cachedUnzip.url);
|
||||
var handleZip = CC_WORKER_ASSET_PIPELINE
|
||||
? function handleZip(url, options, onComplete) {
|
||||
cacheManager.handleZip(url, options, onComplete);
|
||||
}
|
||||
else if (REGEX.test(url)) {
|
||||
downloadFile(url, null, options.header, options.onFileProgress, function (err, downloadedZipPath) {
|
||||
if (err) {
|
||||
onComplete && onComplete(err);
|
||||
return;
|
||||
}
|
||||
cacheManager.unzipAndCacheBundle(url, downloadedZipPath, options.__cacheBundleRoot__, onComplete);
|
||||
});
|
||||
: function handleZip(url, options, onComplete) {
|
||||
let cachedUnzip = cacheManager.cachedFiles.get(url);
|
||||
if (cachedUnzip) {
|
||||
cacheManager.updateLastTime(url);
|
||||
onComplete && onComplete(null, cachedUnzip.url);
|
||||
}
|
||||
else if (REGEX.test(url)) {
|
||||
downloadFile(url, null, options.header, options.onFileProgress, function (err, downloadedZipPath) {
|
||||
if (err) {
|
||||
onComplete && onComplete(err);
|
||||
return;
|
||||
}
|
||||
cacheManager.unzipAndCacheBundle(url, downloadedZipPath, options.__cacheBundleRoot__, onComplete);
|
||||
});
|
||||
}
|
||||
else {
|
||||
cacheManager.unzipAndCacheBundle(url, url, options.__cacheBundleRoot__, onComplete);
|
||||
}
|
||||
}
|
||||
else {
|
||||
cacheManager.unzipAndCacheBundle(url, url, options.__cacheBundleRoot__, onComplete);
|
||||
}
|
||||
}
|
||||
|
||||
function downloadDomAudio (url, options, onComplete) {
|
||||
if (typeof options === 'function') {
|
||||
@@ -68,36 +72,40 @@ function downloadDomAudio (url, options, onComplete) {
|
||||
onComplete && onComplete(null, dom);
|
||||
}
|
||||
|
||||
function download (url, func, options, onFileProgress, onComplete) {
|
||||
var result = transformUrl(url, options);
|
||||
if (result.inLocal) {
|
||||
func(result.url, options, onComplete);
|
||||
var download = CC_WORKER_ASSET_PIPELINE
|
||||
? function download(url, func, options, onFileProgress, onComplete) {
|
||||
cacheManager.download(url, func, options, onFileProgress, onComplete);
|
||||
}
|
||||
else if (result.inCache) {
|
||||
cacheManager.updateLastTime(url);
|
||||
func(result.url, options, function (err, data) {
|
||||
if (err) {
|
||||
cacheManager.removeCache(url);
|
||||
}
|
||||
onComplete(err, data);
|
||||
});
|
||||
}
|
||||
else {
|
||||
downloadFile(url, null, options.header, onFileProgress, function (err, path) {
|
||||
if (err) {
|
||||
onComplete(err, null);
|
||||
return;
|
||||
}
|
||||
func(path, options, function (err, data) {
|
||||
if (!err) {
|
||||
cacheManager.tempFiles.add(url, path);
|
||||
cacheManager.cacheFile(url, path, options.cacheEnabled, options.__cacheBundleRoot__, true);
|
||||
: function download(url, func, options, onFileProgress, onComplete) {
|
||||
var result = transformUrl(url, options);
|
||||
if (result.inLocal) {
|
||||
func(result.url, options, onComplete);
|
||||
}
|
||||
else if (result.inCache) {
|
||||
cacheManager.updateLastTime(url);
|
||||
func(result.url, options, function (err, data) {
|
||||
if (err) {
|
||||
cacheManager.removeCache(url);
|
||||
}
|
||||
onComplete(err, data);
|
||||
});
|
||||
});
|
||||
}
|
||||
else {
|
||||
downloadFile(url, null, options.header, onFileProgress, function (err, path) {
|
||||
if (err) {
|
||||
onComplete(err, null);
|
||||
return;
|
||||
}
|
||||
func(path, options, function (err, data) {
|
||||
if (!err) {
|
||||
cacheManager.tempFiles.add(url, path);
|
||||
cacheManager.cacheFile(url, path, options.cacheEnabled, options.__cacheBundleRoot__, true);
|
||||
}
|
||||
onComplete(err, data);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function parseArrayBuffer (url, options, onComplete) {
|
||||
readArrayBuffer(url, onComplete);
|
||||
@@ -131,7 +139,11 @@ var loadFont = !isSubDomain ? function (url, options, onComplete) {
|
||||
onComplete(null, 'Arial');
|
||||
}
|
||||
|
||||
function doNothing (content, options, onComplete) {
|
||||
function doNothing(content, options, onComplete) {
|
||||
if (CC_WORKER_ASSET_PIPELINE) {
|
||||
onComplete(null, content);
|
||||
return;
|
||||
}
|
||||
exists(content, (existence) => {
|
||||
if (existence) {
|
||||
onComplete(null, content);
|
||||
@@ -402,6 +414,9 @@ parser.register({
|
||||
});
|
||||
|
||||
var transformUrl = !isSubDomain ? function (url, options) {
|
||||
if (CC_WORKER_ASSET_PIPELINE) {
|
||||
console.error('transformUrl should not be called when the macro CC_WORKER_ASSET_PIPELINE is enabled.');
|
||||
}
|
||||
var inLocal = false;
|
||||
var inCache = false;
|
||||
var isInUserDataPath = url.startsWith(getUserDataPath());
|
||||
|
Reference in New Issue
Block a user