Advertisement
Guest User

assetGenerator.py

a guest
Feb 5th, 2013
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.62 KB | None | 0 0
  1. ##################################################################
  2. # Asset Generator (assetGenerator.py)
  3. # Kyle Pulver
  4. # @kylepulver
  5. # http://kpulv.com
  6. # 2013 / 2 / 3
  7. # Version 1.0.0
  8. #
  9. # Put this file in your AS3 Project root.
  10. #
  11. # Modify the ASSET_FOLDER argument to be a path to your assets
  12. # directory from the script.  Usually it's just 'assets'
  13. #
  14. # Modify the OUTPUT_FILE to be the resulting .as file.
  15. #
  16. # Add names of folders to EXCLUDE to skip them.
  17. #
  18. # Dont use spaces in filenames or foldernames if you can help it
  19. #
  20. # The script assumes your dir structure looks something like this
  21. # --
  22. # assets/
  23. #   png/
  24. #     *.png
  25. #   mp3/
  26. #     *.mp3
  27. #   levels/
  28. #     *.oel
  29. # assetGenerator.py
  30. # --
  31. # So your base assets folder has at least one sub directory for
  32. # each major type of asset -- although this is not necessary.
  33. # However, each sub directory beyond the first for each major
  34. # asset type will be added to the asset name in the ouput file.
  35. #
  36. # If you change or improve this script, just let me know!
  37. ##################################################################
  38.  
  39. import os
  40.  
  41. ##################################################################
  42. # Stuff you can change is right below!
  43. ##################################################################
  44.  
  45. # Where to find the assets in relation to the script
  46. ASSET_FOLDER = 'assets'
  47. # The file to output.  Must use / and not \
  48. OUTPUT_FILE = 'src/game/Assets.as'
  49. # What extensions to look for
  50. EXTENSIONS = [
  51.     'png',
  52.     'mp3',
  53.     'oel',
  54.     'ttf'
  55. ]
  56. # What prefixes to give to the assets
  57. PREFIXES = [
  58.     'IMG',
  59.     'SOUND',
  60.     'LEVEL',
  61.     'FONT'
  62. ]
  63. # Which folders to exclude
  64. EXCLUDE = [
  65.     'psd',
  66.     'exclude'
  67. ]
  68.  
  69. ##################################################################
  70. # Magic happens below this line!               
  71. ##################################################################
  72.  
  73. path = os.getcwd() + '\\' + ASSET_FOLDER
  74. output = os.getcwd() + '\\' + OUTPUT_FILE
  75.  
  76. # Figure out the class name based on the file
  77. className = os.path.splitext(os.path.basename(OUTPUT_FILE))[0]
  78.  
  79. # Figure out the package name based on the folder
  80. packageName = OUTPUT_FILE[OUTPUT_FILE.find('/') + 1:]
  81. packageSplit = packageName.split('/')
  82. packageSplit.pop()
  83. if (len(packageSplit) > 0):
  84.     packageName = ".".join(packageSplit) + " "
  85. else:
  86.     packageName = ""
  87.  
  88. # Assemble some data structures!
  89. stuff = {}
  90. prefix = {}
  91. for i in range(len(EXTENSIONS)):
  92.     stuff[EXTENSIONS[i]] = [];
  93.     prefix[EXTENSIONS[i]] = PREFIXES[i]
  94.  
  95. # Walk through dem files
  96. for r,d,f in os.walk(path):
  97.     addToFile = True
  98.  
  99.     for excludes in EXCLUDE:
  100.         if (excludes == os.path.basename(r)):
  101.             addToFile = False
  102.  
  103.     if (addToFile):    
  104.         for files in f:
  105.             ext = os.path.splitext(files)[1][1:] #grab extension without the '.''
  106.             for key, value in stuff.items():
  107.                 if (ext == key):
  108.                     stuff[key].append(os.path.relpath(r, output)[3:] + "\\" + files)
  109.  
  110. # Start writing the file
  111. f = open(output, 'w+')
  112. f.write("""package """ + packageName + """{
  113.     /** Auto generated from assetGenerator.py! :) */
  114.     public class """ + className + """ {
  115.  
  116. """);
  117.  
  118. # Crazy writing all the embeds time
  119. for key, value in stuff.items():
  120.     if (len(stuff[key]) > 0):
  121.         f.write ("\t\t/** Generating " + key + " assets! */\n")
  122.  
  123.         for files in stuff[key]:
  124.             subfolder = files[:files.find(ASSET_FOLDER)]
  125.             subFolders = []
  126.             subFolderCount = len(subfolder.split('\\'))
  127.             folderCount = len(files.split('\\'))
  128.  
  129.             if (folderCount > subFolderCount + 2):
  130.                 start = files.find(ASSET_FOLDER) + len(ASSET_FOLDER) + 1
  131.                 poop = files[start:]
  132.                 start = poop.find('\\') + 1
  133.                 poop = poop[start:]
  134.                 poop = poop.split('\\')
  135.                 subFolders = poop[:-1]
  136.  
  137.             fname = os.path.splitext(os.path.basename(files))[0]
  138.             fname = fname.replace(" ", "_") #get rid of spaces, use underscores instead
  139.             fname = fname.upper().replace(prefix[key] + "_", "") #get rid of prefixes already applied in the file name
  140.             assetName = prefix[key] + "_"
  141.             for i in range(len(subFolders)):
  142.                 assetName += subFolders[i].upper().replace(" ", "_") + "_"
  143.             assetName += fname
  144.  
  145.             f.write("\t\t[Embed(source = \"" + files.replace("\\", "/") + "\"")
  146.            
  147.             # Fonts are special
  148.             if (key == 'ttf'):
  149.                 f.write(", embedAsCFF = \"false\", fontFamily = \"" + fname + "\")] private static const _" + assetName + ":Class\n")
  150.                 f.write("\t\tpublic static const " + assetName + ":String = \"" + fname + "\"\n\n")
  151.             else:
  152.                 if (key != 'png' and key != 'mp3'):
  153.                     f.write(", mimeType = \"application/octet-stream\"")
  154.                 f.write(")] public static const " + assetName + ":Class\n")
  155.  
  156.         if (key != 'ttf'):
  157.             f.write("\n")
  158.  
  159. # All done!
  160. f.write("\t}\n}")
  161. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement