Skip to content

Instantly share code, notes, and snippets.

@Overv
Last active October 28, 2015 13:11
Show Gist options
  • Save Overv/064c95408203a49d95ef to your computer and use it in GitHub Desktop.
Save Overv/064c95408203a49d95ef to your computer and use it in GitHub Desktop.
import os
import re
def get_filepaths(directory):
file_paths = []
for root, directories, files in os.walk(directory):
for filename in files:
filepath = os.path.join(root, filename)
file_paths.append(filepath)
return file_paths
mappings = {}
with open('string_ids.h', 'r') as f:
for line in f.readlines():
m = re.search(r'([A-Z0-9_]+)[^=]*= (-?[0-9]+)', line)
if m != None:
mappings[m.group(2)] = m.group(1)
addr_mappings = {}
with open('addresses.h', 'r') as f:
for line in f.readlines():
m = re.search(r'#define (RCT2_[A-Z0-9_]+)[^0]+(0x[0-9A-F]+)', line)
if m != None:
addr_mappings[m.group(2)] = m.group(1)
def fix_string_id(match):
if match.group(2) in mappings:
return 'RCT2_GLOBAL(' + match.group(1) + ', rct_string_id) = ' + mappings[match.group(2)] + ';'
else:
return match.group(0)
def fix_address(match):
if match.group(2) in addr_mappings:
return 'RCT2_' + match.group(1) + '(' + addr_mappings[match.group(2)] + ','
else:
return match.group(0)
def fix_get_ride_entry(match):
return 'GET_RIDE_ENTRY(' + match.group(1) + ')'
full_file_paths = get_filepaths('OpenRCT2')
for fn in full_file_paths:
if '.git' not in fn and 'ride/ride.h' not in fn:
with open(fn, 'r') as f:
contents = f.read()
new_contents = re.sub(r'RCT2_GLOBAL\((.*?), rct_string_id\) = ([0-9]+);', fix_string_id, contents)
new_contents = re.sub(r'RCT2_(GLOBAL|ADDRESS)\((0x[0-9A-F]+),', fix_address, new_contents)
new_contents = re.sub(r'RCT2_ADDRESS\(RCT2_ADDRESS_RIDE_ENTRIES, uint32\)\[([^)]*)\]', fix_get_ride_entry, new_contents)
with open(fn, 'w') as f:
f.write(new_contents)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment