Advertisement
DeaD_EyE

delete_hd2_shader_cache.py

Aug 13th, 2024 (edited)
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. import platform
  2. import sys
  3. from pathlib import Path
  4. from shutil import rmtree
  5.  
  6. if (system := platform.system()) != "Linux":
  7.     raise SystemExit(f"{system} is not supported")
  8.  
  9.  
  10. def find_hd2_shadercache():
  11.     HD2_APPID = "553850"
  12.     registry = Path.home().joinpath(".steam/steam/steamapps/libraryfolders.vdf")
  13.  
  14.     if not registry.exists():
  15.         raise RuntimeError("Steam is not installed")
  16.  
  17.     with registry.open() as fd:
  18.         for line in fd:
  19.             if '"path"' in line:
  20.                 path = Path(line.split(maxsplit=1)[1].replace('"', "").rstrip())
  21.             elif HD2_APPID in line:
  22.                 shader_cache = path.joinpath("steamapps/shadercache", HD2_APPID)
  23.                 if not shader_cache.exists():
  24.                     raise RuntimeError("Shader cache of HELLDIVERS2 not found")
  25.                 return shader_cache
  26.  
  27.     raise RuntimeError("HELLDIVERS2 not found")
  28.  
  29.  
  30. def human_size(size: int) -> str:
  31.     for unit in ("", "KiB", "MiB", "GiB"):
  32.         if size < 1024:
  33.             break
  34.         size /= 1024
  35.  
  36.     return f"{size:.2f} {unit}"
  37.  
  38.  
  39. def get_size_ext(path: Path) -> (int, set[str]):
  40.     files = [element for element in path.rglob("*") if element.is_file()]
  41.     return sum(element.stat().st_size for element in files), {
  42.         suffix for file in files if (suffix := file.suffix)
  43.     }
  44.  
  45.  
  46. def ask_delete(shader_cache: Path):
  47.     print("Calculating size of shader cache")
  48.     size, extensions_found = get_size_ext(shader_cache)
  49.  
  50.     print("Detected Path:", shader_cache)
  51.     print("Size:", human_size(size))
  52.     print("Found extensions:", " / ".join(sorted(extensions_found)))
  53.  
  54.     user_input = input("Do you want to delete the shader cache? [n/y]: ")
  55.     if user_input.lower() == "y":
  56.         print(f"Deleting path: {shader_cache}")
  57.         raise RuntimeError("NOT TESTED YET. Delete command commented out")
  58.         # rmtree(shader_cache)
  59.     else:
  60.         print("Keeping shader cache")
  61.  
  62.  
  63. def main():
  64.     RED = "\033[0;31m"
  65.     NO_COLOR = "\033[0m"
  66.  
  67.     try:
  68.         ask_delete(find_hd2_shadercache())
  69.  
  70.     except Exception as e:
  71.         info = "no furter information"
  72.  
  73.         if e.args:
  74.             info = e.args[0]
  75.  
  76.         print(f"{type(e).__name__}: {RED}{info}{NO_COLOR}")
  77.         return 1
  78.     except KeyboardInterrupt:
  79.         print("\nKeyboardInterrupt")
  80.         return 1
  81.  
  82.     return 0
  83.  
  84.  
  85. if __name__ == "__main__":
  86.     sys.exit(main())
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement