KAZIK/tools/preview_dbd_hook.py

59 lines
1.6 KiB
Python

from pathlib import Path
import ctypes
import os
import sys
import time
FRAME_DELAY = 0.08
FRAME_SEPARATOR = "\f"
def place_window() -> None:
try:
user32 = ctypes.windll.user32
hwnd = ctypes.windll.kernel32.GetConsoleWindow()
if hwnd:
screen_w = user32.GetSystemMetrics(0)
x = 720 if screen_w >= 1500 else max(420, screen_w // 2)
y = 40
# Do not resize here: `mode con` controls the character grid.
# Pixel resizing can make ANSI frames wrap and visually slide.
user32.SetWindowPos(hwnd, 0, x, y, 0, 0, 0x0001)
except Exception:
pass
def main() -> int:
root = Path(__file__).resolve().parent.parent
path = root / "assets" / "animations" / "dbd_hook.ans"
stop_path = root / "runtime" / "dbd_hook.stop"
if not path.exists():
print("dbd_hook.ans not found")
return 1
if stop_path.exists():
stop_path.unlink()
os.system("chcp 65001 >nul")
place_window()
frames = path.read_text(encoding="utf-8", errors="replace").split(FRAME_SEPARATOR)
try:
while not stop_path.exists():
for frame in frames:
if stop_path.exists():
break
os.system("cls")
sys.stdout.write(frame)
sys.stdout.flush()
time.sleep(FRAME_DELAY)
finally:
sys.stdout.write("\033[0m")
sys.stdout.flush()
if stop_path.exists():
stop_path.unlink()
return 0
if __name__ == "__main__":
raise SystemExit(main())