main.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """Plays quake sounds according to CSGO Gamestate"""
  2. import asyncio
  3. import os
  4. import wx # type: ignore # type: ignore
  5. from openal import oalInit, oalQuit # type: ignore
  6. from pathlib import Path
  7. from shutil import copyfile
  8. from wxasync import WxAsyncApp # type: ignore
  9. # Local files
  10. import gui
  11. import steamfiles
  12. def get_steam_path() -> str:
  13. if os.name == "nt": # windows
  14. import winreg # type: ignore
  15. key = winreg.OpenKey(
  16. winreg.HKEY_LOCAL_MACHINE,
  17. "SOFTWARE\\WOW6432Node\\Valve\\Steam",
  18. 0,
  19. winreg.KEY_READ,
  20. )
  21. value, regtype = winreg.QueryValueEx(key, "InstallPath")
  22. winreg.CloseKey(key)
  23. return value
  24. else:
  25. return os.path.join(Path.home(), ".steam/root")
  26. def get_csgo_path(steamapps_folder):
  27. # Get every SteamLibrary folder
  28. with open(os.path.join(steamapps_folder, "libraryfolders.vdf")) as infile:
  29. libraryfolders = steamfiles.load(infile)
  30. folders = [steamapps_folder]
  31. i = 1
  32. while True:
  33. try:
  34. steamapps = os.path.join(
  35. libraryfolders["LibraryFolders"][str(i)], "steamapps"
  36. )
  37. print(f"Found steamapps folder {steamapps}")
  38. folders.append(steamapps)
  39. except KeyError:
  40. break
  41. i = i + 1
  42. # Find the one CS:GO is in
  43. for folder in folders:
  44. try:
  45. appmanifest = os.path.join(folder, "appmanifest_730.acf")
  46. print(f"Opening appmanifest {appmanifest}...")
  47. with open(appmanifest) as infile:
  48. appmanifest = steamfiles.load(infile)
  49. installdir = os.path.join(
  50. folder, "common", appmanifest["AppState"]["installdir"]
  51. )
  52. print(f"Valid installdir found: {installdir}")
  53. return installdir
  54. except FileNotFoundError:
  55. continue
  56. print("CS:GO not found :/")
  57. async def main():
  58. # Ensure gamestate integration cfg is in csgo's cfg directory
  59. csgo_dir = get_csgo_path(os.path.join(get_steam_path(), "steamapps"))
  60. if csgo_dir is not None:
  61. copyfile(
  62. "gamestate_integration_cqs.cfg",
  63. os.path.join(csgo_dir, "game", "csgo", "cfg", "gamestate_integration_cqs.cfg"),
  64. )
  65. oalInit()
  66. app = WxAsyncApp()
  67. gui.MainFrame(
  68. None,
  69. title="CS2 Quake Sounds",
  70. size=wx.Size(320, 230),
  71. style=wx.DEFAULT_FRAME_STYLE & ~(wx.RESIZE_BORDER | wx.MAXIMIZE_BOX),
  72. )
  73. await app.MainLoop()
  74. # Freeing OpenAL buffers might fail if they are still in use
  75. # We don't really care since the OS will clean up anyway.
  76. try:
  77. oalQuit()
  78. except: # noqa
  79. pass
  80. if __name__ == "__main__":
  81. asyncio.run(main())