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

move subPackPipe to engine and cache asset slowly #72

Merged
merged 2 commits into from
Feb 19, 2019
Merged
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
8 changes: 1 addition & 7 deletions wechatgame/game.js
Expand Up @@ -4,7 +4,6 @@ window.DOMParser = Parser.DOMParser;
require('libs/wx-downloader.js');
require('src/settings');
var settings = window._CCSettings;
var SubPackPipe = require('./libs/subpackage-pipe');
require('main');
require(settings.debug ? 'cocos2d-js.js' : 'cocos2d-js-min.js');
require('./libs/engine/index.js');
Expand All @@ -14,14 +13,9 @@ cc.view._maxPixelRatio = 3;

wxDownloader.REMOTE_SERVER_ROOT = "";
wxDownloader.SUBCONTEXT_ROOT = "";
var pipeBeforeDownloader = cc.loader.md5Pipe || cc.loader.assetLoader;
var pipeBeforeDownloader = cc.loader.subPackPipe || cc.loader.md5Pipe || cc.loader.assetLoader;
cc.loader.insertPipeAfter(pipeBeforeDownloader, wxDownloader);

if (settings.subpackages) {
var subPackPipe = new SubPackPipe(settings.subpackages);
cc.loader.insertPipeAfter(pipeBeforeDownloader, subPackPipe);
}

if (cc.sys.browserType === cc.sys.BROWSER_TYPE_WECHAT_GAME_SUB) {
require('./libs/sub-context-adapter');
}
Expand Down
67 changes: 0 additions & 67 deletions wechatgame/libs/subpackage-pipe.js

This file was deleted.

97 changes: 59 additions & 38 deletions wechatgame/libs/wx-downloader.js
Expand Up @@ -35,6 +35,12 @@ var binary_format = [

const REGEX = /^\w+:\/\/.*/;

// used to control cache
var cacheQueue = {};
var checkNextPeriod = false;
// cache one per cycle
var cachePeriod = 100;

var fs = wx.getFileSystemManager ? wx.getFileSystemManager() : null;

var _newAssets = [];
Expand Down Expand Up @@ -185,12 +191,17 @@ function nextPipe(item, callback) {
var queue = cc.LoadingItems.getQueue(item);
queue.addListener(item.id, function (item) {
if (item.error) {
fs && fs.unlink({
filePath: item.url,
success: function () {
cc.log('Load failed, removed local file ' + item.url + ' successfully!');
}
});
if (item.url in cacheQueue) {
delete cacheQueue[item.url];
}
else {
fs && fs.unlink({
filePath: item.url,
success: function () {
cc.log('Load failed, removed local file ' + item.url + ' successfully!');
}
});
}
}
});
callback(null, null);
Expand Down Expand Up @@ -322,6 +333,38 @@ function ensureDirFor (path, callback) {
});
}

function cacheAsset (url, localPath) {
cacheQueue[url] = localPath;

if (!checkNextPeriod) {
checkNextPeriod = true;
function cache () {
checkNextPeriod = false;
for (var url in cacheQueue) {
var localPath = cacheQueue[url];
ensureDirFor(localPath, function () {
// Save to local path
wx.saveFile({
tempFilePath: url,
filePath: localPath,
success: function (res) {
cc.log('cache success ' + localPath);
}
});
});

delete cacheQueue[url];
if (!cc.js.isEmptyObject(cacheQueue) && !checkNextPeriod) {
checkNextPeriod = true;
setTimeout(cache, cachePeriod);
}
return;
}
};
setTimeout(cache, cachePeriod);
}
}

function downloadRemoteFile (item, callback) {
// Download from remote server
var relatUrl = item.url;
Expand All @@ -340,37 +383,15 @@ function downloadRemoteFile (item, callback) {
if (res.statusCode === 200 && res.tempFilePath) {
// http reading is not cached
var temp = res.tempFilePath;
var localPath = wx.env.USER_DATA_PATH + '/' + relatUrl;
// check and mkdir remote folder has exists
ensureDirFor(localPath, function () {
// Save to local path
wx.saveFile({
tempFilePath: res.tempFilePath,
filePath: localPath,
success: function (res) {
// cc.log('save:' + localPath);
item.url = res.savedFilePath;
if (item.type && non_text_format.indexOf(item.type) !== -1) {
nextPipe(item, callback);
}
else {
readText(item, callback);
}
},
fail: function (res) {
// Failed to save file, then just use temp
console.log(res && res.errMsg ? res.errMsg : 'save file failed: ' + remoteUrl);
console.log('It might be due to out of storage spaces, you can clean your storage spaces manually.');
item.url = temp;
if (item.type && non_text_format.indexOf(item.type) !== -1) {
nextPipe(item, callback);
}
else {
readText(item, callback);
}
}
});
});
item.url = temp;
if (item.type && non_text_format.indexOf(item.type) !== -1) {
nextPipe(item, callback);
}
else {
readText(item, callback);
}
cacheAsset(temp, wx.env.USER_DATA_PATH + '/' + relatUrl);

}
else {
cc.warn("Download file failed: " + remoteUrl);
Expand All @@ -387,7 +408,7 @@ function downloadRemoteFile (item, callback) {
errorMessage: res && res.errMsg ? res.errMsg : "Download file failed: " + remoteUrl
}, null);
}
})
});
}

// function downloadRemoteTextFile (item, callback) {
Expand Down