ccb_redirect.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import json
  4. import os
  5. import libs.file_utils as file_utils
  6. import libs.biplist as biplist
  7. #修改ccb文件,重定位所有引用文件的路径
  8. #例如修改了ccbproject工程目录结构,ccb中的文件图片等重新指定位置
  9. #修改指定ccb节点下所有SpriteFrame(递归)
  10. def redirectAll(ccbNode, fileName2asset):
  11. properties = ccbNode['properties']
  12. #查找节点中的为SpriteFrame
  13. for curPorperty in properties:
  14. properType = curPorperty['type']
  15. if 'SpriteFrame' == properType:
  16. if 'value' in curPorperty:
  17. values = curPorperty['value']
  18. value0 = values[1]
  19. _, fileName = os.path.split(value0)
  20. if fileName in fileName2asset:
  21. values[0] = ''
  22. values[1] = fileName2asset[fileName]
  23. #print(fileName)
  24. if 'baseValue' in curPorperty:
  25. baseValues = curPorperty['baseValue']
  26. baseValue0 = baseValues[0]
  27. _, fileName = os.path.split(baseValue0)
  28. if fileName in fileName2asset:
  29. baseValues[0] = fileName2asset[fileName]
  30. #print(fileName)
  31. elif 'Texture' == properType:
  32. value = curPorperty['value']
  33. _, fileName = os.path.split(value)
  34. if fileName in fileName2asset:
  35. curPorperty['value'] = fileName2asset[fileName]
  36. #print(fileName)
  37. elif 'FntFile' == properType:
  38. value = curPorperty['value']
  39. _, fileName = os.path.split(value)
  40. if fileName in fileName2asset:
  41. curPorperty['value'] = fileName2asset[fileName]
  42. #print(fileName)
  43. elif 'CCBFile' == properType:
  44. value = curPorperty['value']
  45. _, fileName = os.path.split(value)
  46. if fileName in fileName2asset:
  47. curPorperty['value'] = fileName2asset[fileName]
  48. elif 'REDFile' == properType:
  49. value = curPorperty['value']
  50. _, fileName = os.path.split(value)
  51. if fileName in fileName2asset:
  52. curPorperty['value'] = fileName2asset[fileName]
  53. elif 'SpineSkel' == properType:
  54. value = curPorperty['value']
  55. _, fileName = os.path.split(value)
  56. if fileName in fileName2asset:
  57. curPorperty['value'] = fileName2asset[fileName]
  58. #print(fileName)
  59. #查找动时间线中使用SpriteFrame的
  60. if('animatedProperties' in ccbNode):
  61. animationProperties = ccbNode['animatedProperties']
  62. for nodeName in animationProperties:
  63. animProperty = animationProperties[nodeName]
  64. if('displayFrame' not in animProperty):
  65. continue
  66. displayFrame = animProperty['displayFrame']
  67. if('keyframes' not in displayFrame):
  68. continue
  69. keyframes = displayFrame['keyframes']
  70. for key in keyframes:
  71. values = key['value']
  72. val0 = values[0]
  73. val1 = values[1]
  74. _, fileName = os.path.split(val0)
  75. if fileName in fileName2asset:
  76. values[0] = fileName2asset[fileName]
  77. #print(val0)
  78. #递归子节点
  79. children = ccbNode['children']
  80. if(len(children) > 0):
  81. for child in children:
  82. redirectAll(child, fileName2asset)
  83. def excute(ccbProjFile):
  84. ccbProjFile = os.path.abspath(ccbProjFile)
  85. ccbProjRoot, _ = os.path.split(ccbProjFile)
  86. #读取ccb工程中设置的资源文件路径
  87. ccbProj = biplist.readPlist(ccbProjFile)
  88. resPaths = ccbProj['resourcePaths']
  89. ccbResDirs = []
  90. for tempItem in resPaths:
  91. curPath = tempItem['path']
  92. if '' != curPath:
  93. curPath = os.path.join(ccbProjRoot, curPath)
  94. curPath = os.path.abspath(curPath)
  95. ccbResDirs.append(curPath)
  96. #搜索所有ccb工程中的文件
  97. name2path = {}
  98. name2asset = {}
  99. for curDir in ccbResDirs:
  100. curDir = os.path.abspath(curDir)
  101. fileList = file_utils.findFiles(curDir, True, ['.png', '.jpg', '.fnt', '.red', '.skel', '.atlas'])
  102. for file in fileList:
  103. _, fileName = os.path.split(file)
  104. name2path[fileName] = file
  105. name2asset[fileName] = file[len(curDir) + 1:]
  106. for fileName in name2path:
  107. ccbFile = name2path[fileName]
  108. if(not ccbFile.endswith('.red')):
  109. continue
  110. print(ccbFile)
  111. plist = biplist.readPlist(ccbFile)
  112. nodeGraph = plist['nodeGraph']
  113. redirectAll(nodeGraph, name2asset)
  114. biplist.writePlist(plist, ccbFile, False)
  115. projFile = input("指定RED工程文件:").strip()
  116. projFile = os.path.abspath(projFile)
  117. excute(projFile)