# Buggy code
def get_string_resources(args):
    """reads the (language) string resources"""
    from .stringresource import StringResource

    stringres = StringResource()

    srcdir = args.srcdir
    count = 0

    # AoK:TC uses .DLL PE files for its string resources,
    # HD uses plaintext files
    if srcdir["language.dll"].is_file():
        from .pefile import PEFile

        for name in ["language.dll", "language_x1.dll", "language_x1_p1.dll"]:
            pefile = PEFile(srcdir[name].open("rb"))
            stringres.fill_from(pefile.resources().strings)
            count += 1
    elif GameVersion.age2_fe in args.game_versions:
        from .hdlanguagefile import read_hd_language_file

        for lang in srcdir["resources"].list():
            try:
                if lang == b"_common":
                    continue
                langfilename = [
                    "resources",
                    lang.decode(),
                    "strings",
                    "key-value",
                    "key-value-strings-utf8.txt",
                ]

                with srcdir[langfilename].open("rb") as langfile:
                    stringres.fill_from(read_hd_language_file(langfile, lang))

                count += 1
            except FileNotFoundError:
                # that's fine, there are no language files for every language.
                pass
    elif GameVersion.age2_hd_3x in args.game_versions:
        from .hdlanguagefile import read_hd_language_file

        # HD Edition 3.x and below store language .txt files in the Bin/ folder.
        # Specific language strings are in Bin/$LANG/*.txt.
        for lang in srcdir["bin"].list():
            dirname = ["bin", lang.decode()]

            # There are some .txt files immediately in bin/, but they don't seem
            # to contain anything useful. (Everything is overridden by files in
            # Bin/$LANG/.)
            if not srcdir[dirname].is_dir():
                continue

            for basename in srcdir[dirname].list():
                langfilename = ["bin", lang.decode(), basename]
                with srcdir[langfilename].open("rb") as langfile:
                    # No utf-8 :(
                    stringres.fill_from(
                        read_hd_language_file(langfile, lang, enc="iso-8859-1")
                    )
                count += 1

    if not count:
        raise FileNotFoundError("could not find any language files")

    # TODO transform and cleanup the read strings:
    #      convert formatting indicators from HTML to something sensible, etc

    return stringres


# Traceback (Exception)
$ make run
./run game
INFO [py] No converted assets have been found
media conversion is required.
please provide your AoE II installation dir:
/media/niklas/E4DE307EDE304AD6/Program Files (x86)/Steam/SteamApps/common/Age2HD
INFO [py] Game version(s) detected: Age of Empires 2: HD Edition (Version 3.x); Forgotten Empires; Age of Empires 2: The Conquerors, Patch 1.0c
INFO [py] converting metadata
INFO [py] [0] palette
INFO [py] [1] empires.dat
INFO [py] [2] blendomatic.dat
INFO [py] blending masks successfully exported
INFO [py] [3] player color palette
INFO [py] [4] terminal color palette
INFO [py] [5] string resources
Traceback (most recent call last):
File "run.py", line 15, in init run (/home/niklas/Projekte/openage/run.cpp:832)
main()
File "/home/niklas/Projekte/openage/openage/__main__.py", line 94, in main
return args.entrypoint(args, cli.error)
File "/home/niklas/Projekte/openage/openage/game/main.py", line 33, in main
if not convert_assets(assets, args):
File "/home/niklas/Projekte/openage/openage/convert/main.py", line 125, in convert_assets
for current_item in convert(args):
File "/home/niklas/Projekte/openage/openage/convert/driver.py", line 126, in convert
yield from convert_metadata(args)
File "/home/niklas/Projekte/openage/openage/convert/driver.py", line 179, in convert_metadata
stringres = get_string_resources(args)
File "/home/niklas/Projekte/openage/openage/convert/driver.py", line 37, in get_string_resources
pefile = PEFile(srcdir[name].open('rb'))
File "/home/niklas/Projekte/openage/openage/convert/pefile.py", line 191, in __init__
raise Exception("Invalid section name: " + section.name)
Exception: Invalid section name: CODE
Makefile:22: recipe for target 'run' failed
make: *** [run] Error 1

# Fixed code
def get_string_resources(args):
    """reads the (language) string resources"""
    from .stringresource import StringResource

    stringres = StringResource()

    srcdir = args.srcdir
    count = 0

    # AoK:TC uses .DLL PE files for its string resources,
    # HD uses plaintext files
    if GameVersion.age2_fe in args.game_versions:
        from .hdlanguagefile import read_hd_language_file

        for lang in srcdir["resources"].list():
            try:
                if lang == b"_common":
                    continue
                langfilename = [
                    "resources",
                    lang.decode(),
                    "strings",
                    "key-value",
                    "key-value-strings-utf8.txt",
                ]

                with srcdir[langfilename].open("rb") as langfile:
                    stringres.fill_from(read_hd_language_file(langfile, lang))

                count += 1
            except FileNotFoundError:
                # that's fine, there are no language files for every language.
                pass
    elif GameVersion.age2_hd_3x in args.game_versions:
        from .hdlanguagefile import read_hd_language_file

        # HD Edition 3.x and below store language .txt files in the Bin/ folder.
        # Specific language strings are in Bin/$LANG/*.txt.
        for lang in srcdir["bin"].list():
            dirname = ["bin", lang.decode()]

            # There are some .txt files immediately in bin/, but they don't seem
            # to contain anything useful. (Everything is overridden by files in
            # Bin/$LANG/.)
            if not srcdir[dirname].is_dir():
                continue

            for basename in srcdir[dirname].list():
                langfilename = ["bin", lang.decode(), basename]
                with srcdir[langfilename].open("rb") as langfile:
                    # No utf-8 :(
                    stringres.fill_from(
                        read_hd_language_file(langfile, lang, enc="iso-8859-1")
                    )
                count += 1
    elif srcdir["language.dll"].is_file():
        from .pefile import PEFile

        for name in ["language.dll", "language_x1.dll", "language_x1_p1.dll"]:
            pefile = PEFile(srcdir[name].open("rb"))
            stringres.fill_from(pefile.resources().strings)
            count += 1

    if not count:
        raise FileNotFoundError("could not find any language files")

    # TODO transform and cleanup the read strings:
    #      convert formatting indicators from HTML to something sensible, etc

    return stringres