lutris_packaging.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. """
  2. MIT License
  3. Copyright (c) 2022 zeroday0619 <zeroday0619_dev@outlook.com>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. import os
  21. import glob
  22. import logging
  23. import re # regex
  24. import zipfile
  25. from shutil import copyfile, copytree
  26. logging.basicConfig(format=logging.BASIC_FORMAT, level=logging.INFO)
  27. logger = logging.getLogger("lutris_packaging.py")
  28. # Newest patch has the "patch.sh" script inside
  29. def get_patch_ver():
  30. files = glob.glob("../???/patch.sh")
  31. assert len(files) == 1, "Found too many patch versions"
  32. match = re.match(".*/(\d+)/.*", files[0])
  33. return match.group(1)
  34. work_dir = os.getcwd()
  35. patch_version = get_patch_ver()
  36. logger.info(f"Packing for version {patch_version}")
  37. # Destination directory for zipping
  38. dst_dir = f"{work_dir}/gi_patch_{patch_version}"
  39. dst_dir_files = f"{dst_dir}/patch_files"
  40. # Patch source files
  41. src_dir = f"{work_dir}/../{patch_version}"
  42. src_dir_files = f"{src_dir}/patch_files"
  43. def my_mkdir(path):
  44. if not os.path.exists(path):
  45. logger.info(f"create dir -> {path}")
  46. os.makedirs(path)
  47. else:
  48. logger.warning(f"already exist -> {path}")
  49. class CreateDirFaild(OSError):
  50. pass
  51. def mkdir_gi_patch():
  52. try:
  53. my_mkdir(dst_dir)
  54. my_mkdir(dst_dir_files)
  55. except OSError:
  56. raise CreateDirFaild()
  57. def my_copyfile(src, dst):
  58. src_rel = os.path.relpath(src)
  59. dst_rel = os.path.relpath(dst)
  60. logger.info(f"copy {src_rel} -> {dst_rel}")
  61. copyfile(f"{src}", f"{dst}")
  62. def copy_gi_patch():
  63. if not os.path.exists(src_dir):
  64. logger.error(f"Directory is not found -> {src_dir}")
  65. raise NotADirectoryError()
  66. my_copyfile(f"{src_dir}/patch.sh", f"{dst_dir}/patch.sh")
  67. my_copyfile(f"{src_dir}/patch_anti_logincrash.sh", f"{dst_dir}/patch_anti_logincrash.sh")
  68. my_copyfile(f"{src_dir}/patch_revert.sh", f"{dst_dir}/patch_revert.sh")
  69. copytree(src_dir_files, dst_dir_files, dirs_exist_ok=True)
  70. logger.info(f"copy {src_dir_files} -> {dst_dir_files}")
  71. # Scripts for integration with the Lutris configuration file
  72. template_apatch="""#!/bin/bash
  73. DRIVE_C="$(pwd)/.."
  74. cd "$DRIVE_C/Program Files/Genshin Impact/Genshin Impact game/"
  75. bash "$DRIVE_C/gi_patch/patch.sh"
  76. bash "$DRIVE_C/gi_patch/patch_anti_logincrash.sh"
  77. echo "Press enter to close this window..."
  78. read a
  79. """
  80. template_rpatch="""#!/bin/bash
  81. DRIVE_C="$(pwd)/.."
  82. cd "$DRIVE_C/Program Files/Genshin Impact/Genshin Impact game/"
  83. bash "$DRIVE_C/gi_patch/patch_revert.sh"
  84. echo "Press enter to close this window..."
  85. read a
  86. """
  87. def opener(path, flags):
  88. return os.open(path, flags, 0o777)
  89. def create_scripts():
  90. os.umask(0) # why is this needed?
  91. if not os.path.exists(f'{dst_dir}/ex_apatch.sh'):
  92. logger.info(f"create scripts -> {dst_dir}/ex_apatch.sh")
  93. with open(f'{dst_dir}/ex_apatch.sh', 'a', opener=opener) as ex_apatch:
  94. ex_apatch.write(template_apatch)
  95. else:
  96. logger.warning(f"already exist -> {dst_dir}/ex_apatch.sh")
  97. if not os.path.exists(f'{dst_dir}/ex_rpatch.sh'):
  98. logger.info(f"create scripts -> {dst_dir}/ex_rpatch.sh")
  99. with open(f'{dst_dir}/ex_rpatch.sh', 'a', opener=opener) as ex_rpatch:
  100. ex_rpatch.write(template_rpatch)
  101. else:
  102. logger.warning(f"already exist -> {dst_dir}/ex_rpatch.sh")
  103. def compression():
  104. logger.info("Compression start")
  105. zip_f = zipfile.ZipFile(f"gi_patch_{patch_version}.zip", "w")
  106. os.chdir(dst_dir)
  107. for (path, dir, files) in os.walk(dst_dir):
  108. for file in files:
  109. logger.info(f"Add to archive: {os.path.relpath(path, dst_dir)}/{file}")
  110. zip_f.write(os.path.join(os.path.relpath(path, dst_dir), file), compress_type=zipfile.ZIP_DEFLATED)
  111. zip_f.close()
  112. logger.info(f"Compression complete: gi_patch_{patch_version}.zip")
  113. mkdir_gi_patch()
  114. copy_gi_patch()
  115. create_scripts()
  116. compression()