DeaD_EyE icon

delete_hd2_shader_cache.py

DeaD_EyE | PRO | 08/13/24 06:52:18 PM UTC (Edited) | 0 ⭐ | 551 👁️ | Never ⏰ | []
Python |

2.41 KB

|

None

|

0 👍

/

0 👎

import platform
import sys
from pathlib import Path
from shutil import rmtree
 
if (system := platform.system()) != "Linux":
    raise SystemExit(f"{system} is not supported")
 
 
def find_hd2_shadercache():
    HD2_APPID = "553850"
    registry = Path.home().joinpath(".steam/steam/steamapps/libraryfolders.vdf")
 
    if not registry.exists():
        raise RuntimeError("Steam is not installed")
 
    with registry.open() as fd:
        for line in fd:
            if '"path"' in line:
                path = Path(line.split(maxsplit=1)[1].replace('"', "").rstrip())
            elif HD2_APPID in line:
                shader_cache = path.joinpath("steamapps/shadercache", HD2_APPID)
                if not shader_cache.exists():
                    raise RuntimeError("Shader cache of HELLDIVERS2 not found")
                return shader_cache
 
    raise RuntimeError("HELLDIVERS2 not found")
 
 
def human_size(size: int) -> str:
    for unit in ("", "KiB", "MiB", "GiB"):
        if size < 1024:
            break
        size /= 1024
 
    return f"{size:.2f} {unit}"
 
 
def get_size_ext(path: Path) -> (int, set[str]):
    files = [element for element in path.rglob("*") if element.is_file()]
    return sum(element.stat().st_size for element in files), {
        suffix for file in files if (suffix := file.suffix)
    }
 
 
def ask_delete(shader_cache: Path):
    print("Calculating size of shader cache")
    size, extensions_found = get_size_ext(shader_cache)
 
    print("Detected Path:", shader_cache)
    print("Size:", human_size(size))
    print("Found extensions:", " / ".join(sorted(extensions_found)))
 
    user_input = input("Do you want to delete the shader cache? [n/y]: ")
    if user_input.lower() == "y":
        print(f"Deleting path: {shader_cache}")
        raise RuntimeError("NOT TESTED YET. Delete command commented out")
        # rmtree(shader_cache)
    else:
        print("Keeping shader cache")
 
 
def main():
    RED = "\033[0;31m"
    NO_COLOR = "\033[0m"
 
    try:
        ask_delete(find_hd2_shadercache())
 
    except Exception as e:
        info = "no furter information"
 
        if e.args:
            info = e.args[0]
 
        print(f"{type(e).__name__}: {RED}{info}{NO_COLOR}")
        return 1
    except KeyboardInterrupt:
        print("\nKeyboardInterrupt")
        return 1
 
    return 0
 
 
if __name__ == "__main__":
    sys.exit(main())
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎