From d6255845734c989f4e2112b44b068845e71dd4c5 Mon Sep 17 00:00:00 2001 From: jyelon Date: Mon, 23 Jun 2025 15:20:44 -0400 Subject: [PATCH] Modify build.py to make the patch-application process resistant to CRLF nonsense --- build.py | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/build.py b/build.py index a9b05931..0e804e0c 100755 --- a/build.py +++ b/build.py @@ -7,8 +7,6 @@ # script are in README.md # -# - import sys, os, json, shutil, subprocess, re, time, tarfile import itertools, hashlib, zipfile, fnmatch @@ -88,6 +86,40 @@ def expand_json_file(sourcefile, outputfile, config): expanded = expand_json(data, vars(config)) Path(outputfile).write_text(json.dumps(expanded, indent=4)) +# +# Applying patches to files with DOS line endings. +# +# We want to apply a patch to the UnrealEngine source, and some but not all +# of the files we want to patch have DOS line endings. Unfortunately, the +# unix patch utility can't handle a codebase with mixed line endings. +# To make it work, we convert all files to UNIX line endings, apply the +# patch, and then convert the appropriate files back. +# + +def patched_files(patchfn): + patch_lines = Path(patchfn).read_text().splitlines() + return [line[6:].strip() for line in patch_lines if line.startswith('--- a/')] + +def dos2unix(fn): + before = Path(fn).read_text() + after = before.replace("\r\n", "\n") + if before != after: + Path(fn).write_text(after) + return True + return False + +def unix2dos(fn): + before = Path(fn).read_text() + after = before.replace("\n", "\r\n") + if before != after: + Path(fn).write_text(after) + return True + return False + +# +# Given a patch file, generate a list of the files being modified. +# + # # Determining the build configuration. # @@ -159,7 +191,6 @@ def unzip_unreal_engine_and_apply_patch(): """ Unzip the unreal engine source, then apply a patch. """ - # Find out the unreal version that we're using if not Path(UNREALENGINE).is_dir(): zipfn = f"{INTEGRATION}/UnrealEngine.zip"; with zipfile.ZipFile(zipfn, 'r') as z: @@ -170,9 +201,14 @@ def unzip_unreal_engine_and_apply_patch(): shutil.rmtree(UNREALENGINE, ignore_errors=True) shutil.rmtree(unrealversion, ignore_errors=True) shell(INTEGRATION, "unzip UnrealEngine.zip") + patchfile = f"{INTEGRATION}/EnginePatches/EnginePatch" + dos2unix(patchfile) + rel_files = patched_files(patchfile) + abs_files = [ os.path.join(unrealversion, file) for file in rel_files ] + dos_files = [ file for file in abs_files if dos2unix(file)] + shell(unrealversion, f"patch -p1 < {patchfile}") + for file in dos_files: unix2dos(file) Path(unrealversion).rename(UNREALENGINE) - shell(UNREALENGINE, f"patch -p1 < {INTEGRATION}/EnginePatches/EnginePatch") - def generate_buildconfiguration_xml(): """