Add xml-port support.

This commit is contained in:
clienthax 2021-08-10 19:56:58 +01:00
parent 55e2e94b63
commit 1a7ad17c87
4 changed files with 154 additions and 102 deletions

View file

@ -75,7 +75,7 @@ def fix_offset(patch_contents):
return patch_out
# TODO replace with xml-patch version
def make_necessary(version_name, version_code):
# OH GOD OH FUCK
shutil.rmtree(tmp_folder, ignore_errors=True)
@ -88,11 +88,9 @@ def make_necessary(version_name, version_code):
with open(os.path.join(tmp_folder, "AndroidManifest.xml")) as fin:
filec = fin.read()
incorrect_versioncode = re_versioncode_xml.findall(filec)[0]
filec = filec.replace(incorrect_versioncode,
f'platformBuildVersionCode="{to_versioncode}"')
filec = filec.replace(incorrect_versioncode, f'platformBuildVersionCode="{to_versioncode}"')
incorrect_versionname = re_versionname_xml.findall(filec)[0]
filec = filec.replace(incorrect_versionname,
f'platformBuildVersionName="{to_versionname}"')
filec = filec.replace(incorrect_versionname, f'platformBuildVersionName="{to_versionname}"')
with open(os.path.join(tmp_folder, "AndroidManifest.xml"), "w") as fout:
fout.write(filec)
@ -134,37 +132,30 @@ with open(os.path.join(apk_folder, "res", "values", "strings.xml")) as f:
failures = []
for patch in os.listdir(os.path.join(cutthecord_folder, "resources/patches")):
for patch in os.listdir(os.path.join(cutthecord_folder, "resources/xmlpatches")):
if debug:
print(f"going over patch: {patch}")
print(f"going over xml patch: {patch}")
# Ignore non-dirs
if not os.path.isdir(os.path.join(cutthecord_folder, "resources/patches", patch)):
if not os.path.isdir(os.path.join(cutthecord_folder, "resources/xmlpatches", patch)):
if debug:
print(f"patch is not a folder, skipping: {patch}")
continue
pre_in_path = os.path.join(cutthecord_folder, "resources/patches", patch,
f"{from_versioncode}-pre.sh")
post_in_path = os.path.join(cutthecord_folder, "resources/patches", patch,
f"{from_versioncode}-post.sh")
pre_out_path = os.path.join(cutthecord_folder, "resources/patches", patch,
f"{to_versioncode}-pre.sh")
post_out_path = os.path.join(cutthecord_folder, "resources/patches", patch,
f"{to_versioncode}-post.sh")
pre_in_path = os.path.join(cutthecord_folder, "resources/xmlpatches", patch, f"{from_versioncode}-pre.sh")
post_in_path = os.path.join(cutthecord_folder, "resources/xmlpatches", patch, f"{from_versioncode}-post.sh")
pre_out_path = os.path.join(cutthecord_folder, "resources/xmlpatches", patch, f"{to_versioncode}-pre.sh")
post_out_path = os.path.join(cutthecord_folder, "resources/xmlpatches", patch, f"{to_versioncode}-post.sh")
patch_path = os.path.join(cutthecord_folder, "resources/patches", patch,
f"{from_versioncode}.patch")
out_path = os.path.join(cutthecord_folder, "resources/patches", patch,
f"{to_versioncode}.patch")
readme_path = os.path.join(cutthecord_folder, "resources/patches", patch, "README.md")
patch_path = os.path.join(cutthecord_folder, "resources/xmlpatches", patch, f"{from_versioncode}.xml")
out_path = os.path.join(cutthecord_folder, "resources/xmlpatches", patch, f"{to_versioncode}.xml")
tmp_out_path = os.path.join("/tmp/", patch, f"{to_versioncode}.xml")
readme_path = os.path.join(cutthecord_folder, "resources/xmlpatches", patch, "README.md")
# Handle copying of versioned scripts, untested and dirty!
script_path = os.path.join(cutthecord_folder, "resources/patches", patch,
f"{from_versioncode}.sh")
script_path = os.path.join(cutthecord_folder, "resources/xmlpatches", patch, f"{from_versioncode}.sh")
if os.path.exists(script_path):
script_out_path = os.path.join(cutthecord_folder, "resources/patches", patch,
f"{to_versioncode}.sh")
script_out_path = os.path.join(cutthecord_folder, "resources/xmlpatches", patch, f"{to_versioncode}.sh")
with open(script_path) as f:
with open(script_out_path, "w") as f2:
f2.write(f.read())
@ -172,9 +163,7 @@ for patch in os.listdir(os.path.join(cutthecord_folder, "resources/patches")):
# Check if patch exists for from_version, if it doesn't, warn user
if not os.path.isfile(patch_path) and patch not in ["necessary"]:
# Don't warn on instructional patches
if patch not in ["customfont", "customring",
"bettertm", "bettertmlight",
"blobs"]:
if patch not in ["customfont", "customring", "bettertm", "bettertmlight", "blobs"]:
print(f"SKIPPED: No {from_versionname} version found for {patch}.")
continue
@ -188,6 +177,94 @@ for patch in os.listdir(os.path.join(cutthecord_folder, "resources/patches")):
shutil.copyfile(post_in_path, post_out_path)
print(f"POST COPIED: {patch}'s post script was copied.")
# Create necessary instead of porting it.
if patch == "necessary":
patch_contents = make_necessary(to_versioncode, to_versionname)
else:
# Get a modified version of the patch
patch_contents = modify_patch(patch, patch_path)
# Pass the new patch to patch command and get it to attempt to patch
with open(tmp_out_path, "w") as f:
f.write(patch_contents)
out = subprocess.run("java -cp /opt/ctc/tools/xml-patch.jar com.github.dnault.xmlpatch.BatchPatcher --patch {tmp_out_path} --srcdir {apk_folder} --destdir /tmp/", shell=True,
cwd=apk_folder, text=True,
capture_output=True)
# Check for issues
if "ERROR" in out.stdout or "EXCEPTION" in out.stdout:
print(f"FAILED: {patch} failed, please fix by hand.")
failures.append(patch)
out_path += "-failed"
if debug:
print(out.stdout)
# Apply patch to main APK folder too
if patch in ["necessary"]:
apply_patch(patch_contents)
if from_versionname != to_versionname:
# Add supported version to readme of that patch, hacky
# https://stackoverflow.com/a/35130508/3286892
with open(readme_path, 'r') as f:
readme_text = f.read().replace(f'- {from_versionname}\n', f'- {from_versionname}\n' f'- {to_versionname}\n')
with open(readme_path, "w") as f:
f.write(readme_text)
# Save ported patch
with open(out_path, "w") as f:
f.write(patch_contents)
if not out_path.endswith("-failed"):
print(f"PORTED: {patch} was successfully ported.")
for patch in os.listdir(os.path.join(cutthecord_folder, "resources/patches")):
if debug:
print(f"going over patch: {patch}")
# Ignore non-dirs
if not os.path.isdir(os.path.join(cutthecord_folder, "resources/patches", patch)):
if debug:
print(f"patch is not a folder, skipping: {patch}")
continue
pre_in_path = os.path.join(cutthecord_folder, "resources/patches", patch, f"{from_versioncode}-pre.sh")
post_in_path = os.path.join(cutthecord_folder, "resources/patches", patch, f"{from_versioncode}-post.sh")
pre_out_path = os.path.join(cutthecord_folder, "resources/patches", patch, f"{to_versioncode}-pre.sh")
post_out_path = os.path.join(cutthecord_folder, "resources/patches", patch, f"{to_versioncode}-post.sh")
patch_path = os.path.join(cutthecord_folder, "resources/patches", patch, f"{from_versioncode}.patch")
out_path = os.path.join(cutthecord_folder, "resources/patches", patch, f"{to_versioncode}.patch")
readme_path = os.path.join(cutthecord_folder, "resources/patches", patch, "README.md")
# Handle copying of versioned scripts, untested and dirty!
script_path = os.path.join(cutthecord_folder, "resources/patches", patch, f"{from_versioncode}.sh")
if os.path.exists(script_path):
script_out_path = os.path.join(cutthecord_folder, "resources/patches", patch, f"{to_versioncode}.sh")
with open(script_path) as f:
with open(script_out_path, "w") as f2:
f2.write(f.read())
# Check if patch exists for from_version, if it doesn't, warn user
if not os.path.isfile(patch_path) and patch not in ["necessary"]:
# Don't warn on instructional patches
if patch not in ["customfont", "customring", "bettertm", "bettertmlight", "blobs"]:
print(f"SKIPPED: No {from_versionname} version found for {patch}.")
continue
# Check if pre-script exists, if it does copy it
if os.path.isfile(pre_in_path):
shutil.copyfile(pre_in_path, pre_out_path)
print(f"PRE COPIED: {patch}'s pre script was copied.")
# Check if post-script exists, if it does copy it
if os.path.isfile(post_in_path):
shutil.copyfile(post_in_path, post_out_path)
print(f"POST COPIED: {patch}'s post script was copied.")
# Create necessary instead of porting it.
if patch == "necessary":
patch_contents = make_necessary(to_versioncode, to_versionname)
@ -226,9 +303,7 @@ for patch in os.listdir(os.path.join(cutthecord_folder, "resources/patches")):
# Add supported version to readme of that patch, hacky
# https://stackoverflow.com/a/35130508/3286892
with open(readme_path, 'r') as f:
readme_text = f.read().replace(f'- {from_versionname}\n',
f'- {from_versionname}\n'
f'- {to_versionname}\n')
readme_text = f.read().replace(f'- {from_versionname}\n', f'- {from_versionname}\n' f'- {to_versionname}\n')
with open(readme_path, "w") as f:
f.write(readme_text)