main.js 6.2 KB

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