Skip to content

Commit

Permalink
use python iconv if system iconv is broken
Browse files Browse the repository at this point in the history
  • Loading branch information
bates64 committed Jan 21, 2024
1 parent f1edb83 commit ff247c9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
23 changes: 22 additions & 1 deletion tools/build/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,24 @@ def write_ninja_for_tools(ninja: ninja_syntax.Writer):
ninja.build(CRC_TOOL, "cc_tool", f"{BUILD_TOOLS}/rom/n64crc.c")


def does_iconv_work() -> bool:
# run iconv and see if it works
stdin = "エリア OMO2_1".encode("utf-8")

def run(command, stdin):
sub = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, input=stdin)
return sub.stdout

expected_stdout = run(["tools/build/iconv.py", "UTF-8", "CP932"], stdin)
actual_stdout = run(["iconv", "--from", "UTF-8", "--to", "CP932"], stdin)
return expected_stdout == actual_stdout


use_python_iconv = not does_iconv_work()
if use_python_iconv:
print("warning: iconv doesn't work, using python implementation")


class Configure:
def __init__(self, version: str):
self.version = version
Expand Down Expand Up @@ -677,7 +695,10 @@ def build(
if version == "ique":
encoding = "EUC-JP"

iconv = f"iconv --from UTF-8 --to {encoding}"
if use_python_iconv:
iconv = f"tools/build/iconv.py UTF-8 {encoding}"
else:
iconv = f"iconv --from UTF-8 --to {encoding}"

# use tools/sjis-escape.py for src/battle/area/tik2/area.c
if version != "ique" and seg.dir.parts[-3:] == ("battle", "area", "tik2") and seg.name == "area":
Expand Down
23 changes: 23 additions & 0 deletions tools/build/iconv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/python3

import argparse
import sys

parser = argparse.ArgumentParser(description='Convert a file from one encoding to another')
parser.add_argument("f")
parser.add_argument("t")
parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin)
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), default=sys.stdout)

args = parser.parse_args()


def main(args):
sys.stdin.reconfigure(encoding=args.f)
in_data = args.infile.read()
sys.stdout.reconfigure(encoding=args.t)
args.outfile.write(in_data)


if __name__ == "__main__":
main(parser.parse_args())

0 comments on commit ff247c9

Please sign in to comment.