From fdc6ae3f225e6ca2a5ef378212ddec3c1741c31f Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Tue, 4 Sep 2018 20:07:05 -0400 Subject: [PATCH] Recache on file updates Unfortunately, packed images don't appear to store any kind of modify time attribute. In the case of those, we're just idly hoping that we can find one on disk. --- korman/exporter/image.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/korman/exporter/image.py b/korman/exporter/image.py index 1762f7e..dc396f7 100644 --- a/korman/exporter/image.py +++ b/korman/exporter/image.py @@ -16,6 +16,7 @@ import enum from pathlib import Path from PyHSPlasma import * +import time import weakref _HEADER_MAGICK = b"KTH\x00" @@ -44,6 +45,7 @@ class _EntryBits(enum.IntEnum): compression = 3 source_size = 4 export_size = 5 + last_export = 6 class _CachedImage: @@ -55,6 +57,8 @@ class _CachedImage: self.source_size = None self.export_size = None self.compression = None + self.export_time = None + self.modify_time = None def __str__(self): return self.name @@ -127,6 +131,19 @@ class ImageCache: if tuple(bl_image.size) != cached_image.source_size: return None + # if the image is on the disk, we can check the its modify time for changes + if cached_image.modify_time is None: + # if the image is packed, the filepath will be some garbage beginning with + # the string "//". There isn't much we can do with that, unless the user + # happens to have an unpacked copy lying around somewheres... + path = Path(bl_image.filepath_from_user()) + if path.is_file(): + cached_image.modify_time = path.stat().st_mtime + if cached_image.export_time and cached_image.export_time < cached_image.modify_time: + return None + else: + cached_image.modify_time = 0 + # ensure the data has been loaded from the cache if cached_image.image_data is None: try: @@ -231,6 +248,8 @@ class ImageCache: image.source_size = (stream.readInt(), stream.readInt()) if flags[_EntryBits.export_size]: image.export_size = (stream.readInt(), stream.readInt()) + if flags[_EntryBits.last_export]: + image.export_time = stream.readDouble() # do we need to check for duplicate images? self._images[(image.name, image.compression)] = image @@ -307,6 +326,7 @@ class ImageCache: flags[_EntryBits.compression] = True flags[_EntryBits.source_size] = True flags[_EntryBits.export_size] = True + flags[_EntryBits.last_export] = True stream.write(_ENTRY_MAGICK) flags.write(stream) @@ -318,3 +338,4 @@ class ImageCache: stream.writeInt(image.source_size[1]) stream.writeInt(image.export_size[0]) stream.writeInt(image.export_size[1]) + stream.writeDouble(time.time())