Last active
October 22, 2021 04:37
-
-
Save cgarz/78f10dbad37e8fac9bd30070976db938 to your computer and use it in GitHub Desktop.
Simple script to convert text files to WA (Worms Armageddon) encoded format. For wkTextMacros (https://worms2d.info/WkTextMacros).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# based on https://github.com/elfoor/armabuddy/blob/master/wa_encoder.py | |
# char table detailed here: https://worms2d.info/WA_character_table | |
# pyinstaller exe version: | |
# https://cdn.discordapp.com/attachments/863028348351807488/898253659601797190/wa_character_encoding.exe | |
import os | |
import argparse | |
WA_CHAR_TABLE = ( | |
'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F' | |
'\x22\x26\x27\x3C\x3E\x5C\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' | |
'\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F' | |
'\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F' | |
'\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F' | |
'\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F' | |
'\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F' | |
'\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x00' | |
'БГДЖЗИЙКЛПУФЦЧШЩ' | |
'ЪЫЬЭЮ\x00Ябвгджзий\x9F' | |
'\xA0\xA1к\xA3\x00лмнптфцчшщъ' | |
'ыьэюяŐőŰűĞğŞşİı\xBF' | |
'\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF' | |
'\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF' | |
'\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF' | |
'\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF' | |
) | |
BYTE_TABLE = ''.join(chr(c) for c in range(256)) | |
DECODE_TABLE = dict(zip(BYTE_TABLE, WA_CHAR_TABLE)) | |
ENCODE_TABLE = dict(zip(WA_CHAR_TABLE, BYTE_TABLE)) | |
TRANS_TABLE = ''.maketrans( | |
'аеёорстухАВЕЁМНОРСТХĄąĆćĘꣳŃńŚśŹźŻż', | |
'aeëopcтyxABEËMHOPCTXAaCcEeLlNnSsZzZz' | |
) | |
def wa_decode(text: bytes): | |
return ''.join(map(lambda char: DECODE_TABLE[char], text.decode(encoding='raw_unicode_escape'))).encode() | |
def wa_encode(text: str): | |
return bytes( | |
''.join(map(lambda char: ENCODE_TABLE[char] if char in ENCODE_TABLE else '\x20', text)), | |
encoding='raw_unicode_escape') | |
def wa_translate(text: str): | |
return text.translate(TRANS_TABLE) | |
def getch(): | |
try: | |
import termios | |
# If no error, POSIX system. Create and return a getch that manipulates the tty. | |
import sys, tty | |
fd = sys.stdin.fileno() | |
old_settings = termios.tcgetattr(fd) | |
try: | |
tty.setraw(fd) | |
char = sys.stdin.read(1) | |
finally: | |
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) | |
return char.upper() | |
except ImportError: | |
# Non-POSIX. Return msvcrt's (Windows') getch. | |
import msvcrt | |
return msvcrt.getch().decode().upper() | |
def main(): | |
class ArgumentParser(argparse.ArgumentParser): | |
def error(self, message): | |
print('{}: error: {}\n'.format(self.prog, message)) | |
raise Exception | |
parser = ArgumentParser(description='A tool to convert files between unicode and WA (Worms Armageddon) encoding',) | |
group = parser.add_argument_group('conversion arguments') | |
mxg = group.add_mutually_exclusive_group() | |
mxg.add_argument('-w', '--wa', help='Convert the file from unicode into WA encoding.', action='store_true') | |
mxg.add_argument('-u', '--unicode', help='Convert the file from WA encoding into unicode.', action='store_true') | |
parser.add_argument('in_file', help='The path to the file to convert', metavar='FILE') | |
args = parser.parse_args() | |
if not os.path.isfile(args.in_file): | |
parser.error('Specified file does not exist.') | |
ret_char = '' | |
if not args.wa and not args.unicode: | |
print('Press W or U to select desired conversion:') | |
print('W - convert file to WA encoding format') | |
print('U - convert file to unicode') | |
print('Q - quit') | |
while True: | |
ret_char = getch() | |
if ret_char in ('W', 'U'): | |
break | |
elif ret_char == 'Q': | |
return | |
if args.wa or ret_char == 'W': | |
with open(args.in_file, 'rt', encoding='UTF8') as f: | |
lines = f.read().splitlines() | |
line_ends = f.newlines if f.newlines else '\n' | |
with open(args.in_file + '.temp', 'wb') as f: | |
for line in lines: | |
f.write(wa_encode(wa_translate(line))) | |
f.write(line_ends.encode()) | |
elif args.unicode or ret_char == 'U': | |
with open(args.in_file, 'rb') as f: | |
data = f.read() | |
with open(args.in_file + '.temp', 'wb') as f: | |
f.write(wa_decode(data)) | |
os.remove(args.in_file) | |
os.rename(args.in_file + '.temp', args.in_file) | |
if __name__ == '__main__': | |
try: | |
main() | |
except Exception as e: | |
if str(e).strip(): | |
print(e) | |
if os.name == 'nt': | |
print('Press any key to exit.') | |
getch() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment