soyoo-cocos/single-html/loader-and-starter.js

147 lines
94 KiB
JavaScript
Raw Normal View History

2024-12-05 21:14:41 +08:00
window.startGame = function() {
var startInterval = setInterval(() => {
if (window.registerLoad) {
window.boot();
clearInterval(startInterval);
}
}, 16)
2024-12-05 21:14:41 +08:00
}
window.pauseGame = function() {
cc.game.pause()
}
window.resumeGame = function() {
cc.game.resume()
}
window.muteAudio = function() {
cc.audioEngine.pauseAll()
}
window.unmuteAudio = function() {
cc.audioEngine.resumeAll()
}
window.registerLoad = false;
2024-12-05 21:14:41 +08:00
console.log("entry file loaded")
function b64ToUint6(nChr) {
return nChr > 64 && nChr < 91
? nChr - 65 : nChr > 96 && nChr < 123
? nChr - 71 : nChr > 47 && nChr < 58
? nChr + 4 : nChr === 43
? 62 : nChr === 47
? 63 : 0
}
/** 官网范例+1,看不懂+1,作用是将base64编码的字符串转为ArrayBuffer */
function base64DecToArr(sBase64, nBlockSize) {
var sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""), nInLen = sB64Enc.length
var nOutLen = nBlockSize ? Math.ceil((nInLen * 3 + 1 >>> 2) / nBlockSize) * nBlockSize : nInLen * 3 + 1 >>> 2
var aBytes = new Uint8Array(nOutLen)
for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) {
nMod4 = nInIdx & 3
nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4
if (nMod4 === 3 || nInLen - nInIdx === 1) {
for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++ , nOutIdx++) {
aBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255;
}
nUint24 = 0
}
}
return aBytes
}
/**
* 修改部分资源的载入方式,可以根据项目中实际用到的资源进行修改
* - [注意] assetMap 是自己定义的,名称可以修改
*/
function loadJs(item, _, callback) {
if (!item.startsWith("assets")) {
item = "assets/" + item
}
eval(assetMap[item])
callback(null, null)
}
function loadJson(item, _, callback) {
if (!item.startsWith("assets")) {
item = "assets/" + item
}
callback(null, JSON.parse(assetMap[item]))
}
var assetMap = window.assetMap;
var registerInterval = setInterval(() => {
if (window.cc == undefined) {
return;
}
clearInterval(registerInterval);
2024-12-06 18:05:40 +08:00
cc.assetManager.downloader.register({
"bundle": function (item, _, callback) {
var bundleJsKey = Object.keys(assetMap).find(key => key.includes(item + "/index") && key.endsWith(".js"))
if (!bundleJsKey) {
console.error(`bundle js ${item} not found`)
}
loadJs(bundleJsKey, _, (a1, data) => {
console.log("bundle js loaded:", bundleJsKey)
})
2024-12-06 18:05:40 +08:00
var bundleJsonKey = Object.keys(assetMap).find(key => key.includes(item + "/config") && key.endsWith(".json"))
if (!bundleJsonKey) {
console.error(`bundle ${item} not found`)
2024-12-05 21:14:41 +08:00
}
loadJson(bundleJsonKey, _, (a1, data) =>
2024-12-06 18:05:40 +08:00
{
var completeData = data;
completeData.base = "assets/" + item + "/"
callback(null, completeData)
})
},
".json": loadJson,
".plist": function (item, _, callback) {
callback(null, assetMap[item])
},
".png": function (item, _, callback) {
var img = new Image()
img.src = "data:image/png;base64," + assetMap[item] // 注意需要给base64编码添加前缀
callback(null, img)
},
".jpg": function (item, _, callback) {
var img = new Image()
img.src = "data:image/jpeg;base64," + assetMap[item]
callback(null, img)
},
".webp": function (item, _, callback) {
var img = new Image()
img.src = "data:image/webp;base64," + assetMap[item]
callback(null, img)
},
".mp3": function (item, _, callback) {
// 只支持以webAudio形式播放的声音
// 将base64编码的声音文件转化为ArrayBuffer
cc.sys.__audioSupport.context.decodeAudioData(
base64DecToArr(assetMap[item]).buffer,
// success
function (buffer) {
callback(null, buffer)
},
// fail
function (buffer) {
callback(new Error("mp3-res-fail"), null)
}
)
},
})
window.registerLoad = true;
}, 16);
2024-12-06 18:05:40 +08:00
window.base122ToArrayBuffer = (e) => { const n = [0, 10, 13, 34, 38, 92], r = new Uint8Array(1.75 * e.length | 0); let t = 0, o = 0, f = 0; function s(e) { o |= (e <<= 1) >>> f, f += 7, f >= 8 && (r[t++] = o, f -= 8, o = e << 7 - f & 255) } for (let r = 0; r < e.length; r++) { const t = e.charCodeAt(r); if (t > 127) { const e = t >>> 8 & 7; 7 !== e && s(n[e]), s(127 & t) } else s(t) } return new Uint8Array(r.buffer, 0, t) }
// declare window.unbrotli
(()=>{var e={923:e=>{var n=4096,t=new Uint32Array([0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535,131071,262143,524287,1048575,2097151,4194303,8388607,16777215]);function r(e){this.buf_=new Uint8Array(8224),this.input_=e,this.reset()}r.READ_SIZE=n,r.IBUF_MASK=8191,r.prototype.reset=function(){this.buf_ptr_=0,this.val_=0,this.pos_=0,this.bit_pos_=0,this.bit_end_pos_=0,this.eos_=0,this.readMoreInput();for(var e=0;e<4;e++)this.val_|=this.buf_[this.pos_]<<8*e,++this.pos_;return this.bit_end_pos_>0},r.prototype.readMoreInput=function(){if(!(this.bit_end_pos_>256))if(this.eos_){if(this.bit_pos_>this.bit_end_pos_)throw new Error("Unexpected end of input "+this.bit_pos_+" "+this.bit_end_pos_)}else{var e=this.buf_ptr_,t=this.input_.read(this.buf_,e,n);if(t<0)throw new Error("Unexpected end of input");if(t<n){this.eos_=1;for(var r=0;r<32;r++)this.buf_[e+t+r]=0}if(0===e){for(r=0;r<32;r++)this.buf_[8192+r]=this.buf_[r];this.buf_ptr_=n}else this.buf_ptr_=0;this.bit_end_pos_+=t<<3}},r.prototype.fillBitWindow=function(){for(;this.bit_pos_>=8;)this.val_>>>=8,this.val_|=this.buf_[8191&this.pos_]<<24,++this.pos_,this.bit_pos_=this.bit_pos_-8>>>0,this.bit_end_pos_=this.bit_end_pos_-8>>>0},r.prototype.readBits=function(e){32-this.bit_pos_<e&&this.fillBitWindow();var n=this.val_>>>this.bit_pos_&t[e];return this.bit_pos_+=e,n},e.exports=r},901:(e,n)=>{n.lookup=new Uint8Array([0,0,0,0,0,0,0,0,0,4,4,0,0,4,0,0,0,0,0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,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,0,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,56,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,