"""Check that every Dear ImGui function the engine calls exists in our cimgui.dll. ImGui.NET binds native functions by name and lazily, so a call to a function the DLL doesn't export (our build compiles out obsolete APIs, e.g. GetContentRegionMax in 1.91) compiles fine and only fails - on AC's render thread - the first time that line runs. Run after adding ImGui code: python tools/check_imgui_calls.py Exit code 1 lists the calls with no matching export. It also flags ImGui.Text / TextColored / TextWrapped / TextDisabled / BulletText / SetTooltip / SetItemTooltip / LabelText(value) calls anywhere in the engine whose text isn't a plain literal: ImGui.NET passes that text to native ImGui as a printf format, so a '%' in it reads missing arguments. Such text goes through SafeImGui instead. To check another tree (a RynthSuite checkout's Plugins folder) for those calls only: python tools/check_imgui_calls.py C:/path/to/RynthSuite/Plugins """ import re import struct import sys from pathlib import Path ROOT = Path(__file__).resolve().parent.parent DLL = ROOT / "src" / "RynthCore.Engine" / "Native" / "cimgui.dll" ENGINE = ROOT / "src" / "RynthCore.Engine" SOURCES = ENGINE / "ImGui" # Old diagnostic shell, FORCE-gated and never run: not held to this check. SKIP = {"RynthCoreShell.cs"} # Wrapper-object calls: variable/field name -> cimgui prefix. MEMBER_PREFIX = { "dl": "ImDrawList_", "clipper": "ImGuiListClipper_", "io.Fonts": "ImFontAtlas_", "style": "ImGuiStyle_", } def pe_exports(path: Path) -> set[str]: data = path.read_bytes() pe = struct.unpack_from(" int: for va, size, raw in sections: if va <= rva < va + size: return rva - va + raw raise ValueError(hex(rva)) e = off(exp_rva) count, names_rva = struct.unpack_from("= 3: # raw string literal j = q + n close = '"' * n while j < len(s) and not s.startswith(close, j): if dollars and s.startswith("{" * dollars, j): j = skip_code(s, j + dollars, "}") + 1 else: j += 1 return j + n j = q + 1 while j < len(s): ch = s[j] if ch == "\\" and not verbatim: j += 2 elif ch == '"': if verbatim and s.startswith('""', j): j += 2 else: return j + 1 elif dollars and ch == "{": if s.startswith("{{", j): j += 2 else: j = skip_code(s, j + 1, "}") + 1 else: j += 1 return j def skip_code(s: str, i: int, closer: str) -> int: """Index of the closer that ends the code starting at i (nesting, strings and comments skipped).""" depth = 0 j = i while j < len(s): end = skip_token(s, j) if end is not None: j = end continue ch = s[j] if ch in "([{": depth += 1 elif ch in ")]}": if depth == 0 and ch == closer: return j depth -= 1 j += 1 return j def call_args(s: str, open_paren: int): """[(start, end)] of each top-level argument of the call whose '(' is at open_paren.""" args = [] depth = 0 start = open_paren + 1 j = start while j < len(s): end = skip_token(s, j) if end is not None: j = end continue ch = s[j] if ch in "([{": depth += 1 elif ch in ")]}": if depth == 0: args.append((start, j)) return args, j depth -= 1 elif ch == "," and depth == 0: args.append((start, j)) start = j + 1 j += 1 return args, j def literal_kind(arg: str) -> str: """'literal' for plain string literals (joined with '+' at most), 'percent' for such literals holding a lone '%', else 'expression'.""" j = 0 pieces = [] while j < len(arg): ch = arg[j] if ch.isspace() or ch == "+": j += 1 continue end = skip_token(arg, j) if end is None: return "expression" piece = arg[j:end] if not (piece.startswith("//") or piece.startswith("/*")): if piece[0] not in '"@$' or "$" in piece[:piece.index('"')]: return "expression" pieces.append(piece) j = end if not pieces: return "expression" return "percent" if any("%" in p.replace("%%", "") for p in pieces) else "literal" def format_text_calls(text: str): """Every format-text ImGui call in C# source: (match, arg_start, arg_end, kind). Calls inside comments and strings (e.g. Lua script text) are skipped.""" code = [] j = 0 while j < len(text): end = skip_token(text, j) if end is None: code.append(text[j]) j += 1 else: code.append("".join(c if c == "\n" else " " for c in text[j:end])) j = end masked = "".join(code) for m in FORMAT_CALL_RE.finditer(masked): args, _ = call_args(text, m.end() - 1) index = FORMAT_TEXT_CALLS[m.group(1)] if index >= len(args): yield m, m.end(), m.end(), "expression" continue a, b = args[index] yield m, a, b, literal_kind(text[a:b]) def unsafe_format_text(roots) -> list[str]: found = [] for root in roots: for f in sorted(Path(root).rglob("*.cs")): if f.name in FORMAT_SKIP or {"bin", "obj"} & set(f.parts): continue text = f.read_text(encoding="utf-8-sig") for m, a, b, kind in format_text_calls(text): if kind == "literal": continue line = text.count("\n", 0, m.start()) + 1 what = "a literal with a lone '%'" if kind == "percent" else "text that isn't a literal" found.append(f"{f}:{line}: ImGui.{m.group(1)} with {what}: {' '.join(text[a:b].split())[:80]}") return found def main() -> int: # python tools/check_imgui_calls.py DIR... checks only the format text under DIR # (e.g. a RynthSuite checkout's Plugins folder). if len(sys.argv) > 1: unsafe = unsafe_format_text(sys.argv[1:]) if unsafe: print("ImGui text calls that draw non-literal text as a printf format (use SafeImGui):") print("\n".join(" " + u for u in unsafe)) return 1 print("OK: no ImGui text call draws non-literal text as a format.") return 0 exports = pe_exports(DLL) def found(prefix: str, name: str) -> bool: full = prefix + name return full in exports or any(x.startswith(full + "_") for x in exports) missing = [] for f in sorted(SOURCES.rglob("*.cs")): if f.name in SKIP: continue text = f.read_text(encoding="utf-8") for m in re.finditer(r"(?