supertux-level 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env python3
  2. # Flexlay - A Generic 2D Game Editor
  3. # Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. import argparse
  18. import logging
  19. import sys
  20. from flexlay.util.config import Config
  21. from supertux.level import Level
  22. from supertux.config import make_config
  23. def main():
  24. has_error = False
  25. parser = argparse.ArgumentParser(description="SuperTux Level Tool")
  26. parser.add_argument("LEVELFILE", action="store", type=str, nargs="+",
  27. help=".stl file to load")
  28. parser.add_argument("-d", "--datadir", metavar="DIR", action="store", type=str,
  29. help="SuperTux data directory directory")
  30. parser.add_argument("-o", "--output", metavar="FILE", action="store", type=str,
  31. help="Load and resave the level to FILE")
  32. parser.add_argument("--resave", action="store_true", default=False,
  33. help="Load and resave level in place")
  34. args = parser.parse_args()
  35. config = make_config()
  36. if args.datadir is not None:
  37. config.datadir = args.datadir
  38. elif not config.datadir:
  39. raise RuntimeError("datadir missing, use --datadir DIR")
  40. for levelfile in args.LEVELFILE:
  41. logging.info("Loading {}".format(levelfile))
  42. try:
  43. level = Level.from_file(levelfile)
  44. if args.output:
  45. level.save(args.output)
  46. elif args.resave:
  47. print("resaving {}".format(levelfile))
  48. level.save(levelfile)
  49. except Exception as err:
  50. logging.exception("ERROR: {}", err)
  51. has_error = True
  52. return 1 if has_error else 0
  53. if __name__ == "__main__":
  54. sys.exit(main())
  55. # EOF #