gen_xml_resources.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. typical usage example
  5. your project structure
  6. game\
  7. data\
  8. images\*.png
  9. xmls
  10. gen_xml_resources.py
  11. go to game folder and run:
  12. gen_xml_resources.py -d data -i images -o xmls/out.xml
  13. """
  14. from __future__ import print_function, unicode_literals
  15. import os
  16. import glob
  17. def gen_xml(args):
  18. wildcard = "*.*"
  19. path = args.data + "/" + args.images
  20. filelist = glob.glob(path + "/" + args.wildcard)
  21. filelist = glob.glob(path + "/*.jpeg")
  22. filelist.extend(glob.glob(path + "/*.jpg"))
  23. filelist.extend(glob.glob(path + "/*.tga"))
  24. filelist.extend(glob.glob(path + "/*.png"))
  25. print(filelist)
  26. print(path)
  27. dest = open(args.data + "/" + args.out, "w")
  28. write = dest.write
  29. write("<resources>\n")
  30. write("\t<set path=\"%s\"/>\n" % (args.images, ))
  31. if args.sfactor != 1:
  32. write("\t<set scale_factor=\"%s\"/>\n" % (args.sfactor, ))
  33. if not args.load:
  34. write("\t<set load=\"false\"/>\n")
  35. if not args.atlasses:
  36. write("\t<atlas>\n")
  37. for file in filelist:
  38. name = os.path.split(file)[1]
  39. if args.atlasses:
  40. write("\t<atlas>\n\t")
  41. write("\t\t<image file='%s'/>\n" % (name))
  42. if args.atlasses:
  43. write("\t</atlas>\n")
  44. if not args.atlasses:
  45. write("\t</atlas>\n")
  46. filelist = glob.glob(path + "/*.ogg")
  47. for file in filelist:
  48. name = os.path.split(file)[1]
  49. write("\t\t<sound file='%s'/>\n" % (name))
  50. write("</resources>\n")
  51. dest.close()
  52. if __name__ == "__main__":
  53. import argparse
  54. parser = argparse.ArgumentParser(
  55. description="generates xml file with image resources")
  56. parser.add_argument(
  57. "-d", "--data", help="root data folder", default=".")
  58. parser.add_argument("-s", "--sfactor", help="scale factor", default=1)
  59. parser.add_argument(
  60. "-i", "--images", help="folder with images. path relative to --data", default=".")
  61. parser.add_argument(
  62. "-o", "--out", help="output xml file name", default="out.xml")
  63. parser.add_argument("-w", "--wildcard",
  64. help="default is '*.png'", default="(*.png,*.jpg)")
  65. parser.add_argument("-l", "--load", help="preload files?",
  66. action="store_true", default=True)
  67. parser.add_argument("-a", "--atlasses", help="separate atlasses for each file?",
  68. action="store_true", default=False)
  69. args = parser.parse_args()
  70. gen_xml(args)