Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.73 KB | None | 0 0
  1. # mapjson.py
  2.  
  3. import os
  4. import re
  5. import sys
  6. import json
  7.  
  8.  
  9. def abort(message):
  10.     print message
  11.     sys.exit()
  12.  
  13.  
  14. def write_text_file(filepath, text):
  15.     with open(filepath, 'w') as f:
  16.         f.write(text)
  17.  
  18.  
  19. def generate_map_header_text(map_data, layouts_data):
  20.     matched_layouts = filter(lambda layout: layout['id'] == map_data['layout'], layouts_data['layouts'])
  21.     if len(matched_layouts) != 1:
  22.         abort('Failed to find matching layout for "%s"' % (map_data['layout']))
  23.     layout = matched_layouts[0]
  24.     text = '%s:\n' % (map_data['name'])
  25.     text += '\t.4byte %s\n' % (layout['name'])
  26.  
  27.     if 'shared_events_map' in map_data:
  28.         text += '\t.4byte %s\n' % (map_data['shared_events_map'] + '_MapEvents')
  29.     else:
  30.         text += '\t.4byte %s\n' % (map_data['name'] + '_MapEvents')
  31.  
  32.     if 'shared_scripts_map' in map_data:
  33.         text += '\t.4byte %s\n' % (map_data['shared_scripts_map'] + '_MapScripts')
  34.     else:
  35.         text += '\t.4byte %s\n' % (map_data['name'] + '_MapScripts')
  36.  
  37.     if map_data['connections'] is not None and len(map_data['connections']) > 0:
  38.         text += '\t.4byte %s\n' % (map_data['name'] + '_MapConnections')
  39.     else:
  40.         text += '\t.4byte 0x0\n'
  41.  
  42.     text += '\t.2byte %s\n' % (map_data['music'])
  43.     text += '\t.2byte %s\n' % (layout['id'])
  44.     text += '\t.byte %s\n' % (map_data['region_map_section'])
  45.     text += '\t.byte %s\n' % (int(map_data['requires_flash']))
  46.     text += '\t.byte %s\n' % (map_data['weather'])
  47.     text += '\t.byte %s\n' % (map_data['map_type'])
  48.     text += '\t.2byte 0\n'
  49.     text += '\tmap_header_flags allow_bike=%s, allow_escape_rope=%s, allow_run=%s, show_map_name=%s\n' % (int(map_data['allow_bike']), int(map_data['allow_escape_rope']), int(map_data['allow_running']), int(map_data['show_map_name']))
  50.     text += '\t.byte %s\n\n' % (map_data['battle_scene'])
  51.     return text
  52.  
  53.  
  54. def generate_map_connections_text(map_data):
  55.     if map_data['connections'] is None:
  56.         return '\n'
  57.  
  58.     label = '%s_MapConnectionsList' % (map_data['name'])
  59.     text = '%s:\n' % (label)
  60.     for connection in map_data['connections']:
  61.         text += '\tconnection %s, %s, %s\n' % (connection['direction'], connection['offset'], connection['map'])
  62.  
  63.     text += '\n%s_MapConnections:\n' % (map_data['name'])
  64.     text += '\t.4byte %s\n' % (len(map_data['connections']))
  65.     text += '\t.4byte %s\n\n' % (label)
  66.     return text
  67.  
  68.  
  69. def generate_map_events_text(map_data):
  70.     if 'shared_events_map' in map_data:
  71.         return '\n'
  72.  
  73.     text = ''
  74.     if len(map_data['object_events']) > 0:
  75.         objects_label = '%s_EventObjects' % (map_data['name'])
  76.         text += '%s:\n' % (objects_label)
  77.         for i, obj in enumerate(map_data['object_events']):
  78.             text += '\tobject_event %s, %s, 0, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s\n' % (
  79.                 i + 1,
  80.                 obj['graphics_id'],
  81.                 obj['x'],
  82.                 obj['y'],
  83.                 obj['elevation'],
  84.                 obj['movement_type'],
  85.                 obj['movement_range_x'],
  86.                 obj['movement_range_y'],
  87.                 obj['trainer_type'],
  88.                 obj['trainer_sight_or_berry_tree_id'],
  89.                 obj['script'],
  90.                 obj['flag'])
  91.         text += '\n'
  92.     else:
  93.         objects_label = '0x0'
  94.  
  95.     if len(map_data['warp_events']) > 0:
  96.         warps_label = '%s_MapWarps' % (map_data['name'])
  97.         text += '%s:\n' % (warps_label)
  98.         for i, warp in enumerate(map_data['warp_events']):
  99.             text += '\twarp_def %s, %s, %s, %s, %s\n' % (
  100.                 warp['x'],
  101.                 warp['y'],
  102.                 warp['elevation'],
  103.                 warp['dest_warp_id'],
  104.                 warp['dest_map'])
  105.         text += '\n'
  106.     else:
  107.         warps_label = '0x0'
  108.  
  109.     if len(map_data['coord_events']) > 0:
  110.         coords_label = '%s_MapCoordEvents' % (map_data['name'])
  111.         text += '%s:\n' % (coords_label)
  112.         for i, event in enumerate(map_data['coord_events']):
  113.             if event['type'] == 'trigger':
  114.                 text += '\tcoord_event %s, %s, %s, %s, %s, %s\n' % (
  115.                     event['x'],
  116.                     event['y'],
  117.                     event['elevation'],
  118.                     event['var'],
  119.                     event['var_value'],
  120.                     event['script'])
  121.             elif event['type'] == 'weather':
  122.                 text += '\tcoord_weather_event %s, %s, %s, %s\n' % (
  123.                     event['x'],
  124.                     event['y'],
  125.                     event['elevation'],
  126.                     event['weather'])
  127.         text += '\n'
  128.     else:
  129.         coords_label = '0x0'
  130.  
  131.     if len(map_data['bg_events']) > 0:
  132.         bgs_label = '%s_MapBGEvents' % (map_data['name'])
  133.         text += '%s:\n' % (bgs_label)
  134.         for i, event in enumerate(map_data['bg_events']):
  135.             if event['type'] == 'sign':
  136.                 text += '\tbg_event %s, %s, %s, %s, %s\n' % (
  137.                     event['x'],
  138.                     event['y'],
  139.                     event['elevation'],
  140.                     event['player_facing_dir'],
  141.                     event['script'])
  142.             elif event['type'] == 'hidden_item':
  143.                 text += '\tbg_hidden_item_event %s, %s, %s, %s, %s\n' % (
  144.                     event['x'],
  145.                     event['y'],
  146.                     event['elevation'],
  147.                     event['item'],
  148.                     event['flag'])
  149.             elif event['type'] == 'secret_base':
  150.                 text += '\tbg_secret_base_event %s, %s, %s, %s\n' % (
  151.                     event['x'],
  152.                     event['y'],
  153.                     event['elevation'],
  154.                     event['secret_base_id'])
  155.         text += '\n'
  156.     else:
  157.         bgs_label = '0x0'
  158.  
  159.     text += '%s_MapEvents::\n' % (map_data['name'])
  160.     text += '\tmap_events %s, %s, %s, %s\n\n' % (objects_label, warps_label, coords_label, bgs_label)
  161.     return text
  162.  
  163.  
  164. def process_map(map_filepath, layouts_filepath):
  165.     with open(map_filepath) as f:
  166.         map_data = json.load(f)
  167.     with open(layouts_filepath) as f:
  168.         layouts_data = json.load(f)
  169.  
  170.     header_text = generate_map_header_text(map_data, layouts_data)
  171.     events_text = generate_map_events_text(map_data)
  172.     connections_text = generate_map_connections_text(map_data)
  173.  
  174.     files_dir = os.path.dirname(map_filepath)
  175.     write_text_file(os.path.join(files_dir, 'header.inc'), header_text)
  176.     write_text_file(os.path.join(files_dir, 'events.inc'), events_text)
  177.     write_text_file(os.path.join(files_dir, 'connections.inc'), connections_text)
  178.  
  179.  
  180. def generate_groups_text(groups_data):
  181.     text = ''
  182.     for group in groups_data['group_order']:
  183.         text += '%s::\n' % (group)
  184.         maps = groups_data[group]
  185.         for map_name in maps:
  186.             text += '\t.4byte %s\n' % (map_name)
  187.         text += '\n'
  188.  
  189.     text += '\t.align 2\n'
  190.     text += 'gMapGroups::\n'
  191.     for group in groups_data['group_order']:
  192.         text += '\t.4byte %s\n' % (group)
  193.     text += '\n'
  194.     return text
  195.  
  196.  
  197. def index_of(in_list, value):
  198.     try:
  199.         index_value = in_list.index(value)
  200.     except ValueError:
  201.         index_value = sys.maxint
  202.     return index_value
  203.  
  204.  
  205. def generate_connections_text(groups_data):
  206.     map_names = []
  207.     for group in groups_data['group_order']:
  208.         for map_name in groups_data[group]:
  209.             map_names.append(map_name)
  210.  
  211.     if 'connections_include_order' in groups_data:
  212.         map_names.sort(key=lambda x: index_of(groups_data['connections_include_order'], x))
  213.  
  214.     text = ''
  215.     for map_name in map_names:
  216.         text += '\t.include "data/maps/%s/connections.inc"\n' % (map_name)
  217.  
  218.     return text
  219.  
  220.  
  221. def generate_headers_text(groups_data):
  222.     map_names = []
  223.     for group in groups_data['group_order']:
  224.         for map_name in groups_data[group]:
  225.             map_names.append(map_name)
  226.  
  227.     text = ''
  228.     for map_name in map_names:
  229.         text += '\t.include "data/maps/%s/header.inc"\n' % (map_name)
  230.  
  231.     return text
  232.  
  233.  
  234. def generate_events_text(groups_data):
  235.     map_names = []
  236.     for group in groups_data['group_order']:
  237.         for map_name in groups_data[group]:
  238.             map_names.append(map_name)
  239.  
  240.     text = ''
  241.     for map_name in map_names:
  242.         text += '\t.include "data/maps/%s/events.inc"\n' % (map_name)
  243.  
  244.     return text
  245.  
  246.  
  247. def generate_map_constants_text(groups_filepath, groups_data):
  248.     file_dir = os.path.dirname(groups_filepath)
  249.     text = '#ifndef GUARD_CONSTANTS_MAP_GROUPS_H\n'
  250.     text += '#define GUARD_CONSTANTS_MAP_GROUPS_H\n\n'
  251.  
  252.     for i, group in enumerate(groups_data['group_order']):
  253.         text += '// Map Group %s\n' % (i)
  254.         map_ids = []
  255.         max_length = 0
  256.         for map_name in groups_data[group]:
  257.             header_filepath = os.path.join(file_dir, map_name, 'map.json')
  258.             with open(header_filepath) as f:
  259.                 map_data = json.load(f)
  260.             map_ids.append(map_data['id'])
  261.             if len(map_data['id']) > max_length:
  262.                 max_length = len(map_data['id'])
  263.  
  264.         for j, map_id in enumerate(map_ids):
  265.             text += '#define %s%s(%s | (%s << 8))\n' % (map_id, " " * (max_length - len(map_id) + 1), j, i)
  266.  
  267.         text += '\n'
  268.  
  269.     text += '#define MAP_GROUPS_COUNT %s\n\n' % (len(groups_data['group_order']))
  270.     text += '#endif  // GUARD_CONSTANTS_MAP_GROUPS_H\n'
  271.  
  272.     return text
  273.  
  274.  
  275. def process_groups(groups_filepath):
  276.     with open(groups_filepath) as f:
  277.         groups_data = json.load(f)
  278.  
  279.     groups_text = generate_groups_text(groups_data)
  280.     connections_text = generate_connections_text(groups_data)
  281.     headers_text = generate_headers_text(groups_data)
  282.     events_text = generate_events_text(groups_data)
  283.     map_header_text = generate_map_constants_text(groups_filepath, groups_data)
  284.  
  285.     file_dir = os.path.dirname(groups_filepath)
  286.     write_text_file(os.path.join(file_dir, 'groups.inc'), groups_text)
  287.     write_text_file(os.path.join(file_dir, 'connections.inc'), connections_text)
  288.     write_text_file(os.path.join(file_dir, 'headers.inc'), headers_text)
  289.     write_text_file(os.path.join(file_dir, 'events.inc'), events_text)
  290.     write_text_file(os.path.join(file_dir, '../../include/constants/map_groups.h'), map_header_text)
  291.  
  292.  
  293. def generate_layout_headers_text(layouts_data):
  294.     text = ''
  295.     for layout in layouts_data['layouts']:
  296.         border_label = layout['name'] + '_Border'
  297.         blockdata_label = layout['name'] + '_Blockdata'
  298.         text += '%s::\n' % (border_label)
  299.         text += '\t.incbin "%s"\n\n' % (layout['border_filepath'])
  300.         text += '%s::\n' % (blockdata_label)
  301.         text += '\t.incbin "%s"\n\n' % (layout['blockdata_filepath'])
  302.         text += '\t.align 2\n'
  303.         text += '%s::\n' % (layout['name'])
  304.         text += '\t.4byte %s\n' % (layout['width'])
  305.         text += '\t.4byte %s\n' % (layout['height'])
  306.         text += '\t.4byte %s\n' % (border_label)
  307.         text += '\t.4byte %s\n' % (blockdata_label)
  308.         text += '\t.4byte %s\n' % (layout['primary_tileset'])
  309.         text += '\t.4byte %s\n\n' % (layout['secondary_tileset'])
  310.  
  311.     return text
  312.  
  313.  
  314. def generate_layouts_table_text(layouts_data):
  315.     text = '\t.align 2\n'
  316.     text += '%s::\n' % (layouts_data['layouts_table_label'])
  317.     for layout in layouts_data['layouts']:
  318.         text += '\t.4byte %s\n' % (layout['name'])
  319.  
  320.     return text
  321.  
  322.  
  323. def generate_layouts_constants_text(layouts_data):
  324.     text = '#ifndef GUARD_CONSTANTS_LAYOUTS_H\n'
  325.     text += '#define GUARD_CONSTANTS_LAYOUTS_H\n\n'
  326.     for i, layout in enumerate(layouts_data['layouts']):
  327.         text += '#define %s %s\n' % (layout['id'], i + 1)
  328.     text += '\n'
  329.     text += '#endif  // GUARD_CONSTANTS_LAYOUTS_H\n'
  330.     return text
  331.  
  332.  
  333. def process_layouts(layouts_filepath):
  334.     with open(layouts_filepath) as f:
  335.         layouts_data = json.load(f)
  336.  
  337.     layout_headers_text = generate_layout_headers_text(layouts_data)
  338.     layouts_table_text = generate_layouts_table_text(layouts_data)
  339.     layouts_constants_text = generate_layouts_constants_text(layouts_data)
  340.  
  341.     file_dir = os.path.dirname(layouts_filepath)
  342.     write_text_file(os.path.join(file_dir, 'layouts.inc'), layout_headers_text)
  343.     write_text_file(os.path.join(file_dir, 'layouts_table.inc'), layouts_table_text)
  344.     write_text_file(os.path.join(file_dir, '../../include/constants/layouts.h'), layouts_constants_text)
  345.  
  346.  
  347. def main():
  348.     if len(sys.argv) < 2:
  349.         abort('USAGE: python mapjson.py <mode> [options]')
  350.  
  351.     mode = sys.argv[1]
  352.     if mode not in ['layouts', 'map', 'groups']:
  353.         abort("ERROR: <mode> must be 'layouts', 'map', or 'groups'.")
  354.  
  355.     if mode == 'map':
  356.         if len(sys.argv) != 4:
  357.             abort('USAGE: python mapjson.py map <map_file> <layouts_file>')
  358.  
  359.         filepath = sys.argv[2]
  360.         if not os.path.exists(filepath):
  361.             abort('ERROR: Map file "%s" does not exist' % (filepath))
  362.  
  363.         layouts_filepath = sys.argv[3]
  364.         if not os.path.exists(layouts_filepath):
  365.             abort('ERROR: Layouts file "%s" does not exist' % (layouts_filepath))
  366.  
  367.         process_map(filepath, layouts_filepath)
  368.     elif mode == 'groups':
  369.         if len(sys.argv) != 3:
  370.             abort('USAGE: python mapjson.py groups <groups_file>')
  371.  
  372.         filepath = sys.argv[2]
  373.         if not os.path.exists(filepath):
  374.             abort('ERROR: Map file "%s" does not exist' % (filepath))
  375.  
  376.         process_groups(filepath)
  377.     elif mode == 'layouts':
  378.         if len(sys.argv) != 3:
  379.             abort('USAGE: python mapjson.py layouts <layouts_file>')
  380.  
  381.         filepath = sys.argv[2]
  382.         if not os.path.exists(filepath):
  383.             abort('ERROR: Layouts file "%s" does not exist' % (filepath))
  384.  
  385.         process_layouts(filepath)
  386.  
  387.  
  388. if __name__ == '__main__':
  389.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement