176 lines
7.0 KiB
Python
176 lines
7.0 KiB
Python
# G0.4 verification: close GUI -> preset save/restore around reload().
|
|
# Flow: LOAD ch5 Nexus -> OPEN_GUI -> WM_CLOSE -> expect "state captured ch=5"
|
|
# -> OPEN_GUI again -> expect "state restored ch=5" + "GUI attached" (2nd time).
|
|
# No EXCEPTION/FAILED/bridge restart.
|
|
import ctypes, ctypes.wintypes as w, struct, subprocess, sys, os, time, re
|
|
|
|
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
|
|
|
|
APP_PID = 25488 # 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")
|
|
|
|
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:
|
|
r = subprocess.run(['tasklist'], capture_output=True)
|
|
txt = r.stdout.decode('utf-8', 'replace')
|
|
for line in txt.splitlines():
|
|
if 'daw_vst_bridge' in line.lower():
|
|
return line.split()[1]
|
|
except Exception:
|
|
pass
|
|
return None
|
|
|
|
def watch_restart(since, pid0, seconds=15):
|
|
end = time.time() + seconds
|
|
while time.time() < end:
|
|
for l in log_lines()[since:]:
|
|
if "bridge restarted" in l or "EXCEPTION" in l or "FAILED" in l:
|
|
return l.strip()
|
|
pid = bridge_pid()
|
|
if pid != pid0:
|
|
return f"BRIDGE PID CHANGE {pid0}->{pid}"
|
|
time.sleep(0.2)
|
|
return None
|
|
|
|
for attempt in range(1, 4):
|
|
print(f"\n===== ATTEMPT {attempt} =====")
|
|
pid0 = bridge_pid()
|
|
print("bridge pid0:", pid0)
|
|
bad = []
|
|
# 1) ensure ch=5 loaded
|
|
since = len(log_lines())
|
|
push_controls([(2, 0, 0, 5, NEXUS)])
|
|
r = wait_log("instrument loaded ch=5", 90, since)
|
|
print("LOAD ch5:", r or "TIMEOUT")
|
|
# 2) STOP transport (idle state)
|
|
since = len(log_lines())
|
|
push_controls([(3, 0, 0, 0, "")])
|
|
wait_log("transport STOP", 10, since)
|
|
# 3) OPEN_GUI ch=5
|
|
since = len(log_lines())
|
|
push_controls([(4, 0, 0, 5, "Nexus")])
|
|
r = wait_log("GUI attached hwnd=", 60, since)
|
|
print("G0.4 open #1:", r or "TIMEOUT - attach khong xay ra")
|
|
m = re.search(r'hwnd=([0-9A-Fa-f]+)', r or "")
|
|
hwnd = int(m.group(1), 16) if m else None
|
|
restart = watch_restart(since, pid0)
|
|
if restart:
|
|
print(" restart during open #1:", restart); bad.append(restart); time.sleep(5); continue
|
|
if not hwnd:
|
|
bad.append("attach #1 timeout"); time.sleep(2); continue
|
|
time.sleep(3)
|
|
# 4) WM_CLOSE -> close job runs captureState -> reload -> restoreState
|
|
# (all 3 lines appear ~simultaneously; use one shared since window)
|
|
since = len(log_lines())
|
|
ctypes.windll.user32.SendMessageW(hwnd, 0x0010, 0, 0)
|
|
print(f" sent WM_CLOSE hwnd={hwnd:#x}")
|
|
r = wait_log("state captured ch=5", 60, since)
|
|
print("G0.4 capture:", r or "TIMEOUT - khong captureState khi close GUI")
|
|
cap_line = r
|
|
if not r: bad.append("no capture")
|
|
restart = watch_restart(since, pid0)
|
|
if restart:
|
|
print(" restart during close:", restart); bad.append(restart); time.sleep(5); continue
|
|
# 5) reload + restore also inside the same window
|
|
r = wait_log("[Vst3Instrument] loaded", 60, since)
|
|
print("G0.4 reload after close:", r or "TIMEOUT - reload khong xay ra")
|
|
if not r: bad.append("no reload after close")
|
|
r = wait_log("state restored ch=5", 60, since)
|
|
print("G0.4 restore:", r or "TIMEOUT - khong restoreState khi reopen")
|
|
if not r: bad.append("no restore")
|
|
# same component size before/after = preset preserved
|
|
mcap = re.search(r'component=(\d+)B', cap_line or "")
|
|
mres = re.search(r'component=(\d+)B', r or "")
|
|
if mcap and mres and mcap.group(1) != mres.group(1):
|
|
bad.append(f"preset size diff capture={mcap.group(1)} restore={mres.group(1)}")
|
|
# 6) OPEN_GUI again -> attach succeeds after restore
|
|
since = len(log_lines())
|
|
push_controls([(4, 0, 0, 5, "Nexus")])
|
|
r = wait_log("GUI attached hwnd=", 60, since)
|
|
print("G0.4 open #2:", r or "TIMEOUT - attach lan 2 khong xay ra")
|
|
if not r: bad.append("attach #2 timeout")
|
|
restart = watch_restart(since, pid0)
|
|
if restart:
|
|
print(" restart during open #2:", restart); bad.append(restart); time.sleep(5); continue
|
|
# 7) watch 10s for EXCEPTION/FAILED/restart
|
|
since = len(log_lines())
|
|
time.sleep(10)
|
|
for l in log_lines()[since:]:
|
|
if any(k in l for k in ("EXCEPTION", "FAILED", "bridge restarted")):
|
|
bad.append(l.strip())
|
|
pid = bridge_pid()
|
|
if pid != pid0:
|
|
bad.append(f"BRIDGE RESTART {pid0}->{pid}")
|
|
print("G0.4 RESULT:", "PASS - save/restore preset OK, GUI reopen OK" if not bad else "FAIL:\n" + "\n".join(bad[:10]))
|
|
if not bad:
|
|
break
|
|
else:
|
|
print("\nALL ATTEMPTS FAILED - attach treo intermittent (bug G1), G0.4 chua verify duoc")
|