Exemplo n.º 1
0
def generate_hashes(modpack: Modpack,
                    exportdir: Directory,
                    hash_algo: str = 'sha3_256',
                    bufsize: int = 32768) -> None:
    """
    Generate hashes for all the items in a
    given modpack and adds them to the manifest
    instance.

    :param modpack: The target modpack.
    :type modpack: ..dataformats.modpack.Modpack
    :param exportdir: Directory wheere modpacks are stored.
    :type exportdir: ...util.fslike.path.Path
    :param hash_algo: Hashing algorithm used.
    :type hash_algo: str
    :param bufsize: Buffer size for reading files.
    :type bufsize: int
    """
    # set the hashing algorithm in the manifest instance
    modpack.manifest.set_hashing_func(hash_algo)

    # traverse the directory with breadth-first way and
    # generate hash values for the items encountered
    for file in bfs_directory(exportdir):
        hash_val = hash_file(file, hash_algo=hash_algo, bufsize=bufsize)
        relative_path = os.path.relpath(str(file), str(exportdir))
        modpack.manifest.add_hash_value(hash_val, relative_path)
Exemplo n.º 2
0
def debug_media_cache(debugdir, loglevel, sourcedir, cachedata, game_version):
    """
    Create media cache data for graphics files. This allows using deterministic
    packer and compression settings for graphics file conversion.

    :param debugdir: Output directory for the debug info.
    :type debugdir: Directory
    :param loglevel: Determines how detailed the output is.
    :type loglevel: int
    :param sourcedir: Sourcedir where the graphics files are mounted.
    :type sourcedir: int
    :param cachedata: Dict with cache data.
    :type cachedata: dict
    :param game_version: Game version.
    :type game_version: tuple
    """
    if loglevel < 6:
        return

    cache_file = MediaCacheFile("export/", "media_cache.toml", game_version)
    cache_file.set_hash_func("sha3_256")

    # Sort the output by filename
    cache_data = dict(sorted(cachedata.items(), key=lambda item: item[0].source_filename))

    for request, cache in cache_data.items():
        filepath = sourcedir[
            request.get_type().value,
            request.source_filename
        ]

        cache_file.add_cache_data(
            request.get_type(),
            request.source_filename,
            hash_file(filepath),
            cache[1],
            cache[0])

    logfile = debugdir.joinpath("export/")["media_cache.toml"]
    logtext = cache_file.dump()

    with logfile.open("w") as log:
        log.write(logtext)