From 8c56afddbada1b8bed8a74766c49e3f6add0d7e1 Mon Sep 17 00:00:00 2001 From: thecozies <79979276+thecozies@users.noreply.github.com> Date: Fri, 30 Sep 2022 17:40:50 -0500 Subject: [PATCH] Added ability to automatically pull unfloader and build for unf debug (#462) `make unf` added: build rom then pull the unfloader binary if needed, then load rom in debug mode also, `make load` will pull unfloader as well! --- Makefile | 29 ++++++++++++++--- tools/Makefile | 1 + tools/get_latest_unfloader.py | 59 +++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 tools/get_latest_unfloader.py diff --git a/Makefile b/Makefile index 5244fe5f..f109c6ac 100644 --- a/Makefile +++ b/Makefile @@ -210,6 +210,12 @@ endif # 0 - does not UNF ?= 0 $(eval $(call validate-option,UNF,0 1)) + +# if `unf` is a target, make sure that UNF is set +ifneq ($(filter unf,$(MAKECMDGOALS)),) + UNF = 1 +endif + ifeq ($(UNF),1) DEFINES += UNF=1 SRC_DIRS += src/usb @@ -502,8 +508,13 @@ endif ENDIAN_BITWIDTH := $(BUILD_DIR)/endian-and-bitwidth EMULATOR = mupen64plus EMU_FLAGS = -LOADER = loader64 -LOADER_FLAGS = -vwf + +ifneq (,$(call find-command,wslview)) + LOADER = ./$(TOOLS_DIR)/UNFLoader.exe +else + LOADER = ./$(TOOLS_DIR)/UNFLoader +endif + SHA1SUM = sha1sum PRINT = printf @@ -553,8 +564,18 @@ test-pj64: $(ROM) wine ~/Desktop/new64/Project64.exe $< # someone2639 -load: $(ROM) - $(LOADER) $(LOADER_FLAGS) $< +# download and extract most recent unfloader build if needed +$(LOADER): +ifeq (,$(wildcard $(LOADER))) + @$(PRINT) "Downloading latest UNFLoader...$(NO_COL)\n" + $(PYTHON) $(TOOLS_DIR)/get_latest_unfloader.py $(TOOLS_DIR) +endif + +load: $(ROM) $(LOADER) + $(LOADER) -r $< + +unf: $(ROM) $(LOADER) + $(LOADER) -d -r $< libultra: $(BUILD_DIR)/libultra.a diff --git a/tools/Makefile b/tools/Makefile index 458a0d15..54a4059c 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -72,6 +72,7 @@ all: all-except-recomp clean: $(RM) $(ALL_PROGRAMS) + $(RM) UNFLoader* $(MAKE) -C audiofile clean define COMPILE diff --git a/tools/get_latest_unfloader.py b/tools/get_latest_unfloader.py new file mode 100644 index 00000000..3da3e3d9 --- /dev/null +++ b/tools/get_latest_unfloader.py @@ -0,0 +1,59 @@ +import platform +import os +import zipfile +import stat +import sys + +from requests import request + + +def get_latest_build_artifacts_url(): + res = request('GET', 'https://dev.azure.com/buu342/6fa21e4f-4c4b-425c-891d-28537ab457a9/_apis/build/builds?$top=1&api-version=4.1') + body = res.json() + return os.path.join(body['value'][0]['url'], 'artifacts') + +def main(): + destpath = sys.argv[1] if len(sys.argv) > 1 else './' + is_wsl = 'microsoft-standard' in str(platform.uname()).lower() + unf_fn = 'UNFLoader.exe' if is_wsl else 'UNFLoader' + artifact_url = get_latest_build_artifacts_url() + + # get all artifacts from most recent build + artifacts_res = request('GET', f'{artifact_url}?api-version=4.1') + artifacts = artifacts_res.json()['value'] + + # get artifact url for current platform + platform_artifact_url = None + platform_name = 'windows' if is_wsl else 'linux' + for artifact in artifacts: + a_name = artifact['name'] + if platform_name in a_name: + platform_artifact_url = artifact['resource']['downloadUrl'] + + # download unf zipfile + artifact_res = request('GET', platform_artifact_url) + with open('UNFLoader.zip', 'wb') as unf_fp: + unf_fp.write(artifact_res.content) + + # only extract the specific file that we need + unfpath = None + with zipfile.ZipFile('UNFLoader.zip', 'r') as zip_ref: + for zipinfo in zip_ref.infolist(): + if not zipinfo.is_dir(): + unfpath = zip_ref.extract(zipinfo) + + unf_bin_path = os.path.join(destpath, unf_fn) + # file gets extracted to ./unfloader-{platform}/UNFLoader[.exe], + # so move binary to ./UNFLoader[.exe] + os.rename(unfpath, unf_bin_path) + # remove ./unfloader-{platform}/ directory + os.rmdir(unfpath.rstrip(unf_fn)) + # remove UNFLoader.zip + os.remove('UNFLoader.zip') + + # now need to add executable file permissions to unfloader + st = os.stat(unf_bin_path) + os.chmod(unf_bin_path, st.st_mode | stat.S_IEXEC) + +if __name__ == '__main__': + main()