|
| 1 | +from pathlib import Path |
| 2 | +import tkinter as tk |
| 3 | +import tkinter.messagebox as tk1 |
| 4 | +import tkinter.filedialog |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import re |
| 8 | + |
| 9 | +sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) |
| 10 | +try: |
| 11 | + from mod_downloader.mod_downloader import ModDownloader |
| 12 | +except ModuleNotFoundError: |
| 13 | + raise RuntimeError("Couldn't add ModDownloader to the PATH.") |
| 14 | + |
| 15 | + |
| 16 | +OUTPUT_PATH = Path(__file__).parent |
| 17 | +ASSETS_PATH = Path(__file__).resolve().parent / "assets" |
| 18 | + |
| 19 | + |
| 20 | +def relative_to_assets(path: str) -> Path: |
| 21 | + return ASSETS_PATH / Path(path) |
| 22 | + |
| 23 | +def make_label(master, x, y, h, w, *args, **kwargs): |
| 24 | + f = tk.Frame(master, height=h, width=w) |
| 25 | + f.pack_propagate(0) # don't shrink |
| 26 | + f.place(x=x, y=y) |
| 27 | + |
| 28 | + label = tk.Label(f, *args, **kwargs) |
| 29 | + label.pack(fill=tk.BOTH, expand=1) |
| 30 | + |
| 31 | + return label |
| 32 | + |
| 33 | +def select_path(): |
| 34 | + global output_path |
| 35 | + |
| 36 | + output_path = tk.filedialog.askdirectory() |
| 37 | + path_entry.delete(0, tk.END) |
| 38 | + path_entry.insert(0, output_path) |
| 39 | + |
| 40 | + |
| 41 | +def btn_clicked(): |
| 42 | + url = url_entry.get() |
| 43 | + output_path = path_entry.get() |
| 44 | + output_path = output_path.strip() |
| 45 | + |
| 46 | + if not url: |
| 47 | + tk.messagebox.showerror( |
| 48 | + title="Empty Fields!", message="Please enter URL.") |
| 49 | + return |
| 50 | + |
| 51 | + if not output_path: |
| 52 | + tk.messagebox.showerror( |
| 53 | + title="Invalid Path!", message="Enter a valid output path.") |
| 54 | + return |
| 55 | + |
| 56 | + match = re.match(r'^https://mods\.factorio\.com/mod/.*', url.strip()) |
| 57 | + if match is None: |
| 58 | + tk.messagebox.showerror( |
| 59 | + "Invalid URL!", "Please enter a valid file URL.") |
| 60 | + return |
| 61 | + |
| 62 | + output_path = f"{output_path}/mods" |
| 63 | + output = Path(output_path).expanduser().resolve() |
| 64 | + |
| 65 | + if output.exists() and not output.is_dir(): |
| 66 | + tk1.showerror( |
| 67 | + "Exists!", |
| 68 | + f"{output} already exists and is not a directory.\n" |
| 69 | + "Enter a valid output directory.") |
| 70 | + elif output.exists() and output.is_dir() and tuple(output.glob('*')): |
| 71 | + response = tk1.askyesno( |
| 72 | + "Continue?", |
| 73 | + f"Directory {output} is not empty.\n" |
| 74 | + "Do you want to continue and overwrite?") |
| 75 | + if not response: |
| 76 | + return |
| 77 | + |
| 78 | + # Function to add code |
| 79 | + mod_downloader = ModDownloader(url, output_path) |
| 80 | + mod_downloader.start_download() |
| 81 | + |
| 82 | + tk.messagebox.showinfo( |
| 83 | + "Success!", f"Project successfully generated at {output}.") |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +# Required in order to add data files to Windows executable |
| 89 | +path = getattr(sys, '_MEIPASS', os.getcwd()) |
| 90 | +os.chdir(path) |
| 91 | + |
| 92 | +output_path = "" |
| 93 | + |
| 94 | +window = tk.Tk() |
| 95 | +window.geometry("862x519") |
| 96 | +window.configure(bg = "#3A7FF6") |
| 97 | +window.title("Factorio Mod Downloader") |
| 98 | + |
| 99 | + |
| 100 | +canvas = tk.Canvas( |
| 101 | + window, |
| 102 | + bg = "#3A7FF6", |
| 103 | + height = 519, |
| 104 | + width = 862, |
| 105 | + bd = 0, |
| 106 | + highlightthickness = 0, |
| 107 | + relief = "ridge" |
| 108 | +) |
| 109 | + |
| 110 | +canvas.place(x = 0, y = 0) |
| 111 | +canvas.create_rectangle( |
| 112 | + 430.9999999999999, |
| 113 | + 0.0, |
| 114 | + 861.9999999999999, |
| 115 | + 519.0, |
| 116 | + fill="#FCFCFC", |
| 117 | + outline="") |
| 118 | + |
| 119 | +canvas.create_text( |
| 120 | + 482.0, |
| 121 | + 60.0, |
| 122 | + anchor="nw", |
| 123 | + text="Enter the details.", |
| 124 | + fill="#505485", |
| 125 | + font=("Roboto Bold", 24 * -1) |
| 126 | +) |
| 127 | + |
| 128 | +canvas.create_text( |
| 129 | + 490.0, 139.0, text="Factorio Mod Url", |
| 130 | + fill="#515486", font=("Arial-BoldMT", int(13.0)), anchor="w") |
| 131 | + |
| 132 | +url_entry_image = tk.PhotoImage( |
| 133 | + file=relative_to_assets("entry.png")) |
| 134 | +url_entry_bg = canvas.create_image( |
| 135 | + 650.5, |
| 136 | + 184.5, |
| 137 | + image=url_entry_image |
| 138 | +) |
| 139 | +url_entry = tk.Entry( |
| 140 | + bd=0, |
| 141 | + bg="#F1F5FF", |
| 142 | + fg="#000716", |
| 143 | + highlightthickness=0 |
| 144 | +) |
| 145 | +url_entry.place( |
| 146 | + x=490, |
| 147 | + y=154.0, |
| 148 | + width=321.0, |
| 149 | + height=59.0 |
| 150 | +) |
| 151 | + |
| 152 | + |
| 153 | +canvas.create_text( |
| 154 | + 490.0, 251.0, text="Output Path", |
| 155 | + fill="#515486", font=("Arial-BoldMT", int(13.0)), anchor="w") |
| 156 | + |
| 157 | +path_entry_image = tk.PhotoImage( |
| 158 | + file=relative_to_assets("entry.png")) |
| 159 | +path_entry_bg = canvas.create_image( |
| 160 | + 650.5, |
| 161 | + 296.5, |
| 162 | + image=path_entry_image |
| 163 | +) |
| 164 | +path_entry = tk.Entry( |
| 165 | + bd=0, |
| 166 | + bg="#F1F5FF", |
| 167 | + fg="#000716", |
| 168 | + highlightthickness=0 |
| 169 | +) |
| 170 | +path_entry.place( |
| 171 | + x=490.0, |
| 172 | + y=266.0, |
| 173 | + width=321.0, |
| 174 | + height=59.0 |
| 175 | +) |
| 176 | + |
| 177 | +path_picker_img = tk.PhotoImage( |
| 178 | + file=relative_to_assets("path_picker_button.png")) |
| 179 | + |
| 180 | +path_picker_button = tk.Button( |
| 181 | + image = path_picker_img, |
| 182 | + text = '', |
| 183 | + compound = 'center', |
| 184 | + fg = 'white', |
| 185 | + borderwidth = 0, |
| 186 | + highlightthickness = 0, |
| 187 | + command = select_path, |
| 188 | + relief = 'flat') |
| 189 | + |
| 190 | + |
| 191 | +path_picker_button.place( |
| 192 | + x=783.0, |
| 193 | + y=286.0, |
| 194 | + width=24.0, |
| 195 | + height=22.0 |
| 196 | +) |
| 197 | + |
| 198 | +generate_image = tk.PhotoImage( |
| 199 | + file=relative_to_assets("button_1.png")) |
| 200 | +generate_button = tk.Button( |
| 201 | + image=generate_image, |
| 202 | + borderwidth=0, |
| 203 | + highlightthickness=0, |
| 204 | + command=btn_clicked, |
| 205 | + relief="flat" |
| 206 | +) |
| 207 | +generate_button.place( |
| 208 | + x=557.0, |
| 209 | + y=360.0, |
| 210 | + width=180.0, |
| 211 | + height=55.0 |
| 212 | +) |
| 213 | + |
| 214 | + |
| 215 | +canvas.create_text( |
| 216 | + 70.0, |
| 217 | + 214.0, |
| 218 | + anchor="nw", |
| 219 | + text="Factorio Mods\nDownloader", |
| 220 | + fill="#FFFFFF", |
| 221 | + font=("Roboto Bold", 34 * -1) |
| 222 | +) |
| 223 | + |
| 224 | +window.resizable(False, False) |
| 225 | +window.mainloop() |
0 commit comments