From a918901710c8de23089678a7180ee135023ad8ef Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Deuc=D0=B5?= <shurd@sasktel.net>
Date: Fri, 18 Oct 2024 13:46:39 -0400
Subject: [PATCH] Ensure graphics are cached successfully.

It appears that cache operations in SyncTERM on Windows does not
currently work with subdirectories for some reason (likely
backslashes).  Ensure that after sending a file to be cached, it
correctly appears in the cache, and fall back to not using graphics.

This will fix the blank board issue in minesweeper with a Windows
SyncTERM and fallback to text mode.

Once SyncTERM is fixed, it should work in graphics mode too.
---
 xtrn/minesweeper/minesweeper.js | 57 ++++++++++++++++++++++++---------
 1 file changed, 41 insertions(+), 16 deletions(-)

diff --git a/xtrn/minesweeper/minesweeper.js b/xtrn/minesweeper/minesweeper.js
index 8ee300b33e..0dd6ac31d7 100644
--- a/xtrn/minesweeper/minesweeper.js
+++ b/xtrn/minesweeper/minesweeper.js
@@ -1166,6 +1166,7 @@ function detect_graphics()
 	var lst;
 	var m;
 	var b;
+	var sent;
 
 	// Detect PPM graphics and load the cache
 	graph = false;
@@ -1180,6 +1181,7 @@ function detect_graphics()
 		// Load up cache...
 		f = new File(js.exec_dir+'/'+gfile);
 		if (f.open('rb')) {
+			sent = false;
 			md5 = f.md5_hex;
 			console.write('\x1b_SyncTERM:C;L;minesweeper/'+gfile+'\x1b\\');
 			lst = read_apc();
@@ -1190,33 +1192,56 @@ function detect_graphics()
 				f.base64 = true;
 				console.write(f.read());
 				console.write('\x1b\\');
+				sent = true;
 			}
-			f.close();
-			console.write('\x1b_SyncTERM:C;LoadPPM;B=0;minesweeper/'+gfile+'\x1b\\');
-			f = new File(js.exec_dir+'/'+mfile);
-			if (f.open('rb')) {
-				md5 = f.md5_hex;
-				console.write('\x1b_SyncTERM:C;L;minesweeper/'+mfile+'\x1b\\');
+			if (sent) {
+				console.write('\x1b_SyncTERM:C;L;minesweeper/'+gfile+'\x1b\\');
 				lst = read_apc();
-				m = lst.match(/\nselmask.pbm\t([0-9a-f]+)\n/);
+				m = lst.match(/\ngraphics.ppm\t([0-9a-f]+)\n/);
 				if (m == null || m[1] !== md5) {
-					// Store in cache...
-					console.write('\x1b_SyncTERM:C;S;minesweeper/'+mfile+';');
-					f.base64 = true;
-					console.write(f.read());
-					console.write('\x1b\\');
+					graph = false;
 				}
-				f.close();
-				console.write('\x1b_SyncTERM:C;LoadPBM;minesweeper/'+mfile+'\x1b\\');
 			}
-			else {
-				graph = false;
+			f.close();
+			if (graph) {
+				sent = false;
+				f = new File(js.exec_dir+'/'+mfile);
+				if (f.open('rb')) {
+					md5 = f.md5_hex;
+					console.write('\x1b_SyncTERM:C;L;minesweeper/'+mfile+'\x1b\\');
+					lst = read_apc();
+					m = lst.match(/\nselmask.pbm\t([0-9a-f]+)\n/);
+					if (m == null || m[1] !== md5) {
+						// Store in cache...
+						console.write('\x1b_SyncTERM:C;S;minesweeper/'+mfile+';');
+						f.base64 = true;
+						console.write(f.read());
+						console.write('\x1b\\');
+						sent = true;
+					}
+					console.write('\x1b_SyncTERM:C;L;minesweeper/'+mfile+'\x1b\\');
+					lst = read_apc();
+					m = lst.match(/\nselmask.pbm\t([0-9a-f]+)\n/);
+					if (m == null || m[1] !== md5) {
+						graph = false;
+					}
+					f.close();
+				}
+				else {
+					graph = false;
+				}
 			}
 		}
 		else {
 			graph = false;
 		}
 	}
+
+	if (graph) {
+		console.write('\x1b_SyncTERM:C;LoadPPM;B=0;minesweeper/'+gfile+'\x1b\\');
+		console.write('\x1b_SyncTERM:C;LoadPBM;minesweeper/'+mfile+'\x1b\\');
+	}
+
 	console.ctrlkey_passthru = tmpckpt;
 	if (graph) {
 		console.mouse_mode = false;
-- 
GitLab