Skip to content

Commit

Permalink
Show another loader when preparing resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
th3w1zard1 committed Mar 12, 2024
1 parent d67b4eb commit f459a4d
Showing 1 changed file with 83 additions and 34 deletions.
117 changes: 83 additions & 34 deletions Tools/HolocronToolset/src/toolset/gui/windows/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ def _check_toolset_update(self, *, silent: bool):
# endregion

# region Other
def reloadSettings(self):
def reloadSettings(self): # TODO: Don't delete cached resources for unchanged installations.
self.reloadInstallations()

def getActiveResourceWidget(self) -> ResourceList | TextureList | None:
Expand All @@ -716,11 +716,10 @@ def getActiveResourceWidget(self) -> ResourceList | TextureList | None:
return self.ui.texturesWidget
return None

def refreshModuleList(self, *, reload: bool = True):
"""Refreshes the list of modules in the modulesCombo combobox."""
def _getModulesList(self, *, reload: bool = True) -> list[QStandardItem] | None:
if self.active is None:
print("no installation is currently loaded, cannot refresh module list")
return
return None

# If specified the user can forcibly reload the resource list for every module
if reload:
Expand Down Expand Up @@ -756,11 +755,19 @@ def refreshModuleList(self, *, reload: bool = True):
item.setForeground(self.palette().shadow())

modules.append(item)
return modules

self.ui.modulesWidget.setSections(modules)
def refreshModuleList(
self,
*,
reload: bool = True,
moduleItems: list[QStandardItem] | None = None,
):
"""Refreshes the list of modules in the modulesCombo combobox."""
moduleItems = moduleItems or self._getModulesList(reload=reload)
self.ui.modulesWidget.setSections(moduleItems)

def refreshOverrideList(self, *, reload=True):
"""Refreshes the list of override directories in the overrideFolderCombo combobox."""
def _getOverrideList(self, *, reload=True):
if self.active is None:
print("no installation is currently loaded, cannot refresh override list")
return
Expand All @@ -772,12 +779,26 @@ def refreshOverrideList(self, *, reload=True):
section = QStandardItem(directory if directory.strip() else "[Root]")
section.setData(directory, QtCore.Qt.UserRole)
sections.append(section)
self.ui.overrideWidget.setSections(sections)
return sections

def refreshTexturePackList(self, *, reload=True):
def refreshOverrideList(
self,
*,
reload: bool = True,
overrideItems: list[QStandardItem] | None = None,
):
"""Refreshes the list of override directories in the overrideFolderCombo combobox."""
overrideItems = overrideItems or self._getOverrideList(self, reload=reload)
self.ui.overrideWidget.setSections(overrideItems)

def _getTexturePackList(
self,
*,
reload: bool = True,
) -> list[QStandardItem] | None:
if self.active is None:
print("no installation is currently loaded, cannot refresh texturepack list")
return
return None
if reload:
self.active.load_textures()

Expand All @@ -786,7 +807,10 @@ def refreshTexturePackList(self, *, reload=True):
section = QStandardItem(texturepack)
section.setData(texturepack, QtCore.Qt.UserRole)
sections.append(section)
return sections

def refreshTexturePackList(self, *, reload=True):
sections = self._getTexturePackList(reload=reload)
self.ui.texturesWidget.setSections(sections)

def changeModule(self, moduleName: str):
Expand Down Expand Up @@ -858,6 +882,12 @@ def changeActiveInstallation(self, index: int):
self.updateMenus()

if index <= 0:
print(f"Index out of range - self.changeActiveInstallation({index})")
self.ui.gameCombo.setCurrentIndex(0)
self.active = None
if self.dogObserver is not None:
self.dogObserver.stop()
self.dogObserver = None
return

self.ui.resourceTabs.setEnabled(True)
Expand All @@ -868,7 +898,9 @@ def changeActiveInstallation(self, index: int):
tsl: bool = self.settings.installations()[name].tsl

# If the user has not set a path for the particular game yet, ask them too.
if not path:
if not path or not Path(path).safe_isdir():
if path and path.strip():
QMessageBox(QMessageBox.Warning, f"installation '{path}' not found", "Select another path now.").exec_()
path = QFileDialog.getExistingDirectory(self, f"Select the game directory for {name}")

# If the user still has not set a path, then return them to the [None] option.
Expand All @@ -881,39 +913,56 @@ def changeActiveInstallation(self, index: int):
self.dogObserver = None
return

def task(active: HTInstallation | None = None):
self.active = active or HTInstallation(path, name, tsl, self)
def load_task(active: HTInstallation | None = None) -> HTInstallation:
new_active = active or HTInstallation(path, name, tsl, self)
if not active:
new_active.reload_all()
return new_active

active = self.installations.get(name)
loader = AsyncLoader(self, "Loading Installation" if not active else "Refreshing installation", lambda: task(active), "Failed to load installation")
loader = AsyncLoader(self, "Loading Installation" if not active else "Refreshing installation", lambda: load_task(active), "Failed to load installation")
if not loader.exec_():
self.active = None
self.ui.gameCombo.setCurrentIndex(0)
if self.dogObserver is not None:
self.dogObserver.stop()
self.dogObserver = None
print("Loader task completed.")
else:
print("Loading core installation resources into UI...")
self.ui.coreWidget.setResources(self.active.chitin_resources())
print("Loading module resources into UI...")
self.refreshModuleList(reload=False)
print("Loading override resources into UI...")
self.refreshOverrideList(reload=False)
print("Loading TexturePack resources into UI...")
self.refreshTexturePackList(reload=False)
self.ui.texturesWidget.setInstallation(self.active)

print("Updating menus...")
self.updateMenus()
print("Setting up watchdog observer...")
return
self.active = loader.value
# KEEP UI CODE IN MAIN THREAD!
def prepare_task() -> tuple[list[QStandardItem] | None, ...]:
return (
self._getModulesList(reload=False),
self._getOverrideList(reload=False),
self._getTexturePackList(reload=False),
)
loader = AsyncLoader(self, "Preparing resources...", lambda: prepare_task(), "Failed to load installation")
if not loader.exec_():
self.active = None
self.ui.gameCombo.setCurrentIndex(0)
if self.dogObserver is not None:
print("Stopping old watchdog service...")
self.dogObserver.stop()
self.dogObserver = Observer()
self.dogObserver.schedule(self.dogHandler, self.active.path(), recursive=True)
self.dogObserver.start()

self.dogObserver = None
return
print("Loading core installation resources into UI...")
self.ui.coreWidget.setResources(self.active.chitin_resources())
moduleItems, overrideItems, saveItems, textureItems = loader.value
self.ui.modulesWidget.setSections(moduleItems)
self.ui.overrideWidget.setSections(overrideItems)
self.ui.texturesWidget.setSections(textureItems)
print("Remove unused categories...")
self.ui.coreWidget.modulesModel.removeUnusedCategories()
self.ui.texturesWidget.setInstallation(self.active)
print("Updating menus...")
self.updateMenus()
print("Setting up watchdog observer...")
if self.dogObserver is not None:
print("Stopping old watchdog service...")
self.dogObserver.stop()
self.dogObserver = Observer()
self.dogObserver.schedule(self.dogHandler, self.active.path(), recursive=True)
self.dogObserver.start()
print("Loader task completed.")
self.settings.installations()[name].path = path

def _extractResource(self, resource: FileResource, filepath: os.PathLike | str, loader: AsyncBatchLoader):
Expand Down

0 comments on commit f459a4d

Please sign in to comment.