Skip to content

Commit

Permalink
ADDED: REVERT ICONS TO DEFAULT
Browse files Browse the repository at this point in the history
  • Loading branch information
OfficialAhmed committed Jul 5, 2023
1 parent 49b2832 commit 4e39768
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 84 deletions.
12 changes: 10 additions & 2 deletions Interface/Mask.py
Expand Up @@ -231,5 +231,13 @@ def retranslateUi(self, window):
self.RevertBtn.clicked.connect(self.revert_to_default)

multi_upload_obj = multi_upload()
self.UploadBtn.clicked.connect(lambda: multi_upload_obj.generate_icons_from_baked(self.UploadState, self.UploadBtn, self.group_path))
self.ContinueProcessBtn.clicked.connect(lambda: multi_upload_obj.continue_upload(self.ContinueProcessBtn, self.UploadBtn, self.UploadState))
self.UploadBtn.clicked.connect(lambda: multi_upload_obj.generate_and_upload_icons(self.selected_group_path))
self.ContinueProcessBtn.clicked.connect(lambda: multi_upload_obj.continue_upload(self.ContinueProcessBtn))

"""
#################################################################
Set common widgets
#################################################################
"""
self.set_state_widget(self.UploadState)
self.set_upload_btn_widget(self.UploadBtn)
2 changes: 1 addition & 1 deletion Interface/Upload.py
Expand Up @@ -158,5 +158,5 @@ def retranslateUi(self):
self.ValidationLabel.setText(_translate(win_name, translated_content.get("ValidationLabel")))
self.ConversionLabel.setText(_translate(win_name, translated_content.get("ConversionLabel")))
self.SendingLabel.setText(_translate(win_name, translated_content.get("SendingLabel")))
self.msg.setText(_translate(win_name, warning_message))
warning_message = f"{self.html.p_tag('margin: 0px;font-size:12pt; -qt-block-indent:0; text-indent:0px; color:#e83c3c', {translated_content.get('Warning')})}{translated_content.get('Question')}?"
self.msg.setText(_translate(win_name, warning_message))
32 changes: 30 additions & 2 deletions Module/Alerts.py
@@ -1,4 +1,4 @@
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMessageBox

class Main:

Expand Down Expand Up @@ -62,4 +62,32 @@ def display(self, window_title:str, description:str) -> None:
case _:
msg = f"{description}."

QtWidgets.QMessageBox.warning(None, f"{window_title.upper()}", f"{msg}")
QMessageBox.warning(None, f"{window_title.upper()}", f"{msg}")


def alert(self, title:str, message:str, question=True) -> bool:
"""
Render message box window for confirmation/Information
"""

msg_window = QMessageBox()

icon = QMessageBox.Question
buttons = QMessageBox.Yes | QMessageBox.No
default_btn = QMessageBox.No

if not question:
icon = QMessageBox.Information
buttons = QMessageBox.Yes
default_btn = QMessageBox.Yes

msg_window.setIcon(icon)
msg_window.setWindowTitle(title)
msg_window.setText(message)
msg_window.setStandardButtons(buttons)
msg_window.setDefaultButton(default_btn)

if msg_window.exec_() == QMessageBox.Yes:
return True

return False
2 changes: 1 addition & 1 deletion Module/Default_icons.py
Expand Up @@ -55,7 +55,7 @@ def calc_new_titles(
if self.is_default_set():

custom_group_btn_obj.setEnabled(True)
total_default_ids = len(json.load(open(self.default_group_file)))
total_default_ids = len(self.read_json(self.default_group_file))

new_titles = total_ids - total_default_ids
new_icons_number.setProperty("intValue", new_titles)
11 changes: 9 additions & 2 deletions Module/Iconit.py
Expand Up @@ -65,6 +65,7 @@ def check_ip_port(self) -> None:


def change_state(self, connected: bool) -> None:

translated_content: dict = self.translation.get_translation(self.language, "Iconit")

labels = {
Expand All @@ -91,7 +92,7 @@ def load_ignored_ids(self) -> list:
data = []
file = self.mode.get(self.selected_mode).get("ignored file")
try:
data = json.load(open(file)).get("ids")
data = self.read_json(file).get("ids")
except: pass
finally: return data

Expand Down Expand Up @@ -125,7 +126,7 @@ def get_cache(self) -> dict:
file = mode.get("cache file")

if os.path.isfile(file):
try: result = json.load(open(file))
try: result = self.read_json(file)
except: pass

return result
Expand Down Expand Up @@ -231,22 +232,28 @@ def connect_ps4(self, is_valid):

# Check selected mode
if self.game_icon_radio.isChecked():

self.set_selected_mode("game")
self.selected_mode = "game"
is_cached = Game().start_cache()


elif self.SystemIconsRadio.isChecked():

self.set_selected_mode("system apps")
self.selected_mode = "system apps"
is_cached = System().start_cache()


else:

self.set_selected_mode("avatar")
self.selected_mode = "avatar"
is_cached = Avatar().start_cache()

self.selected_mode = self.get_selected_mode()


if is_cached:
self.progress_bar(self.CacheBar, 100)
self.render_window()
Expand Down
63 changes: 40 additions & 23 deletions Module/Mask.py
@@ -1,11 +1,11 @@
from PIL import Image
from PyQt5 import QtWidgets
from threading import Lock
from environment import Common
from Module.Multi_upload import Main as multi_uploader
from PyQt5.QtWidgets import QFileDialog

from PyQt5 import QtWidgets
from PIL import Image
from PyQt5.QtWidgets import QFileDialog, QMessageBox

import os, shutil, json, concurrent.futures
import os, shutil, concurrent.futures


class Main(Common):
Expand All @@ -20,6 +20,7 @@ def __init__(self) -> None:
self.group_icons_is_changed = False
self.preview_icon_path = self.pref_path.replace('\\', '/')
self.ps4_icon_dimension = self.constant.get_ps4_icon_size()
self.multi_uploader = multi_uploader()

win_name = "MaskMakerWindow"
self.translated_content: dict = self.translation.get_translation(self.language, win_name)
Expand Down Expand Up @@ -75,7 +76,7 @@ def check_bake_state(self):
"""

is_found_baked = False
try: group_ids = json.load(open(self.group_path))
try: group_ids = self.read_json(self.selected_group_path)
except: group_ids = []

# Only check if group ids not empty
Expand Down Expand Up @@ -129,15 +130,15 @@ def browse_icon_group(self) -> None:

self.RevertBtn.setEnabled(True)
self.selected_group = group_path.split('/')[-1]
self.selected_group_path = group_path

self.group_path = group_path
self.check_bake_state()
self.BakeState.setText("Baking mask required")
self.BakedView.setStyleSheet(f'border-image: url({self.preview_icon_path}previewTest.@OfficialAhmed0);')

self.group_icons_is_changed = True
self.GroupName.setText(self.selected_group)
self.group_ids: dict = json.load(open(group_path))
self.group_ids: dict = self.read_json(group_path)

else:

Expand Down Expand Up @@ -210,21 +211,38 @@ def revert_to_default(self):
"""

translated_content: dict = self.translated_content.get("RevertToDefaultWindow")
msg_window = QMessageBox()

msg_window.setIcon(QMessageBox.Question)
msg_window.setWindowTitle(translated_content.get("WinTitle"))
msg_window.setText(f"{translated_content.get('Msg1')} *{self.selected_group}* {translated_content.get('Msg2')}")

msg_window.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
msg_window.setDefaultButton(QMessageBox.No)

is_confirmed = self.alerts.alert(
translated_content.get("WinTitle"),
f"{translated_content.get('Msg1')} *{self.selected_group}* {translated_content.get('Msg2')}"
)

if msg_window.exec_() == QMessageBox.Yes:
if is_confirmed:

self.RevertBtn.setEnabled(False)
selected_group = self.read_json(self.selected_group_path)

# Copy group icons from cache to baked folder
for id in selected_group:

# read from default cached icons -> generate No of icons (png/dds) -> upload to PS4
shutil.copy(
f"{self.mode.get(self.get_selected_mode()).get('default group path')}{id}.png",
f"{self.baked_path}{id}.png"
)

if self.multi_uploader.generate_and_upload_icons(self.selected_group_path):
title = "SUCCESSFULL REVERT"
msg = f"*{self.selected_group}* has been successfully reverted to default"

else:
title = "UNSUCCESSFULL REVERT"
msg = f"Something went wrong. Please refer to Logs.txt"

self.alerts.alert(
title,
msg,
False
)


def apply_mask(self, id, cover, mask:Image, lock):
Expand All @@ -245,12 +263,11 @@ def apply_mask(self, id, cover, mask:Image, lock):

try:
# Reading mask specifications from JSON
with open(f"{self.temp_path}set.json") as file:
info = json.load(file)
info = self.read_json(f"{self.temp_path}set.json")

vertical_shift = info.get("vertical_shift")
horizontal_shift = info.get("horizontal_shift")
width, height = info.get("width"), info.get("height")
vertical_shift = info.get("vertical_shift")
horizontal_shift = info.get("horizontal_shift")
width, height = info.get("width"), info.get("height")
except: pass

resized_game_icon = game_icon.resize((width, height))
Expand Down
76 changes: 41 additions & 35 deletions Module/Multi_upload.py
@@ -1,26 +1,25 @@
"""
Class:
Handles the generation of required NO. of icons and uploading them to PS4.
Desc:
Upon icons generation sucess, upload to PS4. Otherwise, keep them stored to retry on user next login
"""

import os, json, PIL
import os, PIL
import Common.Uploader as Uploader

from environment import Common
from ftplib import FTP
from PIL import Image


class Main(Common):
"""
#### Generate required NO. of icons and uploading them to PS4.
* Desc: Upon icons generation sucess, upload to PS4.
Otherwise, keep them stored to retry on user next start
"""

def __init__(self) -> None:
super().__init__()

self.ftp:FTP = self.get_ftp()
self.game_ids:dict = self.get_ids()
self.uploader = Uploader.Main()
self.multi_uploader = Uploader.Main()


def last_process(self, last_group_path:str = "") -> str|None:
Expand All @@ -38,37 +37,25 @@ def last_process(self, last_group_path:str = "") -> str|None:
return file.readline()


def continue_upload(self, continue_btn_widget, upload_btn_widget, state_widget):
def continue_upload(self, continue_btn_widget):
"""
Unfinished process detected, allow user to continue uploading the rest of baked icons
"""

self.generate_icons_from_baked(state_widget, upload_btn_widget, self.last_process())
self.generate_and_upload_icons(self.get_state_widget(), self.get_upload_btn_widget(), self.last_process())
continue_btn_widget.setEnabled(False)


def upload_baked_icons(self, game_directory:str, icons:list):
"""
Upload set of generated icons for the game. One call per game
"""

self.ftp.cwd(f"/{game_directory}")

for icon in icons:
icon_dir = f"{self.icons_cache_path}{icon}"
self.uploader.upload_to_server(icon_dir, icon)


def generate_icons_from_baked(self, state_widget, upload_btn_widget, group_path):
def generate_and_upload_icons(self, group_path) -> bool:
"""
Baked icons are ready.
Make required NO. of icons for each baked icon in the selected group
generate required NO. of icons for each baked icon in the selected group
* NOTE: Icons must be in baked folder for default and baking
"""

state_widget.setText("UPLOADING... PLEASE WAIT")
self.get_state_widget().setText("UPLOADING... PLEASE WAIT")
self.last_process(group_path)

self.selected_group_icons = json.load(open(group_path))
self.selected_group_icons = self.read_json(group_path)
baked_icons = os.listdir(self.baked_path)
icons_to_upload = []

Expand All @@ -78,11 +65,12 @@ def generate_icons_from_baked(self, state_widget, upload_btn_widget, group_path)
if f"{current_icon_id}.png" not in baked_icons:
continue


icon = Image.open(f"{self.baked_path}{current_icon_id}.png")
resized_icon = icon.resize(self.constant.get_ps4_icon_size(), PIL.Image.ANTIALIAS)

self.current_game_icons = self.game_ids.get(current_icon_id).get("icons")


# Iterate through the icons required for the current game
for current_image in self.current_game_icons:

Expand All @@ -104,20 +92,38 @@ def generate_icons_from_baked(self, state_widget, upload_btn_widget, group_path)
# Creat a temp .png, then convert it to .dds

resized_icon.save(f"{icon_cache_path_no_extension}.png")
self.uploader.png_to_dds(f"{icon_cache_path_no_extension}.png", self.icons_cache_path)
self.multi_uploader.png_to_dds(f"{icon_cache_path_no_extension}.png", self.icons_cache_path)

icons_to_upload.append(current_image)


try:
game_directory = self.get_ps4_game_location(current_icon_id)
self.upload_baked_icons(game_directory, icons_to_upload)

# Move baked icon to cache for local view upon upload success
self.uploader.update_local_icon(f"{self.baked_path}{current_icon_id}.png", current_icon_id)
state_widget.setText("DONE!")
self.multi_uploader.update_local_icon(f"{self.baked_path}{current_icon_id}.png", current_icon_id)
self.get_state_widget().setText("DONE!")

except Exception as e:
self.log_to_external_file(str(e), "upload - baked mask")
state_widget.setText("ERROR - READ logs.txt")
self.get_state_widget().setText("ERROR - READ logs.txt")
self.upload_btn_widget.setEnabled(False)
return False

self.upload_btn_widget.setEnabled(False)
return True


def upload_baked_icons(self, game_directory:str, icons:list):
"""
Upload set of generated icons for the game. One call per game
"""

self.ftp.cwd(f"/{game_directory}")

for icon in icons:
icon_dir = f"{self.icons_cache_path}{icon}"
self.multi_uploader.upload_to_server(icon_dir, icon)

upload_btn_widget.setEnabled(False)

0 comments on commit 4e39768

Please sign in to comment.