Skip to content

Commit

Permalink
md5 cache respect libraryPath and rawAssetsPath
Browse files Browse the repository at this point in the history
improve libraryBase

use slice instead replace
  • Loading branch information
zilongshanren committed Jul 19, 2017
1 parent 8c24ea1 commit 6ea5d79
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
12 changes: 10 additions & 2 deletions cocos2d/core/load-pipeline/md5-pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,24 @@ var Pipeline = require('./pipeline');
var ID = 'MD5Pipe';
var ExtnameRegex = /(\.[^.\n\\/]*)$/;

var MD5Pipe = function (md5AssetsMap) {
var MD5Pipe = function (md5AssetsMap, libraryBase, rawAssetsBase) {
this.id = ID;
this.async = false;
this.pipeline = null;
this.md5AssetsMap = md5AssetsMap;
this.libraryBase = libraryBase;
this.rawAssetsBase = rawAssetsBase;
};
MD5Pipe.ID = ID;

MD5Pipe.prototype.handle = function(item) {
let hashValue = this.md5AssetsMap[item.url];
var key = item.url;
if (item.url.startsWith(this.libraryBase)) {
key = key.slice(this.libraryBase.length);
} else {
key = key.slice(this.rawAssetsBase.length);
}
let hashValue = this.md5AssetsMap[key];
if (hashValue) {
var matched = false;
item.url = item.url.replace(ExtnameRegex, function(match, p1) {
Expand Down
9 changes: 5 additions & 4 deletions cocos2d/core/platform/CCAssetLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,6 @@ var AssetLibrary = {
return;
}

var md5AssetsMap = options.md5AssetsMap;
if (md5AssetsMap) {
cc.loader.insertPipeAfter(cc.loader.assetLoader, new MD5Pipe(md5AssetsMap));
}

// 这里将路径转 url,不使用路径的原因是有的 runtime 不能解析 "\" 符号。
// 不使用 url.format 的原因是 windows 不支持 file:// 和 /// 开头的协议,所以只能用 replace 操作直接把路径转成 URL。
Expand All @@ -288,6 +284,11 @@ var AssetLibrary = {

_rawAssetsBase = options.rawAssetsBase;

var md5AssetsMap = options.md5AssetsMap;
if (md5AssetsMap) {
cc.loader.insertPipeAfter(cc.loader.assetLoader, new MD5Pipe(md5AssetsMap, _libraryBase, _rawAssetsBase));
}

// init raw assets

var resources = Loader._resources;
Expand Down

0 comments on commit 6ea5d79

Please sign in to comment.