| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- /**
- * 自动生成需要的资源列表
- * 修改 res_list_arr 生成需要前期加载的资源
- */
- const path = require('path')
- const fs = require('fs')
- var FileUtil = require('./FileUtil');
- /** 需要加载到资源文件夹 module内的为 */
- var res_list_arr = ['font','data', 'music', 'module/loading', 'module/main', 'module/fight'];
- //自动获取项目名称
- var programepath = process.cwd();
- var arr = programepath.split('\\');
- arr = arr.slice(0, arr.length - 1);
- var programename = arr[arr.length - 1];
- var dirpath = arr.join('\\') + '\\' + programename + '\\assets\\resources';
- /** 进入游戏需要加载的资源文件夹 */
- var resjsondata = {};
- var reslistpath = dirpath + "\\data\\resList.json";
- //遍历文件夹内所有文件
- var mapDir = (dir, callback, finish) => {
- fs.readdir(dir, function (err, files) {
- if (err) {
- console.error(err);
- return;
- }
- files.forEach((filename, index) => {
- let pathname = path.join(dir, filename)
- fs.stat(pathname, (err, stats) => { // 读取文件信息
- if (err) {
- console.log('获取文件stats失败');
- return;
- }
- if (stats.isDirectory()) {
- mapDir(pathname, callback, finish);
- } else if (stats.isFile()) {
- if (['.meta'].includes(path.extname(pathname))) { // 排除 目录下的 meta 文件
- return;
- }
- fs.readFile(pathname, (err, data) => {
- if (err) {
- console.error(err);
- return;
- }
- callback && callback(pathname);
- })
- }
- })
- })
- })
- }
- /**
- * 单个资源处理
- * @param {*} pathname
- * @returns
- */
- var addFileName = (pathname) => {
- let a = pathname.split("\\").slice(arr.length + 3);
- if (a.indexOf("prefab_texture") != -1) {
- return;
- }
- let vo = {};
- let url = a.join("/");
- let d = url.split(".");
- let type = d[1];
- vo.url = d[0];
- if(vo.url == 'module/fight/spine/attack_full/shifang2'){
- return;
- }
- if (a.indexOf("spine") != -1) {
- type = 'spine';
- }
- else if (a.indexOf("font") != -1) {
- type = 'font';
- }
- else if (a.indexOf("atlas") != -1) {
- type = 'atlas';
- }
- else if (type == 'png' || type == 'jpg') {
- type = 'texture';
- }
-
- vo.type = type;
- resjsondata[vo.url] = vo;
- };
- var finish = () => {
- };
- for (let i = 0; i < res_list_arr.length; i++) {
- let p = dirpath + "\\" + res_list_arr[i];
- mapDir(p, addFileName, finish);
- }
- /** 因为异步多文件夹处理,做个定时,等文件夹处理完后生成文件 */
- setTimeout(() => {
- let res_arr = [];
- for (let i in resjsondata) {
- res_arr.push(resjsondata[i]);
- }
- FileUtil.createJson(res_arr, reslistpath);
- }, 3000);
|