Implement disk cache for renders

This commit is contained in:
LoafyLemon 2024-09-10 14:31:48 +01:00
parent 1571daa6f7
commit 83383f71f7
4 changed files with 55 additions and 5 deletions

View File

@ -176,6 +176,9 @@ init python:
@functools.cache
def build_icon(self, hash):
if (d := self.get_disk_cache(hash)):
return d
matrix = SaturationMatrix(0.0)
sprites = [i for i in self.build_image(hash, matrix=matrix) if not i[0] == "mask"]
@ -205,7 +208,8 @@ init python:
if y+h > hmax:
y = hmax-h
return Transform(Fixed(*[i[1] for i in sprites], fit_first=True), crop=(x, y, w, h), size=(96, 96), fit="contain", align=(0.5, 0.5))
d = Transform(Fixed(*[i[1] for i in sprites], fit_first=True), crop=(x, y, w, h), size=(256, 256), fit="contain", align=(0.5, 0.5))
return self.create_disk_cache(d, hash)
@property
def icon(self):
@ -269,7 +273,7 @@ init python:
foreground = "#b2000040"
hover_foreground = "#CD5C5C40"
return Button(child=child, focus_mask=None, xysize=(96, 96), background=self.icon, action=action, tooltip=("\n".join(warnings)), foreground=foreground, hover_foreground=hover_foreground, unhovered=unhovered, style=style)
return Button(child=child, focus_mask=None, xysize=(96, 96), background=Transform(self.icon, xysize=(96, 96)), action=action, tooltip=("\n".join(warnings)), foreground=foreground, hover_foreground=hover_foreground, unhovered=unhovered, style=style)
@functools.cache
def build_button(self, _=None):

View File

@ -164,6 +164,9 @@ init python:
else:
tracking_object = self.tracking_object
if (d := self.get_disk_cache(hash)):
return d
matrix = SaturationMatrix(0.0)
sprites = [i for i in self.build_image(hash, matrix=matrix) if not i[0] == "mask"]
@ -192,7 +195,8 @@ init python:
if y+h > hmax:
y = hmax-h
return Transform(Fixed(*[i[1] for i in sprites], fit_first=True), crop=(x, y, w, h), size=(96, 96), fit="contain", align=(0.5, 0.5))
d = Transform(Fixed(*[i[1] for i in sprites], fit_first=True), crop=(x, y, w, h), size=(256, 256), fit="contain", align=(0.5, 0.5))
return self.create_disk_cache(d, hash)
def clone(self):
"""Creates a clone of this cloth object. Since it requires a parent object it should be used internally only to avoid object depth issue."""

View File

@ -71,10 +71,48 @@ init -1 python:
def is_stale(self):
curr_hash = self.generate_hash()
stale = curr_hash != self._hash
if (stale := curr_hash != self._hash):
self.remove_disk_cache(self._hash)
self._hash = curr_hash
return stale
def create_disk_cache(self, d, hash, size=None, type="img"):
width, height = size or self.sizes
filepath = os.path.join("cache", type, f"{hash}.png")
rendpath = os.path.join("game", filepath)
syspath = os.path.join(config.gamedir, "cache", type)
try:
os.makedirs(syspath, exist_ok=True)
except OSError as e:
print(f"Warning! Failed to create cache directory: {cache_dir} - {e}")
return d
try:
if not renpy.loadable(filepath):
renpy.render_to_file(d, rendpath, width=width, height=height, resize=True)
return Image(filepath)
except Exception as e:
print(f"Warning! Failed to return cached file: {filepath} - {e}")
return d
def get_disk_cache(self, hash, type="img"):
filepath = os.path.join("cache", type, f"{hash}.png")
return Image(filepath) if renpy.loadable(filepath) else None
def remove_disk_cache(self, hash, type="img"):
syspath = os.path.join(config.gamedir, "cache", type, f"{hash}.png")
try:
os.unlink(syspath)
except FileNotFoundError:
print(f"Warning! Cached file {syspath} not found")
except PermissionError:
print(f"Warning! Permission denied to remove cached file: {syspath}")
except Exception as e:
print(f"Warning! Failed to remove cached file: {syspath} - {e}")
def DollRebuild():
for i in states.dolls:
doll = getattr(store, i)

View File

@ -46,6 +46,9 @@ init python:
@functools.cache
def build_image(self, hash):
if (d := self.get_disk_cache(hash)):
return d
from itertools import chain
matrix = SaturationMatrix(0.0)
@ -79,7 +82,8 @@ init python:
sprites = back_sprites + [x[1] for x in sprites]
return Fixed(*sprites, fit_first=True)
d = Fixed(*sprites, fit_first=True)
return self.create_disk_cache(d, hash)
@property
def image(self):