Advertisement
historic_bruno

Port map converter script to python 2.x

May 24th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.57 KB | None | 0 0
  1. #! /usr/bin/env python3
  2.  
  3. # Copyright (c) 2015 Sanderd17
  4. #
  5. # Licensed under the MIT License:
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in
  15. # all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. # THE SOFTWARE.
  24.  
  25. import argparse
  26. import io
  27. import os
  28. import struct
  29. import sys
  30.  
  31. parser = argparse.ArgumentParser(description="Convert maps compatible with 0 A.D. version Alpha XVIII (A18) to maps compatible with version Alpha XIX (A19), or the other way around.")
  32.  
  33. parser.add_argument("--reverse", action="store_true", help="Make an A19 map compatible with A18 (note that conversion will fail if mountains are too high)")
  34. parser.add_argument("--no-version-bump", action="store_true", help="Don't change the version number of the map")
  35. parser.add_argument("--no-color-spelling", action="store_true", help="Don't change the spelling of color and colour")
  36. parser.add_argument("--no-height-change", action="store_true", help="Don't change the heightmap")
  37.  
  38. parser.add_argument("files", nargs="+", help="XML file to process (use wildcards '*' to select multiple files)")
  39. args = parser.parse_args()
  40.  
  41.  
  42. HEIGHTMAP_BIT_SHIFT = 3
  43.  
  44. for xmlFile in args.files:
  45.     pmpFile = xmlFile[:-3] + "pmp"
  46.  
  47.     print("Processing " + xmlFile + " ...")
  48.  
  49.     if os.path.isfile(pmpFile):
  50.         with open(pmpFile, "rb") as f1, open(pmpFile + "~", "wb") as f2:
  51.             # 4 bytes PSMP to start the file
  52.             f2.write(f1.read(4))
  53.  
  54.             # 4 bytes to encode the version of the file format
  55.             version = struct.unpack("<I", f1.read(4))[0]
  56.             #version = int.from_bytes(f1.read(4), byteorder='little')
  57.             if args.no_version_bump:
  58.                 f2.write(struct.pack("<I", version))
  59.                 #f2.write((version).to_bytes(4, byteorder='little'))
  60.             else:
  61.                 if args.reverse:
  62.                     if version != 6:
  63.                         print("Warning: File " + pmpFile + " was not at version 6, while a negative version bump was requested.\nABORTING ...")
  64.                         continue
  65.                     f2.write(struct.pack("<I", version-1))
  66.                     #f2.write((version-1).to_bytes(4, byteorder='little'))
  67.                 else:
  68.                     if version != 5:
  69.                         print("Warning: File " + pmpFile + " was not at version 5, while a version bump was requested.\nABORTING ...")
  70.                         continue
  71.                     f2.write(struct.pack("<I", version+1))
  72.                     #f2.write((version+1).to_bytes(4, byteorder='little'))
  73.  
  74.             # 4 bytes a for file size (which shouldn't change)
  75.             f2.write(f1.read(4))
  76.  
  77.             # 4 bytes to encode the map size
  78.             map_size = struct.unpack("<I", f1.read(4))[0]
  79.             #map_size = int.from_bytes(f1.read(4), byteorder='little')
  80.             f2.write(struct.pack("<I", map_size))
  81.             #f2.write(map_size.to_bytes(4, byteorder='little'))
  82.  
  83.             # half all heights using the shift '>>' operator
  84.             if args.no_height_change:
  85.                 def height_transform(h):
  86.                     return h
  87.             else:
  88.                 if args.reverse:
  89.                     def height_transform(h):
  90.                         return h << HEIGHTMAP_BIT_SHIFT
  91.                 else:
  92.                     def height_transform(h):
  93.                         return h >> HEIGHTMAP_BIT_SHIFT
  94.                    
  95.             for i in range(0, (map_size*16+1)*(map_size*16+1)):
  96.                 height = struct.unpack("<H", f1.read(2))[0]
  97.                 #height = int.from_bytes(f1.read(2), byteorder='little')
  98.                 f2.write(struct.pack("<H", height_transform(height)))
  99.                 #f2.write(height_transform(height).to_bytes(2, byteorder='little'))
  100.            
  101.             # copy the rest of the file
  102.             byte = f1.read(1)
  103.             while byte != b"":
  104.                 f2.write(byte)
  105.                 byte = f1.read(1)
  106.  
  107.             f2.close()
  108.             f1.close()
  109.  
  110.         # replace the old file, comment to see both files
  111.         os.remove(pmpFile)
  112.         os.rename(pmpFile + "~", pmpFile)
  113.  
  114.  
  115.     if os.path.isfile(xmlFile):
  116.         with open(xmlFile, "r") as f1, open(xmlFile + "~", "w") as f2:
  117.             data = f1.read()
  118.  
  119.             # bump version number (rely on how Atlas formats the XML)
  120.             if not args.no_version_bump:
  121.                 if args.reverse:
  122.                     if data.find('<Scenario version="6">') == -1:
  123.                         print("Warning: File " + xmlFile + " was not at version 6, while a negative version bump was requested.\nABORTING ...")
  124.                         sys.exit()
  125.                     else:
  126.                         data = data.replace('<Scenario version="6">', '<Scenario version="5">')
  127.                 else:
  128.                     if data.find('<Scenario version="5">') == -1:
  129.                         print("Warning: File " + xmlFile + " was not at version 5, while a version bump was requested.\nABORTING ...")
  130.                         sys.exit()
  131.                     else:
  132.                         data = data.replace('<Scenario version="5">', '<Scenario version="6">')
  133.                        
  134.  
  135.             # transform the color keys
  136.             if not args.no_color_spelling:
  137.                 if args.reverse:
  138.                     data = data.replace("color", "colour").replace("Color", "Colour")
  139.                 else:
  140.                     data = data.replace("colour", "color").replace("Colour", "Color")
  141.            
  142.             f2.write(data)
  143.             f1.close()
  144.             f2.close()
  145.  
  146.         # replace the old file, comment to see both files
  147.         os.remove(xmlFile)
  148.         os.rename(xmlFile + "~", xmlFile)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement