|
| 1 | +import logging |
| 2 | + |
| 3 | +import platform |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | + |
| 7 | +def rmdir(directory): |
| 8 | + directory = Path(directory) |
| 9 | + for item in directory.iterdir(): |
| 10 | + if item.is_dir(): |
| 11 | + rmdir(item) |
| 12 | + else: |
| 13 | + item.unlink() |
| 14 | + directory.rmdir() |
| 15 | + |
| 16 | + |
| 17 | +def init_tmrl_data(): |
| 18 | + """ |
| 19 | + Wipes and re-generates the TmrlData folder. |
| 20 | + """ |
| 21 | + from shutil import copy2 |
| 22 | + from zipfile import ZipFile |
| 23 | + import urllib.request |
| 24 | + import urllib.error |
| 25 | + import socket |
| 26 | + |
| 27 | + resources_url = "https://github.com/trackmania-rl/tmrl/releases/download/v0.6.0/resources.zip" |
| 28 | + |
| 29 | + def url_retrieve(url: str, outfile: Path, overwrite: bool = False): |
| 30 | + """ |
| 31 | + Adapted from https://www.scivision.dev/python-switch-urlretrieve-requests-timeout/ |
| 32 | + """ |
| 33 | + outfile = Path(outfile).expanduser().resolve() |
| 34 | + if outfile.is_dir(): |
| 35 | + raise ValueError("Please specify full filepath, including filename") |
| 36 | + if overwrite or not outfile.is_file(): |
| 37 | + outfile.parent.mkdir(parents=True, exist_ok=True) |
| 38 | + try: |
| 39 | + urllib.request.urlretrieve(url, str(outfile)) |
| 40 | + except (socket.gaierror, urllib.error.URLError) as err: |
| 41 | + raise ConnectionError(f"could not download {url} due to {err}") |
| 42 | + |
| 43 | + # destination folder: |
| 44 | + home_folder = Path.home() |
| 45 | + tmrl_folder = home_folder / "TmrlData" |
| 46 | + |
| 47 | + # Wipe the tmrl folder: |
| 48 | + if tmrl_folder.exists(): |
| 49 | + rmdir(tmrl_folder) |
| 50 | + |
| 51 | + # download relevant items IF THE tmrl FOLDER DOESN'T EXIST: |
| 52 | + assert not tmrl_folder.exists(), f"Failed to delete {tmrl_folder}" |
| 53 | + |
| 54 | + checkpoints_folder = tmrl_folder / "checkpoints" |
| 55 | + dataset_folder = tmrl_folder / "dataset" |
| 56 | + reward_folder = tmrl_folder / "reward" |
| 57 | + weights_folder = tmrl_folder / "weights" |
| 58 | + config_folder = tmrl_folder / "config" |
| 59 | + checkpoints_folder.mkdir(parents=True, exist_ok=True) |
| 60 | + dataset_folder.mkdir(parents=True, exist_ok=True) |
| 61 | + reward_folder.mkdir(parents=True, exist_ok=True) |
| 62 | + weights_folder.mkdir(parents=True, exist_ok=True) |
| 63 | + config_folder.mkdir(parents=True, exist_ok=True) |
| 64 | + |
| 65 | + # download resources: |
| 66 | + resources_target = tmrl_folder / "resources.zip" |
| 67 | + url_retrieve(resources_url, resources_target) |
| 68 | + |
| 69 | + # unzip downloaded resources: |
| 70 | + with ZipFile(resources_target, 'r') as zip_ref: |
| 71 | + zip_ref.extractall(tmrl_folder) |
| 72 | + |
| 73 | + # delete zip file: |
| 74 | + resources_target.unlink() |
| 75 | + |
| 76 | + # copy relevant files: |
| 77 | + resources_folder = tmrl_folder / "resources" |
| 78 | + copy2(resources_folder / "config.json", config_folder) |
| 79 | + copy2(resources_folder / "reward.pkl", reward_folder) |
| 80 | + copy2(resources_folder / "SAC_4_LIDAR_pretrained.tmod", weights_folder) |
| 81 | + copy2(resources_folder / "SAC_4_imgs_pretrained.tmod", weights_folder) |
| 82 | + |
| 83 | + # on Windows, look for OpenPlanet: |
| 84 | + if platform.system() == "Windows": |
| 85 | + openplanet_folder = home_folder / "OpenplanetNext" |
| 86 | + |
| 87 | + if openplanet_folder.exists(): |
| 88 | + # copy the OpenPlanet script: |
| 89 | + try: |
| 90 | + # remove old script if found |
| 91 | + op_scripts_folder = openplanet_folder / 'Scripts' |
| 92 | + if op_scripts_folder.exists(): |
| 93 | + to_remove = [op_scripts_folder / 'Plugin_GrabData_0_1.as', |
| 94 | + op_scripts_folder / 'Plugin_GrabData_0_1.as.sig', |
| 95 | + op_scripts_folder / 'Plugin_GrabData_0_2.as', |
| 96 | + op_scripts_folder / 'Plugin_GrabData_0_2.as.sig'] |
| 97 | + for old_file in to_remove: |
| 98 | + if old_file.exists(): |
| 99 | + old_file.unlink() |
| 100 | + # copy new plugin |
| 101 | + op_plugins_folder = openplanet_folder / 'Plugins' |
| 102 | + op_plugins_folder.mkdir(parents=True, exist_ok=True) |
| 103 | + tm20_plugin_1 = resources_folder / 'Plugins' / 'TMRL_GrabData.op' |
| 104 | + tm20_plugin_2 = resources_folder / 'Plugins' / 'TMRL_SaveGhost.op' |
| 105 | + copy2(tm20_plugin_1, op_plugins_folder) |
| 106 | + copy2(tm20_plugin_2, op_plugins_folder) |
| 107 | + except Exception as e: |
| 108 | + print( |
| 109 | + f"An exception was caught when trying to copy the OpenPlanet plugin automatically. \ |
| 110 | + Please copy the plugin manually for TrackMania 2020 support. The caught exception was: {str(e)}.") |
| 111 | + else: |
| 112 | + # warn the user that OpenPlanet couldn't be found: |
| 113 | + print(f"The OpenPlanet folder was not found at {openplanet_folder}. \ |
| 114 | + Please copy the OpenPlanet script and signature manually for TrackMania 2020 support.") |
| 115 | + |
| 116 | + |
| 117 | +TMRL_FOLDER = Path.home() / "TmrlData" |
| 118 | + |
| 119 | +if not TMRL_FOLDER.exists(): |
| 120 | + logging.warning(f"The TMRL folder was not found on your machine. Attempting download...") |
| 121 | + init_tmrl_data() |
| 122 | + logging.info(f"TMRL folder successfully downloaded, please wait for initialization to complete...") |
0 commit comments