createResList.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * 自动生成需要的资源列表
  3. * 修改 res_list_arr 生成需要前期加载的资源
  4. */
  5. const path = require('path')
  6. const fs = require('fs')
  7. var FileUtil = require('./FileUtil');
  8. /** 需要加载到资源文件夹 module内的为 */
  9. var res_list_arr = ['font','data', 'music', 'module/loading', 'module/main', 'module/fight'];
  10. //自动获取项目名称
  11. var programepath = process.cwd();
  12. var arr = programepath.split('\\');
  13. arr = arr.slice(0, arr.length - 1);
  14. var programename = arr[arr.length - 1];
  15. var dirpath = arr.join('\\') + '\\' + programename + '\\assets\\resources';
  16. /** 进入游戏需要加载的资源文件夹 */
  17. var resjsondata = {};
  18. var reslistpath = dirpath + "\\data\\resList.json";
  19. //遍历文件夹内所有文件
  20. var mapDir = (dir, callback, finish) => {
  21. fs.readdir(dir, function (err, files) {
  22. if (err) {
  23. console.error(err);
  24. return;
  25. }
  26. files.forEach((filename, index) => {
  27. let pathname = path.join(dir, filename)
  28. fs.stat(pathname, (err, stats) => { // 读取文件信息
  29. if (err) {
  30. console.log('获取文件stats失败');
  31. return;
  32. }
  33. if (stats.isDirectory()) {
  34. mapDir(pathname, callback, finish);
  35. } else if (stats.isFile()) {
  36. if (['.meta'].includes(path.extname(pathname))) { // 排除 目录下的 meta 文件
  37. return;
  38. }
  39. fs.readFile(pathname, (err, data) => {
  40. if (err) {
  41. console.error(err);
  42. return;
  43. }
  44. callback && callback(pathname);
  45. })
  46. }
  47. })
  48. })
  49. })
  50. }
  51. /**
  52. * 单个资源处理
  53. * @param {*} pathname
  54. * @returns
  55. */
  56. var addFileName = (pathname) => {
  57. let a = pathname.split("\\").slice(arr.length + 3);
  58. if (a.indexOf("prefab_texture") != -1) {
  59. return;
  60. }
  61. let vo = {};
  62. let url = a.join("/");
  63. let d = url.split(".");
  64. let type = d[1];
  65. vo.url = d[0];
  66. if(vo.url == 'module/fight/spine/attack_full/shifang2'){
  67. return;
  68. }
  69. if (a.indexOf("spine") != -1) {
  70. type = 'spine';
  71. }
  72. else if (a.indexOf("font") != -1) {
  73. type = 'font';
  74. }
  75. else if (a.indexOf("atlas") != -1) {
  76. type = 'atlas';
  77. }
  78. else if (type == 'png' || type == 'jpg') {
  79. type = 'texture';
  80. }
  81. vo.type = type;
  82. resjsondata[vo.url] = vo;
  83. };
  84. var finish = () => {
  85. };
  86. for (let i = 0; i < res_list_arr.length; i++) {
  87. let p = dirpath + "\\" + res_list_arr[i];
  88. mapDir(p, addFileName, finish);
  89. }
  90. /** 因为异步多文件夹处理,做个定时,等文件夹处理完后生成文件 */
  91. setTimeout(() => {
  92. let res_arr = [];
  93. for (let i in resjsondata) {
  94. res_arr.push(resjsondata[i]);
  95. }
  96. FileUtil.createJson(res_arr, reslistpath);
  97. }, 3000);