白鹭引擎支持自定义资源加载器, 可以方便的使用加密资源;
例如有一个 expmble.png, 加密后为 example.png.enc
下面是自定义资源处理器的示例:
在Main.ts里面, 注册一个自定义的解析器, EncryptAnalyzer;
//Main.ts入口文件, 增加enc格式注册
RES.processor.map("enc",EncryptAnalyzer);
增加 EncryptAnalyzer.ts 文件
//EncryptAnalyzer.ts
var EncryptAnalyzer:RES.processor.Processor = {
onLoadStart(host,resource){
return new Promise(( reslove ,reject ) => {
var request: egret.HttpRequest = new egret.HttpRequest();
request.responseType = egret.HttpResponseType.ARRAY_BUFFER;
request.open(resource.root+resource.url, "get");
request.send();
let onSuccess = ()=>{
let bufData = request['response'];<br>
//对加密的数据, 使用xxtea解密
let _xxtea = xxtea.getInstance();
let bytes = _xxtea.decrypt(new Uint8Array(bufData), 'password');
egret.BitmapData.create('arraybuffer', bytes, function(bmpData){
let texture = new egret.Texture();
texture._setBitmapData(bmpData);
return reslove(texture);
});
}
request.addEventListener(egret.Event.COMPLETE, onSuccess, this);
})
},
onRemoveStart(host,resource){
let texture = host.get(resource);
texture.dispose();
return Promise.resolve();
}
}