Loader.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. window.onerror = function handleErr(msg, url, line, colno, error) {
  2. try {
  3. let data = "";
  4. let title = "";
  5. if (error.stack) {
  6. let text = "----------------------------------------------------------------------------\n";
  7. text = text + error.stack + "\n";
  8. text = text + "-----------------------------------------------------------------------------\n";
  9. title = "onerror.stack__" + url;
  10. data = error.stack;
  11. console.error('error.stack', text);
  12. }
  13. else {
  14. let text = "----------------------------------------------------------------------------\n";
  15. text = text + url + " (" + line + ":" + colno + ")\n";
  16. text = text + "-----------------------------------------------------------------------------\n";
  17. title = "onerro.nostack__" + url;
  18. data = line + ":" + colno;
  19. console.error('error.stackelse', text);
  20. }
  21. let senddata = {
  22. "level": "error",
  23. "game": "tower",
  24. "message": data,
  25. }
  26. const Http = new XMLHttpRequest();
  27. Http.responseType = "text";
  28. const send_url = 'https://elktest.duiweize.com/api/logs/record';
  29. Http.open("POST", send_url);
  30. Http.setRequestHeader("Content-Type", 'application/json;charset=UTF-8');
  31. let str = JSON.stringify(senddata);
  32. let options = {
  33. default_key_size: 2048,
  34. default_public_exponent: new Uint8Array([0x01, 0x00, 0x01])
  35. }
  36. let jsb = new window['JSEncrypt'](options)
  37. //公钥
  38. var publicString = '-----BEGIN PUBLIC KEY-----\n' +
  39. 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuwz5V8wt525dMS+A9wOd\n' +
  40. 'NAjvKgxdyoXYuZSlAJEXLhvJlaXwFCKEsUg/qBBeo4nrRnwaFAjG7ItqbCWR9Ozv\n' +
  41. '6oY5tMYQnFFK82XkMGbKm1gG0z9vYKhcNYgHZHi+QKAw4tLLWXWO/QBTGelEXptD\n' +
  42. 'azlNmdM+8wHOknTcMuIyjc9Yo5qCoNsCJpA465vnREiBezBbKB3rSw+LmrRcZcFk\n' +
  43. 's8hmGtjpabcQ8sCxco7mRzVh6/DomNWfNm7hUP6shrrJirwjQtaS51grlScj8KhN\n' +
  44. 'h+kBoiI4Ad0Xz0GHasK5hAMf3sXDsN61r0An8KTfa/nsYSJDwogPIKkeKAsmw9Df\n' +
  45. 'EQIDAQAB\n' +
  46. '-----END PUBLIC KEY-----'
  47. jsb.setPublicKey(publicString)
  48. // 加密内容
  49. str = jsb.encrypt(str)
  50. Http.send(str);
  51. } catch (e) { }
  52. return false;
  53. };
  54. /** 加载页 */
  55. import GameM from "./manager/GameM";
  56. import AdM from "./manager/AdM";
  57. import PoolMgr from "./manager/PoolMgr";
  58. import { STORAGE_KEY } from "./datas/GlobalStorage";
  59. import UiM, { PANEL_NAME } from "./manager/UiM";
  60. import RuleNode from "./prefabs/RuleNode";
  61. import DownLoadUI from "./ui/DownLoadUI";
  62. import LogUtil from "./utils/LogUtil";
  63. import PrefabManager from "./manager/PrefabManager";
  64. import { Utils } from "./utils/Utils";
  65. const { ccclass, property } = cc._decorator;
  66. @ccclass
  67. export default class Loader extends cc.Component {
  68. private res_dirArr = ['configs', 'gift']
  69. //private res_dirArr = null
  70. @property(cc.Node)
  71. progressNode: cc.Node = null;
  72. @property(cc.Label)
  73. progressLabel: cc.Label = null;
  74. @property(cc.Sprite)
  75. progressSlider: cc.Sprite = null;
  76. @property(cc.Node)
  77. chuan: cc.Node = null;
  78. @property(cc.Node)
  79. tipNode: cc.Node = null;
  80. @property(cc.Label)
  81. tipTxt: cc.Label = null;
  82. hotUpdate = null
  83. loadComplete: boolean = false;
  84. hasTap = false
  85. private progressNum: number = 0;
  86. private progressRun: number = 0;
  87. private isLoadedScene: boolean = false;
  88. isLoaded: boolean = false;
  89. /** 是否检查热更 */
  90. hasCheck = false
  91. hasCheckPrivacy = false
  92. /** 检查防沉迷 */
  93. hasCheckAnti = false
  94. passAnti = false
  95. /** 检查安装更新 */
  96. checkInstallUpdate = false
  97. /** 安装更新 */
  98. installUpdate = false
  99. /** 安装更新显示 */
  100. hasShowUpdate = false
  101. /** 忽略更新 */
  102. ignoreInstall = false
  103. /** 检查是否清除本地热更资源 */
  104. checkRemoveLocalHot = false
  105. public static Inst: Loader = null
  106. onLoad() {
  107. Loader.Inst = this
  108. this.hotUpdate = cc.find('Canvas').getComponent('HotUpdate')
  109. }
  110. start() {
  111. // // 开启调试
  112. // cc.dynamicAtlasManager.showDebug(true);
  113. // // 关闭调试
  114. // cc.dynamicAtlasManager.showDebug(false);
  115. cc.macro.ENABLE_MULTI_TOUCH = false;
  116. this.loadLocalRes()
  117. let loadIn = GameM.globalStorage.getStorage(STORAGE_KEY.firstLoadIn)
  118. if (loadIn == null) {
  119. loadIn = 1
  120. GameM.globalStorage.setStorage(STORAGE_KEY.firstLoadIn, loadIn)
  121. AdM.onSendEvent('firstLoadIn', '第一次加载开始')
  122. }
  123. this.loadScene("Main");
  124. //报错监听
  125. //this.CheckError();
  126. }
  127. /** 检查弹出隐私 */
  128. checkPrivacy() {
  129. let hasAgree = GameM.globalStorage.getStorage(STORAGE_KEY.popRule)
  130. if (hasAgree != null) {
  131. GameM.commonData.passPrivacy = true
  132. } else {
  133. UiM.Instance.onPanel(PANEL_NAME.RuleNode, false, () => {
  134. UiM.Instance.ruleNode.getComponent(RuleNode).init(1)
  135. })
  136. }
  137. }
  138. /** 检查认证和防沉迷 */
  139. checkShowIdentification() {
  140. let value = GameM.globalStorage.getStorage(STORAGE_KEY.identification)
  141. if (value != null) {
  142. GameM.commonData.identification = value
  143. }
  144. console.log('GameM.commonData.identification ', GameM.commonData.identification)
  145. value = GameM.globalStorage.getStorage(STORAGE_KEY.todayPlayTime)
  146. if (value != null) {
  147. GameM.commonData.todayPlayTime = value
  148. }
  149. if (GameM.commonData.identification == 0) {
  150. if (UiM.Instance.CertificationNode == null) {
  151. UiM.Instance.onPanel(PANEL_NAME.CertificationNode)
  152. }
  153. }
  154. else if (GameM.commonData.identification == 2) {
  155. if (GameM.commonData.todayPlayTime >= 5400) {
  156. if (UiM.Instance.AntiAddictionNode == null) {
  157. UiM.Instance.onPanel(PANEL_NAME.AntiAddictionNode)
  158. }
  159. }
  160. else {
  161. this.passAnti = true
  162. }
  163. }
  164. else {
  165. this.passAnti = true
  166. }
  167. }
  168. /** 加载本地资源 */
  169. loadLocalRes() {
  170. let resDirLength = this.res_dirArr.length
  171. let loadedResNum = 0
  172. console.log('self.resDirLength ', resDirLength)
  173. this.res_dirArr.forEach(element => {
  174. cc.loader.loadResDir(element,
  175. (completedCount, totalCount, item) => {
  176. },
  177. () => {
  178. loadedResNum++
  179. console.log('this.loadedResNum ', loadedResNum)
  180. if (loadedResNum == resDirLength) {
  181. console.log('加载完成')
  182. PoolMgr.Inst.initPoolPrefab();
  183. GameM.commonData.carCfg = cc.loader.getRes('configs/CarCfg').json
  184. GameM.commonData.buyNumCfg = cc.loader.getRes('configs/BuynumCfg').json
  185. GameM.commonData.roleCft = cc.loader.getRes('configs/RoleCfg').json
  186. GameM.commonData.TxtCft = cc.loader.getRes('configs/TxtCfg').json
  187. GameM.commonData.achieveCft = cc.loader.getRes('configs/AchieveCfg').json
  188. GameM.commonData.taskCft = cc.loader.getRes('configs/TaskCfg').json
  189. GameM.commonData.taskAwardCft = cc.loader.getRes('configs/TaskAwardCfg').json
  190. GameM.ClubData.clubShowShareInfo = cc.loader.getRes('configs/ClubShowShareInfoCfg').json
  191. //西游
  192. GameM.commonData.fightCfg = cc.loader.getRes('configs/fightCfg').json
  193. GameM.commonData.fightRewardCfg = cc.loader.getRes('configs/fightRewardCfg').json
  194. GameM.commonData.fightRoleCfg = cc.loader.getRes('configs/fightRoleCfg').json
  195. GameM.commonData.fightRoleServerCfg = cc.loader.getRes('configs/fightRoleServerCfg').json
  196. GameM.commonData.buffCfg = cc.loader.getRes('configs/buffCfg').json
  197. GameM.commonData.buffbagCfg = cc.loader.getRes('configs/buffbagCfg').json
  198. GameM.commonData.mateCfg = cc.loader.getRes('configs/mateCfg').json
  199. GameM.commonData.mateTalentCfg = cc.loader.getRes('configs/mateTalentCfg').json
  200. GameM.commonData.mainBgCfg = cc.loader.getRes('configs/mainBgCfg').json
  201. if (CC_JSB) {
  202. AdM.getWxAppId();
  203. }
  204. else {
  205. if (GameM.commonData.session_key != '' && GameM.commonData.uin != '') {
  206. console.log(">>>>>5uin:" + GameM.commonData.uin);
  207. GameM.httpM.getAccountInfo()
  208. }
  209. else {
  210. GameM.httpM.init();
  211. }
  212. }
  213. this.isLoaded = true;
  214. console.log('MMM self.isLoaded')
  215. }
  216. });
  217. });
  218. }
  219. update(dt) {
  220. if (this.hasCheck && !this.hotUpdate.updateFinish) {
  221. if (this.hotUpdate._updating && this.hotUpdate.percent != 100) {
  222. this.progressSlider.fillRange = this.hotUpdate.percent * 0.01;
  223. this.progressLabel.string = '更新中 ' + this.hotUpdate.percent + "%";
  224. }
  225. }
  226. else {
  227. this.progressRun += 0.03;
  228. let num = this.progressRun * 100
  229. if (num >= 100) {
  230. num = 99
  231. }
  232. this.progressLabel.string = '加载不耗流量 ' + parseInt(num + "") + "%";
  233. this.chuan.parent.getComponent(cc.Sprite).fillRange = this.progressRun
  234. }
  235. if (cc.sys.os == cc.sys.OS_ANDROID) {
  236. if (GameM.commonData.appVersion == '') {
  237. return
  238. }
  239. else {
  240. if (!this.checkRemoveLocalHot) {
  241. let currentVersion = GameM.globalStorage.getStorage('currentVersion', 1)
  242. GameM.globalStorage.setStorage('currentVersion', GameM.commonData.appVersion)
  243. LogUtil.logV('MMM currentVersion >>> ', currentVersion)
  244. LogUtil.logV('MMM appVersion >>> ', GameM.commonData.appVersion)
  245. let path = localStorage.getItem('HotUpdateSearchPaths');
  246. LogUtil.logV('MMM path >>> ', path)
  247. if (currentVersion) {
  248. let compareVer = Utils.versionCompareHandle(currentVersion, GameM.commonData.appVersion)
  249. if (compareVer < 0) {
  250. // 热更新的储存路径,如果旧版本中有多个,可能需要记录在列表中,全部清理
  251. var storagePath = localStorage.getItem('HotUpdateSearchPaths');
  252. if (storagePath) {
  253. let storageArr = JSON.parse(storagePath)
  254. let len = storageArr.length
  255. for (var i = 0; i < len; i++) {
  256. let path = storageArr.shift()
  257. jsb.fileUtils.removeDirectory(path);
  258. LogUtil.logV('MMM removeDirectory', storageArr[i])
  259. }
  260. LogUtil.logV('MMM newStorageArr', storageArr)
  261. cc.sys.localStorage.removeItem('HotUpdateSearchPaths');
  262. jsb.fileUtils.setSearchPaths(storageArr);
  263. cc.audioEngine.stopAll();
  264. cc.game.restart();
  265. }
  266. }
  267. }
  268. this.checkRemoveLocalHot = true
  269. LogUtil.logV('MMM storagePath', storagePath)
  270. return
  271. }
  272. }
  273. }
  274. if (!this.isLoadedScene && this.isLoaded && this.loadComplete && GameM.commonData.serverFinish && PrefabManager.Loaded) {
  275. if (GameM.commonData.popPrivacySwitch) {
  276. if (!this.hasCheckPrivacy) {
  277. this.hasCheckPrivacy = true
  278. this.checkPrivacy()
  279. }
  280. if (!GameM.commonData.passPrivacy) {
  281. return
  282. }
  283. }
  284. if (GameM.commonData.popIdentifySwitch) {
  285. if (!this.hasCheckAnti) {
  286. this.hasCheckAnti = true
  287. this.checkShowIdentification()
  288. }
  289. if (!this.passAnti) {
  290. return
  291. }
  292. }
  293. //检查安装更新
  294. if (cc.sys.os == cc.sys.OS_ANDROID) {
  295. if (GameM.commonData.appVersion == '' || GameM.commonData.needForceUpdate == -1) {
  296. return
  297. }
  298. else {
  299. // if (!this.checkRemoveLocalHot) {
  300. // let currentVersion = GameM.globalStorage.getStorage('currentVersion', 1)
  301. // GameM.globalStorage.setStorage('currentVersion', GameM.commonData.appVersion)
  302. // LogUtil.logV('MMM currentVersion >>> ', currentVersion)
  303. // LogUtil.logV('MMM appVersion >>> ', GameM.commonData.appVersion)
  304. // let path = localStorage.getItem('HotUpdateSearchPaths');
  305. // LogUtil.logV('MMM path >>> ', path)
  306. // if (currentVersion) {
  307. // let compareVer = Utils.versionCompareHandle(currentVersion, GameM.commonData.appVersion)
  308. // if (compareVer < 0) {
  309. // // 热更新的储存路径,如果旧版本中有多个,可能需要记录在列表中,全部清理
  310. // var storagePath = localStorage.getItem('HotUpdateSearchPaths');
  311. // if (storagePath) {
  312. // let storageArr = JSON.parse(storagePath)
  313. // let len = storageArr.length - 2
  314. // for (var i = 0; i < len; i++) {
  315. // let path = storageArr.shift()
  316. // jsb.fileUtils.removeDirectory(path);
  317. // LogUtil.logV('MMM removeDirectory', storageArr[i])
  318. // }
  319. // LogUtil.logV('MMM newStorageArr', storageArr)
  320. // cc.sys.localStorage.removeItem('HotUpdateSearchPaths');
  321. // jsb.fileUtils.setSearchPaths(storageArr);
  322. // cc.audioEngine.stopAll();
  323. // cc.game.restart();
  324. // }
  325. // }
  326. // }
  327. // this.checkRemoveLocalHot = true
  328. // LogUtil.logV('MMM storagePath', storagePath)
  329. // return
  330. // }
  331. }
  332. if (!this.checkInstallUpdate) {
  333. // LogUtil.logV('MMM GameM.commonData.localVersion ', GameM.commonData.localVersion + ' installVersion ' + installVersion)
  334. if (GameM.commonData.needForceUpdate == 1) {
  335. this.installUpdate = true
  336. this.ignoreInstall = (GameM.globalStorage.getStorage('installVersion' + GameM.commonData.installVersion, 1) != null)
  337. }
  338. else {
  339. this.installUpdate = false
  340. }
  341. this.checkInstallUpdate = true
  342. return
  343. }
  344. //需要安装最新包
  345. if (this.installUpdate) {
  346. //此版本没有 不再提醒
  347. if (!this.ignoreInstall) {
  348. if (!this.hasShowUpdate) {
  349. this.hasShowUpdate = true
  350. UiM.Instance.onPanel(PANEL_NAME.DownLoadUI, false, () => {
  351. UiM.Instance.downLoadUI.getComponent(DownLoadUI).init(true)
  352. })
  353. }
  354. //下次再说
  355. if (!GameM.commonData.skipUpdate) {
  356. return
  357. }
  358. }
  359. }
  360. else {
  361. //检查热更新
  362. if (!this.hasCheck) {
  363. if (window.jsb) {
  364. this.hotUpdate.checkUpdate()
  365. console.log('MMM checkUpdate ')
  366. }
  367. else {
  368. this.hotUpdate.updateFinish = true
  369. console.log('MMM updateFinish yse ')
  370. }
  371. this.hasCheck = true
  372. console.log('MMM updateFinish NO ')
  373. return
  374. }
  375. if (!this.hotUpdate.updateFinish) {
  376. console.log('MMM updateFinish state false')
  377. return
  378. }
  379. }
  380. }
  381. this.isLoadedScene = true;
  382. this.progressRun = 0.99
  383. this.progressLabel.string = '加载不耗流量 ' + parseInt(99 + "") + "%";
  384. this.chuan.parent.getComponent(cc.Sprite).fillRange = 1
  385. console.log('MMM getloadscene ')
  386. //获取玩家数据后进入main
  387. cc.director.loadScene("Main");
  388. }
  389. }
  390. loadScene(scene: string) {
  391. let time1 = (new Date()).getTime();
  392. let self = this;
  393. cc.director.preloadScene(scene,
  394. (completedCount, totalCount, item) => {
  395. if (completedCount / totalCount > self.progressNum) {
  396. self.progressNum = completedCount / totalCount;
  397. }
  398. else {
  399. self.progressNum = 1
  400. }
  401. },
  402. () => {
  403. self.loadComplete = true;
  404. console.log("---------->SceneLoaded:" + ((new Date()).getTime() - time1) * 0.001);
  405. })
  406. }
  407. /**
  408. * 提示语显示
  409. */
  410. PlayTip(content: string) {
  411. if (!this.tipNode) {
  412. return
  413. }
  414. this.tipNode.stopAllActions();
  415. this.tipNode.setPosition(cc.v2(0, 0));
  416. this.tipNode.active = true;
  417. this.tipNode.opacity = 255;
  418. this.tipTxt.string = content;
  419. let ac1 = cc.moveTo(0.35, cc.v2(0, this.node.height / 4)).easing(cc.easeSineOut());
  420. let wait = cc.delayTime(1.5);
  421. let fade = cc.fadeOut(1).easing(cc.easeQuinticActionIn());
  422. let backcall = cc.callFunc(() => this.tipNode.active = false);
  423. let total = cc.sequence(ac1, wait, fade, backcall);
  424. this.tipNode.runAction(total);
  425. }
  426. //CheckError() {
  427. //}
  428. }