Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions tests/test_bink_play.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""tools/bink_play.py: name matching and the name-table cache. Synthetic names only, no game files."""
import sys
from io import StringIO
import tempfile
import threading
import unittest
Expand Down Expand Up @@ -177,5 +178,71 @@ def look_up(mount):
self.assertEqual(sorted(p.name for p in (d / "cache").iterdir()), ["bink_names.csv"])


class NoCacheTest(unittest.TestCase):
def setUp(self):
temporary = tempfile.TemporaryDirectory()
self.addCleanup(temporary.cleanup)
self.directory = Path(temporary.name)
self.archive = self.directory / "streams.csv"
self.mount = self.directory / "mount.xml"
self.archive.write_text("source,offset,size,width,height,frames\nsample,32,64,16,16,2\n",
encoding="utf-8", newline="\n")
self.mount.write_text("<filename>$(common)/movies/synthetic.bk2</filename>",
encoding="utf-8", newline="\n")
self.cache = self.directory / "cache" / "names.csv"
self.expected = [("synthetic.bk2", 32, "16", "16", "2")]

def test_cli_lists_without_accessing_an_unusable_cache_directory(self):
self.cache.parent.write_bytes(b"unrelated file, not a directory")
with mock.patch.object(bink_play, "cache_path", return_value=self.cache), \
mock.patch.object(sys, "stdout", new_callable=StringIO) as stdout:
rc = bink_play.main(["--game-dir", str(self.directory), "--archive", self.archive.name,
"--mount", self.mount.name, "--no-cache", "--list"])
self.assertEqual(rc, 0)
self.assertIn("synthetic.bk2 16x16, 2 frames", stdout.getvalue().splitlines())
self.assertEqual(self.cache.parent.read_bytes(), b"unrelated file, not a directory")

def test_no_cache_does_not_create_a_persistent_cache_directory(self):
with mock.patch.object(bink_play, "cache_path", return_value=self.cache):
self.assertEqual(bink_play.load_names(self.archive, self.mount, use_cache=False), self.expected)
self.assertFalse(self.cache.parent.exists())

def test_existing_cache_is_untouched_and_temporary_report_is_removed(self):
self.cache.parent.mkdir()
self.cache.write_bytes(b"existing cache must remain unchanged")
reports = []
real_names = bink_play.bink_names.main

def generate(argv):
reports.append(Path(argv[argv.index("--out") + 1]))
return real_names(argv)

with mock.patch.object(bink_play, "cache_path", return_value=self.cache) as cache_path, \
mock.patch.object(bink_play.bink_names, "main", side_effect=generate):
self.assertEqual(bink_play.load_names(self.archive, self.mount, use_cache=False), self.expected)
cache_path.assert_not_called()
self.assertEqual(self.cache.read_bytes(), b"existing cache must remain unchanged")
self.assertEqual(list(self.cache.parent.iterdir()), [self.cache])
self.assertEqual(len(reports), 1)
self.assertFalse(reports[0].exists())

def test_failed_no_cache_generation_removes_temporary_report(self):
reports = []

def fail(argv):
path = Path(argv[argv.index("--out") + 1])
reports.append(path)
path.write_bytes(b"partial report")
raise OSError("synthetic report failure")

with mock.patch.object(bink_play, "cache_path", return_value=self.cache), \
mock.patch.object(bink_play.bink_names, "main", side_effect=fail):
with self.assertRaisesRegex(OSError, "synthetic report failure"):
bink_play.load_names(self.archive, self.mount, use_cache=False)
self.assertEqual(len(reports), 1)
self.assertFalse(reports[0].exists())
self.assertFalse(self.cache.parent.exists())


if __name__ == "__main__":
unittest.main()
22 changes: 13 additions & 9 deletions tools/bink_play.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

``--game-dir`` defaults to ``LEONIDA_GAME_DIR``. Naming the streams takes about half a minute, so the name table
(names, offsets and frame sizes only, no movie data) is kept in ``%LOCALAPPDATA%\\Leonida\\bink_names.csv`` and rebuilt
when the archive or mount source changes, or the cached table cannot be parsed. ``--no-cache`` skips it.
when the archive or mount source changes, or the cached table cannot be parsed. ``--no-cache`` skips it and
keeps its temporary report in the system temporary directory instead of the persistent cache directory.
The names are a local work product: don't commit or post them.
"""
import argparse
Expand Down Expand Up @@ -62,15 +63,18 @@ def _read_cache(cache, key):

def load_names(archive, mount, use_cache=True):
"""[(path, offset, width, height, frames)] for the streams of ``archive``, named from ``mount``."""
cache = cache_path()
key = stamp(archive) + "|" + stamp(mount) # the names come from the mount lists too
if use_cache and cache.is_file():
rows = _read_cache(cache, key)
if rows is not None:
return rows
cache.parent.mkdir(parents=True, exist_ok=True)
report_dir = None
if use_cache:
cache = cache_path()
key = stamp(archive) + "|" + stamp(mount) # the names come from the mount lists too
if cache.is_file():
rows = _read_cache(cache, key)
if rows is not None:
return rows
cache.parent.mkdir(parents=True, exist_ok=True)
report_dir = cache.parent
# Each lookup gets its own report and staging file: two runs at once must not remove or replace each other's.
fd, name = tempfile.mkstemp(prefix="bink_names.", suffix=".tmp.csv", dir=cache.parent)
fd, name = tempfile.mkstemp(prefix="bink_names.", suffix=".tmp.csv", dir=report_dir)
os.close(fd)
tmp = Path(name)
try:
Expand Down