Advertisement
DeaD_EyE

Untitled

Nov 19th, 2024
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.33 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.         rmtree(shader_cache)
  58.     else:
  59.         print("Keeping shader cache")
  60.  
  61.  
  62. def main():
  63.     RED = "\033[0;31m"
  64.     NO_COLOR = "\033[0m"
  65.  
  66.     try:
  67.         ask_delete(find_hd2_shadercache())
  68.  
  69.     except Exception as e:
  70.         info = "no furter information"
  71.  
  72.         if e.args:
  73.             info = e.args[0]
  74.  
  75.         print(f"{type(e).__name__}: {RED}{info}{NO_COLOR}")
  76.         return 1
  77.     except KeyboardInterrupt:
  78.         print("\nKeyboardInterrupt")
  79.         return 1
  80.  
  81.     return 0
  82.  
  83.  
  84. if __name__ == "__main__":
  85.     sys.exit(main())
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement