def preprocess_assembly(filepath, charmappath, outpath, terminator=0xFF): """ Preprocesses an assembly file. """ ps = pstring.Pstring(charmappath, terminator=terminator) output = "" with open(filepath, "r", encoding="utf-8") as f: assembly = f.read() # Preprocess assembly linewise for line in splitlines_continued(assembly): # Normalize line by replacing # all tabs with whitespace # and cumulating consecutive # whitespaces tokens = line.split() # Check for directives if len(tokens) < 1: output += os.linesep elif tokens[0] == DIRECTIVE_STRING: string = line[line.index( tokens[1] ):] # This way we do not loose multiple " " delimiters output += ".byte " + ", ".join(map(str, process_string( string, ps))) + os.linesep elif tokens[0] == DIRECTIVE_AUTOSTRING: string = line[line.index( tokens[3] ):] # This way we do not loose multiple " " delimiters line_cnt = int(tokens[2], 0) linewidth = int(tokens[1], 0) bytes = process_string(string, ps) # Format string bytes = format.format_pstring(bytes, linewidth, line_cnt) output += ".byte " + ", ".join(map(str, bytes)) + os.linesep elif tokens[0] == DIRECTIVE_STRINGPAD: padding = int(tokens[1], 0) string = line[line.index( tokens[2] ):] # This way we do not loose multiple " " delimiters bytes = process_string(string, ps) bytes += [0] * (padding - len(bytes)) output += ".byte " + ", ".join(map(str, bytes)) + os.linesep else: output += line + os.linesep with open(outpath, "w+", encoding="utf-8") as f: f.write(output)
#!/usr/bin/env python3 from agb import agbrom from pymap import project from pokestring import pstring rom = agbrom.Agbrom("/media/d/romhacking/violetbuilt.gba") trainer_base = rom.pointer(0xfb84) ps = pstring.Pstring("/media/d/romhacking/Violet_Sources/charmap.txt") proj = project.Project.load_project( "/media/d/romhacking/Violet_Sources/proj.pmp") consts = proj.constants trainers = [] parties = [] headers = [] def ai_to_str(ai): strategies = [] for i in range(32): if ai & (1 << i): try: strategies.append( consts.constantize(1 << i, "trainer_ai_flags")) except: strategies.append(hex(1 << i)) if len(strategies): return " | ".join(strategies) else: return "TRAINER_AI_STRATEGY_NONE"
import core, symbols from agb import agbrom from pymap import project from pokestring import pstring symtbl = symbols.parse_symbols("bld/symbols") rom = agbrom.Agbrom("bld/violet.gba") symbols.verify_symbols(symtbl, rom) proj = project.Project.load_project("proj.pmp") ps = pstring.Pstring("charmap.txt") core.index_species(symtbl, rom, proj, ps)
def preprocess_c(filepath, charmappath, outpath, macro, terminator=0xFF): """ Preprocesses a c or cpp file. """ with open(filepath, "r", encoding="utf-8") as f: src = f.read() offset = 0 output = "" skipable_characters = set(("\t", " ", "\\", os.linesep)) ps = pstring.Pstring(charmappath, terminator=terminator) while offset < len(src): # Parse the entire file # Find the next occurence of macro next = src.find(macro, offset) if next == -1: # No more occrences of the macro # simply append the rest of the file output += src[offset:] offset = len(src) else: # Macro found, skip sementically # irrelevant characters output += src[offset:next] offset = next + len(macro) output += macro skipped = skip(src, offset, skipable_characters) if offset + skipped >= len(src): raise Exception("Unexpected eof while parsing \ macro.") output += src[offset:offset + skipped] offset += skipped if src[offset] != '(': raise Exception("Expected '(' after macro \ definition.") offset += 1 output += '(' # Parse string # Collect characters string = "" while True: # Collect as many "..." sets as possible # Parse '"' skipped = skip(src, offset, skipable_characters) if offset + skipped >= len(src): raise Exception("Unexpected eof while expecting \ '\"'.") offset += skipped # If got a ')' instead end parsing if src[offset] == ")": # Parse collected string and convert # into bytes output += "{" + ", ".join(map(str, ps.str2hex(string))) + "}" output += ")" offset += 1 break if src[offset] != "\"": raise Exception("Expected '\"'. Got {0}".format( src[offset:offset + 10])) offset += 1 # Parse characters until next '"' while offset < len(src): if src[offset] == "\"": offset += 1 break string += src[offset] offset += 1 if offset >= len(src): raise Exception("Unexpected eof while parsing \ string.") # Output with open(outpath, "w+", encoding="utf-8") as f: f.write(output)
#!/usr/bin/env python3 from pokestring import pstring from agb import agbrom from pymap import constants with open("../../map/proj.pmp.config") as f: conf = eval(f.read())["macro"] violet = agbrom.Agbrom("../../../violetbuilt.gba") bpre = agbrom.Agbrom("../../base/bpre.gba") const = constants.Constants("../../map/proj.pmp.constants", conf) ps = pstring.Pstring("../../charmap.txt") ger_item_base = violet.pointer(0x1C8) en_item_base = bpre.pointer(0x1C8) item_out = "" for i in range(0, len(const.values("items"))): item_out += "{\n\t//" + const.constantize(i, "items") + "\n\t" ger_off = ger_item_base + 0x2C * i en_off = en_item_base + 0x2C * i ger_name = ps.hex2str(violet, ger_off) print(ger_name) en_name = ps.hex2str(bpre, en_off) print(en_name) item_out += "LANGDEP(PSTRING(\"{0}\"), PSTRING(\"{1}\")),\n".format( ger_name, en_name) item_out += "\t" + hex(violet.u16(ger_off + 14)) + ", //index\n" item_out += "\t" + str(violet.u16(ger_off + 16)) + ", //price\n" item_out += "\t" + str(violet.u8(ger_off + 18)) + ", //holding_effect_id\n"