Files

110 lines
4.1 KiB
Python

# Test A: load 6 Nexus + EZkeys, KHONG PLAY, OPEN_GUI ch5 -> attach co OK khong?
import ctypes, ctypes.wintypes as w, struct, subprocess, sys, os, time
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
APP_PID = 23760
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:
hMap = kernel32.OpenFileMappingA(FILE_MAP_ALL_ACCESS, False, SHM_NAME_LEGACY)
name_used = SHM_NAME_LEGACY
if not hMap:
print("no SHM", 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
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 ch in range(7):
r = wait_log(f"instrument loaded ch={ch}", 60, since)
print(f" ch={ch}: {'loaded' if r else 'TIMEOUT'}")
print("Khong PLAY - mo GUI ch5 ngay (transport stopped)")
since = len(log_lines())
push_controls([(4, 0, 0, 5, "Nexus")])
pid0 = bridge_pid()
r = wait_log("GUI attached hwnd=", 40, since)
print("ATTACH:", r or "TIMEOUT - GUI khong attach")
end = time.time() + 8
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("RESULT:", "PASS - attach OK khi STOP, khong loi" if (r and not bad) else "FAIL: " + "; ".join(bad[:5]) if bad else "FAIL - attach TIMEOUT")