TRX/docs/render_progress

230 lines
5.4 KiB
Text
Raw Normal View History

2021-02-11 00:26:17 +01:00
#!/usr/bin/python3
import re
2021-02-11 00:26:17 +01:00
import typing as T
from dataclasses import dataclass
from pathlib import Path
2021-02-12 13:43:19 +01:00
GRID_MAX_SQUARES = 50
GRID_SQUARE_SIZE = 12
GRID_SQUARE_MARGIN = 2
LEGEND_SQUARE_SIZE = 12
LEGEND_SQUARE_MARGIN = 2
LEGEND_ROW_PADDING = 3
LEGEND_MARGIN = 15
TEXT_SIZE = 15
2021-02-12 13:43:19 +01:00
TEXT_MARGIN = 5
2021-02-11 00:26:17 +01:00
DOCS_DIR = Path(__file__).parent
PROGRESS_TXT_FILE = DOCS_DIR / "progress.txt"
PROGRESS_SVG_FILE = DOCS_DIR / "progress.svg"
2021-02-12 13:09:45 +01:00
COLOR_DECOMPILED = "green"
2021-02-12 13:43:19 +01:00
COLOR_TODO = "lightpink"
2021-02-12 13:09:45 +01:00
2021-02-11 00:26:17 +01:00
@dataclass
class Function:
name: str
offset: int
size: int
flags: str
@property
2021-02-12 13:09:45 +01:00
def is_decompiled(self):
2021-02-11 00:26:17 +01:00
return "+" in self.flags
def collect_functions() -> T.Iterable[Function]:
for line in PROGRESS_TXT_FILE.open():
if line.startswith("#"):
continue
func_name, offset, size, flags = re.split(r"\s+", line.strip())
yield Function(
name=func_name,
offset=int(offset, 16),
size=int(size, 16),
flags=flags,
)
2021-02-12 13:43:19 +01:00
@dataclass
class BoundingBox:
x1: int
y1: int
x2: int
y2: int
2021-02-12 13:09:45 +01:00
class Shape:
@property
2021-02-12 13:43:19 +01:00
def bounds(self) -> BoundingBox:
2021-02-12 13:09:45 +01:00
raise NotImplementedError("not implemented")
def render(self) -> str:
raise NotImplementedError("not implemented")
2021-02-12 13:43:19 +01:00
def get_common_bbox(shapes: T.List[Shape]) -> BoundingBox:
return BoundingBox(
x1=min(shape.bounds.x1 for shape in shapes),
y1=min(shape.bounds.y1 for shape in shapes),
x2=max(shape.bounds.x2 for shape in shapes),
y2=max(shape.bounds.y2 for shape in shapes),
)
2021-02-12 13:09:45 +01:00
@dataclass
class Square(Shape):
x: int
y: int
color: str
2021-02-12 13:43:19 +01:00
size: int = GRID_SQUARE_SIZE
2021-02-12 13:09:45 +01:00
@property
2021-02-12 13:43:19 +01:00
def bounds(self) -> BoundingBox:
return BoundingBox(
x1=self.x, y1=self.y, x2=self.x + self.size, y2=self.y + self.size
)
2021-02-12 13:09:45 +01:00
def render(self) -> str:
return (
f"<rect "
2021-02-12 13:43:19 +01:00
f'width="{self.size}" '
f'height="{self.size}" '
2021-02-12 13:09:45 +01:00
f'x="{self.x}" '
f'y="{self.y}" '
f'fill="{self.color}"/>'
)
@dataclass
class Text(Shape):
x: int
y: int
text: str
@property
2021-02-12 13:43:19 +01:00
def bounds(self) -> BoundingBox:
return BoundingBox(
x1=self.x, y1=self.y, x2=self.x, y2=self.y + TEXT_SIZE
)
2021-02-12 13:09:45 +01:00
def render(self) -> str:
return (
2021-02-12 13:43:19 +01:00
f'<text alignment-baseline="central" '
f'x="{self.x}" y="{self.y + TEXT_SIZE/2}" '
2021-02-12 13:09:45 +01:00
f'style="font-family: sans-serif; font-size: {TEXT_SIZE}px">'
f"{self.text}"
f"</text>"
)
2021-02-12 13:43:19 +01:00
@dataclass
class LegendText(Shape):
x: int
y: int
color: str
text: str
@property
def _square(self) -> Square:
return Square(
x=self.x,
y=self.y + (TEXT_SIZE - LEGEND_SQUARE_SIZE) / 2,
color=self.color,
size=LEGEND_SQUARE_SIZE,
)
@property
def _text(self) -> Text:
return Text(
x=LEGEND_SQUARE_SIZE + TEXT_MARGIN,
y=self.y,
text=self.text,
)
@property
def bounds(self) -> BoundingBox:
return get_common_bbox([self._square, self._text])
def render(self) -> str:
return self._square.render() + self._text.render()
2021-02-12 13:09:45 +01:00
def render_svg(functions: T.List[Function]) -> T.Iterable[Shape]:
for i, function in enumerate(functions):
2021-02-12 13:43:19 +01:00
x = (i % GRID_MAX_SQUARES) * (GRID_SQUARE_SIZE + GRID_SQUARE_MARGIN)
y = (i // GRID_MAX_SQUARES) * (GRID_SQUARE_SIZE + GRID_SQUARE_MARGIN)
2021-02-12 13:09:45 +01:00
if function.is_decompiled:
color = COLOR_DECOMPILED
else:
color = COLOR_TODO
yield Square(x=x, y=y, color=color)
2021-02-12 13:43:19 +01:00
y += GRID_SQUARE_SIZE + LEGEND_MARGIN
2021-02-12 13:09:45 +01:00
ready_functions = sum(function.is_decompiled for function in functions)
total_functions = len(functions)
ready_size = sum(
function.size for function in functions if function.is_decompiled
)
total_size = sum(function.size for function in functions)
2021-02-11 00:26:17 +01:00
2021-02-12 13:43:19 +01:00
for (color, text) in [
(
COLOR_DECOMPILED,
(
f"Functions decompiled (count): "
f"{ready_functions/total_functions:.02%}"
),
),
(
COLOR_DECOMPILED,
f"Functions decompiled (bytesize): {ready_size/total_size:.02%}",
),
(
COLOR_TODO,
(
f"Functions remaining (count): "
f"{(total_functions-ready_functions)/total_functions:.02%}"
),
),
(
COLOR_TODO,
(
f"Functions remaining (bytesize): "
f"{(total_size-ready_size)/total_size:.02%}"
),
),
]:
yield LegendText(
x=0,
y=y,
color=color,
text=text,
)
y += TEXT_SIZE + LEGEND_ROW_PADDING
2021-02-11 00:26:17 +01:00
2021-02-12 13:09:45 +01:00
def main() -> None:
functions = list(collect_functions())
2021-02-11 00:26:17 +01:00
with PROGRESS_SVG_FILE.open("w") as handle:
2021-02-12 13:09:45 +01:00
shapes = list(render_svg(functions))
2021-02-12 13:43:19 +01:00
bbox = get_common_bbox(shapes)
2021-02-12 13:09:45 +01:00
2021-02-11 00:26:17 +01:00
print(
f'<svg version="1.1" '
2021-02-12 13:43:19 +01:00
f'width="{bbox.x2}" '
f'height="{bbox.y2}" '
2021-02-11 00:26:17 +01:00
f'xmlns="http://www.w3.org/2000/svg">',
file=handle,
)
2021-02-12 13:09:45 +01:00
for shape in shapes:
print(shape.render(), file=handle)
2021-02-11 00:26:17 +01:00
print("</svg>", file=handle)
if __name__ == "__main__":
main()