file.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. var fs = require("fs");
  2. var path = require('path');
  3. var uuidUtils = require('./uuidUtils')
  4. var uuidlist = {};
  5. var Reg_Uuid = /^[0-9a-fA-F-]{36}$/;
  6. module.exports = {
  7. readFileSync: function (path) {
  8. return fs.readFileSync(path, 'utf-8');
  9. },
  10. writeFile: function (_path, _msg) {
  11. fs.writeFile(_path, _msg, function (err) {
  12. if (err) throw err;
  13. })
  14. },
  15. writeFileSync: function (_path, _msg) {
  16. fs.writeFileSync(_path, _msg)
  17. },
  18. cleanFile: function (_path) {
  19. fs.unlink(_path, function (err) {
  20. if (err) throw err;
  21. })
  22. },
  23. cleanFileSync: function (_path) {
  24. fs.unlinkSync(_path); // Sync 表示是同步方法
  25. },
  26. deleteall: function (path) {
  27. var files = [];
  28. if (fs.existsSync(path)) {
  29. files = fs.readdirSync(path);
  30. files.forEach(function (file, index) {
  31. var curPath = path + "/" + file;
  32. if (fs.statSync(curPath).isDirectory()) { // recurse
  33. deleteall(curPath);
  34. } else { // delete file
  35. fs.unlinkSync(curPath);
  36. }
  37. });
  38. fs.rmdirSync(path);
  39. }
  40. },
  41. //创建目录
  42. createMkdir: function (_path) {
  43. console.log('createMkdir:' + _path);
  44. if (!fs.existsSync(_path)) {
  45. try {
  46. fs.mkdirSync(_path);
  47. } catch (e) {
  48. if (e.code != 'EEXIST') throw e;
  49. }
  50. }
  51. },
  52. isPath: function (path) {
  53. return fs.existsSync(path);
  54. },
  55. //更新uuid
  56. createUUIDlist: function (dir) {
  57. var stat = fs.statSync(dir);
  58. if (!stat.isDirectory()) {
  59. return;
  60. }
  61. var subpaths = fs.readdirSync(dir), subpath;
  62. for (var i = 0; i < subpaths.length; ++i) {
  63. if (subpaths[i][0] === '.') {
  64. continue;
  65. }
  66. subpath = path.join(dir, subpaths[i]);
  67. stat = fs.statSync(subpath);
  68. if (stat.isDirectory()) {
  69. this.createUUIDlist(subpath);
  70. }
  71. else if (stat.isFile()) {
  72. // Size in Bytes
  73. var metastr = subpath.substr(subpath.length - 5, 5)
  74. if (metastr == '.meta') {
  75. var jstr = this.readFileSync(subpath);
  76. var json = JSON.parse(jstr);
  77. if (uuidUtils.isUuid(json['uuid'])) {
  78. this._upUUIDList(json);
  79. if (json['subMetas'] && typeof json['subMetas'] == 'object') {
  80. for (var bb in json['subMetas']) {
  81. this._upUUIDList(json['subMetas'][bb]);
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. },
  89. //更新uuid列表
  90. _upUUIDList: function (json) {
  91. if (uuidUtils.isUuid(json['uuid']) && !uuidlist[json['uuid']]) {
  92. uuidlist[json['uuid']] = {
  93. uuid: uuidUtils.uuidv4(),
  94. }
  95. if (uuidUtils.isUuid(json['rawTextureUuid'])) {
  96. uuidlist[json['rawTextureUuid']] = {
  97. uuid: uuidUtils.uuidv4(),
  98. }
  99. }
  100. }
  101. },
  102. //获取uuid列表
  103. getUUIDList: function () {
  104. return uuidlist;
  105. },
  106. //替换uuid
  107. replaceUUID: function (dir) {
  108. var stat = fs.statSync(dir);
  109. if (!stat.isDirectory()) {
  110. return;
  111. }
  112. var subpaths = fs.readdirSync(dir), subpath;
  113. for (var i = 0; i < subpaths.length; ++i) {
  114. if (subpaths[i][0] === '.') {
  115. continue;
  116. }
  117. subpath = path.join(dir, subpaths[i]);
  118. stat = fs.statSync(subpath);
  119. if (stat.isDirectory()) {
  120. this.replaceUUID(subpath);
  121. }
  122. else if (stat.isFile()) {
  123. if (this._isReplaceFile(subpath)) {
  124. var jstr = this.readFileSync(subpath);
  125. var json;
  126. try {
  127. json = JSON.parse(jstr);
  128. } catch (error) {
  129. console.log(subpath);
  130. }
  131. if (json) {
  132. this._replacePrefabFileUUID(json);
  133. this.writeFileSync(subpath, JSON.stringify(json, null, 2));
  134. }
  135. }
  136. }
  137. }
  138. },
  139. _isReplaceFile: function (subpath) {
  140. let conf = [
  141. '.anim',
  142. '.prefab',
  143. '.fire',
  144. '.meta',
  145. ]
  146. for (let i = 0; i < conf.length; i++) {
  147. let count = conf[i].length;
  148. if (subpath.substr(subpath.length - count, count) == conf[i]) {
  149. return true;
  150. }
  151. }
  152. return false;
  153. },
  154. //替换文件meta文件uuid
  155. _replaceMetaFileUUID: function (json) {
  156. if (json && typeof json == 'object') {
  157. if (Object.prototype.toString.call(json) === '[object Array]') {
  158. for (var prebidx = 0; prebidx < json.length; prebidx++) {
  159. if (json[prebidx] && typeof json[prebidx] == 'object') {
  160. this._replaceMetaFileUUID(json[prebidx]);
  161. }
  162. }
  163. } else if (Object.prototype.toString.call(json) === '[object Object]') {
  164. for (var prebidx in json) {
  165. if (json[prebidx] && typeof json[prebidx] == 'object') {
  166. this._replaceMetaFileUUID(json[prebidx]);
  167. }
  168. }
  169. }
  170. }
  171. },
  172. _replacePrefabFileUUID: function (json) {
  173. if (json && typeof json == 'object') {
  174. if (json['uuid'] && uuidUtils.isUuid(json['uuid'])) {
  175. json['uuid'] = uuidlist[json['uuid']].uuid;
  176. }
  177. if (json['rawTextureUuid'] && uuidUtils.isUuid(json['rawTextureUuid'])) {
  178. json['rawTextureUuid'] = uuidlist[json['rawTextureUuid']].uuid;
  179. }
  180. if (json['textureUuid'] && uuidUtils.isUuid(json['textureUuid'])) {
  181. json['textureUuid'] = uuidlist[json['textureUuid']].uuid;
  182. }
  183. var __type__ = json['__type__'];
  184. if (__type__ && uuidUtils.isUuid(__type__)) {
  185. if (Reg_Uuid.test(__type__)) {
  186. if (uuidlist[__type__]) {
  187. json['__type__'] = uuidlist[__type__].uuid;
  188. }
  189. } else {
  190. var de__type__ = uuidUtils.decompressUuid(__type__);
  191. if (uuidlist[de__type__]) {
  192. json['__type__'] = uuidUtils.compressUuid(uuidlist[de__type__].uuid, false);
  193. }
  194. }
  195. }
  196. var __uuid__ = json['__uuid__'];
  197. if (__uuid__ && uuidUtils.isUuid(__uuid__)) {
  198. if (Reg_Uuid.test(__uuid__)) {
  199. if (uuidlist[__uuid__]) {
  200. json['__uuid__'] = uuidlist[__uuid__].uuid;
  201. }
  202. } else {
  203. var __uuid__ = uuidUtils.decompressUuid(__uuid__);
  204. if (uuidlist[__uuid__]) {
  205. json['__uuid__'] = UuidUtils.compressUuid(uuidlist[__uuid__], false);
  206. }
  207. }
  208. }
  209. if (Object.prototype.toString.call(json) === '[object Array]') {
  210. for (var prebidx = 0; prebidx < json.length; prebidx++) {
  211. if (json[prebidx] && typeof json[prebidx] == 'object') {
  212. this._replacePrefabFileUUID(json[prebidx]);
  213. }
  214. }
  215. } else if (Object.prototype.toString.call(json) === '[object Object]') {
  216. for (var prebidx in json) {
  217. if (json[prebidx] && typeof json[prebidx] == 'object') {
  218. this._replacePrefabFileUUID(json[prebidx]);
  219. }
  220. }
  221. }
  222. }
  223. }
  224. }