Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Base64 hardcoded icons in binary files #653

Merged
merged 9 commits into from
Aug 5, 2020
38 changes: 30 additions & 8 deletions HardcodeTray/modules/applications/b64electron.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,46 @@ def install_icon(self, icon, icon_path):
png_bytes = get_pngbytes(icon)

if png_bytes:
binary_file = path.join(str(icon_path), self.binary)
asar = AsarFile(binary_file)
target = B64ElectronApplication.get_real_path(self.file)
# Read a target file
asar = AsarFile(path.join(str(icon_path), self.binary))
target = B64ElectronApplication.get_real_path(self.file)
file_content = asar.read_file(target).decode()
# Create new base64 binary file
base64_icon = b64encode(App.svg().to_bin(icon.theme, icon.icon_size, icon.icon_size))
# Build new icon
new_icon = "data:image/png;base64," + base64_icon.decode()
# Replace the original icon with newly built one
file_content = file_content.replace(icon.original, new_icon)
# Write changed file
asar.write(target, file_content.encode())
bytes = file_content.encode()
self.set_icon(target, icon_path, bytes, True)
else:
Logger.error("Icon file was not found.")

def revert_icon(self, icon, icon_path):
"""Revert to the original icon."""
bilelmoussaoui marked this conversation as resolved.
Show resolved Hide resolved
# I don't know what is the best way to handle backup here.
# Maybe backup whole app.asar file?
Logger.error("Backup file of {0} was not found".format(self.name))
target = B64ElectronApplication.get_real_path(self.file)
backup_file = "|".join(target.split("/"))

bytes = self.get_backup_file(backup_file)
if bytes:
self.set_icon(target, icon_path, bytes)
else:
Logger.error("Backup file of {0} was not found".format(self.name))
print("Backup file of {0} was not found".format(self.name))
bilelmoussaoui marked this conversation as resolved.
Show resolved Hide resolved

def set_icon(self, icon_to_replace, binary_path, png_bytes, backup=False):
"""Set the icon into the electron binary file."""
binary_file = path.join(str(binary_path), self.binary)

asar = AsarFile(binary_file)
asar.write(icon_to_replace, png_bytes)
backup_file = "|".join(asar.keys)
# If script edits one file multiple times,
# it has to backup only the first time.
if backup and not self.backup._exists:
content = asar.old_content
if content: # in case the icon doesn't exists anymore
self.backup.file(backup_file, content)
self.backup._exists = True

self.is_corrupted = not asar.success