Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- import os
- import sys
- import threading
- import time
- import ctypes
- import shutil
- import win32com.client
- import pystray
- from PIL import Image
- import win32gui
- import win32con
- def hide_console():
- """Hides the console window (Windows only)."""
- ctypes.windll.user32.ShowWindow(ctypes.windll.kernel32.GetConsoleWindow(), 0)
- def resource_path(relative_path):
- """ Get absolute path to resource, works for dev and PyInstaller """
- try:
- base_path = sys._MEIPASS
- print(f"Running in PyInstaller, base path: {base_path}") # Add this
- except AttributeError:
- base_path = os.path.abspath(".")
- print(f"Running outside PyInstaller, base path: {base_path}")
- return os.path.join(base_path, relative_path)
- def save_profile(monitor_switcher, profiles_dir, filename, profile_type):
- """Saves the current monitor setup to the given filename."""
- profile_path = os.path.join(profiles_dir, filename)
- subprocess.run(
- [monitor_switcher, f'-save:{profile_path}'],
- check=True,
- creationflags=subprocess.CREATE_NO_WINDOW
- )
- # Show confirmation popup
- ctypes.windll.user32.MessageBoxW(
- 0,
- f"{profile_type} Profile saved successfully!",
- "Oblivion Launcher",
- 0
- )
- def apply_profile(monitor_switcher, profile_path):
- """Applies the given monitor profile."""
- subprocess.run(
- [monitor_switcher, f'-load:{profile_path}'],
- check=True,
- creationflags=subprocess.CREATE_NO_WINDOW
- )
- def close_warning_window():
- """Searches for and closes the specific warning window."""
- warning_title = "Thank you for installing TESIV: Oblivion (Remastered)"
- for _ in range(30): # Try for 30 seconds
- hwnd = win32gui.FindWindow(None, warning_title)
- if hwnd:
- print("Warning window detected. Closing it...")
- win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
- return
- time.sleep(1)
- def check_game_exe(launcher_dir):
- """Checks if OblivionRemastered.exe exists and shows a popup if not."""
- game_exe = os.path.join(launcher_dir, "OblivionRemastered.exe")
- if not os.path.exists(game_exe):
- # Display the message box
- ctypes.windll.user32.MessageBoxW(
- 0,
- "Game executable not found.\n\n"
- "Please ensure the Launcher is in the Oblivion Remastered game directory with the 'OblivionRemastered.exe' file.",
- "Oblivion Launcher",
- 0 # MB_OK style for a simple OK button
- )
- sys.exit(0) # Exit the program if the OK button is clicked
- def check_profiles(profiles_dir, launcher_path):
- """Checks if the necessary profiles exist. If not, show instructions."""
- game_profile = os.path.join(profiles_dir, "Game.xml")
- default_profile = os.path.join(profiles_dir, "Default.xml")
- if not os.path.exists(game_profile) or not os.path.exists(default_profile):
- # Create shortcuts for setting the profiles
- create_shortcut(launcher_path, "-set-default", "Launcher - Set Default Profile.lnk")
- create_shortcut(launcher_path, "-set-game", "Launcher - Set Game Profile.lnk")
- # Show tips message
- message = (
- "Monitor Profiles Not Found \n\n"
- "Please use the newly created shortcuts to configure your profiles.\n\n"
- "How to Use:\n"
- "When you run one of the shortcuts, it will save your current Monitor Configuration settings for the corresponding mode.\n\n"
- "- Run 'Launcher - Set Default Profile' while you are in your main monitor configuration that you regularly use.\n\n"
- "- Run 'Launcher - Set Game Profile' while you are in a monitor configuration that you know works for the game.\n\n"
- "The Launcher will change your display settings to your Game configuration while you're playing, and automatically change it back when you close the game. It will also automatically close the warning you get if you do not meet the minimum specifications."
- )
- ctypes.windll.user32.MessageBoxW(
- 0,
- message,
- "Oblivion Launcher",
- 0
- )
- sys.exit(0)
- def create_shortcut(target_path, arguments, shortcut_name):
- """Creates a shortcut with the given target and arguments."""
- shell = win32com.client.Dispatch('WScript.Shell')
- desktop = os.path.dirname(os.path.abspath(target_path)) # Directory of the launcher
- shortcut_path = os.path.join(desktop, shortcut_name)
- shortcut = shell.CreateShortCut(shortcut_path)
- shortcut.TargetPath = target_path
- shortcut.Arguments = arguments
- shortcut.IconLocation = target_path # You can use an icon for the shortcut
- shortcut.save()
- def main():
- # Hide the console immediately
- hide_console()
- # Get the directory where the launcher script is located
- launcher_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
- check_game_exe(launcher_dir)
- # Make sure necessary folders exist
- os.makedirs(os.path.join(launcher_dir, ".Launcher"), exist_ok=True)
- # Paths
- monitor_switcher = resource_path("MonitorSwitcher.exe")
- profiles_dir = os.path.join(launcher_dir, ".Launcher")
- game_exe = os.path.join(launcher_dir, "OblivionRemastered.exe")
- icon_path = resource_path("icon.ico")
- # Handle CLI arguments
- if len(sys.argv) > 1:
- arg = sys.argv[1].lower()
- try:
- if arg == "-set-default":
- save_profile(monitor_switcher, profiles_dir, "Default.xml", "Default")
- elif arg == "-set-game":
- save_profile(monitor_switcher, profiles_dir, "Game.xml", "Game")
- else:
- ctypes.windll.user32.MessageBoxW(
- 0,
- f"Unknown argument: {arg}",
- "Error",
- 0
- )
- except subprocess.CalledProcessError as e:
- ctypes.windll.user32.MessageBoxW(
- 0,
- f"Error saving monitor profile:\n{str(e)}",
- "Error",
- 0
- )
- sys.exit(0)
- # Check if profiles exist
- check_profiles(profiles_dir, os.path.join(launcher_dir, "Oblivion Launcher.exe"))
- # Setup system tray icon
- icon_image = Image.open(icon_path)
- icon = pystray.Icon("OblivionRemastered", icon_image, "Oblivion is running...", menu=None)
- def run_icon():
- icon.run()
- try:
- # Apply the Game monitor profile
- game_profile = os.path.join(profiles_dir, "Game.xml")
- default_profile = os.path.join(profiles_dir, "Default.xml")
- apply_profile(monitor_switcher, game_profile)
- # Launch the game
- game_proc = subprocess.Popen([game_exe])
- # Start the tray icon in a separate thread
- tray_thread = threading.Thread(target=run_icon, daemon=True)
- tray_thread.start()
- # Start a thread to close the warning window
- warning_thread = threading.Thread(target=close_warning_window, daemon=True)
- warning_thread.start()
- # Wait for the game to close
- game_proc.wait()
- except subprocess.CalledProcessError as e:
- print(f"Error applying monitor profile: {e}")
- except FileNotFoundError as e:
- print(f"Error launching application: {e}")
- finally:
- # Reapply the Default monitor profile
- try:
- apply_profile(monitor_switcher, default_profile)
- except subprocess.CalledProcessError as e:
- print(f"Error reverting to default profile: {e}")
- finally:
- icon.stop()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement