|
| 1 | +# ------------------------------------------------------------------------------------------------ |
| 2 | +# Copyright (c) 2018 Microsoft Corporation |
| 3 | +# |
| 4 | +# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and |
| 5 | +# associated documentation files (the "Software"), to deal in the Software without restriction, |
| 6 | +# including without limitation the rights to use, copy, modify, merge, publish, distribute, |
| 7 | +# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in all copies or |
| 11 | +# substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT |
| 14 | +# NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 15 | +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 16 | +# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 17 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 18 | +# ------------------------------------------------------------------------------------------------ |
| 19 | +import os |
| 20 | +import subprocess |
| 21 | +import pathlib |
| 22 | + |
| 23 | +from malmoenv.version import malmo_version |
| 24 | + |
| 25 | + |
| 26 | +def download(branch=None, build=True, installdir="MalmoPlatform"): |
| 27 | + """Download Malmo from github and build (by default) the Minecraft Mod. |
| 28 | + Example usage: import malmoenv.bootstrap; malmoenv.bootstrap.download() |
| 29 | + Args: |
| 30 | + branch: optional branch to clone. TODO Default is release version. |
| 31 | + build: build the Mod unless build arg is given as False. |
| 32 | + installdir: the install dir name. Defaults to MalmoPlatform. |
| 33 | + Returns: |
| 34 | + The path for the Malmo Minecraft mod. |
| 35 | + """ |
| 36 | + |
| 37 | + if branch is None: |
| 38 | + branch = malmo_version |
| 39 | + |
| 40 | + subprocess.check_call(["git", "clone", "-b", branch, "https://github.com/Microsoft/malmo.git", installdir]) |
| 41 | + |
| 42 | + return setup(build=build, installdir=installdir) |
| 43 | + |
| 44 | + |
| 45 | +def setup(build=True, installdir="MalmoPlatform"): |
| 46 | + """Set up Minecraft for use with the MalmoEnv gym environment""" |
| 47 | + |
| 48 | + gradlew = './gradlew' |
| 49 | + if os.name == 'nt': |
| 50 | + gradlew = 'gradlew.bat' |
| 51 | + |
| 52 | + cwd = os.getcwd() |
| 53 | + os.chdir(installdir) |
| 54 | + os.chdir("Minecraft") |
| 55 | + try: |
| 56 | + # Create the version properties file. |
| 57 | + pathlib.Path("src/main/resources/version.properties").write_text("malmomod.version={}\n".format(malmo_version)) |
| 58 | + # Optionally do a test build. |
| 59 | + if build: |
| 60 | + subprocess.check_call([gradlew, "setupDecompWorkspace", "build", "testClasses", |
| 61 | + "-x", "test", "--stacktrace", "-Pversion={}".format(malmo_version)]) |
| 62 | + minecraft_dir = os.getcwd() |
| 63 | + finally: |
| 64 | + os.chdir(cwd) |
| 65 | + return minecraft_dir |
| 66 | + |
| 67 | + |
| 68 | +def launch_minecraft(port, installdir="MalmoPlatform", replaceable=False): |
| 69 | + """Launch Minecraft listening for malmoenv connections. |
| 70 | + Args: |
| 71 | + port: the TCP port to listen on. |
| 72 | + installdir: the install dir name. Defaults to MalmoPlatform. |
| 73 | + Must be same as given (or defaulted) in download call if used. |
| 74 | + replaceable: whether or not to automatically restart Minecraft (default is false). |
| 75 | + """ |
| 76 | + launch_script = 'launchClient.sh' |
| 77 | + if os.name == 'nt': |
| 78 | + launch_script = 'launchClient.bat' |
| 79 | + cwd = os.getcwd() |
| 80 | + os.chdir(installdir) |
| 81 | + os.chdir("Minecraft") |
| 82 | + try: |
| 83 | + cmd = [launch_script, '-port', str(port), '-env'] |
| 84 | + if replaceable: |
| 85 | + cmd.append('-replaceable') |
| 86 | + subprocess.check_call(cmd) |
| 87 | + finally: |
| 88 | + os.chdir(cwd) |
0 commit comments