mirror of
https://github.com/leminlimez/Nugget.git
synced 2025-04-08 04:23:05 +08:00
error handling over whole apply statement
This commit is contained in:
@@ -286,134 +286,134 @@ class DeviceManager:
|
|||||||
|
|
||||||
## APPLYING OR REMOVING TWEAKS AND RESTORING
|
## APPLYING OR REMOVING TWEAKS AND RESTORING
|
||||||
def apply_changes(self, resetting: bool = False, update_label=lambda x: None):
|
def apply_changes(self, resetting: bool = False, update_label=lambda x: None):
|
||||||
# set the tweaks and apply
|
try:
|
||||||
# first open the file in read mode
|
# set the tweaks and apply
|
||||||
update_label("Applying changes to files...")
|
# first open the file in read mode
|
||||||
gestalt_plist = None
|
update_label("Applying changes to files...")
|
||||||
if self.data_singleton.gestalt_path != None:
|
gestalt_plist = None
|
||||||
with open(self.data_singleton.gestalt_path, 'rb') as in_fp:
|
if self.data_singleton.gestalt_path != None:
|
||||||
gestalt_plist = plistlib.load(in_fp)
|
with open(self.data_singleton.gestalt_path, 'rb') as in_fp:
|
||||||
# create the other plists
|
gestalt_plist = plistlib.load(in_fp)
|
||||||
flag_plist: dict = {}
|
# create the other plists
|
||||||
eligibility_files = None
|
flag_plist: dict = {}
|
||||||
ai_file = None
|
eligibility_files = None
|
||||||
basic_plists: dict = {}
|
ai_file = None
|
||||||
basic_plists_ownership: dict = {}
|
basic_plists: dict = {}
|
||||||
files_data: dict = {}
|
basic_plists_ownership: dict = {}
|
||||||
uses_domains: bool = False
|
files_data: dict = {}
|
||||||
# create the restore file list
|
uses_domains: bool = False
|
||||||
files_to_restore: dict[FileToRestore] = [
|
# create the restore file list
|
||||||
]
|
files_to_restore: dict[FileToRestore] = [
|
||||||
tmp_pb_dir = None # temporary directory for unzipping pb files
|
]
|
||||||
|
tmp_pb_dir = None # temporary directory for unzipping pb files
|
||||||
|
|
||||||
# set the plist keys
|
# set the plist keys
|
||||||
if not resetting:
|
if not resetting:
|
||||||
for tweak_name in tweaks:
|
for tweak_name in tweaks:
|
||||||
tweak = tweaks[tweak_name]
|
tweak = tweaks[tweak_name]
|
||||||
if isinstance(tweak, FeatureFlagTweak):
|
if isinstance(tweak, FeatureFlagTweak):
|
||||||
flag_plist = tweak.apply_tweak(flag_plist)
|
flag_plist = tweak.apply_tweak(flag_plist)
|
||||||
elif isinstance(tweak, EligibilityTweak):
|
elif isinstance(tweak, EligibilityTweak):
|
||||||
eligibility_files = tweak.apply_tweak()
|
eligibility_files = tweak.apply_tweak()
|
||||||
elif isinstance(tweak, AITweak):
|
elif isinstance(tweak, AITweak):
|
||||||
ai_file = tweak.apply_tweak()
|
ai_file = tweak.apply_tweak()
|
||||||
elif isinstance(tweak, BasicPlistTweak) or isinstance(tweak, RdarFixTweak) or isinstance(tweak, AdvancedPlistTweak):
|
elif isinstance(tweak, BasicPlistTweak) or isinstance(tweak, RdarFixTweak) or isinstance(tweak, AdvancedPlistTweak):
|
||||||
basic_plists = tweak.apply_tweak(basic_plists, self.allow_risky_tweaks)
|
basic_plists = tweak.apply_tweak(basic_plists, self.allow_risky_tweaks)
|
||||||
basic_plists_ownership[tweak.file_location] = tweak.owner
|
basic_plists_ownership[tweak.file_location] = tweak.owner
|
||||||
if tweak.enabled and tweak.owner == 0:
|
if tweak.enabled and tweak.owner == 0:
|
||||||
uses_domains = True
|
uses_domains = True
|
||||||
elif isinstance(tweak, NullifyFileTweak):
|
elif isinstance(tweak, NullifyFileTweak):
|
||||||
tweak.apply_tweak(files_data)
|
tweak.apply_tweak(files_data)
|
||||||
if tweak.enabled and tweak.file_location.value.startswith("/var/mobile/"):
|
if tweak.enabled and tweak.file_location.value.startswith("/var/mobile/"):
|
||||||
uses_domains = True
|
uses_domains = True
|
||||||
elif isinstance(tweak, PosterboardTweak):
|
elif isinstance(tweak, PosterboardTweak):
|
||||||
tmp_pb_dir = TemporaryDirectory()
|
tmp_pb_dir = TemporaryDirectory()
|
||||||
tweak.apply_tweak(files_to_restore=files_to_restore, output_dir=tmp_pb_dir.name)
|
tweak.apply_tweak(files_to_restore=files_to_restore, output_dir=tmp_pb_dir.name)
|
||||||
else:
|
else:
|
||||||
if gestalt_plist != None:
|
if gestalt_plist != None:
|
||||||
gestalt_plist = tweak.apply_tweak(gestalt_plist)
|
gestalt_plist = tweak.apply_tweak(gestalt_plist)
|
||||||
elif tweak.enabled:
|
elif tweak.enabled:
|
||||||
# no mobilegestalt file provided but applying mga tweaks, give warning
|
# no mobilegestalt file provided but applying mga tweaks, give warning
|
||||||
show_error_msg("No mobilegestalt file provided! Please select your file to apply mobilegestalt tweaks.")
|
show_error_msg("No mobilegestalt file provided! Please select your file to apply mobilegestalt tweaks.")
|
||||||
update_label("Failed.")
|
update_label("Failed.")
|
||||||
return
|
return
|
||||||
# set the custom gestalt keys
|
# set the custom gestalt keys
|
||||||
if gestalt_plist != None:
|
if gestalt_plist != None:
|
||||||
gestalt_plist = CustomGestaltTweaks.apply_tweaks(gestalt_plist)
|
gestalt_plist = CustomGestaltTweaks.apply_tweaks(gestalt_plist)
|
||||||
|
|
||||||
gestalt_data = None
|
gestalt_data = None
|
||||||
if resetting:
|
if resetting:
|
||||||
gestalt_data = b""
|
gestalt_data = b""
|
||||||
elif gestalt_plist != None:
|
elif gestalt_plist != None:
|
||||||
gestalt_data = plistlib.dumps(gestalt_plist)
|
gestalt_data = plistlib.dumps(gestalt_plist)
|
||||||
|
|
||||||
# Generate backup
|
# Generate backup
|
||||||
update_label("Generating backup...")
|
update_label("Generating backup...")
|
||||||
self.concat_file(
|
|
||||||
contents=plistlib.dumps(flag_plist),
|
|
||||||
path="/var/preferences/FeatureFlags/Global.plist",
|
|
||||||
files_to_restore=files_to_restore
|
|
||||||
)
|
|
||||||
self.add_skip_setup(files_to_restore, uses_domains)
|
|
||||||
if gestalt_data != None:
|
|
||||||
self.concat_file(
|
self.concat_file(
|
||||||
contents=gestalt_data,
|
contents=plistlib.dumps(flag_plist),
|
||||||
path="/var/containers/Shared/SystemGroup/systemgroup.com.apple.mobilegestaltcache/Library/Caches/com.apple.MobileGestalt.plist",
|
path="/var/preferences/FeatureFlags/Global.plist",
|
||||||
files_to_restore=files_to_restore
|
files_to_restore=files_to_restore
|
||||||
)
|
)
|
||||||
if eligibility_files:
|
self.add_skip_setup(files_to_restore, uses_domains)
|
||||||
new_eligibility_files: dict[FileToRestore] = []
|
if gestalt_data != None:
|
||||||
if not self.get_current_device_supported():
|
|
||||||
# update the files
|
|
||||||
for file in eligibility_files:
|
|
||||||
self.concat_file(
|
|
||||||
contents=file.contents,
|
|
||||||
path=file.restore_path,
|
|
||||||
files_to_restore=new_eligibility_files
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
new_eligibility_files = eligibility_files
|
|
||||||
files_to_restore += new_eligibility_files
|
|
||||||
if ai_file != None:
|
|
||||||
self.concat_file(
|
|
||||||
contents=ai_file.contents,
|
|
||||||
path=ai_file.restore_path,
|
|
||||||
files_to_restore=files_to_restore
|
|
||||||
)
|
|
||||||
for location, plist in basic_plists.items():
|
|
||||||
ownership = basic_plists_ownership[location]
|
|
||||||
self.concat_file(
|
|
||||||
contents=plistlib.dumps(plist),
|
|
||||||
path=location.value,
|
|
||||||
files_to_restore=files_to_restore,
|
|
||||||
owner=ownership, group=ownership
|
|
||||||
)
|
|
||||||
for location, data in files_data.items():
|
|
||||||
self.concat_file(
|
|
||||||
contents=data,
|
|
||||||
path=location.value,
|
|
||||||
files_to_restore=files_to_restore,
|
|
||||||
owner=ownership, group=ownership
|
|
||||||
)
|
|
||||||
# reset basic tweaks
|
|
||||||
if resetting:
|
|
||||||
empty_data = plistlib.dumps({})
|
|
||||||
for location in FileLocationsList:
|
|
||||||
self.concat_file(
|
self.concat_file(
|
||||||
contents=empty_data,
|
contents=gestalt_data,
|
||||||
path=location.value,
|
path="/var/containers/Shared/SystemGroup/systemgroup.com.apple.mobilegestaltcache/Library/Caches/com.apple.MobileGestalt.plist",
|
||||||
files_to_restore=files_to_restore
|
files_to_restore=files_to_restore
|
||||||
)
|
)
|
||||||
if self.allow_risky_tweaks:
|
if eligibility_files:
|
||||||
for location in RiskyFileLocationsList:
|
new_eligibility_files: dict[FileToRestore] = []
|
||||||
|
if not self.get_current_device_supported():
|
||||||
|
# update the files
|
||||||
|
for file in eligibility_files:
|
||||||
|
self.concat_file(
|
||||||
|
contents=file.contents,
|
||||||
|
path=file.restore_path,
|
||||||
|
files_to_restore=new_eligibility_files
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
new_eligibility_files = eligibility_files
|
||||||
|
files_to_restore += new_eligibility_files
|
||||||
|
if ai_file != None:
|
||||||
|
self.concat_file(
|
||||||
|
contents=ai_file.contents,
|
||||||
|
path=ai_file.restore_path,
|
||||||
|
files_to_restore=files_to_restore
|
||||||
|
)
|
||||||
|
for location, plist in basic_plists.items():
|
||||||
|
ownership = basic_plists_ownership[location]
|
||||||
|
self.concat_file(
|
||||||
|
contents=plistlib.dumps(plist),
|
||||||
|
path=location.value,
|
||||||
|
files_to_restore=files_to_restore,
|
||||||
|
owner=ownership, group=ownership
|
||||||
|
)
|
||||||
|
for location, data in files_data.items():
|
||||||
|
self.concat_file(
|
||||||
|
contents=data,
|
||||||
|
path=location.value,
|
||||||
|
files_to_restore=files_to_restore,
|
||||||
|
owner=ownership, group=ownership
|
||||||
|
)
|
||||||
|
# reset basic tweaks
|
||||||
|
if resetting:
|
||||||
|
empty_data = plistlib.dumps({})
|
||||||
|
for location in FileLocationsList:
|
||||||
self.concat_file(
|
self.concat_file(
|
||||||
contents=empty_data,
|
contents=empty_data,
|
||||||
path=location.value,
|
path=location.value,
|
||||||
files_to_restore=files_to_restore
|
files_to_restore=files_to_restore
|
||||||
)
|
)
|
||||||
|
if self.allow_risky_tweaks:
|
||||||
|
for location in RiskyFileLocationsList:
|
||||||
|
self.concat_file(
|
||||||
|
contents=empty_data,
|
||||||
|
path=location.value,
|
||||||
|
files_to_restore=files_to_restore
|
||||||
|
)
|
||||||
|
|
||||||
# restore to the device
|
# restore to the device
|
||||||
update_label("Restoring to device...")
|
update_label("Restoring to device...")
|
||||||
try:
|
|
||||||
restore_files(files=files_to_restore, reboot=self.auto_reboot, lockdown_client=self.data_singleton.current_device.ld)
|
restore_files(files=files_to_restore, reboot=self.auto_reboot, lockdown_client=self.data_singleton.current_device.ld)
|
||||||
if tmp_pb_dir != None:
|
if tmp_pb_dir != None:
|
||||||
tmp_pb_dir.cleanup()
|
tmp_pb_dir.cleanup()
|
||||||
|
|||||||
Reference in New Issue
Block a user