Example #1
0
def test_arc_from_dir_re5(tmpdir, arc_file):
    """get an arc file (ideally from the game), unpack it, repackit, unpack it again
    compare the 2 arc files and the 2 output folders"""
    arc_original = Arc(file_path=arc_file)
    arc_original_out = os.path.join(str(tmpdir), os.path.basename(arc_file).replace('.arc', ''))
    arc_original.unpack(arc_original_out)

    arc_from_dir = Arc.from_dir(arc_original_out)
    arc_from_dir_out = os.path.join(str(tmpdir), 'arc-from-dir.arc')
    with open(arc_from_dir_out, 'wb') as w:
        w.write(arc_from_dir)

    arc_from_arc_from_dir = Arc(file_path=arc_from_dir_out)
    arc_from_arc_from_dir_out = os.path.join(str(tmpdir), 'arc-from-arc-from-dir')
    arc_from_arc_from_dir.unpack(arc_from_arc_from_dir_out)

    files_extracted_1 = [f for _, _, files in os.walk(arc_original_out) for f in files]
    files_extracted_2 = [f for _, _, files in os.walk(arc_from_arc_from_dir_out) for f in files]

    # Assumming zlib default compression used in all original arc files.
    assert os.path.getsize(arc_file) == os.path.getsize(arc_from_dir_out)
    # The hashes would be different due to the file_paths ordering
    assert arc_original.files_count == arc_from_arc_from_dir.files_count
    assert sorted(files_extracted_1) == sorted(files_extracted_2)
    assert arc_from_arc_from_dir.file_entries[0].offset == 32768
Example #2
0
def export_arc(blender_object):
    '''Exports an arc file containing mod and tex files, among others from a
    previously imported arc.'''
    mods = {}
    try:
        saved_arc = Arc(
            file_path=BytesIO(blender_object.albam_imported_item.data))
    except AttributeError:
        raise ExportError(
            'Object {0} did not come from the original arc'.format(
                blender_object.name))

    for child in blender_object.children:
        try:
            basename = posixpath.basename(child.name)
            folder = child.albam_imported_item.folder
            if os.sep == ntpath.sep:  # Windows
                mod_filepath = ntpath.join(ensure_ntpath(folder), basename)
            else:
                mod_filepath = os.path.join(folder, basename)
        except AttributeError:
            raise ExportError(
                'Object {0} did not come from the original arc'.format(
                    child.name))
        assert child.albam_imported_item.file_type == 'mtframework.mod'
        mod, textures = export_mod156(child)
        mods[mod_filepath] = (mod, textures)

    with tempfile.TemporaryDirectory() as tmpdir:
        tmpdir_slash_ending = tmpdir + os.sep if not tmpdir.endswith(
            os.sep) else tmpdir
        saved_arc.unpack(tmpdir)
        mod_files = [
            os.path.join(root, f) for root, _, files in os.walk(tmpdir)
            for f in files if f.endswith('.mod')
        ]
        # tex_files = {os.path.join(root, f) for root, _, files in os.walk(tmpdir)
        #             for f in files if f.endswith('.tex')}
        new_tex_files = set()
        for modf in mod_files:
            rel_path = modf.split(tmpdir_slash_ending)[1]
            try:
                new_mod = mods[rel_path]
            except KeyError:
                raise ExportError(
                    "Can't export to arc, a mod file is missing: {}".format(
                        rel_path))

            with open(modf, 'wb') as w:
                w.write(new_mod[0])
            mod_textures = new_mod[1]
            for texture in mod_textures:
                tex = Tex112.from_dds(
                    file_path=bpy.path.abspath(texture.image.filepath))
                try:
                    tex.unk_float_1 = texture.albam_imported_texture_value_1
                    tex.unk_float_2 = texture.albam_imported_texture_value_2
                    tex.unk_float_3 = texture.albam_imported_texture_value_3
                    tex.unk_float_4 = texture.albam_imported_texture_value_4
                except AttributeError:
                    pass

                tex_name = os.path.basename(texture.image.filepath)
                tex_filepath = os.path.join(os.path.dirname(modf),
                                            tex_name.replace('.dds', '.tex'))
                new_tex_files.add(tex_filepath)
                with open(tex_filepath, 'wb') as w:
                    w.write(tex)
        # probably other files can reference textures besides mod, this is in case
        # textures applied have other names.
        # TODO: delete only textures referenced from saved_mods at import time
        # unused_tex_files = tex_files - new_tex_files
        # for utex in unused_tex_files:
        #    os.unlink(utex)
        new_arc = Arc.from_dir(tmpdir)
    return new_arc
Example #3
0
def export_arc(blender_object):
    '''Exports an arc file containing mod and tex files, among others from a
    previously imported arc.'''
    mods = {}
    try:
        saved_arc = Arc(file_path=BytesIO(blender_object.albam_imported_item.data))
    except AttributeError:
        raise ExportError('Object {0} did not come from the original arc'.format(blender_object.name))

    for child in blender_object.children:
        try:
            basename = posixpath.basename(child.name)
            folder = child.albam_imported_item.folder
            if os.sep == ntpath.sep:  # Windows
                mod_filepath = ntpath.join(ensure_ntpath(folder), basename)
            else:
                mod_filepath = os.path.join(folder, basename)
        except AttributeError:
            raise ExportError('Object {0} did not come from the original arc'.format(child.name))
        assert child.albam_imported_item.file_type == 'mtframework.mod'
        mod, textures = export_mod156(child)
        mods[mod_filepath] = (mod, textures)

    with tempfile.TemporaryDirectory() as tmpdir:
        tmpdir_slash_ending = tmpdir + os.sep if not tmpdir.endswith(os.sep) else tmpdir
        saved_arc.unpack(tmpdir)
        mod_files = [os.path.join(root, f) for root, _, files in os.walk(tmpdir)
                     for f in files if f.endswith('.mod')]
        # tex_files = {os.path.join(root, f) for root, _, files in os.walk(tmpdir)
        #             for f in files if f.endswith('.tex')}
        new_tex_files = set()
        for modf in mod_files:
            rel_path = modf.split(tmpdir_slash_ending)[1]
            try:
                new_mod = mods[rel_path]
            except KeyError:
                raise ExportError("Can't export to arc, a mod file is missing: {}".format(rel_path))

            with open(modf, 'wb') as w:
                w.write(new_mod[0])
            mod_textures = new_mod[1]
            for texture in mod_textures:
                tex = Tex112.from_dds(file_path=bpy.path.abspath(texture.image.filepath))
                try:
                    tex.unk_float_1 = texture.albam_imported_texture_value_1
                    tex.unk_float_2 = texture.albam_imported_texture_value_2
                    tex.unk_float_3 = texture.albam_imported_texture_value_3
                    tex.unk_float_4 = texture.albam_imported_texture_value_4
                except AttributeError:
                    pass

                tex_name = os.path.basename(texture.image.filepath)
                tex_filepath = os.path.join(os.path.dirname(modf), tex_name.replace('.dds', '.tex'))
                new_tex_files.add(tex_filepath)
                with open(tex_filepath, 'wb') as w:
                    w.write(tex)
        # probably other files can reference textures besides mod, this is in case
        # textures applied have other names.
        # TODO: delete only textures referenced from saved_mods at import time
        # unused_tex_files = tex_files - new_tex_files
        # for utex in unused_tex_files:
        #    os.unlink(utex)
        new_arc = Arc.from_dir(tmpdir)
    return new_arc