|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import filedialog, messagebox, Toplevel |
| 3 | +import zipfile |
| 4 | +import shutil # Added for removing temp directory |
| 5 | +import re |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +PREMIUM_PATTERNS = [ |
| 9 | + (re.compile(r"\b(isPremium|licenseValid|proVersion|hasProAccess)\s*=\s*false\b"), r"\1 = True"), |
| 10 | + (re.compile(r"if\s*\(!?\s*(isPremium|licenseValid|proVersion|hasProAccess)\)"), r"if True"), # Simplified for editor context |
| 11 | + (re.compile(r"checkLicense\s*\([^)]*\)\s*{[^}]*}"), r"checkLicense() { return True }"), # JS/TS like |
| 12 | +] |
| 13 | + |
| 14 | +TEXT_FILE_EXTENSIONS = {'.js', '.ts', '.json', '.html', '.py', '.txt', '.cfg', '.xml', '.md', '.java', '.cs', '.cpp', '.c', '.php'} # Expanded list |
| 15 | + |
| 16 | +class KNEAUXPatchSuiteIntegration: |
| 17 | + def __init__(self, editor_root): |
| 18 | + self.editor_root = editor_root # Main editor window to anchor dialogs |
| 19 | + self.patch_window = None |
| 20 | + |
| 21 | + def show_patch_dialog(self): |
| 22 | + if self.patch_window and self.patch_window.winfo_exists(): |
| 23 | + self.patch_window.lift() |
| 24 | + return |
| 25 | + |
| 26 | + self.patch_window = Toplevel(self.editor_root) |
| 27 | + self.patch_window.title("KNEAUX-COD3 EDU Patch Suite") |
| 28 | + self.patch_window.geometry("450x200") |
| 29 | + self.patch_window.transient(self.editor_root) # Make it a child of the main window |
| 30 | + |
| 31 | + tk.Label(self.patch_window, text="🔧 Select an extension (ZIP/CRX) or folder to patch:").pack(pady=15) |
| 32 | + tk.Button(self.patch_window, text="📂 Select File or Folder", command=self.select_and_process).pack(pady=5) |
| 33 | + tk.Button(self.patch_window, text="✖️ Close", command=self.patch_window.destroy).pack(pady=10) |
| 34 | + |
| 35 | + |
| 36 | + def select_and_process(self): |
| 37 | + # Determine if it's a file or folder dialog needed |
| 38 | + # For simplicity, starting with askopenfilename and then checking type |
| 39 | + path_str = filedialog.askopenfilename( |
| 40 | + parent=self.patch_window, |
| 41 | + title="Select ZIP/CRX file or any file in a target folder", |
| 42 | + filetypes=[("Supported Archives", "*.zip *.crx"), ("All files", "*.*")] |
| 43 | + ) |
| 44 | + |
| 45 | + if not path_str: |
| 46 | + # User cancelled |
| 47 | + # Try asking for a directory if no file was selected, or provide a separate button for directory |
| 48 | + path_str = filedialog.askdirectory( |
| 49 | + parent=self.patch_window, |
| 50 | + title="Select Folder to Patch" |
| 51 | + ) |
| 52 | + if not path_str: |
| 53 | + messagebox.showinfo("Info", "No file or folder selected.", parent=self.patch_window) |
| 54 | + return |
| 55 | + |
| 56 | + file_path = Path(path_str) |
| 57 | + |
| 58 | + if file_path.is_dir(): |
| 59 | + self.scan_and_patch_folder(file_path) |
| 60 | + elif file_path.is_file(): |
| 61 | + if file_path.suffix in ['.zip', '.crx']: |
| 62 | + temp_dir = file_path.parent / (file_path.stem + "_unzipped_temp") |
| 63 | + if temp_dir.exists(): |
| 64 | + shutil.rmtree(temp_dir) # Clean up previous attempt |
| 65 | + temp_dir.mkdir(parents=True, exist_ok=True) |
| 66 | + |
| 67 | + try: |
| 68 | + with zipfile.ZipFile(file_path, 'r') as zip_ref: |
| 69 | + zip_ref.extractall(temp_dir) |
| 70 | + |
| 71 | + patched_files_count = self.scan_and_patch_folder(temp_dir, is_temp=True) |
| 72 | + |
| 73 | + if patched_files_count > 0: |
| 74 | + self.rezip_folder(temp_dir, file_path.parent / (file_path.stem + "_patched" + file_path.suffix)) |
| 75 | + elif patched_files_count == 0: |
| 76 | + messagebox.showinfo("ℹ️ No Changes", "No patchable code found in the archive.", parent=self.patch_window) |
| 77 | + # If patched_files_count is None, an error occurred during patching |
| 78 | + |
| 79 | + except zipfile.BadZipFile: |
| 80 | + messagebox.showerror("Error", "Invalid or corrupted ZIP/CRX file.", parent=self.patch_window) |
| 81 | + except Exception as e: |
| 82 | + messagebox.showerror("Error", f"Failed to process archive: {e}", parent=self.patch_window) |
| 83 | + finally: |
| 84 | + if temp_dir.exists(): |
| 85 | + shutil.rmtree(temp_dir) # Clean up |
| 86 | + elif file_path.suffix in TEXT_FILE_EXTENSIONS: |
| 87 | + # Allow patching a single, currently open, or selected text file |
| 88 | + if self.patch_single_file(file_path): |
| 89 | + messagebox.showinfo("✅ Patch Complete", f"File '{file_path.name}' patched.", parent=self.patch_window) |
| 90 | + else: |
| 91 | + messagebox.showinfo("ℹ️ No Changes", f"No patchable patterns found in '{file_path.name}'.", parent=self.patch_window) |
| 92 | + |
| 93 | + elif file_path.suffix in ['.exe', '.bin']: |
| 94 | + messagebox.showinfo("Note", "Binary patching is not supported. Please select source files, folders, or archives (ZIP/CRX).", parent=self.patch_window) |
| 95 | + else: |
| 96 | + messagebox.showwarning("Unsupported File", f"File type '{file_path.suffix}' is not directly patchable. Select a folder, ZIP, or CRX.", parent=self.patch_window) |
| 97 | + else: |
| 98 | + messagebox.showerror("Error", "Selected path is not a valid file or folder.", parent=self.patch_window) |
| 99 | + |
| 100 | + |
| 101 | + def patch_code_content(self, content): |
| 102 | + original_content = content |
| 103 | + for pattern, repl in PREMIUM_PATTERNS: |
| 104 | + content = pattern.sub(repl, content) |
| 105 | + return content, content != original_content |
| 106 | + |
| 107 | + def patch_single_file(self, file_path: Path): |
| 108 | + try: |
| 109 | + content = file_path.read_text(encoding='utf-8') |
| 110 | + patched_content, changed = self.patch_code_content(content) |
| 111 | + if changed: |
| 112 | + file_path.write_text(patched_content, encoding='utf-8') |
| 113 | + print(f"[+] Patched: {file_path}") |
| 114 | + return True |
| 115 | + except UnicodeDecodeError: |
| 116 | + print(f"[!] Skipping binary or non-UTF-8 file: {file_path.name}") |
| 117 | + except Exception as e: |
| 118 | + messagebox.showerror("File Patch Error", f"Error patching file {file_path.name}:\n{e}", parent=self.patch_window or self.editor_root) |
| 119 | + print(f"[!] Error processing {file_path.name}: {e}") |
| 120 | + return False |
| 121 | + |
| 122 | + def scan_and_patch_folder(self, folder_path: Path, is_temp: bool = False): |
| 123 | + patched_files_count = 0 |
| 124 | + error_occurred = False |
| 125 | + for path_object in folder_path.rglob('*'): |
| 126 | + if path_object.is_file() and path_object.suffix in TEXT_FILE_EXTENSIONS: |
| 127 | + if self.patch_single_file(path_object): |
| 128 | + patched_files_count += 1 |
| 129 | + # Check if patch_single_file returned None due to an error, if so, set error_occurred flag |
| 130 | + # This check is a bit tricky as False means no patches applied, not necessarily an error. |
| 131 | + # We rely on messagebox shown in patch_single_file for error reporting. |
| 132 | + |
| 133 | + if not is_temp: # Only show message for direct folder patching here. Archive patching has its own summary. |
| 134 | + if patched_files_count > 0: |
| 135 | + messagebox.showinfo("✅ Patch Complete", f"{patched_files_count} file(s) patched in folder '{folder_path.name}'.", parent=self.patch_window) |
| 136 | + else: |
| 137 | + messagebox.showinfo("ℹ️ No Changes", f"No patchable code found in folder '{folder_path.name}'.", parent=self.patch_window) |
| 138 | + |
| 139 | + return patched_files_count # Return count for archive processing logic |
| 140 | + |
| 141 | + |
| 142 | + def rezip_folder(self, folder_to_zip: Path, output_zip_path: Path): |
| 143 | + try: |
| 144 | + with zipfile.ZipFile(output_zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf: |
| 145 | + for file in folder_to_zip.rglob('*'): |
| 146 | + if file.is_file(): |
| 147 | + zipf.write(file, file.relative_to(folder_to_zip)) |
| 148 | + messagebox.showinfo("📦 Repack Complete", f"Patched archive saved to:\n{output_zip_path}", parent=self.patch_window) |
| 149 | + except Exception as e: |
| 150 | + messagebox.showerror("Repack Error", f"Failed to repack archive: {e}", parent=self.patch_window) |
| 151 | + print(f"[!] Error repacking {folder_to_zip} to {output_zip_path}: {e}") |
| 152 | + |
| 153 | +# Example of how it might be instantiated from the main editor (for testing purposes) |
| 154 | +if __name__ == '__main__': |
| 155 | + root = tk.Tk() |
| 156 | + root.title("Main Editor Window (Test)") |
| 157 | + root.geometry("600x400") |
| 158 | + |
| 159 | + def open_patcher(): |
| 160 | + patch_suite_instance = KNEAUXPatchSuiteIntegration(root) |
| 161 | + patch_suite_instance.show_patch_dialog() |
| 162 | + |
| 163 | + tk.Button(root, text="Open KNEAUX Patcher", command=open_patcher).pack(pady=20) |
| 164 | + root.mainloop() |
0 commit comments