source.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. ########################################################################
  2. # Wiizard - A Wii games manager
  3. # Copyright (C) 2023 CYBERDEViL
  4. #
  5. # This file is part of Wiizard.
  6. #
  7. # Wiizard is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Wiizard is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. ########################################################################
  21. import os
  22. import pywwt
  23. from wiizard.fileInfo import LocalGameFileInfo, FILE_TYPE_ISO, FILE_TYPE_WBFS
  24. from wiizard.thread import AbstractThread, THREAD_FLAG_IS_CANCELLABLE
  25. import wiizard.const as Const
  26. class PartitionRepairThread(AbstractThread):
  27. def __init__(self, path, repairMode=pywwt.REPAIR_ALL, testMode=True):
  28. AbstractThread.__init__(self, flags=0)
  29. self.__path = path
  30. self.__testMode = testMode
  31. self.__repairMode = repairMode
  32. self.result = None
  33. self.error = ""
  34. def run(self):
  35. result = None
  36. try:
  37. result = pywwt.repair_wbfs_partition(self.__path,
  38. repairMode=self.__repairMode,
  39. testMode=self.__testMode)
  40. except pywwt.error as err:
  41. print("Repair failed:", err)
  42. self.error = str(err)
  43. self.result = result
  44. self.completed.emit()
  45. class PartitionScanThread(AbstractThread):
  46. def __init__(self, path):
  47. AbstractThread.__init__(self, flags=0)
  48. self.__path = path
  49. self.result = None
  50. self.error = ""
  51. def run(self):
  52. self.result = {}
  53. try:
  54. self.result.update(pywwt.check_wbfs_partition(self.__path))
  55. except pywwt.error as err:
  56. self.error += f"Checking WBFS partition failed: {err}"
  57. self.completed.emit()
  58. return
  59. try:
  60. self.result.update(pywwt.get_wbfs_partition_info(self.__path, discs=True))
  61. except pywwt.error as err:
  62. self.error += f"Failed to get WBFS partition info: {err}"
  63. self.completed.emit()
  64. class InvalidFile:
  65. @staticmethod
  66. def isValid():
  67. return False
  68. """ Read the ID6 and title from .iso and .wbfs files
  69. """
  70. def getLocalGameFileInfo(path, ext, sourceModel):
  71. offset = 0
  72. fileType = FILE_TYPE_ISO
  73. if ext == ".wbfs":
  74. fileType = FILE_TYPE_WBFS
  75. offset = 512
  76. with open(path, 'rb') as fd:
  77. fd.seek(offset)
  78. try:
  79. id6 = fd.read(6).decode("ascii")
  80. except UnicodeDecodeError:
  81. return InvalidFile
  82. fd.seek(offset + 32)
  83. try:
  84. title = fd.read(64).decode("utf-8").rstrip("\x00")
  85. except UnicodeDecodeError:
  86. return InvalidFile
  87. fd.seek(0, os.SEEK_END)
  88. size = round(fd.tell() / Const.MiB, 2)
  89. return LocalGameFileInfo(id6, title, os.path.dirname(path),
  90. os.path.basename(path), fileType, size,
  91. sourceModel)
  92. """ Recursive find all files with given extensions on local disk (non-wbfs)
  93. """
  94. class FileFinder(AbstractThread):
  95. def __init__(self, path, sourceModel, extensions=Const.EXTS_WII, maxDepth=4):
  96. AbstractThread.__init__(self, flags=THREAD_FLAG_IS_CANCELLABLE)
  97. self.__path = path
  98. self.__exts = extensions
  99. self.__maxDepth = maxDepth
  100. self.__sourceModel = sourceModel
  101. self.result = None
  102. def run(self):
  103. self.result = self.findFiles(self.__path, self.__exts, maxDepth=self.__maxDepth)
  104. self.completed.emit()
  105. def findFiles(self, path, extensions=Const.EXTS_WII, depth=0, maxDepth=4):
  106. files = []
  107. try:
  108. for file_ in os.listdir(path):
  109. if self.cancelled:
  110. break
  111. realPath = os.path.join(path, file_)
  112. if os.path.isdir(realPath):
  113. if depth < maxDepth:
  114. files += self.findFiles(realPath, depth=depth+1, maxDepth=maxDepth)
  115. continue
  116. for ext in extensions:
  117. if file_.endswith(ext):
  118. fileInfo = getLocalGameFileInfo(realPath, ext, self.__sourceModel)
  119. if fileInfo.isValid():
  120. files.append(fileInfo)
  121. break
  122. except PermissionError:
  123. pass # TODO
  124. return files