From 58ad8b66eef2f9268818cd78627f8eec938981b5 Mon Sep 17 00:00:00 2001 From: biqqles Date: Sun, 13 Sep 2020 20:29:24 +0100 Subject: [PATCH] Interface: move to own file --- flint/formats/dll.py | 54 ++++----------------------------- flint/interface.py | 71 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 48 deletions(-) create mode 100644 flint/interface.py diff --git a/flint/formats/dll.py b/flint/formats/dll.py index 2764145..64379e9 100755 --- a/flint/formats/dll.py +++ b/flint/formats/dll.py @@ -12,10 +12,10 @@ """ from typing import Dict from os import SEEK_CUR -import xml.etree.ElementTree as xml import deconstruct as c +from ..interface import rdl_to_html, rdl_to_plaintext from .. import cached from .. import paths from . import WinStruct @@ -25,7 +25,7 @@ @cached -def lookup(resource_id: int): +def lookup(resource_id: int) -> str: """Looks up the text associated with a resource ID (string or HTML) in the resource dlls.""" if resource_id is None: # sometimes objects which should have infocards don't. Freelancer doesn't seem to care return '' @@ -49,20 +49,15 @@ def lookup(resource_id: int): @cached -def lookup_as_html(resource_id: int): +def lookup_as_html(resource_id: int) -> str: """Looks up the given resource ID and translates RDL to HTML.""" - result = lookup(resource_id) - for rdl, html in RDL_TO_HTML.items(): - result = result.replace(rdl, html) - return result + return rdl_to_html(lookup(resource_id)) @cached -def lookup_as_plain(resource_id: int): +def lookup_as_plain(resource_id: int) -> str: """Looks up the given resource ID and strips out all RDL tags. Paragraph tags are replaced with newlines.""" - rdl = lookup(resource_id).replace('', '\n').replace('', '') - tree = xml.fromstring(rdl) - return xml.tostring(tree, encoding='unicode', method='text') + return rdl_to_plaintext(lookup(resource_id)) def parse(path: str, external_strid_offset: int) -> Dict[int, str]: @@ -204,40 +199,3 @@ class ResourceDataEntry(WinStruct): class ResourceDirectoryString(WinStruct): Length: c.int16 - - -# A lookup table mapping RDL (Render Display List) tags to HTML(4). Freelancer, to my eternal horror, uses these for -# formatting for strings inside these resource DLLs. Based on work by adoxa and cshake. -# More information can be found in this thread: -RDL_TO_HTML = { - '': '', # bold - '': '', # rare bold - '': '', # un-bold - '': '', # italic 1 - '': '', # un-italic 1 - '': '', # italic 2 - '': '', # un-italic 2 - '': '', # italic 3 - '': '', # un-italic 3 - '': '', # (bold, underline) 1 - '': '', # un-(bold, underline) 1 - '': '', # (bold, underline) 2 - '': '', # un-(bold, underline) 2 - '': '', # red - '': '', # un-colour - '': '', # (bold, red) - '': '', # un-(bold, red) - '': '', # blue - '': '

', # newline - '': '

', - '': '

', # newline with left-aligned text - '': '

', # newline with centred text - '\xa0': ' ', # non-breaking space, is often present after the title - '': '', # seemingly meaningless tags... - '': '', - '': '', - '': '', - '': '', - '': '', - '': '', # xml header; removed for neatness -} diff --git a/flint/interface.py b/flint/interface.py new file mode 100644 index 0000000..87c6576 --- /dev/null +++ b/flint/interface.py @@ -0,0 +1,71 @@ +""" +Copyright (C) 2016, 2017, 2020 biqqles. + +This Source Code Form is subject to the terms of the Mozilla Public +License, v. 2.0. If a copy of the MPL was not distributed with this +file, You can obtain one at http://mozilla.org/MPL/2.0/. + +Interface-related functions, such as routines for translating RDL. +""" +import xml.etree.ElementTree as xml + + +def rdl_to_html(rdl: str) -> str: + """Translate RDL to HTML. Currently this uses a crude lookup table. In future I want to replace this with + proper interpretation of the XML.""" + result = rdl + for rdl_tag, html_tag in RDL_TO_HTML.items(): + result = result.replace(rdl_tag, html_tag) + return result + + +def rdl_to_plaintext(rdl: str) -> str: + """Translate RDL to plaintext, stripping all tags and replacing with a newline.""" + rdl = rdl.replace('', '\n').replace('', '') + tree = xml.fromstring(rdl) + return xml.tostring(tree, encoding='unicode', method='text') + + +def html_to_rdl(html: str) -> str: + """Translate HTML to RDL. See the docstring for `rdl_to_html` for more information.""" + result = html + for rdl_tag, html_tag in RDL_TO_HTML.items(): + result = result.replace(html_tag, rdl_tag) + return result + + +# A lookup table mapping RDL (Render Display List) tags to HTML(4). Freelancer, to my eternal horror, uses these for +# formatting for strings inside these resource DLLs. Based on work by adoxa and cshake. +# More information can be found in this thread: +RDL_TO_HTML = { + '': '', # bold + '': '', # rare bold + '': '', # un-bold + '': '', # italic 1 + '': '', # un-italic 1 + '': '', # italic 2 + '': '', # un-italic 2 + '': '', # italic 3 + '': '', # un-italic 3 + '': '', # (bold, underline) 1 + '': '', # un-(bold, underline) 1 + '': '', # (bold, underline) 2 + '': '', # un-(bold, underline) 2 + '': '', # red + '': '', # un-colour + '': '', # (bold, red) + '': '', # un-(bold, red) + '': '', # blue + '': '

', # newline + '': '

', + '': '

', # newline with left-aligned text + '': '

', # newline with centred text + '\xa0': ' ', # non-breaking space, is often present after the title + '': '', # seemingly meaningless tags... + '': '', + '': '', + '': '', + '': '', + '': '', + '': '', # xml header; removed for neatness +}