main.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. window._game_res_version = '220122134660'
  2. let version = localStorage.getItem('game_res_version')
  3. console.log('check old version:',version != window._game_res_version)
  4. if(version != window._game_res_version){
  5. let storagePath = (jsb.fileUtils ? jsb.fileUtils.getWritablePath() : '/') + 'remote-asset'
  6. console.log('has old version file:'+jsb.fileUtils.isDirectoryExist(storagePath))
  7. if(jsb.fileUtils.isDirectoryExist(storagePath)){
  8. let hasRemove = jsb.fileUtils.removeDirectory(storagePath);
  9. console.log('remove old version file:'+hasRemove)
  10. }
  11. }
  12. localStorage.setItem('game_res_version',window._game_res_version)
  13. !function(){if("object"==typeof window.jsb){var e=localStorage.getItem("HotUpdateSearchPaths");if(e){var i=JSON.parse(e);jsb.fileUtils.setSearchPaths(i);var s=[],t=i[0]||"",l=t+"_temp/",r=l.length;jsb.fileUtils.isDirectoryExist(l)&&!jsb.fileUtils.isFileExist(l+"project.manifest.temp")&&(jsb.fileUtils.listFilesRecursively(l,s),s.forEach(e=>{var i=e.substr(r),s=t+i;"/"===e[e.length]?jsb.fileUtils.createDirectory(s):(jsb.fileUtils.isFileExist(s)&&jsb.fileUtils.removeFile(s),jsb.fileUtils.renameFile(e,s))}),jsb.fileUtils.removeDirectory(l))}}}();
  14. window.boot = function () {
  15. var settings = window._CCSettings;
  16. window._CCSettings = undefined;
  17. var onProgress = null;
  18. var RESOURCES = cc.AssetManager.BuiltinBundleName.RESOURCES;
  19. var INTERNAL = cc.AssetManager.BuiltinBundleName.INTERNAL;
  20. var MAIN = cc.AssetManager.BuiltinBundleName.MAIN;
  21. function setLoadingDisplay () {
  22. // Loading splash scene
  23. var splash = document.getElementById('splash');
  24. var progressBar = splash.querySelector('.progress-bar span');
  25. onProgress = function (finish, total) {
  26. var percent = 100 * finish / total;
  27. if (progressBar) {
  28. progressBar.style.width = percent.toFixed(2) + '%';
  29. }
  30. };
  31. splash.style.display = 'block';
  32. progressBar.style.width = '0%';
  33. cc.director.once(cc.Director.EVENT_AFTER_SCENE_LAUNCH, function () {
  34. splash.style.display = 'none';
  35. });
  36. }
  37. var onStart = function () {
  38. cc.view.enableRetina(true);
  39. cc.view.resizeWithBrowserSize(true);
  40. if (cc.sys.isBrowser) {
  41. setLoadingDisplay();
  42. }
  43. if (cc.sys.isMobile) {
  44. if (settings.orientation === 'landscape') {
  45. cc.view.setOrientation(cc.macro.ORIENTATION_LANDSCAPE);
  46. }
  47. else if (settings.orientation === 'portrait') {
  48. cc.view.setOrientation(cc.macro.ORIENTATION_PORTRAIT);
  49. }
  50. cc.view.enableAutoFullScreen([
  51. cc.sys.BROWSER_TYPE_BAIDU,
  52. cc.sys.BROWSER_TYPE_BAIDU_APP,
  53. cc.sys.BROWSER_TYPE_WECHAT,
  54. cc.sys.BROWSER_TYPE_MOBILE_QQ,
  55. cc.sys.BROWSER_TYPE_MIUI,
  56. cc.sys.BROWSER_TYPE_HUAWEI,
  57. cc.sys.BROWSER_TYPE_UC,
  58. ].indexOf(cc.sys.browserType) < 0);
  59. }
  60. // Limit downloading max concurrent task to 2,
  61. // more tasks simultaneously may cause performance draw back on some android system / browsers.
  62. // You can adjust the number based on your own test result, you have to set it before any loading process to take effect.
  63. if (cc.sys.isBrowser && cc.sys.os === cc.sys.OS_ANDROID) {
  64. cc.assetManager.downloader.maxConcurrency = 2;
  65. cc.assetManager.downloader.maxRequestsPerFrame = 2;
  66. }
  67. var launchScene = settings.launchScene;
  68. var bundle = cc.assetManager.bundles.find(function (b) {
  69. return b.getSceneInfo(launchScene);
  70. });
  71. bundle.loadScene(launchScene, null, onProgress,
  72. function (err, scene) {
  73. if (!err) {
  74. cc.director.runSceneImmediate(scene);
  75. if (cc.sys.isBrowser) {
  76. // show canvas
  77. var canvas = document.getElementById('GameCanvas');
  78. canvas.style.visibility = '';
  79. var div = document.getElementById('GameDiv');
  80. if (div) {
  81. div.style.backgroundImage = '';
  82. }
  83. console.log('Success to load scene: ' + launchScene);
  84. }
  85. }
  86. }
  87. );
  88. };
  89. var option = {
  90. id: 'GameCanvas',
  91. debugMode: settings.debug ? cc.debug.DebugMode.INFO : cc.debug.DebugMode.ERROR,
  92. showFPS: settings.debug,
  93. frameRate: 60,
  94. groupList: settings.groupList,
  95. collisionMatrix: settings.collisionMatrix,
  96. };
  97. cc.assetManager.init({
  98. bundleVers: settings.bundleVers,
  99. remoteBundles: settings.remoteBundles,
  100. server: settings.server
  101. });
  102. var bundleRoot = [INTERNAL];
  103. settings.hasResourcesBundle && bundleRoot.push(RESOURCES);
  104. var count = 0;
  105. function cb (err) {
  106. if (err) return console.error(err.message, err.stack);
  107. count++;
  108. if (count === bundleRoot.length + 1) {
  109. cc.assetManager.loadBundle(MAIN, function (err) {
  110. if (!err) cc.game.run(option, onStart);
  111. });
  112. }
  113. }
  114. cc.assetManager.loadScript(settings.jsList.map(function (x) { return 'src/' + x;}), cb);
  115. for (var i = 0; i < bundleRoot.length; i++) {
  116. cc.assetManager.loadBundle(bundleRoot[i], cb);
  117. }
  118. };
  119. if (window.jsb) {
  120. var isRuntime = (typeof loadRuntime === 'function');
  121. if (isRuntime) {
  122. require('src/settings.js');
  123. require('src/cocos2d-runtime.js');
  124. if (CC_PHYSICS_BUILTIN || CC_PHYSICS_CANNON) {
  125. require('src/physics.js');
  126. }
  127. require('jsb-adapter/engine/index.js');
  128. }
  129. else {
  130. require('src/settings.js');
  131. require('src/cocos2d-jsb.js');
  132. if (CC_PHYSICS_BUILTIN || CC_PHYSICS_CANNON) {
  133. require('src/physics.js');
  134. }
  135. require('jsb-adapter/jsb-engine.js');
  136. }
  137. cc.macro.CLEANUP_IMAGE_CACHE = true;
  138. window.boot();
  139. }