|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import subprocess |
| 4 | +import threading |
| 5 | +import tkinter as tk |
| 6 | +from tkinter import ttk, messagebox |
| 7 | +import requests |
| 8 | + |
| 9 | +# Constants |
| 10 | +LUA_FILES_DIR = r"d:\antigravity\luapatcher\All Games Files" |
| 11 | +STEAM_PLUGIN_DIR = r"C:\Program Files (x86)\Steam\config\stplug-in" |
| 12 | +STEAM_EXE_PATH = r"C:\Program Files (x86)\Steam\Steam.exe" |
| 13 | + |
| 14 | +class SteamPatcherApp: |
| 15 | + def __init__(self, root): |
| 16 | + self.root = root |
| 17 | + self.root.title("Steam Lua Patcher") |
| 18 | + self.root.geometry("600x500") |
| 19 | + |
| 20 | + # Style |
| 21 | + style = ttk.Style() |
| 22 | + style.theme_use('clam') |
| 23 | + |
| 24 | + # UI Elements |
| 25 | + self.create_widgets() |
| 26 | + |
| 27 | + # Data |
| 28 | + self.search_results = [] |
| 29 | + |
| 30 | + def create_widgets(self): |
| 31 | + # Search Frame |
| 32 | + search_frame = ttk.LabelFrame(self.root, text="Search Game", padding=(10, 10)) |
| 33 | + search_frame.pack(fill="x", padx=10, pady=5) |
| 34 | + |
| 35 | + self.search_var = tk.StringVar() |
| 36 | + self.search_entry = ttk.Entry(search_frame, textvariable=self.search_var) |
| 37 | + self.search_entry.pack(side="left", fill="x", expand=True, padx=5) |
| 38 | + self.search_entry.bind("<Return>", lambda e: self.start_search()) |
| 39 | + |
| 40 | + search_btn = ttk.Button(search_frame, text="Search", command=self.start_search) |
| 41 | + search_btn.pack(side="right", padx=5) |
| 42 | + |
| 43 | + # Results Frame |
| 44 | + results_frame = ttk.LabelFrame(self.root, text="Results", padding=(10, 10)) |
| 45 | + results_frame.pack(fill="both", expand=True, padx=10, pady=5) |
| 46 | + |
| 47 | + columns = ("name", "appid", "status") |
| 48 | + self.tree = ttk.Treeview(results_frame, columns=columns, show="headings", selectmode="browse") |
| 49 | + self.tree.heading("name", text="Game Name") |
| 50 | + self.tree.heading("appid", text="App ID") |
| 51 | + self.tree.heading("status", text="Lua File Status") |
| 52 | + self.tree.column("name", width=300) |
| 53 | + self.tree.column("appid", width=100) |
| 54 | + self.tree.column("status", width=120) |
| 55 | + self.tree.pack(fill="both", expand=True, side="left") |
| 56 | + |
| 57 | + scrollbar = ttk.Scrollbar(results_frame, orient="vertical", command=self.tree.yview) |
| 58 | + scrollbar.pack(side="right", fill="y") |
| 59 | + self.tree.configure(yscrollcommand=scrollbar.set) |
| 60 | + |
| 61 | + # self.tree.bind("<<TreeviewSelect>>", self.on_select) |
| 62 | + |
| 63 | + # Actions Frame |
| 64 | + actions_frame = ttk.Frame(self.root, padding=(10, 10)) |
| 65 | + actions_frame.pack(fill="x", padx=10, pady=5) |
| 66 | + |
| 67 | + self.patch_btn = ttk.Button(actions_frame, text="Patch (Copy Lua)", command=self.patch_selected) |
| 68 | + self.patch_btn.pack(side="left", padx=5) |
| 69 | + |
| 70 | + self.restart_btn = ttk.Button(actions_frame, text="Restart Steam", command=self.restart_steam) |
| 71 | + self.restart_btn.pack(side="right", padx=5) |
| 72 | + |
| 73 | + # Status Bar |
| 74 | + self.status_var = tk.StringVar(value="Ready") |
| 75 | + status_bar = ttk.Label(self.root, textvariable=self.status_var, relief="sunken", anchor="w") |
| 76 | + status_bar.pack(fill="x", side="bottom") |
| 77 | + |
| 78 | + def start_search(self): |
| 79 | + query = self.search_var.get().strip() |
| 80 | + if not query: |
| 81 | + return |
| 82 | + |
| 83 | + self.patch_btn.config(state="disabled") |
| 84 | + self.status_var.set("Searching...") |
| 85 | + self.tree.delete(*self.tree.get_children()) |
| 86 | + |
| 87 | + # Run in thread to not freeze UI |
| 88 | + threading.Thread(target=self.search_logic, args=(query,), daemon=True).start() |
| 89 | + |
| 90 | + def search_logic(self, query): |
| 91 | + try: |
| 92 | + url = "https://store.steampowered.com/api/storesearch" |
| 93 | + params = { |
| 94 | + "term": query, |
| 95 | + "l": "english", |
| 96 | + "cc": "US" |
| 97 | + } |
| 98 | + response = requests.get(url, params=params) |
| 99 | + response.raise_for_status() |
| 100 | + data = response.json() |
| 101 | + |
| 102 | + items = data.get("items", []) |
| 103 | + self.root.after(0, self.update_results, items) |
| 104 | + |
| 105 | + except Exception as e: |
| 106 | + self.root.after(0, lambda: self.status_var.set(f"Error: {e}")) |
| 107 | + |
| 108 | + def update_results(self, items): |
| 109 | + self.search_results = items |
| 110 | + for item in items: |
| 111 | + name = item.get("name") |
| 112 | + appid = item.get("id") |
| 113 | + |
| 114 | + # Check if lua file exists |
| 115 | + lua_path = os.path.join(LUA_FILES_DIR, f"{appid}.lua") |
| 116 | + status = "Found" if os.path.exists(lua_path) else "Not Found" |
| 117 | + |
| 118 | + self.tree.insert("", "end", values=(name, appid, status)) |
| 119 | + |
| 120 | + self.status_var.set(f"Found {len(items)} results.") |
| 121 | + self.patch_btn.config(state="normal") |
| 122 | + |
| 123 | + def patch_selected(self): |
| 124 | + selected = self.tree.selection() |
| 125 | + if not selected: |
| 126 | + messagebox.showwarning("No Selection", "Please select a game to patch.") |
| 127 | + return |
| 128 | + |
| 129 | + item_values = self.tree.item(selected[0])['values'] |
| 130 | + name = item_values[0] |
| 131 | + appid = str(item_values[1]) |
| 132 | + status = item_values[2] |
| 133 | + |
| 134 | + if status != "Found": |
| 135 | + messagebox.showerror("Error", f"Lua file for '{name}' (AppID: {appid}) not found in repository.") |
| 136 | + return |
| 137 | + |
| 138 | + src_file = os.path.join(LUA_FILES_DIR, f"{appid}.lua") |
| 139 | + dest_file = os.path.join(STEAM_PLUGIN_DIR, f"{appid}.lua") |
| 140 | + |
| 141 | + try: |
| 142 | + # Ensure destination dir exists |
| 143 | + if not os.path.exists(STEAM_PLUGIN_DIR): |
| 144 | + os.makedirs(STEAM_PLUGIN_DIR) |
| 145 | + |
| 146 | + shutil.copy2(src_file, dest_file) |
| 147 | + messagebox.showinfo("Success", f"Patched '{name}' successfully!\nCopied to: {dest_file}") |
| 148 | + self.status_var.set(f"Patched {name}") |
| 149 | + except Exception as e: |
| 150 | + messagebox.showerror("Error", f"Failed to copy file: {e}") |
| 151 | + |
| 152 | + def restart_steam(self): |
| 153 | + if not messagebox.askyesno("Confirm Restart", "This will close Steam and all running games. Continue?"): |
| 154 | + return |
| 155 | + |
| 156 | + self.status_var.set("Restarting Steam...") |
| 157 | + |
| 158 | + def restart_thread(): |
| 159 | + try: |
| 160 | + # Kill Steam |
| 161 | + subprocess.run("taskkill /F /IM steam.exe", shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
| 162 | + |
| 163 | + # Wait a bit |
| 164 | + import time |
| 165 | + time.sleep(2) |
| 166 | + |
| 167 | + # Start Steam |
| 168 | + if os.path.exists(STEAM_EXE_PATH): |
| 169 | + subprocess.Popen([STEAM_EXE_PATH]) |
| 170 | + self.root.after(0, lambda: self.status_var.set("Steam restarted.")) |
| 171 | + else: |
| 172 | + # Try protocol handler |
| 173 | + subprocess.run("start steam://open/main", shell=True) |
| 174 | + self.root.after(0, lambda: self.status_var.set("Steam restart command sent.")) |
| 175 | + |
| 176 | + except Exception as e: |
| 177 | + self.root.after(0, lambda: messagebox.showerror("Error", f"Failed to restart Steam: {e}")) |
| 178 | + |
| 179 | + threading.Thread(target=restart_thread, daemon=True).start() |
| 180 | + |
| 181 | +if __name__ == "__main__": |
| 182 | + # Check dependencies check |
| 183 | + try: |
| 184 | + import requests |
| 185 | + except ImportError: |
| 186 | + messagebox.showerror("Error", "Missing 'requests' library. Please run 'pip install requests'") |
| 187 | + sys.exit(1) |
| 188 | + |
| 189 | + root = tk.Tk() |
| 190 | + app = SteamPatcherApp(root) |
| 191 | + root.mainloop() |
0 commit comments