Exemple #1
0
 def _download_library(self, library_metadata):
     if self.download_exclusive and not library_metadata.current_system_supported():
         return
     if library_metadata.is_natives():
         if self.download_exclusive:
             all_extensions = [library_metadata.get_current_system_natives_extension()]
         else:
             all_extensions = library_metadata.get_all_natives_extensions()
         natives_list = all_extensions
     if self.is_library_existant(library_metadata):
         if library_metadata.is_natives():
             natives_list = list()
             for current_extension in all_extensions:
                 if not self.is_natives_existant(library_metadata, current_extension):
                     natives_list.append(current_extension)
             if natives_list == list():
                 return # Natives already exists
         else:
             return # Library already exists
     if library_metadata.is_natives():
         download_list = library_metadata.get_download_list(natives_list)
     else:
         download_list = library_metadata.get_download_list()
     for current_library in download_list:
         current_tries = 1
         while current_tries <= 3:
             correct_hash = current_library["hash"].url_object().read().decode("UTF-8")
             FileTools.write_object(str(self.BASE_PATH.joinpath(current_library["path"])), current_library["url"].url_object())
             hasher = hashlib.sha1()
             hasher.update(open(str(self.BASE_PATH.joinpath(current_library["path"])), mode="rb").read())
             if hasher.hexdigest() == correct_hash:
                 if library_metadata.is_natives():
                     natives_directory = self.BASE_PATH.joinpath(current_library["path"].parent.joinpath(current_library["natives_extension"]))
                     jar_path = str(self.BASE_PATH.joinpath(current_library["path"]))
                     FileTools.extract_jar_files(FileTools.get_jar_object(jar_path), str(natives_directory), library_metadata.get_natives_exclude())
                     FileTools.delete_and_clean(jar_path)
                 break
             else:
                 current_tries += 1
         if current_tries == 3:
             raise Exception("Failed to download library " + library_metadata.get_id()) # TODO: More appropriate exception
     self.index[library_metadata.get_id()] = dict()
     if library_metadata.is_natives():
         self.index[library_metadata.get_id()]["path"] = download_list[0]["path"].parent.parts
     else:
         self.index[library_metadata.get_id()]["path"] = download_list[0]["path"].parts