Multi-threading preference

* Implemented multi-threading preference option
* Adjusted code to make asynchronous calls optional
* Fixed initialization order for certain tasks
This commit is contained in:
LoafyLemon 2023-07-14 02:33:58 +01:00
parent 4b1b01eb09
commit b6c77b1992
8 changed files with 50 additions and 20 deletions

View File

@ -281,11 +281,19 @@ init python:
thread = DollThread(target=_func, args=(self, self._hash))
thread.start()
@property
def button(self):
return self._button.get_with_default(self._loading)
if settings.get("multithreading"):
@property
def button(self):
return self._button.get_with_default(self._loading)
else:
@property
def button(self):
return self._build_button(self._hash)
def clear_button_cache(self):
if not settings.get("multithreading"):
return
self.build_button.cache_clear()
def apply_color(self, img, n, maxsize):

View File

@ -79,6 +79,7 @@ init -1 python:
for i in states.dolls:
doll = getattr(store, i)
doll.build_image()
renpy.restart_interaction()
config.after_load_callbacks.append(DollRebuild)
# end_skip_callbacks.append(DollRebuild)

View File

@ -141,17 +141,22 @@ init python:
return Fixed(*sprites, self.emote, fit_first=True)
def build_image(self):
if settings.get("multithreading"):
def build_image(self):
def _func(self, hash):
result = self._build_image(hash)
self._sprite.put(result)
def _func(self, hash):
result = self._build_image(hash)
self._sprite.put(result)
thread = DollThread(target=_func, args=(self, self._hash))
thread.start()
thread = DollThread(target=_func, args=(self, self._hash))
thread.start()
else:
def build_image(self):
self._sprite.put(self._build_image(self._hash))
renpy.restart_interaction()
def _image(self, st, at):
return self._sprite.get_with_default(None), None
return self._sprite.get_with_default(Null()), None
@property
def image(self):
@ -619,6 +624,8 @@ init python:
self.is_stale()
def clear_outfit_button_cache(self):
if not settings.get("multithreading"):
return
DollThread.stop_all()

View File

@ -180,9 +180,15 @@ init python:
thread = DollThread(target=_func, args=(self, self._hash, subcat))
thread.start()
@property
def button(self):
return self._button.get_with_default(self._loading)
if settings.get("multithreading"):
@property
def button(self):
return self._button.get_with_default(self._loading)
else:
@property
def button(self):
global current_subcategory
return self._build_button(self._hash, current_subcategory)
def clear_button_cache(self):
self.build_button.cache_clear()

View File

@ -166,6 +166,7 @@ screen preferences_visuals():
textbutton _("Transitions") action Preference("transitions", "toggle")
textbutton _("Videos") action InvertSelected(Preference("video sprites", "toggle"))
textbutton _("Power-saving") action Preference("gl powersave", "toggle")
textbutton _("multithreading") action settings.Toggle("multithreading") tooltip "Improves performance by executing tasks asynchronously. (Requires restart)"
#if not renpy.mobile:
#textbutton _("Preserve Aspect Ratio") action [settings.Toggle("preserve_aspect_ratio"), _DisplayReset()]

View File

@ -10,7 +10,7 @@ default preferences.renderer = "auto"
default preferences.gl_powersave = False
default preferences.audio_when_minimized = False
init python:
init -5 python:
settings.default("theme", "auto")
settings.default("text_color_day", "#402313ff")
settings.default("text_color_night", "#341c0fff")
@ -21,6 +21,7 @@ init python:
settings.default("animations", True)
settings.default("updates", True)
settings.default("image_cache_size", 512)
settings.default("multithreading", True)
renpy.music.register_channel("background", "sfx", True)
renpy.music.register_channel("sound2", "sfx", False)

View File

@ -6,7 +6,7 @@ init offset = -10
default persistent.custom_settings = {}
default persistent.custom_settings_default = {}
init python in settings:
init -10 python in settings:
from store import persistent, Action, DictEquality
not_set = object()

View File

@ -88,10 +88,14 @@ init python:
rebuild_wardrobe_icons(category_items, current_subcategory)
thread = DollThread(target=_func, args=(cloth,), interval=0.05)
thread.start()
if settings.get("multithreading"):
thread = DollThread(target=_func, args=(cloth,), interval=0.05)
thread.start()
else:
_func(cloth)
renpy.restart_interaction()
@functools.cache # Cache resets on wardrobe exit
# @functools.cache # Cache resets on wardrobe exit
def set_wardrobe_categories(current_category):
category_items = OrderedDict(
sorted(
@ -425,6 +429,8 @@ label wardrobe_menu():
current_item = char_active.get_equipped_wardrobe_item(category_items, current_subcategory)
category_items = set_wardrobe_categories(current_category)
elif _choice[0] == "deloutfit":
python:
_confirmed = wardrobe_suppress_warnings or renpy.call_screen("confirm", "Delete this outfit?")
@ -580,7 +586,7 @@ label wardrobe_menu():
renpy.music.play(last_track)
DollThread.stop_all()
set_wardrobe_categories.cache_clear()
# set_wardrobe_categories.cache_clear()
char_active.build_image()
enable_game_menu()