Ejemplo n.º 1
0
def upgrade_project(project_path, base_rom_filename, progress_bar=None):
    if not os.path.isdir(project_path):
        raise RuntimeError("Project directory \"" + project_path + "\" is not a directory.")
    if not os.path.isfile(base_rom_filename):
        raise RuntimeError("Base Rom \"" + base_rom_filename + "\" is not a file.")

    modules = load_modules()

    # Open project
    project_filename = os.path.join(project_path, PROJECT_FILENAME)

    project = Project()
    project.load(project_filename)
    check_if_project_too_new(project)

    if project.version == FORMAT_VERSION:
        log.info("Project is already up to date.")
        return

    log.info("Upgrading project from version {} to {}".format(
        get_version_name(project.version),
        get_version_name(FORMAT_VERSION)))
    upgrade_start_time = time.time()

    rom = Rom()
    rom.from_file(base_rom_filename)
    check_if_types_match(project=project, rom=rom)

    compatible_modules = [(name, clazz) for name, clazz in modules if clazz.is_compatible_with_romtype(rom.type)]
    tick_amount = 1.0/len(compatible_modules)

    for module_name, module_class in compatible_modules:
        log.info("Upgrading {}...".format(module_class.NAME))
        start_time = time.time()
        with module_class() as module:
            module.upgrade_project(project.version, FORMAT_VERSION, rom,
                                   lambda x, y, astext=False : project.get_resource(module_name, x, y, 'rt' if astext else 'rb', 'utf-8' if astext else None),
                                   lambda x, y, astext=False: 
                                        project.get_resource(module_name, x, y, 
                                            'wt' if astext else 'wb', 
                                            'utf-8' if astext else None,
                                            '\n' if astext else None),
                                   lambda x: project.delete_resource(module_name, x))
            if progress_bar:
                progress_bar.tick(tick_amount)
        log.info("Finished upgrading {} in {:.2f}s".format(module_class.NAME, time.time() - start_time))

    project.version = FORMAT_VERSION
    project.write(project_filename)

    log.info("Upgraded {} in {:.2f}s".format(project_path, time.time() - upgrade_start_time))
Ejemplo n.º 2
0
def decompile_rom(rom_filename, project_path, progress_bar=None):
    modules = load_modules()

    rom = Rom()
    rom.from_file(rom_filename)

    project = Project()
    project.load(os.path.join(project_path, PROJECT_FILENAME), rom.type)

    compatible_modules = [(name, clazz) for name, clazz in modules if clazz.is_compatible_with_romtype(rom.type)]
    tick_amount = 1.0/(2*len(compatible_modules))

    log.info("Decompiling ROM {}".format(rom_filename))
    decompile_start_time = time.time()

    for module_name, module_class in compatible_modules:
        if not module_class.is_compatible_with_romtype(rom.type):
            continue

        log.info("Decompiling {}...".format(module_class.NAME))
        start_time = time.time()
        with module_class() as module:
            module.read_from_rom(rom)
            if progress_bar:
                progress_bar.tick(tick_amount)
            module.write_to_project(lambda x, y: project.get_resource(module_name, x, y, 'wb'))
            if progress_bar:
                progress_bar.tick(tick_amount)
        log.info("Finished decompiling {} in {:.2f}s".format(module_class.NAME, time.time() - start_time))

    log.debug("Saving Project")
    project.write(os.path.join(project_path, PROJECT_FILENAME))

    log.info("Decompiled to {} in {:.2f}s".format(project_path, time.time() - decompile_start_time))
Ejemplo n.º 3
0
def upgrade_project(project_path, base_rom_filename, progress_bar=None):
    modules = load_modules()

    # Open project
    project_filename = os.path.join(project_path, PROJECT_FILENAME)

    project = Project()
    project.load(project_filename)
    check_if_project_too_new(project)

    if project.version == FORMAT_VERSION:
        log.info("Project is already up to date.")
        return

    log.info(
        "Upgrading project from version {} to {}".format(
            get_version_name(project.version), get_version_name(FORMAT_VERSION)
        )
    )
    upgrade_start_time = time.time()

    rom = Rom()
    rom.from_file(base_rom_filename)
    check_if_types_match(project=project, rom=rom)

    compatible_modules = [(name, clazz) for name, clazz in modules if clazz.is_compatible_with_romtype(rom.type)]
    tick_amount = 1.0 / len(compatible_modules)

    for module_name, module_class in compatible_modules:
        log.info("Upgrading {}...".format(module_class.NAME))
        start_time = time.time()
        with module_class() as module:
            module.upgrade_project(
                project.version,
                FORMAT_VERSION,
                rom,
                lambda x, y: project.get_resource(module_name, x, y, "rb"),
                lambda x, y: project.get_resource(module_name, x, y, "wb"),
                lambda x: project.delete_resource(module_name, x),
            )
            if progress_bar:
                progress_bar.tick(tick_amount)
        log.info("Finished upgrading {} in {:.2f}s".format(module_class.NAME, time.time() - start_time))

    project.version = FORMAT_VERSION
    project.write(project_filename)

    log.info("Upgraded {} in {:.2f}s".format(project_path, time.time() - upgrade_start_time))
Ejemplo n.º 4
0
def compile_project(project_path, base_rom_filename, output_rom_filename, ccscript_offset=None, progress_bar=None):
    modules = load_modules()

    project_filename = os.path.join(project_path, PROJECT_FILENAME)
    project = Project()
    project.load(project_filename)
    check_if_project_too_old(project)
    check_if_project_too_new(project)

    if base_rom_filename != output_rom_filename:
        copyfile(base_rom_filename, output_rom_filename)

    # Compile scripts using CCScript
    script_filenames = [os.path.join(project_path, "ccscript", x)
                        for x in os.listdir(os.path.join(project_path, "ccscript"))
                        if x.lower().endswith('.ccs')]

    if script_filenames:
        log.info("Compiling CCScript")
        if not ccscript_offset:
            ccscript_offset = "F10000"
        elif type(ccscript_offset) == int:
            ccscript_offset = "{:x}".format(ccscript_offset)

        ccc_args = ["-n",
                    "--libs", ccscript_library_path(),
                    "--summary", os.path.join(project_path, "ccscript", "summary.txt"),
                    "-s", ccscript_offset,
                    "-o", output_rom_filename] + script_filenames
        ccc_returncode, ccc_log = ccc(ccc_args)

        if ccc_returncode == 0:
            log.info("Finished compiling CCScript")
        else:
            raise CCScriptCompilationError("CCScript compilation failed with output:\n" + ccc_log)

    rom = Rom()
    rom.from_file(output_rom_filename)
    check_if_types_match(project=project, rom=rom)

    compatible_modules = [(name, clazz) for name, clazz in modules if clazz.is_compatible_with_romtype(rom.type)]
    tick_amount = 1.0/(2*len(compatible_modules))

    log.info("Compiling Project {}".format(project_path))
    compile_start_time = time.time()

    for module_name, module_class in modules:
        if module_class.is_compatible_with_romtype(rom.type):
            for free_range in module_class.FREE_RANGES:
                rom.deallocate(free_range)

    for module_name, module_class in modules:
        if not module_class.is_compatible_with_romtype(rom.type):
            continue

        log.info("Compiling {}...".format(module_class.NAME))
        start_time = time.time()
        with module_class() as module:
            module.read_from_project(lambda x, y: project.get_resource(module_name, x, y, 'rb'))
            if progress_bar:
                progress_bar.tick(tick_amount)
            module.write_to_rom(rom)
            if progress_bar:
                progress_bar.tick(tick_amount)
        log.info("Finished compiling {} in {:.2f}s".format(module_class.NAME, time.time() - start_time))

    log.debug("Saving ROM")
    rom.to_file(output_rom_filename)

    log.info("Compiled to {} in {:.2f}s, finished at {}".format(
        output_rom_filename, time.time() - compile_start_time, datetime.now().strftime('%I:%M:%S %p')))