| 
									
										
										
										
											2022-12-08 08:54:49 -06:00
										 |  |  | import base64 | 
					
						
							|  |  |  | import datetime | 
					
						
							| 
									
										
										
										
											2020-02-02 06:25:06 +01:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2022-12-08 08:54:49 -06:00
										 |  |  | import platform | 
					
						
							| 
									
										
										
										
											2020-02-02 06:25:06 +01:00
										 |  |  | import shutil | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | import sysconfig | 
					
						
							| 
									
										
										
										
											2023-07-05 02:53:34 +02:00
										 |  |  | import warnings | 
					
						
							| 
									
										
										
										
											2022-12-07 06:40:30 +01:00
										 |  |  | import zipfile | 
					
						
							| 
									
										
										
										
											2023-03-05 14:10:05 +01:00
										 |  |  | import urllib.request | 
					
						
							|  |  |  | import io | 
					
						
							|  |  |  | import json | 
					
						
							|  |  |  | import threading | 
					
						
							| 
									
										
										
										
											2023-03-06 12:54:32 +01:00
										 |  |  | import subprocess | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-08 08:54:49 -06:00
										 |  |  | from hashlib import sha3_512 | 
					
						
							|  |  |  | from pathlib import Path | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  | from typing import Dict, Iterable, List, Optional, Sequence, Set, Tuple, Union | 
					
						
							| 
									
										
										
										
											2023-03-06 12:54:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-08 08:54:49 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-30 01:47:28 +01:00
										 |  |  | # This is a bit jank. We need cx-Freeze to be able to run anything from this script, so install it | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  | requirement = 'cx-Freeze==7.2.0' | 
					
						
							| 
									
										
										
										
											2023-01-30 01:47:28 +01:00
										 |  |  | try: | 
					
						
							| 
									
										
										
										
											2023-03-25 19:54:42 +01:00
										 |  |  |     import pkg_resources | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         pkg_resources.require(requirement) | 
					
						
							|  |  |  |         install_cx_freeze = False | 
					
						
							|  |  |  |     except pkg_resources.ResolutionError: | 
					
						
							|  |  |  |         install_cx_freeze = True | 
					
						
							|  |  |  | except ImportError: | 
					
						
							|  |  |  |     install_cx_freeze = True | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |     pkg_resources = None  # type: ignore[assignment] | 
					
						
							| 
									
										
										
										
											2023-03-25 19:54:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | if install_cx_freeze: | 
					
						
							|  |  |  |     # check if pip is available | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         import pip  # noqa: F401 | 
					
						
							|  |  |  |     except ImportError: | 
					
						
							|  |  |  |         raise RuntimeError("pip not available. Please install pip.") | 
					
						
							|  |  |  |     # install and import cx_freeze | 
					
						
							| 
									
										
										
										
											2023-01-30 01:47:28 +01:00
										 |  |  |     if '--yes' not in sys.argv and '-y' not in sys.argv: | 
					
						
							|  |  |  |         input(f'Requirement {requirement} is not satisfied, press enter to install it') | 
					
						
							|  |  |  |     subprocess.call([sys.executable, '-m', 'pip', 'install', requirement, '--upgrade']) | 
					
						
							| 
									
										
										
										
											2023-03-25 19:54:42 +01:00
										 |  |  |     import pkg_resources | 
					
						
							| 
									
										
										
										
											2023-03-26 00:13:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import cx_Freeze | 
					
						
							| 
									
										
										
										
											2023-01-30 01:47:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | # .build only exists if cx-Freeze is the right version, so we have to update/install that first before this line | 
					
						
							|  |  |  | import setuptools.command.build | 
					
						
							| 
									
										
										
										
											2020-02-02 06:25:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-17 18:49:33 +01:00
										 |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											2023-01-30 01:47:28 +01:00
										 |  |  |     # need to run this early to import from Utils and Launcher | 
					
						
							|  |  |  |     # TODO: move stuff to not require this | 
					
						
							| 
									
										
										
										
											2023-01-17 18:49:33 +01:00
										 |  |  |     import ModuleUpdate | 
					
						
							| 
									
										
										
										
											2023-01-30 01:47:28 +01:00
										 |  |  |     ModuleUpdate.update(yes="--yes" in sys.argv or "-y" in sys.argv) | 
					
						
							| 
									
										
										
										
											2023-01-17 18:49:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 21:24:47 +01:00
										 |  |  | from worlds.LauncherComponents import components, icon_paths | 
					
						
							| 
									
										
										
										
											2022-12-08 08:54:49 -06:00
										 |  |  | from Utils import version_tuple, is_windows, is_linux | 
					
						
							| 
									
										
										
										
											2023-07-04 19:12:43 +02:00
										 |  |  | from Cython.Build import cythonize | 
					
						
							| 
									
										
										
										
											2020-12-06 14:36:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-30 01:47:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-19 06:49:38 +01:00
										 |  |  | # On  Python < 3.10 LogicMixin is not currently supported. | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  | non_apworlds: Set[str] = { | 
					
						
							| 
									
										
										
										
											2023-06-25 03:47:38 +02:00
										 |  |  |     "A Link to the Past", | 
					
						
							|  |  |  |     "Adventure", | 
					
						
							|  |  |  |     "ArchipIDLE", | 
					
						
							|  |  |  |     "Archipelago", | 
					
						
							|  |  |  |     "Clique", | 
					
						
							|  |  |  |     "Final Fantasy", | 
					
						
							|  |  |  |     "Lufia II Ancient Cave", | 
					
						
							|  |  |  |     "Meritous", | 
					
						
							|  |  |  |     "Ocarina of Time", | 
					
						
							|  |  |  |     "Overcooked! 2", | 
					
						
							|  |  |  |     "Raft", | 
					
						
							|  |  |  |     "Slay the Spire", | 
					
						
							|  |  |  |     "Sudoku", | 
					
						
							|  |  |  |     "Super Mario 64", | 
					
						
							|  |  |  |     "VVVVVV", | 
					
						
							|  |  |  |     "Wargroove", | 
					
						
							| 
									
										
										
										
											2022-12-07 06:40:30 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-29 17:40:13 +02:00
										 |  |  | # LogicMixin is broken before 3.10 import revamp | 
					
						
							|  |  |  | if sys.version_info < (3,10): | 
					
						
							|  |  |  |     non_apworlds.add("Hollow Knight") | 
					
						
							| 
									
										
										
										
											2023-03-05 14:10:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  | def download_SNI() -> None: | 
					
						
							| 
									
										
										
										
											2023-03-05 14:10:05 +01:00
										 |  |  |     print("Updating SNI") | 
					
						
							|  |  |  |     machine_to_go = { | 
					
						
							|  |  |  |         "x86_64": "amd64", | 
					
						
							|  |  |  |         "aarch64": "arm64", | 
					
						
							|  |  |  |         "armv7l": "arm" | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     platform_name = platform.system().lower() | 
					
						
							|  |  |  |     machine_name = platform.machine().lower() | 
					
						
							|  |  |  |     # force amd64 on macos until we have universal2 sni, otherwise resolve to GOARCH | 
					
						
							| 
									
										
										
										
											2024-10-27 00:28:47 +02:00
										 |  |  |     machine_name = "universal" if platform_name == "darwin" else machine_to_go.get(machine_name, machine_name) | 
					
						
							| 
									
										
										
										
											2023-03-05 14:10:05 +01:00
										 |  |  |     with urllib.request.urlopen("https://api.github.com/repos/alttpo/sni/releases/latest") as request: | 
					
						
							|  |  |  |         data = json.load(request) | 
					
						
							|  |  |  |     files = data["assets"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     source_url = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for file in files: | 
					
						
							|  |  |  |         download_url: str = file["browser_download_url"] | 
					
						
							|  |  |  |         machine_match = download_url.rsplit("-", 1)[1].split(".", 1)[0] == machine_name | 
					
						
							|  |  |  |         if platform_name in download_url and machine_match: | 
					
						
							| 
									
										
										
										
											2024-10-27 00:28:47 +02:00
										 |  |  |             source_url = download_url | 
					
						
							| 
									
										
										
										
											2023-03-05 14:10:05 +01:00
										 |  |  |             # prefer "many" builds | 
					
						
							|  |  |  |             if "many" in download_url: | 
					
						
							|  |  |  |                 break | 
					
						
							| 
									
										
										
										
											2024-10-27 00:28:47 +02:00
										 |  |  |             # prefer the correct windows or windows7 build | 
					
						
							|  |  |  |             if platform_name == "windows" and ("windows7" in download_url) == (sys.version_info < (3, 9)): | 
					
						
							|  |  |  |                 break | 
					
						
							| 
									
										
										
										
											2023-03-05 14:10:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if source_url and source_url.endswith(".zip"): | 
					
						
							|  |  |  |         with urllib.request.urlopen(source_url) as download: | 
					
						
							|  |  |  |             with zipfile.ZipFile(io.BytesIO(download.read()), "r") as zf: | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |                 for zf_member in zf.infolist(): | 
					
						
							|  |  |  |                     zf.extract(zf_member, path="SNI") | 
					
						
							| 
									
										
										
										
											2023-03-05 14:10:05 +01:00
										 |  |  |         print(f"Downloaded SNI from {source_url}") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     elif source_url and (source_url.endswith(".tar.xz") or source_url.endswith(".tar.gz")): | 
					
						
							|  |  |  |         import tarfile | 
					
						
							|  |  |  |         mode = "r:xz" if source_url.endswith(".tar.xz") else "r:gz" | 
					
						
							|  |  |  |         with urllib.request.urlopen(source_url) as download: | 
					
						
							|  |  |  |             sni_dir = None | 
					
						
							|  |  |  |             with tarfile.open(fileobj=io.BytesIO(download.read()), mode=mode) as tf: | 
					
						
							|  |  |  |                 for member in tf.getmembers(): | 
					
						
							|  |  |  |                     if member.name.startswith("/") or "../" in member.name: | 
					
						
							|  |  |  |                         raise ValueError(f"Unexpected file '{member.name}' in {source_url}") | 
					
						
							|  |  |  |                     elif member.isdir() and not sni_dir: | 
					
						
							|  |  |  |                         sni_dir = member.name | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |                     elif member.isfile() and not sni_dir or sni_dir and not member.name.startswith(sni_dir): | 
					
						
							| 
									
										
										
										
											2023-03-05 14:10:05 +01:00
										 |  |  |                         raise ValueError(f"Expected folder before '{member.name}' in {source_url}") | 
					
						
							|  |  |  |                     elif member.isfile() and sni_dir: | 
					
						
							|  |  |  |                         tf.extract(member) | 
					
						
							|  |  |  |             # sadly SNI is in its own folder on non-windows, so we need to rename | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |             if not sni_dir: | 
					
						
							|  |  |  |                 raise ValueError("Did not find SNI in archive") | 
					
						
							| 
									
										
										
										
											2023-03-05 14:10:05 +01:00
										 |  |  |             shutil.rmtree("SNI", True) | 
					
						
							|  |  |  |             os.rename(sni_dir, "SNI") | 
					
						
							|  |  |  |         print(f"Downloaded SNI from {source_url}") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     elif source_url: | 
					
						
							|  |  |  |         print(f"Don't know how to extract SNI from {source_url}") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         print(f"No SNI found for system spec {platform_name} {machine_name}") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  | signtool: Optional[str] | 
					
						
							| 
									
										
										
										
											2020-05-01 18:51:08 +02:00
										 |  |  | if os.path.exists("X:/pw.txt"): | 
					
						
							|  |  |  |     print("Using signtool") | 
					
						
							| 
									
										
										
										
											2022-05-17 21:40:03 +02:00
										 |  |  |     with open("X:/pw.txt", encoding="utf-8-sig") as f: | 
					
						
							| 
									
										
										
										
											2020-05-01 18:51:08 +02:00
										 |  |  |         pw = f.read() | 
					
						
							| 
									
										
										
										
											2023-01-29 22:14:20 +01:00
										 |  |  |     signtool = r'signtool sign /f X:/_SITS_Zertifikat_.pfx /p "' + pw + \ | 
					
						
							|  |  |  |                r'" /fd sha256 /tr http://timestamp.digicert.com/ ' | 
					
						
							| 
									
										
										
										
											2020-05-01 18:51:08 +02:00
										 |  |  | else: | 
					
						
							|  |  |  |     signtool = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 06:25:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-05 22:52:16 +02:00
										 |  |  | build_platform = sysconfig.get_platform() | 
					
						
							|  |  |  | arch_folder = "exe.{platform}-{version}".format(platform=build_platform, | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |                                                 version=sysconfig.get_python_version()) | 
					
						
							|  |  |  | buildfolder = Path("build", arch_folder) | 
					
						
							| 
									
										
										
										
											2022-06-05 22:52:16 +02:00
										 |  |  | build_arch = build_platform.split('-')[-1] if '-' in build_platform else platform.machine() | 
					
						
							| 
									
										
										
										
											2020-12-06 14:36:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 19:33:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  | # see Launcher.py on how to add scripts to setup.py | 
					
						
							| 
									
										
										
										
											2023-04-30 18:10:58 +02:00
										 |  |  | def resolve_icon(icon_name: str): | 
					
						
							|  |  |  |     base_path = icon_paths[icon_name] | 
					
						
							|  |  |  |     if is_windows: | 
					
						
							|  |  |  |         path, extension = os.path.splitext(base_path) | 
					
						
							|  |  |  |         ico_file = path + ".ico" | 
					
						
							|  |  |  |         assert os.path.exists(ico_file), f"ico counterpart of {base_path} should exist." | 
					
						
							|  |  |  |         return ico_file | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         return base_path | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  | exes = [ | 
					
						
							|  |  |  |     cx_Freeze.Executable( | 
					
						
							| 
									
										
										
										
											2023-08-29 20:59:39 +02:00
										 |  |  |         script=f"{c.script_name}.py", | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         target_name=c.frozen_name + (".exe" if is_windows else ""), | 
					
						
							| 
									
										
										
										
											2023-04-30 18:10:58 +02:00
										 |  |  |         icon=resolve_icon(c.icon), | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         base="Win32GUI" if is_windows and not c.cli else None | 
					
						
							| 
									
										
										
										
											2023-03-25 19:54:42 +01:00
										 |  |  |     ) for c in components if c.script_name and c.frozen_name | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  | ] | 
					
						
							| 
									
										
										
										
											2021-08-06 19:33:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-29 20:59:39 +02:00
										 |  |  | if is_windows: | 
					
						
							|  |  |  |     # create a duplicate Launcher for Windows, which has a working stdout/stderr, for debugging and --help | 
					
						
							|  |  |  |     c = next(component for component in components if component.script_name == "Launcher") | 
					
						
							|  |  |  |     exes.append(cx_Freeze.Executable( | 
					
						
							|  |  |  |         script=f"{c.script_name}.py", | 
					
						
							| 
									
										
										
										
											2024-06-05 21:00:53 +02:00
										 |  |  |         target_name=f"{c.frozen_name}Debug.exe", | 
					
						
							| 
									
										
										
										
											2023-08-29 20:59:39 +02:00
										 |  |  |         icon=resolve_icon(c.icon), | 
					
						
							|  |  |  |     )) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-05 22:39:35 +02:00
										 |  |  | extra_data = ["LICENSE", "data", "EnemizerCLI", "SNI"] | 
					
						
							| 
									
										
										
										
											2023-01-25 00:20:26 +01:00
										 |  |  | extra_libs = ["libssl.so", "libcrypto.so"] if is_linux else [] | 
					
						
							| 
									
										
										
										
											2020-02-02 06:25:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
											  
											
												WebUI (#100)
* Object-Oriented base changes for web-ui prep
* remove debug raise
* optimize broadcast to serialize once
* Implement WebUI socket, static assets, and classes
- Still need to wrap logging functions and send output to UI
- UI commands are successfully being sent to the server
* GUI operational. Wrap logging functions, implement server address selection on GUI, automatically launch web browser when client websocket is served
* Update MultiServer status when a user disconnects / reconnects
* Implement colored item and hint checks, improve GUI readability
* Fix improper formatting on received items
* Update SNES connection status on disconnect / reconnect. Implement itemFound, prevent accidentally printing JS objects
* Minor text change for itemFound
* Fixed a very wrong comment
* Fixed client commands not working, fixed un-helpful error messages appearing in GUI
* Fix a bug causing a failure to connect to a multiworld server if a previously existing cached address was present and the client was loaded without an address passed in
* Convert WebUI to React /w Redux. WebSocket communications not yet operational.
* WebUI fully converted to React / Redux.
- Websocket communication operational
- Added a button to connect to the multiserver which appears only when a SNES is connected and a server connection is not active
* Restore some features lost in WebUI
- Restore (found) notification on hints if the item has already been obtained
- Restore (x/y) indicator on received items, which indicates the number of items the client is waiting to receive from the client in a queue
* Fix a grammatical UI big causing player names to show only an apostrophe when possessive
* Add support for multiple SNES Devices, and switching between them
* freeze support for client
* make sure flask works when frozen
* UI Improvements
- Hint messages now actually show a found status via ✔ and ❌ emoji
- Active player name is always a different color than other players (orange for now)
- Add a toggle to show only entries relevant to the active player
- Added a WidgetArea
- Added a notes widget
* Received items now marked as relevant
* Include production build for deployment
* Notes now survive a browser close. Minimum width applied to monitor to prevent CSS issues.
* include webUi folder in setup.py
* Bugfixes for Monitor
- Fix a bug causing the monitor window to grow beyond it's intended content limit
- Reduced monitor content limit to 200 items
- Ensured each monitor entry has a unique key
* Prevent eslint from yelling at me about stupid things
* Add button to collapse sidebar, press enter on empty server input to disconnect on purpose
* WebUI is now aware of client disconnect, message log limit increased to 350, fix !missing output
* Update WebUI to v2.2.1
- Added color to WebUI for entrance-span
- Make !missing show total count at bottom of list to match /missing behavior
* Fix a bug causing clients version <= 2.2.0 to crash when anyone asks for a hint
- Also fix a bug in the WebUI causing the entrance location to always show as "somewhere"
* Update WebUI color palette (this cost me $50)
* allow text console input alongside web-ui
* remove Flask
a bit overkill for what we're doing
* remove jinja2
* Update WebUI to work with new hosting mechanism
* with flask gone, we no longer need subprocess shenanigans
* If multiple web ui clients try to run, at least present a working console
* Update MultiClient and WebUI to handle multiple clients simultaneously.
- The port on which the websocket for the WebUI is hosted is not chosen randomly from 5000 - 5999. This port is passed to the browser so it knows which MultiClient to connect to
- Removed failure condition if a web server is already running, as there is no need to run more than one web server on a single system. If an exception is thrown while attempting to launch a web server, a check is made for the port being unavailable. If the port is unavailable, it probably means the user is launching a second MultiClient. A web browser is then opened with a connection to the correct webui_socket_port.
- Add a /web command to the MultiClient to repoen the appropriate browser window and get params in case a user accidentally closes the tab
* Use proper name for WebUI
* move webui into /data with other data files
* make web ui optional
This is mostly for laptop users wanting to preserve some battery, should not be needed outside of that.
* fix direct server start
* re-add connection timer
* fix indentation
Co-authored-by: Chris <chris@legendserver.info>
											
										 
											2020-06-03 21:29:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  | def remove_sprites_from_folder(folder: Path) -> None: | 
					
						
							| 
									
										
										
										
											2021-07-31 00:03:48 +02:00
										 |  |  |     for file in os.listdir(folder): | 
					
						
							|  |  |  |         if file != ".gitignore": | 
					
						
							|  |  |  |             os.remove(folder / file) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  | def _threaded_hash(filepath: Union[str, Path]) -> str: | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |     hasher = sha3_512() | 
					
						
							|  |  |  |     hasher.update(open(filepath, "rb").read()) | 
					
						
							|  |  |  |     return base64.b85encode(hasher.digest()).decode() | 
					
						
							| 
									
										
										
										
											2020-02-02 06:25:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  | # cx_Freeze's build command runs other commands. Override to accept --yes and store that. | 
					
						
							| 
									
										
										
										
											2023-01-25 00:20:26 +01:00
										 |  |  | class BuildCommand(setuptools.command.build.build): | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |     user_options = [ | 
					
						
							|  |  |  |         ('yes', 'y', 'Answer "yes" to all questions.'), | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |     yes: bool | 
					
						
							|  |  |  |     last_yes: bool = False  # used by sub commands of build | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |     def initialize_options(self) -> None: | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         super().initialize_options() | 
					
						
							|  |  |  |         type(self).last_yes = self.yes = False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |     def finalize_options(self) -> None: | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         super().finalize_options() | 
					
						
							|  |  |  |         type(self).last_yes = self.yes | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Override cx_Freeze's build_exe command for pre and post build steps | 
					
						
							| 
									
										
										
										
											2024-05-01 01:48:32 +02:00
										 |  |  | class BuildExeCommand(cx_Freeze.command.build_exe.build_exe): | 
					
						
							|  |  |  |     user_options = cx_Freeze.command.build_exe.build_exe.user_options + [ | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         ('yes', 'y', 'Answer "yes" to all questions.'), | 
					
						
							|  |  |  |         ('extra-data=', None, 'Additional files to add.'), | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |     yes: bool | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |     extra_data: Iterable[str] | 
					
						
							|  |  |  |     extra_libs: Iterable[str]  # work around broken include_files | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     buildfolder: Path | 
					
						
							|  |  |  |     libfolder: Path | 
					
						
							|  |  |  |     library: Path | 
					
						
							|  |  |  |     buildtime: datetime.datetime | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |     def initialize_options(self) -> None: | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         super().initialize_options() | 
					
						
							|  |  |  |         self.yes = BuildCommand.last_yes | 
					
						
							|  |  |  |         self.extra_data = [] | 
					
						
							| 
									
										
										
										
											2023-01-25 00:20:26 +01:00
										 |  |  |         self.extra_libs = [] | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |     def finalize_options(self) -> None: | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         super().finalize_options() | 
					
						
							|  |  |  |         self.buildfolder = self.build_exe | 
					
						
							|  |  |  |         self.libfolder = Path(self.buildfolder, "lib") | 
					
						
							|  |  |  |         self.library = Path(self.libfolder, "library.zip") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |     def installfile(self, path: Path, subpath: Optional[Union[str, Path]] = None, keep_content: bool = False) -> None: | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         folder = self.buildfolder | 
					
						
							| 
									
										
										
										
											2022-06-14 08:55:57 +02:00
										 |  |  |         if subpath: | 
					
						
							|  |  |  |             folder /= subpath | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         print('copying', path, '->', folder) | 
					
						
							|  |  |  |         if path.is_dir(): | 
					
						
							|  |  |  |             folder /= path.name | 
					
						
							|  |  |  |             if folder.is_dir() and not keep_content: | 
					
						
							|  |  |  |                 shutil.rmtree(folder) | 
					
						
							|  |  |  |             shutil.copytree(path, folder, dirs_exist_ok=True) | 
					
						
							|  |  |  |         elif path.is_file(): | 
					
						
							|  |  |  |             shutil.copy(path, folder) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             print('Warning,', path, 'not found') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |     def create_manifest(self, create_hashes: bool = False) -> None: | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         # Since the setup is now split into components and the manifest is not, | 
					
						
							|  |  |  |         # it makes most sense to just remove the hashes for now. Not aware of anyone using them. | 
					
						
							|  |  |  |         hashes = {} | 
					
						
							|  |  |  |         manifestpath = os.path.join(self.buildfolder, "manifest.json") | 
					
						
							|  |  |  |         if create_hashes: | 
					
						
							|  |  |  |             from concurrent.futures import ThreadPoolExecutor | 
					
						
							|  |  |  |             pool = ThreadPoolExecutor() | 
					
						
							|  |  |  |             for dirpath, dirnames, filenames in os.walk(self.buildfolder): | 
					
						
							|  |  |  |                 for filename in filenames: | 
					
						
							|  |  |  |                     path = os.path.join(dirpath, filename) | 
					
						
							|  |  |  |                     hashes[os.path.relpath(path, start=self.buildfolder)] = pool.submit(_threaded_hash, path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         import json | 
					
						
							|  |  |  |         manifest = { | 
					
						
							|  |  |  |             "buildtime": self.buildtime.isoformat(sep=" ", timespec="seconds"), | 
					
						
							|  |  |  |             "hashes": {path: hash.result() for path, hash in hashes.items()}, | 
					
						
							|  |  |  |             "version": version_tuple} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         json.dump(manifest, open(manifestpath, "wt"), indent=4) | 
					
						
							|  |  |  |         print("Created Manifest") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |     def run(self) -> None: | 
					
						
							| 
									
										
										
										
											2023-03-05 14:10:05 +01:00
										 |  |  |         # start downloading sni asap | 
					
						
							|  |  |  |         sni_thread = threading.Thread(target=download_SNI, name="SNI Downloader") | 
					
						
							|  |  |  |         sni_thread.start() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-04 19:12:43 +02:00
										 |  |  |         # pre-build steps | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         print(f"Outputting to: {self.buildfolder}") | 
					
						
							|  |  |  |         os.makedirs(self.buildfolder, exist_ok=True) | 
					
						
							|  |  |  |         import ModuleUpdate | 
					
						
							|  |  |  |         ModuleUpdate.update(yes=self.yes) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-04 19:12:43 +02:00
										 |  |  |         # auto-build cython modules | 
					
						
							|  |  |  |         build_ext = self.distribution.get_command_obj("build_ext") | 
					
						
							| 
									
										
										
										
											2023-07-05 02:53:34 +02:00
										 |  |  |         build_ext.inplace = False | 
					
						
							| 
									
										
										
										
											2023-07-04 19:12:43 +02:00
										 |  |  |         self.run_command("build_ext") | 
					
						
							| 
									
										
										
										
											2023-07-05 02:53:34 +02:00
										 |  |  |         # find remains of previous in-place builds, try to delete and warn otherwise | 
					
						
							|  |  |  |         for path in build_ext.get_outputs(): | 
					
						
							|  |  |  |             parts = os.path.split(path)[-1].split(".") | 
					
						
							|  |  |  |             pattern = parts[0] + ".*." + parts[-1] | 
					
						
							|  |  |  |             for match in Path().glob(pattern): | 
					
						
							|  |  |  |                 try: | 
					
						
							|  |  |  |                     match.unlink() | 
					
						
							|  |  |  |                     print(f"Removed {match}") | 
					
						
							|  |  |  |                 except Exception as ex: | 
					
						
							|  |  |  |                     warnings.warn(f"Could not delete old build output: {match}\n" | 
					
						
							|  |  |  |                                   f"{ex}\nPlease close all AP instances and delete manually.") | 
					
						
							| 
									
										
										
										
											2023-07-04 19:12:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         # regular cx build | 
					
						
							| 
									
										
										
										
											2024-11-29 01:57:18 +01:00
										 |  |  |         self.buildtime = datetime.datetime.now(datetime.timezone.utc) | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         super().run() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-05 02:53:34 +02:00
										 |  |  |         # manually copy built modules to lib folder. cx_Freeze does not know they exist. | 
					
						
							|  |  |  |         for src in build_ext.get_outputs(): | 
					
						
							|  |  |  |             print(f"copying {src} -> {self.libfolder}") | 
					
						
							|  |  |  |             shutil.copy(src, self.libfolder, follow_symlinks=False) | 
					
						
							| 
									
										
										
										
											2023-07-04 19:12:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-05 14:10:05 +01:00
										 |  |  |         # need to finish download before copying | 
					
						
							|  |  |  |         sni_thread.join() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-25 00:20:26 +01:00
										 |  |  |         # include_files seems to not be done automatically. implement here | 
					
						
							| 
									
										
										
										
											2022-06-05 22:52:16 +02:00
										 |  |  |         for src, dst in self.include_files: | 
					
						
							| 
									
										
										
										
											2023-01-25 00:20:26 +01:00
										 |  |  |             print(f"copying {src} -> {self.buildfolder / dst}") | 
					
						
							|  |  |  |             shutil.copyfile(src, self.buildfolder / dst, follow_symlinks=False) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # now that include_files is completely broken, run find_libs here | 
					
						
							|  |  |  |         for src, dst in find_libs(*self.extra_libs): | 
					
						
							|  |  |  |             print(f"copying {src} -> {self.buildfolder / dst}") | 
					
						
							| 
									
										
										
										
											2022-06-05 22:52:16 +02:00
										 |  |  |             shutil.copyfile(src, self.buildfolder / dst, follow_symlinks=False) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         # post build steps | 
					
						
							| 
									
										
										
										
											2023-01-25 00:20:26 +01:00
										 |  |  |         if is_windows:  # kivy_deps is win32 only, linux picks them up automatically | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |             from kivy_deps import sdl2, glew  # type: ignore | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |             for folder in sdl2.dep_bins + glew.dep_bins: | 
					
						
							|  |  |  |                 shutil.copytree(folder, self.libfolder, dirs_exist_ok=True) | 
					
						
							| 
									
										
										
										
											2023-01-25 00:20:26 +01:00
										 |  |  |                 print(f"copying {folder} -> {self.libfolder}") | 
					
						
							| 
									
										
										
										
											2024-01-18 01:52:33 +01:00
										 |  |  |             # windows needs Visual Studio C++ Redistributable | 
					
						
							|  |  |  |             # Installer works for x64 and arm64 | 
					
						
							|  |  |  |             print("Downloading VC Redist") | 
					
						
							|  |  |  |             import certifi | 
					
						
							|  |  |  |             import ssl | 
					
						
							|  |  |  |             context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile=certifi.where()) | 
					
						
							|  |  |  |             with urllib.request.urlopen(r"https://aka.ms/vs/17/release/vc_redist.x64.exe", | 
					
						
							|  |  |  |                                         context=context) as download: | 
					
						
							|  |  |  |                 vc_redist = download.read() | 
					
						
							|  |  |  |             print(f"Download complete, {len(vc_redist) / 1024 / 1024:.2f} MBytes downloaded.", ) | 
					
						
							|  |  |  |             with open("VC_redist.x64.exe", "wb") as vc_file: | 
					
						
							|  |  |  |                 vc_file.write(vc_redist) | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         for data in self.extra_data: | 
					
						
							|  |  |  |             self.installfile(Path(data)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-14 08:55:57 +02:00
										 |  |  |         # kivi data files | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |         import kivy  # type: ignore[import-untyped] | 
					
						
							| 
									
										
										
										
											2022-06-14 08:55:57 +02:00
										 |  |  |         shutil.copytree(os.path.join(os.path.dirname(kivy.__file__), "data"), | 
					
						
							|  |  |  |                         self.buildfolder / "data", | 
					
						
							|  |  |  |                         dirs_exist_ok=True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         os.makedirs(self.buildfolder / "Players" / "Templates", exist_ok=True) | 
					
						
							| 
									
										
										
										
											2023-04-16 01:57:52 +02:00
										 |  |  |         from Options import generate_yaml_templates | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         from worlds.AutoWorld import AutoWorldRegister | 
					
						
							| 
									
										
										
										
											2023-06-25 03:47:38 +02:00
										 |  |  |         assert not non_apworlds - set(AutoWorldRegister.world_types), \ | 
					
						
							|  |  |  |             f"Unknown world {non_apworlds - set(AutoWorldRegister.world_types)} designated for .apworld" | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |         folders_to_remove: List[str] = [] | 
					
						
							| 
									
										
										
										
											2023-10-07 16:44:21 +02:00
										 |  |  |         disabled_worlds_folder = "worlds_disabled" | 
					
						
							|  |  |  |         for entry in os.listdir(disabled_worlds_folder): | 
					
						
							|  |  |  |             if os.path.isdir(os.path.join(disabled_worlds_folder, entry)): | 
					
						
							|  |  |  |                 folders_to_remove.append(entry) | 
					
						
							| 
									
										
										
										
											2023-04-16 01:57:52 +02:00
										 |  |  |         generate_yaml_templates(self.buildfolder / "Players" / "Templates", False) | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         for worldname, worldtype in AutoWorldRegister.world_types.items(): | 
					
						
							| 
									
										
										
										
											2023-06-25 03:47:38 +02:00
										 |  |  |             if worldname not in non_apworlds: | 
					
						
							| 
									
										
										
										
											2022-12-07 06:40:30 +01:00
										 |  |  |                 file_name = os.path.split(os.path.dirname(worldtype.__file__))[1] | 
					
						
							|  |  |  |                 world_directory = self.libfolder / "worlds" / file_name | 
					
						
							|  |  |  |                 # this method creates an apworld that cannot be moved to a different OS or minor python version, | 
					
						
							|  |  |  |                 # which should be ok | 
					
						
							|  |  |  |                 with zipfile.ZipFile(self.libfolder / "worlds" / (file_name + ".apworld"), "x", zipfile.ZIP_DEFLATED, | 
					
						
							|  |  |  |                                      compresslevel=9) as zf: | 
					
						
							|  |  |  |                     for path in world_directory.rglob("*.*"): | 
					
						
							|  |  |  |                         relative_path = os.path.join(*path.parts[path.parts.index("worlds")+1:]) | 
					
						
							|  |  |  |                         zf.write(path, relative_path) | 
					
						
							|  |  |  |                     folders_to_remove.append(file_name) | 
					
						
							|  |  |  |                 shutil.rmtree(world_directory) | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         shutil.copyfile("meta.yaml", self.buildfolder / "Players" / "Templates" / "meta.yaml") | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |             from maseya import z3pr  # type: ignore[import-untyped] | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         except ImportError: | 
					
						
							|  |  |  |             print("Maseya Palette Shuffle not found, skipping data files.") | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             # maseya Palette Shuffle exists and needs its data files | 
					
						
							|  |  |  |             print("Maseya Palette Shuffle found, including data files...") | 
					
						
							|  |  |  |             file = z3pr.__file__ | 
					
						
							|  |  |  |             self.installfile(Path(os.path.dirname(file)) / "data", keep_content=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if signtool: | 
					
						
							|  |  |  |             for exe in self.distribution.executables: | 
					
						
							|  |  |  |                 print(f"Signing {exe.target_name}") | 
					
						
							|  |  |  |                 os.system(signtool + os.path.join(self.buildfolder, exe.target_name)) | 
					
						
							| 
									
										
										
										
											2023-03-25 19:54:42 +01:00
										 |  |  |             print("Signing SNI") | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |             os.system(signtool + os.path.join(self.buildfolder, "SNI", "SNI.exe")) | 
					
						
							| 
									
										
										
										
											2023-03-25 19:54:42 +01:00
										 |  |  |             print("Signing OoT Utils") | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |             for exe_path in (("Compress", "Compress.exe"), ("Decompress", "Decompress.exe")): | 
					
						
							|  |  |  |                 os.system(signtool + os.path.join(self.buildfolder, "lib", "worlds", "oot", "data", *exe_path)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         remove_sprites_from_folder(self.buildfolder / "data" / "sprites" / "alttpr") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.create_manifest() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if is_windows: | 
					
						
							| 
									
										
										
										
											2022-12-07 06:40:30 +01:00
										 |  |  |             # Inno setup stuff | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |             with open("setup.ini", "w") as f: | 
					
						
							|  |  |  |                 min_supported_windows = "6.2.9200" if sys.version_info > (3, 9) else "6.0.6000" | 
					
						
							|  |  |  |                 f.write(f"[Data]\nsource_path={self.buildfolder}\nmin_windows={min_supported_windows}\n") | 
					
						
							| 
									
										
										
										
											2022-12-07 06:40:30 +01:00
										 |  |  |             with open("installdelete.iss", "w") as f: | 
					
						
							|  |  |  |                 f.writelines("Type: filesandordirs; Name: \"{app}\\lib\\worlds\\"+world_directory+"\"\n" | 
					
						
							|  |  |  |                              for world_directory in folders_to_remove) | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         else: | 
					
						
							|  |  |  |             # make sure extra programs are executable | 
					
						
							|  |  |  |             enemizer_exe = self.buildfolder / 'EnemizerCLI/EnemizerCLI.Core' | 
					
						
							|  |  |  |             sni_exe = self.buildfolder / 'SNI/sni' | 
					
						
							|  |  |  |             extra_exes = (enemizer_exe, sni_exe) | 
					
						
							|  |  |  |             for extra_exe in extra_exes: | 
					
						
							|  |  |  |                 if extra_exe.is_file(): | 
					
						
							|  |  |  |                     extra_exe.chmod(0o755) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AppImageCommand(setuptools.Command): | 
					
						
							|  |  |  |     description = "build an app image from build output" | 
					
						
							|  |  |  |     user_options = [ | 
					
						
							|  |  |  |         ("build-folder=", None, "Folder to convert to AppImage."), | 
					
						
							|  |  |  |         ("dist-file=", None, "AppImage output file."), | 
					
						
							|  |  |  |         ("app-dir=", None, "Folder to use for packaging."), | 
					
						
							|  |  |  |         ("app-icon=", None, "The icon to use for the AppImage."), | 
					
						
							|  |  |  |         ("app-exec=", None, "The application to run inside the image."), | 
					
						
							|  |  |  |         ("yes", "y", 'Answer "yes" to all questions.'), | 
					
						
							|  |  |  |     ] | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |     build_folder: Optional[Path] | 
					
						
							|  |  |  |     dist_file: Optional[Path] | 
					
						
							|  |  |  |     app_dir: Optional[Path] | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |     app_name: str | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |     app_exec: Optional[Path] | 
					
						
							|  |  |  |     app_icon: Optional[Path]  # source file | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |     app_id: str  # lower case name, used for icon and .desktop | 
					
						
							|  |  |  |     yes: bool | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |     def write_desktop(self) -> None: | 
					
						
							| 
									
										
										
										
											2023-03-25 19:54:42 +01:00
										 |  |  |         assert self.app_dir, "Invalid app_dir" | 
					
						
							|  |  |  |         desktop_filename = self.app_dir / f"{self.app_id}.desktop" | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         with open(desktop_filename, 'w', encoding="utf-8") as f: | 
					
						
							|  |  |  |             f.write("\n".join(( | 
					
						
							|  |  |  |                 "[Desktop Entry]", | 
					
						
							|  |  |  |                 f'Name={self.app_name}', | 
					
						
							|  |  |  |                 f'Exec={self.app_exec}', | 
					
						
							|  |  |  |                 "Type=Application", | 
					
						
							|  |  |  |                 "Categories=Game", | 
					
						
							|  |  |  |                 f'Icon={self.app_id}', | 
					
						
							|  |  |  |                 '' | 
					
						
							|  |  |  |             ))) | 
					
						
							|  |  |  |         desktop_filename.chmod(0o755) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |     def write_launcher(self, default_exe: Path) -> None: | 
					
						
							| 
									
										
										
										
											2023-03-25 19:54:42 +01:00
										 |  |  |         assert self.app_dir, "Invalid app_dir" | 
					
						
							|  |  |  |         launcher_filename = self.app_dir / "AppRun" | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         with open(launcher_filename, 'w', encoding="utf-8") as f: | 
					
						
							|  |  |  |             f.write(f"""#!/bin/sh
 | 
					
						
							|  |  |  | exe="{default_exe}" | 
					
						
							|  |  |  | match="${{1#--executable=}}" | 
					
						
							|  |  |  | if [ "${{#match}}" -lt "${{#1}}" ]; then | 
					
						
							|  |  |  |     exe="$match" | 
					
						
							|  |  |  |     shift | 
					
						
							| 
									
										
										
										
											2022-06-03 02:00:21 +02:00
										 |  |  | elif [ "$1" = "-executable" ] || [ "$1" = "--executable" ]; then | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |     exe="$2" | 
					
						
							|  |  |  |     shift; shift | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | tmp="${{exe#*/}}" | 
					
						
							|  |  |  | if [ ! "${{#tmp}}" -lt "${{#exe}}" ]; then | 
					
						
							|  |  |  |     exe="{default_exe.parent}/$exe" | 
					
						
							|  |  |  | fi | 
					
						
							| 
									
										
										
										
											2022-10-04 23:50:02 +02:00
										 |  |  | export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$APPDIR/{default_exe.parent}/lib" | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  | $APPDIR/$exe "$@" | 
					
						
							|  |  |  | """)
 | 
					
						
							|  |  |  |         launcher_filename.chmod(0o755) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |     def install_icon(self, src: Path, name: Optional[str] = None, symlink: Optional[Path] = None) -> None: | 
					
						
							| 
									
										
										
										
											2023-03-25 19:54:42 +01:00
										 |  |  |         assert self.app_dir, "Invalid app_dir" | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         try: | 
					
						
							|  |  |  |             from PIL import Image | 
					
						
							|  |  |  |         except ModuleNotFoundError: | 
					
						
							|  |  |  |             if not self.yes: | 
					
						
							| 
									
										
										
										
											2023-03-25 19:54:42 +01:00
										 |  |  |                 input("Requirement PIL is not satisfied, press enter to install it") | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |             subprocess.call([sys.executable, '-m', 'pip', 'install', 'Pillow', '--upgrade']) | 
					
						
							|  |  |  |             from PIL import Image | 
					
						
							|  |  |  |         im = Image.open(src) | 
					
						
							|  |  |  |         res, _ = im.size | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not name: | 
					
						
							|  |  |  |             name = src.stem | 
					
						
							|  |  |  |         ext = src.suffix | 
					
						
							|  |  |  |         dest_dir = Path(self.app_dir / f'usr/share/icons/hicolor/{res}x{res}/apps') | 
					
						
							|  |  |  |         dest_dir.mkdir(parents=True, exist_ok=True) | 
					
						
							|  |  |  |         dest_file = dest_dir / f'{name}{ext}' | 
					
						
							|  |  |  |         shutil.copy(src, dest_file) | 
					
						
							|  |  |  |         if symlink: | 
					
						
							|  |  |  |             symlink.symlink_to(dest_file.relative_to(symlink.parent)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |     def initialize_options(self) -> None: | 
					
						
							|  |  |  |         assert self.distribution.metadata.name | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         self.build_folder = None | 
					
						
							|  |  |  |         self.app_dir = None | 
					
						
							|  |  |  |         self.app_name = self.distribution.metadata.name | 
					
						
							|  |  |  |         self.app_icon = self.distribution.executables[0].icon | 
					
						
							|  |  |  |         self.app_exec = Path('opt/{app_name}/{exe}'.format( | 
					
						
							|  |  |  |             app_name=self.distribution.metadata.name, exe=self.distribution.executables[0].target_name | 
					
						
							|  |  |  |         )) | 
					
						
							|  |  |  |         self.dist_file = Path("dist", "{app_name}_{app_version}_{platform}.AppImage".format( | 
					
						
							|  |  |  |             app_name=self.distribution.metadata.name, app_version=self.distribution.metadata.version, | 
					
						
							|  |  |  |             platform=sysconfig.get_platform() | 
					
						
							|  |  |  |         )) | 
					
						
							|  |  |  |         self.yes = False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |     def finalize_options(self) -> None: | 
					
						
							|  |  |  |         assert self.build_folder | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         if not self.app_dir: | 
					
						
							|  |  |  |             self.app_dir = self.build_folder.parent / "AppDir" | 
					
						
							|  |  |  |         self.app_id = self.app_name.lower() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |     def run(self) -> None: | 
					
						
							|  |  |  |         assert self.build_folder and self.dist_file, "Command not properly set up" | 
					
						
							|  |  |  |         assert ( | 
					
						
							|  |  |  |             self.app_icon and self.app_id and self.app_dir and self.app_exec and self.app_name | 
					
						
							|  |  |  |         ), "AppImageCommand not properly set up" | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         self.dist_file.parent.mkdir(parents=True, exist_ok=True) | 
					
						
							|  |  |  |         if self.app_dir.is_dir(): | 
					
						
							|  |  |  |             shutil.rmtree(self.app_dir) | 
					
						
							|  |  |  |         self.app_dir.mkdir(parents=True) | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |         opt_dir = self.app_dir / "opt" / self.app_name | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         shutil.copytree(self.build_folder, opt_dir) | 
					
						
							|  |  |  |         root_icon = self.app_dir / f'{self.app_id}{self.app_icon.suffix}' | 
					
						
							|  |  |  |         self.install_icon(self.app_icon, self.app_id, symlink=root_icon) | 
					
						
							|  |  |  |         shutil.copy(root_icon, self.app_dir / '.DirIcon') | 
					
						
							|  |  |  |         self.write_desktop() | 
					
						
							|  |  |  |         self.write_launcher(self.app_exec) | 
					
						
							|  |  |  |         print(f'{self.app_dir} -> {self.dist_file}') | 
					
						
							| 
									
										
										
										
											2022-06-05 22:52:16 +02:00
										 |  |  |         subprocess.call(f'ARCH={build_arch} ./appimagetool -n "{self.app_dir}" "{self.dist_file}"', shell=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  | def find_libs(*args: str) -> Sequence[Tuple[str, str]]: | 
					
						
							| 
									
										
										
										
											2022-06-05 22:52:16 +02:00
										 |  |  |     """Try to find system libraries to be included.""" | 
					
						
							| 
									
										
										
										
											2023-01-25 00:20:26 +01:00
										 |  |  |     if not args: | 
					
						
							|  |  |  |         return [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-05 22:52:16 +02:00
										 |  |  |     arch = build_arch.replace('_', '-') | 
					
						
							|  |  |  |     libc = 'libc6'  # we currently don't support musl | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |     def parse(line: str) -> Tuple[Tuple[str, str, str], str]: | 
					
						
							| 
									
										
										
										
											2022-06-05 22:52:16 +02:00
										 |  |  |         lib, path = line.strip().split(' => ') | 
					
						
							|  |  |  |         lib, typ = lib.split(' ', 1) | 
					
						
							|  |  |  |         for test_arch in ('x86-64', 'i386', 'aarch64'): | 
					
						
							|  |  |  |             if test_arch in typ: | 
					
						
							|  |  |  |                 lib_arch = test_arch | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             lib_arch = '' | 
					
						
							|  |  |  |         for test_libc in ('libc6',): | 
					
						
							|  |  |  |             if test_libc in typ: | 
					
						
							|  |  |  |                 lib_libc = test_libc | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             lib_libc = '' | 
					
						
							|  |  |  |         return (lib, lib_arch, lib_libc), path | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if not hasattr(find_libs, "cache"): | 
					
						
							| 
									
										
										
										
											2023-03-25 19:54:42 +01:00
										 |  |  |         ldconfig = shutil.which("ldconfig") | 
					
						
							|  |  |  |         assert ldconfig, "Make sure ldconfig is in PATH" | 
					
						
							|  |  |  |         data = subprocess.run([ldconfig, "-p"], capture_output=True, text=True).stdout.split("\n")[1:] | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |         find_libs.cache = {  # type: ignore[attr-defined] | 
					
						
							| 
									
										
										
										
											2023-03-25 19:54:42 +01:00
										 |  |  |             k: v for k, v in (parse(line) for line in data if "=>" in line) | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-06-05 22:52:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |     def find_lib(lib: str, arch: str, libc: str) -> Optional[str]: | 
					
						
							|  |  |  |         cache: Dict[Tuple[str, str, str], str] = getattr(find_libs, "cache") | 
					
						
							|  |  |  |         for k, v in cache.items(): | 
					
						
							| 
									
										
										
										
											2022-06-05 22:52:16 +02:00
										 |  |  |             if k == (lib, arch, libc): | 
					
						
							|  |  |  |                 return v | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |         for k, v, in cache.items(): | 
					
						
							| 
									
										
										
										
											2022-06-05 22:52:16 +02:00
										 |  |  |             if k[0].startswith(lib) and k[1] == arch and k[2] == libc: | 
					
						
							|  |  |  |                 return v | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |     res: List[Tuple[str, str]] = [] | 
					
						
							| 
									
										
										
										
											2022-06-05 22:52:16 +02:00
										 |  |  |     for arg in args: | 
					
						
							|  |  |  |         # try exact match, empty libc, empty arch, empty arch and libc | 
					
						
							|  |  |  |         file = find_lib(arg, arch, libc) | 
					
						
							|  |  |  |         file = file or find_lib(arg, arch, '') | 
					
						
							|  |  |  |         file = file or find_lib(arg, '', libc) | 
					
						
							|  |  |  |         file = file or find_lib(arg, '', '') | 
					
						
							| 
									
										
										
										
											2024-10-25 08:51:53 +02:00
										 |  |  |         if not file: | 
					
						
							|  |  |  |             raise ValueError(f"Could not find lib {arg}") | 
					
						
							| 
									
										
										
										
											2022-06-05 22:52:16 +02:00
										 |  |  |         # resolve symlinks | 
					
						
							|  |  |  |         for n in range(0, 5): | 
					
						
							|  |  |  |             res.append((file, os.path.join('lib', os.path.basename(file)))) | 
					
						
							|  |  |  |             if not os.path.islink(file): | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |             dirname = os.path.dirname(file) | 
					
						
							|  |  |  |             file = os.readlink(file) | 
					
						
							|  |  |  |             if not os.path.isabs(file): | 
					
						
							|  |  |  |                 file = os.path.join(dirname, file) | 
					
						
							|  |  |  |     return res | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 06:25:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | cx_Freeze.setup( | 
					
						
							| 
									
										
										
										
											2021-01-03 14:32:32 +01:00
										 |  |  |     name="Archipelago", | 
					
						
							| 
									
										
										
										
											2021-08-06 19:33:17 +02:00
										 |  |  |     version=f"{version_tuple.major}.{version_tuple.minor}.{version_tuple.build}", | 
					
						
							| 
									
										
										
										
											2021-01-03 14:32:32 +01:00
										 |  |  |     description="Archipelago", | 
					
						
							| 
									
										
										
										
											2020-02-02 22:36:55 +01:00
										 |  |  |     executables=exes, | 
					
						
							| 
									
										
										
										
											2023-07-04 19:12:43 +02:00
										 |  |  |     ext_modules=cythonize("_speedups.pyx"), | 
					
						
							| 
									
										
										
										
											2020-02-02 06:25:06 +01:00
										 |  |  |     options={ | 
					
						
							|  |  |  |         "build_exe": { | 
					
						
							| 
									
										
										
										
											2023-07-24 00:16:06 +02:00
										 |  |  |             "packages": ["worlds", "kivy", "cymem", "websockets"], | 
					
						
							| 
									
										
										
										
											2020-08-24 04:35:32 +02:00
										 |  |  |             "includes": [], | 
					
						
							| 
									
										
										
										
											2020-09-20 00:34:35 +02:00
										 |  |  |             "excludes": ["numpy", "Cython", "PySide2", "PIL", | 
					
						
							| 
									
										
										
										
											2024-11-07 09:41:42 +01:00
										 |  |  |                          "pandas", "zstandard"], | 
					
						
							| 
									
										
										
										
											2020-02-02 06:25:06 +01:00
										 |  |  |             "zip_include_packages": ["*"], | 
					
						
							| 
									
										
										
										
											2024-11-27 03:28:00 +01:00
										 |  |  |             "zip_exclude_packages": ["worlds", "sc2"], | 
					
						
							| 
									
										
										
										
											2023-01-25 00:20:26 +01:00
										 |  |  |             "include_files": [],  # broken in cx 6.14.0, we use more special sauce now | 
					
						
							| 
									
										
										
										
											2021-07-31 00:03:48 +02:00
										 |  |  |             "include_msvcr": False, | 
					
						
							| 
									
										
										
										
											2023-01-25 00:20:26 +01:00
										 |  |  |             "replace_paths": ["*."], | 
					
						
							| 
									
										
										
										
											2021-07-28 13:31:27 +02:00
										 |  |  |             "optimize": 1, | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |             "build_exe": buildfolder, | 
					
						
							|  |  |  |             "extra_data": extra_data, | 
					
						
							| 
									
										
										
										
											2023-01-25 00:20:26 +01:00
										 |  |  |             "extra_libs": extra_libs, | 
					
						
							| 
									
										
										
										
											2022-06-05 22:52:16 +02:00
										 |  |  |             "bin_includes": ["libffi.so", "libcrypt.so"] if is_linux else [] | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         }, | 
					
						
							|  |  |  |         "bdist_appimage": { | 
					
						
							|  |  |  |            "build_folder": buildfolder, | 
					
						
							| 
									
										
										
										
											2020-02-02 06:25:06 +01:00
										 |  |  |         }, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |     # override commands to get custom stuff in | 
					
						
							|  |  |  |     cmdclass={ | 
					
						
							|  |  |  |         "build": BuildCommand, | 
					
						
							|  |  |  |         "build_exe": BuildExeCommand, | 
					
						
							|  |  |  |         "bdist_appimage": AppImageCommand, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2020-02-02 06:25:06 +01:00
										 |  |  | ) |