128 lines
4.9 KiB
Python
128 lines
4.9 KiB
Python
# G0.1 verification: PLAY -> OPEN_GUI must be DEFERRED (not attach-while-playing);
|
|
# after STOP the pending GUI must attach OK. No EXCEPTION/FAILED, no watchdog kill.
|
|
import ctypes, ctypes.wintypes as w, struct, subprocess, sys, os, time
|
|
|
|
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
|
|
|
|
APP_PID = 30228 # sonicforge-daw.exe (update per run)
|
|
SHM_NAME = f"SonicForge_DAW_IPC_{APP_PID}".encode()
|
|
SHM_NAME_LEGACY = b"SonicForge_DAW_IPC"
|
|
APP_LOG = os.path.join(os.environ.get('APPDATA', '.'), 'SonicForgeDAW', 'logs', 'bridge.log')
|
|
VST = r"C:\Program Files\Common Files\VST3"
|
|
NEXUS = os.path.join(VST, "Nexus.vst3")
|
|
EZKEYS = os.path.join(VST, "Toontrack", "EZkeys 2.vst3")
|
|
|
|
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
|
|
kernel32.OpenFileMappingA.argtypes = [ctypes.wintypes.DWORD, w.BOOL, ctypes.c_char_p]
|
|
kernel32.OpenFileMappingA.restype = ctypes.c_void_p
|
|
kernel32.MapViewOfFile.argtypes = [ctypes.c_void_p, ctypes.wintypes.DWORD, ctypes.wintypes.DWORD, ctypes.wintypes.DWORD, ctypes.c_size_t]
|
|
kernel32.MapViewOfFile.restype = ctypes.c_void_p
|
|
FILE_MAP_ALL_ACCESS = 0xF001F
|
|
|
|
hMap = kernel32.OpenFileMappingA(FILE_MAP_ALL_ACCESS, False, SHM_NAME)
|
|
name_used = SHM_NAME
|
|
if not hMap:
|
|
print(f"OpenFileMapping({SHM_NAME}) failed err={ctypes.get_last_error()}, trying legacy...")
|
|
hMap = kernel32.OpenFileMappingA(FILE_MAP_ALL_ACCESS, False, SHM_NAME_LEGACY)
|
|
name_used = SHM_NAME_LEGACY
|
|
if not hMap:
|
|
print(f"OpenFileMapping({SHM_NAME_LEGACY}) failed err={ctypes.get_last_error()}")
|
|
sys.exit(1)
|
|
ptr = kernel32.MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0)
|
|
if not ptr:
|
|
print("MapViewOfFile failed", ctypes.get_last_error()); sys.exit(1)
|
|
print(f"SHM {name_used} mapped")
|
|
|
|
def w32(off, fmt, *args):
|
|
ctypes.memmove(ptr + off, struct.pack(fmt, *args), struct.calcsize(fmt))
|
|
|
|
def rd32(off):
|
|
return ctypes.c_uint32.from_address(ptr + off).value
|
|
|
|
def write_control(ctype, arg0, arg1, ch, path):
|
|
for i in range(8):
|
|
off = 2836 + i * 1040
|
|
if rd32(off) == 0:
|
|
w32(off, 'IIII', ctype, arg0, arg1, ch)
|
|
if path:
|
|
b = path.encode('utf-8')[:1023] + b'\x00'
|
|
ctypes.memmove(ptr + off + 16, b, len(b))
|
|
return i
|
|
raise RuntimeError("control queue full")
|
|
|
|
def push_controls(events):
|
|
for i in range(8):
|
|
w32(2836 + i * 1040, 'I', 0)
|
|
for (ctype, arg0, arg1, ch, path) in events:
|
|
write_control(ctype, arg0, arg1, ch, path)
|
|
w32(11156, 'I', len(events))
|
|
|
|
def log_lines():
|
|
try:
|
|
with open(APP_LOG, 'r', encoding='utf-8', errors='replace') as f:
|
|
return f.readlines()
|
|
except FileNotFoundError:
|
|
return []
|
|
|
|
def wait_log(pattern, timeout, since, verbose=True):
|
|
end = time.time() + timeout
|
|
while time.time() < end:
|
|
lines = log_lines()
|
|
for i in range(since, len(lines)):
|
|
if pattern in lines[i]:
|
|
if verbose: print(f" LOG: {lines[i].strip()}")
|
|
return lines[i].strip()
|
|
time.sleep(0.2)
|
|
return None
|
|
|
|
def bridge_pid():
|
|
try:
|
|
out = subprocess.run(["cmd", "/c", "tasklist /FI \"IMAGENAME eq daw_vst_bridge.exe\" /FO CSV"],
|
|
capture_output=True, text=True).stdout
|
|
for line in out.splitlines()[1:]:
|
|
if "daw_vst_bridge" in line:
|
|
return line.split(',')[1].strip('"')
|
|
except Exception:
|
|
pass
|
|
return None
|
|
|
|
# 1) LOAD 6 Nexus ch0-5 + EZkeys ch6
|
|
since = len(log_lines())
|
|
push_controls([(2, 0, 0, ch, NEXUS) for ch in range(6)] + [(2, 0, 0, 6, EZKEYS)])
|
|
print("pushed LOAD x7; waiting for instruments...")
|
|
for ch in range(7):
|
|
r = wait_log(f"instrument loaded ch={ch}", 60, since)
|
|
print(f" ch={ch}: {'loaded' if r else 'TIMEOUT'}")
|
|
# 2) TRANSPORT PLAY
|
|
since = len(log_lines())
|
|
push_controls([(3, 1, 0, 0, "")])
|
|
r = wait_log("transport PLAY", 10, since)
|
|
print("transport:", r or "TIMEOUT")
|
|
# 3) OPEN_GUI ch=5 while PLAYING -> expect DEFERRED
|
|
since = len(log_lines())
|
|
push_controls([(4, 0, 0, 5, "Nexus")])
|
|
r = wait_log("GUI deferred ch=5", 10, since)
|
|
print("G0.1 defer:", r or "TIMEOUT - KHONG DEFER (bug chua fix)")
|
|
pid0 = bridge_pid()
|
|
# 4) TRANSPORT STOP -> pending GUI should attach now
|
|
since = len(log_lines())
|
|
push_controls([(3, 0, 0, 0, "")])
|
|
r = wait_log("transport STOP", 10, since)
|
|
print("transport:", r or "TIMEOUT")
|
|
r = wait_log("GUI attached hwnd=", 30, since)
|
|
print("G0.1 attach after STOP:", r or "TIMEOUT - attach khong xay ra")
|
|
# 5) watch 10s for EXCEPTION/FAILED/restart
|
|
since = len(log_lines())
|
|
end = time.time() + 10
|
|
bad = []
|
|
while time.time() < end:
|
|
for l in log_lines()[since:]:
|
|
if any(k in l for k in ("EXCEPTION", "FAILED", "restarted")):
|
|
bad.append(l.strip())
|
|
pid = bridge_pid()
|
|
if pid != pid0:
|
|
bad.append(f"BRIDGE RESTART {pid0}->{pid}")
|
|
break
|
|
time.sleep(0.2)
|
|
print("G0.1 RESULT:", "PASS - khong loi, attach OK sau STOP" if not bad else "FAIL:\n" + "\n".join(bad[:10]))
|