forked from anixart-mod/patcher
Обновление структуры проекта, использование pydantic для конфигураций, улучшение отчёта о патчах
This commit is contained in:
+37
-25
@@ -34,28 +34,41 @@ import os
|
||||
import shutil
|
||||
import subprocess
|
||||
from tqdm import tqdm
|
||||
from typing import Dict, List, Any
|
||||
from pydantic import Field
|
||||
|
||||
from utils.config import PatchConfig
|
||||
from utils.smali_parser import get_smali_lines, save_smali_lines
|
||||
|
||||
#Config
|
||||
class Config(PatchConfig):
|
||||
remove_language_files: bool = Field(True, description="Удаляет все языки кроме русского и английского")
|
||||
remove_AI_voiceover: bool = Field(True, description="Заменяет ИИ озвучку маскота аниксы пустыми mp3 файлами")
|
||||
remove_debug_lines: bool = Field(False, description="Удаляет строки `.line n` из smali файлов использованные для дебага")
|
||||
remove_drawable_files: bool = Field(False, description="Удаляет неиспользованные drawable-* из директории decompiled/res")
|
||||
remove_unknown_files: bool = Field(True, description="Удаляет файлы из директории decompiled/unknown")
|
||||
remove_unknown_files_keep_dirs: List[str] = Field(["META-INF", "kotlin"], description="Оставляет указанные директории в decompiled/unknown")
|
||||
compress_png_files: bool = Field(True, description="Сжимает PNG в директории decompiled/res")
|
||||
|
||||
# Patch
|
||||
def remove_unknown_files(config):
|
||||
def remove_unknown_files(config: Config, base: Dict[str, Any]):
|
||||
path = "./decompiled/unknown"
|
||||
items = os.listdir(path)
|
||||
for item in items:
|
||||
item_path = f"{path}/{item}"
|
||||
if os.path.isfile(item_path):
|
||||
os.remove(item_path)
|
||||
if config.get("verbose", False):
|
||||
if base.get("verbose", False):
|
||||
tqdm.write(f"Удалён файл: {item_path}")
|
||||
elif os.path.isdir(item_path):
|
||||
if item not in config["remove_unknown_files_keep_dirs"]:
|
||||
if item not in config.remove_unknown_files_keep_dirs:
|
||||
shutil.rmtree(item_path)
|
||||
if config.get("verbose", False):
|
||||
if base.get("verbose", False):
|
||||
tqdm.write(f"Удалёна директория: {item_path}")
|
||||
return True
|
||||
|
||||
|
||||
def remove_debug_lines(config):
|
||||
def remove_debug_lines(config: Dict[str, Any]):
|
||||
for root, _, files in os.walk("./decompiled"):
|
||||
for filename in files:
|
||||
file_path = os.path.join(root, filename)
|
||||
@@ -72,14 +85,13 @@ def remove_debug_lines(config):
|
||||
return True
|
||||
|
||||
|
||||
def compress_png(config, png_path: str):
|
||||
def compress_png(config: Dict[str, Any], png_path: str):
|
||||
try:
|
||||
assert subprocess.run(
|
||||
[
|
||||
"pngquant",
|
||||
"--force",
|
||||
"--ext",
|
||||
".png",
|
||||
"--ext", ".png",
|
||||
"--quality=65-90",
|
||||
png_path,
|
||||
],
|
||||
@@ -93,7 +105,7 @@ def compress_png(config, png_path: str):
|
||||
return False
|
||||
|
||||
|
||||
def compress_png_files(config):
|
||||
def compress_png_files(config: Dict[str, Any]):
|
||||
compressed = []
|
||||
for root, _, files in os.walk("./decompiled"):
|
||||
for file in files:
|
||||
@@ -103,7 +115,7 @@ def compress_png_files(config):
|
||||
return len(compressed) > 0 and any(compressed)
|
||||
|
||||
|
||||
def remove_AI_voiceover(config):
|
||||
def remove_AI_voiceover(config: Dict[str, Any]):
|
||||
blank = "./resources/blank.mp3"
|
||||
path = "./decompiled/res/raw"
|
||||
files = [
|
||||
@@ -135,7 +147,7 @@ def remove_AI_voiceover(config):
|
||||
return True
|
||||
|
||||
|
||||
def remove_language_files(config):
|
||||
def remove_language_files(config: Dict[str, Any]):
|
||||
path = "./decompiled/res"
|
||||
folders = [
|
||||
"values-af",
|
||||
@@ -236,7 +248,7 @@ def remove_language_files(config):
|
||||
return True
|
||||
|
||||
|
||||
def remove_drawable_files(config):
|
||||
def remove_drawable_files(config: Dict[str, Any]):
|
||||
path = "./decompiled/res"
|
||||
folders = [
|
||||
"drawable-en-hdpi",
|
||||
@@ -269,29 +281,29 @@ def remove_drawable_files(config):
|
||||
return True
|
||||
|
||||
|
||||
def apply(config) -> bool:
|
||||
if config["remove_unknown_files"]:
|
||||
def apply(config: Config, base: Dict[str, Any]) -> bool:
|
||||
if config.remove_unknown_files:
|
||||
tqdm.write(f"Удаление неизвестных файлов...")
|
||||
remove_unknown_files(config)
|
||||
remove_unknown_files(config, base)
|
||||
|
||||
if config["remove_drawable_files"]:
|
||||
if config.remove_drawable_files:
|
||||
tqdm.write(f"Удаление директорий drawable-xx...")
|
||||
remove_drawable_files(config)
|
||||
remove_drawable_files(base)
|
||||
|
||||
if config["compress_png_files"]:
|
||||
if config.compress_png_files:
|
||||
tqdm.write(f"Сжатие PNG файлов...")
|
||||
compress_png_files(config)
|
||||
compress_png_files(base)
|
||||
|
||||
if config["remove_language_files"]:
|
||||
if config.remove_language_files:
|
||||
tqdm.write(f"Удаление языков...")
|
||||
remove_language_files(config)
|
||||
remove_language_files(base)
|
||||
|
||||
if config["remove_AI_voiceover"]:
|
||||
if config.remove_AI_voiceover:
|
||||
tqdm.write(f"Удаление ИИ озвучки...")
|
||||
remove_AI_voiceover(config)
|
||||
remove_AI_voiceover(base)
|
||||
|
||||
if config["remove_debug_lines"]:
|
||||
if config.remove_debug_lines:
|
||||
tqdm.write(f"Удаление дебаг линий...")
|
||||
remove_debug_lines(config)
|
||||
remove_debug_lines(base)
|
||||
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user