Skip to content

Commit

Permalink
修复mods下载的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
DoiiarX committed Jun 8, 2024
1 parent aa73bb9 commit 50a2a9a
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions cddagl/ui/views/mods.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,8 @@ def install_new(self):


main_window = self.get_main_window()
status_bar = main_window.statusBar()
self.status_bar = main_window.statusBar()
status_bar = self.status_bar

self.install_type = selected_info['type']

Expand Down Expand Up @@ -573,6 +574,7 @@ def install_new(self):
else:
main_window = self.get_main_window()
status_bar = main_window.statusBar()
status_bar = status_bar

# Cancel installation
if self.downloading_new_mod:
Expand Down Expand Up @@ -644,7 +646,8 @@ def download_http_finished(self):

self.clear_download_ui()
main_window = self.get_main_window()
status_bar = main_window.statusBar()
self.status_bar = main_window.statusBar()
status_bar = self.status_bar
if self.download_aborted:
delete_path(self.download_dir)

Expand All @@ -653,7 +656,7 @@ def download_http_finished(self):
redirect = self.download_http_reply.attribute(
QNetworkRequest.RedirectionTargetAttribute)
if redirect is not None:
self.handle_redirection(redirect)
redirected_url = self.handle_redirection(redirect)
dowloading_speed_label = QLabel()
status_bar.addWidget(dowloading_speed_label)
self.dowloading_speed_label = dowloading_speed_label
Expand Down Expand Up @@ -760,7 +763,7 @@ def download_http_finished(self):
def handle_redirection(self, redirect):
delete_path(self.download_dir)
os.makedirs(self.download_dir)

status_bar = self.status_bar
status_bar.busy += 1

redirected_url = urljoin(
Expand All @@ -772,12 +775,13 @@ def handle_redirection(self, redirect):
redirected_url))
status_bar.addWidget(downloading_label, 100)
self.downloading_label = downloading_label

return redirected_url
def clear_download_ui(self):
if self.downloading_file is not None:
self.downloading_file.close()

main_window = self.get_main_window()
status_bar = self.status_bar
status_bar = main_window.statusBar()
status_bar.removeWidget(self.downloading_label)
status_bar.removeWidget(self.dowloading_speed_label)
Expand Down Expand Up @@ -875,7 +879,8 @@ def extract_new_mod(self):
self.extracting_index = 0

main_window = self.get_main_window()
status_bar = main_window.statusBar()
self.status_bar = main_window.statusBar()
status_bar = self.status_bar

status_bar.busy += 1

Expand All @@ -899,7 +904,8 @@ def timeout():
self.extracting_timer.stop()

main_window = self.get_main_window()
status_bar = main_window.statusBar()
self.status_bar = main_window.statusBar()
status_bar = self.status_bar

status_bar.removeWidget(self.extracting_label)
status_bar.removeWidget(self.extracting_progress_bar)
Expand Down Expand Up @@ -948,24 +954,28 @@ def move_new_mod(self):
self.moving_new_mod = True

main_window = self.get_main_window()
status_bar = main_window.statusBar()
self.status_bar = main_window.statusBar()
status_bar = self.status_bar
status_bar.showMessage(_('Finding the mod(s)'))

mod_dirs = self.find_mods_in_directory(self.extract_dir)
mod_dirs = self.__find_mods_in_directory(self.extract_dir)

if not mod_dirs:
status_bar.showMessage(_('Mod installation cancelled - No mod found in the downloaded archive'))
self.clean_up_after_install()
self.__clean_up_after_install()
return

if self.move_mods_to_mods_dir(mod_dirs):
if self.__move_mods_to_mods_dir(mod_dirs):
status_bar.showMessage(_('Mod installation completed'))
else:
status_bar.showMessage(_('Mod installation cancelled - Conflict with existing mod directories'))

self.clean_up_after_install()
self.__clean_up_after_install()

def find_mods_in_directory(self, directory):
def __find_mods_in_directory(self, directory):
"""
查找目录中的mod
"""
mod_dirs = set()
next_scans = deque([directory])

Expand All @@ -976,21 +986,22 @@ def find_mods_in_directory(self, directory):
next_scans.append(entry.path)
elif entry.is_file() and os.path.basename(entry.path).lower() == 'modinfo.json':
mod_dirs.add(os.path.dirname(entry.path))

logger.debug(f"find mod here: {os.path.dirname(entry.path)}")
return mod_dirs

def move_mods_to_mods_dir(self, mod_dirs):
def __move_mods_to_mods_dir(self, mod_dirs):
all_moved = True
for mod_dir in mod_dirs:
mod_dir_name = os.path.basename(mod_dir)
target_dir = os.path.join(self.mods_dir, mod_dir_name)
if os.path.exists(target_dir):
logger.error("Mod directory %s already exists in mods directory", mod_dir_name)
all_moved = False
break
continue
shutil.move(mod_dir, self.mods_dir)
return all_moved

def clean_up_after_install(self):
def __clean_up_after_install(self):
delete_path(self.extract_dir)
self.moving_new_mod = False
self.game_dir_changed(self.game_dir)
Expand All @@ -1017,7 +1028,8 @@ def disable_existing(self):
self.disable_existing_button.setText(_('Enable'))
except OSError as e:
main_window = self.get_main_window()
status_bar = main_window.statusBar()
self.status_bar = main_window.statusBar()
status_bar = self.status_bar

status_bar.showMessage(str(e))
else:
Expand All @@ -1033,7 +1045,8 @@ def disable_existing(self):
self.disable_existing_button.setText(_('Disable'))
except OSError as e:
main_window = self.get_main_window()
status_bar = main_window.statusBar()
self.status_bar = main_window.statusBar()
status_bar = self.status_bar

status_bar.showMessage(str(e))

Expand All @@ -1059,7 +1072,8 @@ def delete_existing(self):

if confirm_msgbox.exec() == 0:
main_window = self.get_main_window()
status_bar = main_window.statusBar()
self.status_bar = main_window.statusBar()
status_bar = self.status_bar

if not delete_path(selected_info['path']):
status_bar.showMessage(_('Mod deletion cancelled'))
Expand Down

0 comments on commit 50a2a9a

Please sign in to comment.