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 = DollThread(target=_func, args=(self, self._hash))
thread.start() thread.start()
@property if settings.get("multithreading"):
def button(self): @property
return self._button.get_with_default(self._loading) 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): def clear_button_cache(self):
if not settings.get("multithreading"):
return
self.build_button.cache_clear() self.build_button.cache_clear()
def apply_color(self, img, n, maxsize): def apply_color(self, img, n, maxsize):

View File

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

View File

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

View File

@ -180,9 +180,15 @@ init python:
thread = DollThread(target=_func, args=(self, self._hash, subcat)) thread = DollThread(target=_func, args=(self, self._hash, subcat))
thread.start() thread.start()
@property if settings.get("multithreading"):
def button(self): @property
return self._button.get_with_default(self._loading) 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): def clear_button_cache(self):
self.build_button.cache_clear() self.build_button.cache_clear()

View File

@ -166,6 +166,7 @@ screen preferences_visuals():
textbutton _("Transitions") action Preference("transitions", "toggle") textbutton _("Transitions") action Preference("transitions", "toggle")
textbutton _("Videos") action InvertSelected(Preference("video sprites", "toggle")) textbutton _("Videos") action InvertSelected(Preference("video sprites", "toggle"))
textbutton _("Power-saving") action Preference("gl powersave", "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: #if not renpy.mobile:
#textbutton _("Preserve Aspect Ratio") action [settings.Toggle("preserve_aspect_ratio"), _DisplayReset()] #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.gl_powersave = False
default preferences.audio_when_minimized = False default preferences.audio_when_minimized = False
init python: init -5 python:
settings.default("theme", "auto") settings.default("theme", "auto")
settings.default("text_color_day", "#402313ff") settings.default("text_color_day", "#402313ff")
settings.default("text_color_night", "#341c0fff") settings.default("text_color_night", "#341c0fff")
@ -21,6 +21,7 @@ init python:
settings.default("animations", True) settings.default("animations", True)
settings.default("updates", True) settings.default("updates", True)
settings.default("image_cache_size", 512) settings.default("image_cache_size", 512)
settings.default("multithreading", True)
renpy.music.register_channel("background", "sfx", True) renpy.music.register_channel("background", "sfx", True)
renpy.music.register_channel("sound2", "sfx", False) 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 persistent.custom_settings_default = {} default persistent.custom_settings_default = {}
init python in settings: init -10 python in settings:
from store import persistent, Action, DictEquality from store import persistent, Action, DictEquality
not_set = object() not_set = object()

View File

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