From 0420e9a7fb01663dc5f1aa212590af3cd97509df Mon Sep 17 00:00:00 2001 From: AffectedArc07 <25063394+AffectedArc07@users.noreply.github.com> Date: Sat, 25 May 2024 17:05:38 +0100 Subject: [PATCH] Adds CI to verify maps are within 255x255x1 (#25369) * Adds CI to verify maps are within 255x255x1 * Ooops platform agnostic * Webedit to fight autoformat * Time that shit --------- Signed-off-by: AffectedArc07 <25063394+AffectedArc07@users.noreply.github.com> --- .github/workflows/ci.yml | 17 +++---- tools/ci/check_map_sizes.py | 91 +++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 tools/ci/check_map_sizes.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cd7f7667f245..ec168a9276cc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,10 +2,10 @@ name: CI on: push: branches: - - master + - master pull_request: branches: - - master + - master merge_group: jobs: @@ -49,6 +49,7 @@ jobs: python tools/ci/illegal_dme_files.py ${GITHUB_WORKSPACE} python tools/ci/define_sanity.py python tools/ci/restrict_file_types.py + python tools/ci/check_map_sizes.py python tools/ci/verify_sql_version.py python tools/ci/no_duplicate_definitions.py python -m tools.ci.check_icon_conflicts @@ -134,9 +135,9 @@ jobs: name: Windows RUSTG Validation runs-on: windows-latest steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: '3.8.2' # Script was made for 3.8.2 - architecture: 'x86' # This MUST be x86 - - run: python tools/ci/validate_rustg_windows.py + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.8.2' # Script was made for 3.8.2 + architecture: 'x86' # This MUST be x86 + - run: python tools/ci/validate_rustg_windows.py diff --git a/tools/ci/check_map_sizes.py b/tools/ci/check_map_sizes.py new file mode 100644 index 000000000000..0f45b5177ae2 --- /dev/null +++ b/tools/ci/check_map_sizes.py @@ -0,0 +1,91 @@ +import glob +import os +import sys +import subprocess +import platform +import json +import time + +parent_directory = "_maps/**/*.dmm" + +MAX_X_SIZE = 255 +MAX_Y_SIZE = 255 +MAX_Z_SIZE = 1 + +how_to_fix_message = f"Please make sure maps are <= {MAX_X_SIZE}x{MAX_Y_SIZE}x{MAX_Z_SIZE}." + +def green(text): + return "\033[32m" + str(text) + "\033[0m" + +def red(text): + return "\033[31m" + str(text) + "\033[0m" + +def blue(text): + return "\033[34m" + str(text) + "\033[0m" + +def post_error(file, map_data, github_error_style): + if github_error_style: + print(f"::error file={file},title=Map Size::{file} is >{MAX_X_SIZE}x{MAX_Y_SIZE}x{MAX_Z_SIZE} (Found: {map_data['x']},{map_data['y']},{map_data['z']})!") + else: + print(f"- Failure: {red(file)} is is >{MAX_X_SIZE}x{MAX_Y_SIZE}x{MAX_Z_SIZE} (Found: {map_data['x']},{map_data['y']},{map_data['z']})") + +def do_dmmtools_call(file): + # Windows - hopefully local + exec_path = None + if platform.system() == 'Windows': + exec_path = "dmm-tools.exe" + # Linux - CI + else: + exec_path = "tools/github-actions/nanomap-renderer" + + exec_args = f"{exec_path} map-info \"{file}\"" + result = subprocess.run(exec_args, shell=True, capture_output=True, text=True) + + res_obj = json.loads(result.stdout) + + return_obj = { + "x": res_obj[file]["size"][0], + "y": res_obj[file]["size"][1], + "z": res_obj[file]["size"][2] + } + + return return_obj + +def main(): + start = time.time() + # simple way to check if we're running on github actions, or on a local machine + on_github = os.getenv("GITHUB_ACTIONS") == "true" + + maps_greater_than_allowed = [] + + map_count = 0 + + for map_file in glob.glob(parent_directory, recursive=True): + map_count += 1 + # Open the map in the nanomap dmm tools - it works + map_data = do_dmmtools_call(map_file) + + if map_data["x"] > MAX_X_SIZE or map_data["y"] > MAX_Y_SIZE or map_data["z"] > MAX_Z_SIZE: + maps_greater_than_allowed.append((map_file, map_data)) + + if len(maps_greater_than_allowed): + for error in maps_greater_than_allowed: + post_error(error[0], error[1], on_github) + + print(red(how_to_fix_message)) + + end = time.time() + print(f"\ncheck_map_sizes.py completed in {(end - start):.2f}s\n") + + sys.exit(1) + + else: + print(green(f"No oversized maps found (checked {map_count} maps).")) + + end = time.time() + print(f"\ncheck_map_sizes.py completed in {(end - start):.2f}s\n") + +if __name__ == "__main__": + main() + +