Skip to content

Instantly share code, notes, and snippets.

@flamendless
Created February 27, 2023 14:12
Show Gist options
  • Save flamendless/4d835661d86092daa7fb56c70de83a61 to your computer and use it in GitHub Desktop.
Save flamendless/4d835661d86092daa7fb56c70de83a61 to your computer and use it in GitHub Desktop.
Build/Run love from WSL2 to Windows (Native)
import os
import platform
import argparse
from enum import Enum
from zipfile import ZipFile
from typing import Dict, Callable, List
GAME_NAME: str = "NAME"
IDENTITY: str = "IDENT"
WSL_DRIVE: str = "Z:"
GAME_DIR: str = "game/"
RELEASE_DIR: str = "release/"
ROOT_DIR: str = os.path.join(os.getcwd())
EXCLUDE: List[str] = [
"*.ase",
".git",
".test",
".gitignore",
".gitattributes",
".travis.yml",
"test",
"docs",
"spec",
"cases",
"bench",
".github",
"README.md",
"CHANGELOG.md",
"changelog.txt",
"Makefile",
"assets_old",
]
class Mode(Enum):
NONE = -1
LINUX = 0,
WSL = 1,
WINDOWS = 2,
def get_mode() -> Mode:
if "wsl" in platform.platform().lower():
return Mode.WSL
else:
return Mode[platform.system().upper()]
def zip_files():
filename: str = f"{GAME_NAME}.love"
os.chdir(GAME_DIR)
with ZipFile(f"{ROOT_DIR}/{RELEASE_DIR}/{filename}", "w") as zip:
for path, subdirs, files in os.walk("."):
for ex in EXCLUDE:
if ex in subdirs:
subdirs.remove(ex)
if ex in files:
files.remove(ex)
if ex.startswith("*."):
for file in list(files):
if file.endswith(ex[1:]):
files.remove(file)
for file in files:
f: str = os.path.join(path, file)
print(f"Adding {f} to {filename}")
zip.write(f)
os.chdir(ROOT_DIR)
def build():
print("building...")
zip_files()
def run(args):
print("running...")
if not os.path.exists(f"{RELEASE_DIR}{GAME_NAME}.love"):
raise Exception(f"no love file found in {RELEASE_DIR}")
dev_mode: str = "dev" if args.dev else None
profile_mode: str = "profile" if args.profile else None
love_file_path: str = f"{ROOT_DIR}/{RELEASE_DIR}/{GAME_NAME}.love"
cmd: str = None
mode: Mode = get_mode()
if mode == Mode.WSL:
love_win_dir = "C:\\Program Files\\LOVE"
cmd_path = "/mnt/c/Windows/System32/cmd.exe"
game_path: str = f"{WSL_DRIVE}/{love_file_path}"
cmd = f"{cmd_path} /c start cmd.exe /c 'cd {love_win_dir} && lovec.exe {game_path} {dev_mode} {profile_mode} & pause'"
elif mode == Mode.LINUX:
cmd = f"love {love_file_path} {dev_mode}"
if cmd:
print(f"Running {cmd}")
os.system(cmd)
def clean(*args):
appdata_dir: str = None
mode: Mode = get_mode()
if mode == Mode.WSL:
appdata_dir = f"/mnt/c/Users/user/AppData/Roaming/LOVE/{IDENTITY}"
elif mode == Mode.LINUX:
home_dir: str = os.path.expanduser("~")
appdata_dir = f"{home_dir}/share/love/{IDENTITY}"
if appdata_dir:
print(f"Deleting {appdata_dir}")
os.rmdir(appdata_dir)
def build_run(*args):
build(args)
run(args)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-d",
"--dev",
action="store_true",
dest="dev",
help="Enable DEV mode"
)
parser.add_argument(
"-p",
"--profile",
action="store_true",
dest="profile",
help="Enable PROFILE mode"
)
parser.add_argument(
"-b",
"--build",
action="store_true",
dest="build",
help="Build game"
)
parser.add_argument(
"-r",
"--run",
action="store_true",
dest="run",
help="Run game"
)
args = parser.parse_args()
if (not args.build) and (not args.run):
raise Exception("Must pass argument(s)")
cmds: Dict[str, Callable] = {
"build": build,
"run": run,
"clean": clean,
}
if args.build:
build()
if args.run:
run(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment