mirror of
https://github.com/Radiquum/anixart-patcher.git
synced 2025-09-08 12:13:51 +05:00
feat: [R] Include enabled patches, developer and more info in settings
Fixes #4
This commit is contained in:
parent
67a45a9ac6
commit
7cae88c2ef
5 changed files with 117 additions and 8 deletions
|
@ -1,4 +1,5 @@
|
|||
"""Adds used patches and custom menu items to app settings"""
|
||||
|
||||
# Developer: Radiquum
|
||||
# URL:
|
||||
|
||||
|
@ -9,6 +10,7 @@ priority = -95
|
|||
# imports
|
||||
## bundled
|
||||
import os
|
||||
import shutil
|
||||
import random
|
||||
import string
|
||||
from typing import TypedDict
|
||||
|
@ -21,9 +23,23 @@ from config import config, log
|
|||
|
||||
|
||||
# Patch
|
||||
class PatchConfig_AddSettingsMenuItemsCategoryItem(TypedDict):
|
||||
title: str
|
||||
summary: str | None
|
||||
url: str | None
|
||||
icon: str | None
|
||||
icon_space_reserved: bool
|
||||
|
||||
|
||||
class PatchConfig_AddSettingsMenuItemsCategory(TypedDict):
|
||||
title: str
|
||||
items: list[PatchConfig_AddSettingsMenuItemsCategoryItem]
|
||||
|
||||
|
||||
class PatchConfig_AddSettingsMenuItems(TypedDict):
|
||||
_internal_all_patch_statuses: list
|
||||
add_patch_info: bool
|
||||
main_settings_categories: list[PatchConfig_AddSettingsMenuItemsCategory]
|
||||
|
||||
|
||||
def random_key():
|
||||
|
@ -53,14 +69,14 @@ def create_intent(
|
|||
|
||||
def create_Preference(
|
||||
title: str,
|
||||
description: str | None = None,
|
||||
summary: str | None = None,
|
||||
icon: str | None = None,
|
||||
icon_space_reserved: bool = False,
|
||||
):
|
||||
ns = config["xml_ns"]
|
||||
item = etree.Element("Preference", nsmap=ns)
|
||||
item.set(f"{{{ns['android']}}}title", title)
|
||||
item.set(f"{{{ns['android']}}}summary", description or "")
|
||||
item.set(f"{{{ns['android']}}}summary", summary or "")
|
||||
if icon:
|
||||
item.set(f"{{{ns['app']}}}icon", icon)
|
||||
item.set(f"{{{ns['app']}}}iconSpaceReserved", str(icon_space_reserved).lower())
|
||||
|
@ -77,6 +93,23 @@ def create_PreferenceCategory(title: str):
|
|||
return category
|
||||
|
||||
|
||||
def add_icons():
|
||||
src_icon_path = f"{config["folders"]["patches"]}/resources/icons"
|
||||
src_icon_night_path = f"{config["folders"]["patches"]}/resources/icons-night"
|
||||
dst_icon_path = f"{config["folders"]["decompiled"]}/res/drawable"
|
||||
dst_icon_night_path = f"{config["folders"]["decompiled"]}/res/drawable-night"
|
||||
icons = os.listdir(src_icon_path)
|
||||
if len(icons) == 0:
|
||||
return
|
||||
|
||||
for icon in icons:
|
||||
shutil.copy(f"{src_icon_path}/{icon}", f"{dst_icon_path}/{icon}")
|
||||
if os.path.exists(f"{src_icon_night_path}/{icon}"):
|
||||
shutil.copy(
|
||||
f"{src_icon_night_path}/{icon}", f"{dst_icon_night_path}/{icon}"
|
||||
)
|
||||
|
||||
|
||||
def add_patch_info(patch_statuses: list):
|
||||
category = create_PreferenceCategory("Использованные патчи")
|
||||
for patch in patch_statuses:
|
||||
|
@ -91,23 +124,48 @@ def add_patch_info(patch_statuses: list):
|
|||
) as f:
|
||||
line = f.readline()
|
||||
if line.startswith('"""'):
|
||||
description.append(line.strip().removeprefix('"""').removesuffix('"""').strip())
|
||||
description.append(
|
||||
line.strip().removeprefix('"""').removesuffix('"""').strip()
|
||||
)
|
||||
line = f.readline()
|
||||
if line.startswith("# Developer:"):
|
||||
description.append("by")
|
||||
description.append(line.strip().removeprefix("# Developer:").strip())
|
||||
description.append(
|
||||
line.strip().removeprefix("# Developer:").strip()
|
||||
)
|
||||
line = f.readline()
|
||||
if line.startswith("# URL:"):
|
||||
url = line.strip().removeprefix("# URL:").strip()
|
||||
|
||||
item = create_Preference(patch["name"].replace("_", " ").strip().title(), description=" ".join(description))
|
||||
item = create_Preference(
|
||||
patch["name"].replace("_", " ").strip().title(),
|
||||
description=" ".join(description),
|
||||
)
|
||||
if url:
|
||||
item.append(create_intent(data = url))
|
||||
item.append(create_intent(data=url))
|
||||
category.append(item)
|
||||
return category
|
||||
|
||||
|
||||
def add_custom_category(
|
||||
title: str, items: list[PatchConfig_AddSettingsMenuItemsCategoryItem]
|
||||
):
|
||||
category = create_PreferenceCategory(title)
|
||||
for item in items:
|
||||
new_item = create_Preference(
|
||||
item["title"],
|
||||
item["summary"],
|
||||
item["icon"],
|
||||
item["icon_space_reserved"],
|
||||
)
|
||||
if item["url"]:
|
||||
new_item.append(create_intent(data=item["url"]))
|
||||
category.append(new_item)
|
||||
return category
|
||||
|
||||
|
||||
def apply(patch_conf: PatchConfig_AddSettingsMenuItems) -> bool:
|
||||
parser = etree.XMLParser(remove_blank_text=True)
|
||||
preference_main_xml = (
|
||||
f"{config['folders']['decompiled']}/res/xml/preference_main.xml"
|
||||
)
|
||||
|
@ -115,7 +173,22 @@ def apply(patch_conf: PatchConfig_AddSettingsMenuItems) -> bool:
|
|||
f"{config['folders']['decompiled']}/res/xml/preference_additional.xml"
|
||||
)
|
||||
|
||||
parser = etree.XMLParser(remove_blank_text=True)
|
||||
add_icons()
|
||||
|
||||
if os.path.exists(preference_main_xml):
|
||||
tree = etree.parse(preference_main_xml, parser)
|
||||
root = tree.getroot()
|
||||
|
||||
last = root[-1]; pos = root.index(last)
|
||||
for item in patch_conf["main_settings_categories"]:
|
||||
root.insert(pos, add_custom_category(item["title"], item["items"])); pos += 1
|
||||
|
||||
tree.write(
|
||||
preference_main_xml,
|
||||
pretty_print=True,
|
||||
xml_declaration=True,
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
if os.path.exists(preference_additional_xml):
|
||||
tree = etree.parse(preference_additional_xml, parser)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue