emerginginstance

Untitled

Sep 9th, 2021
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $VERSION = "3.0";
  2.  
  3. #
  4. #
  5. #       VOLUME CHANGER SEGMENT
  6. #
  7. #
  8.  
  9. Add-Type -AssemblyName PresentationFramework
  10.  
  11. Add-Type -TypeDefinition @'
  12. using System.Runtime.InteropServices;
  13. [Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  14. interface IAudioEndpointVolume
  15. {
  16.    // f(), g(), ... are unused COM method slots. Define these if you care
  17.    int f(); int g(); int h(); int i();
  18.    int SetMasterVolumeLevelScalar(float fLevel, System.Guid pguidEventContext);
  19.    int j();
  20.    int GetMasterVolumeLevelScalar(out float pfLevel);
  21.    int k(); int l(); int m(); int n();
  22.    int SetMute([MarshalAs(UnmanagedType.Bool)] bool bMute, System.Guid pguidEventContext);
  23.    int GetMute(out bool pbMute);
  24. }
  25. [Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  26. interface IMMDevice
  27. {
  28.    int Activate(ref System.Guid id, int clsCtx, int activationParams, out IAudioEndpointVolume aev);
  29. }
  30. [Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  31. interface IMMDeviceEnumerator
  32. {
  33.    int f(); // Unused
  34.    int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice endpoint);
  35. }
  36. [ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] class MMDeviceEnumeratorComObject { }
  37. public class Audio
  38. {
  39.    static IAudioEndpointVolume Vol()
  40.    {
  41.        var enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
  42.        IMMDevice dev = null;
  43.        Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(/*eRender*/ 0, /*eMultimedia*/ 1, out dev));
  44.        IAudioEndpointVolume epv = null;
  45.        var epvid = typeof(IAudioEndpointVolume).GUID;
  46.        Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv));
  47.        return epv;
  48.    }
  49.    public static float Volume
  50.    {
  51.        get { float v = -1; Marshal.ThrowExceptionForHR(Vol().GetMasterVolumeLevelScalar(out v)); return v; }
  52.        set { Marshal.ThrowExceptionForHR(Vol().SetMasterVolumeLevelScalar(value, System.Guid.Empty)); }
  53.    }
  54.    public static bool Mute
  55.    {
  56.        get { bool mute; Marshal.ThrowExceptionForHR(Vol().GetMute(out mute)); return mute; }
  57.        set { Marshal.ThrowExceptionForHR(Vol().SetMute(value, System.Guid.Empty)); }
  58.    }
  59. }
  60. '@
  61.  
  62. #
  63. #
  64. #       VOLUME CHANGER SEGMENT END
  65. #
  66. #
  67.  
  68.  
  69.  
  70.     $count = [int]0;
  71.     Get-WmiObject Win32_process -filter "name = 'powershell.exe'" | forEach {
  72.         if($_.CommandLine -Match "TAManager.ps1")  {
  73.             $count++;
  74.         }
  75.     }
  76.  
  77.     if($count -ne 1) {
  78.         Write-Output "TAM DETECTED IN ANOTHER WINDOW!"
  79.         exit
  80.     }
  81.  
  82.  
  83.     [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  84.     [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
  85.    
  86.     # Set the size of your form
  87.     $Form = New-Object System.Windows.Forms.Form
  88.     $Form.width = 600
  89.     $Form.height = 600
  90.     $Form.Text = "TAManger Settings"
  91.  
  92.  
  93.     # Set the font of the text to be used within the form
  94.     $Font = New-Object System.Drawing.Font("Console",12)
  95.     $Form.Font = $Font
  96.  
  97.     $MANAGE_PRIORITY = new-object System.Windows.Forms.checkbox
  98.     $MANAGE_PRIORITY.Location = new-object System.Drawing.Size(30,25)
  99.     $MANAGE_PRIORITY.Size = new-object System.Drawing.Size(250,50)
  100.     $MANAGE_PRIORITY.Text = "High Priority"
  101.     $MANAGE_PRIORITY.Checked = $true
  102.     $Form.Controls.Add($MANAGE_PRIORITY)
  103.    
  104.     $MANAGE_APPS = new-object System.Windows.Forms.checkbox
  105.     $MANAGE_APPS.Location = new-object System.Drawing.Size(30,75)
  106.     $MANAGE_APPS.Size = new-object System.Drawing.Size(250,50)
  107.     $MANAGE_APPS.Text = "Manage Apps"
  108.     $MANAGE_APPS.Checked = $true
  109.     $Form.Controls.Add($MANAGE_APPS)
  110.    
  111.     $MANAGE_KEYBINDS = new-object System.Windows.Forms.checkbox
  112.     $MANAGE_KEYBINDS.Location = new-object System.Drawing.Size(30,125)
  113.     $MANAGE_KEYBINDS.Size = new-object System.Drawing.Size(250,50)
  114.     $MANAGE_KEYBINDS.Text = "Manage Keybinds"
  115.     $MANAGE_KEYBINDS.Checked = $true
  116.     $Form.Controls.Add($MANAGE_KEYBINDS)
  117.    
  118.     $MANAGE_VOLUME = new-object System.Windows.Forms.checkbox
  119.     $MANAGE_VOLUME.Location = new-object System.Drawing.Size(30,175)
  120.     $MANAGE_VOLUME.Size = new-object System.Drawing.Size(250,50)
  121.     $MANAGE_VOLUME.Text = "Force Volume"
  122.     $MANAGE_VOLUME.Checked = $true
  123.     $Form.Controls.Add($MANAGE_VOLUME)
  124.    
  125.     $MANAGE_VOLUME_INPUT = new-object System.Windows.Forms.TextBox
  126.     $MANAGE_VOLUME_INPUT.Location = new-object System.Drawing.Size(30,225)
  127.     $MANAGE_VOLUME_INPUT.Size = new-object System.Drawing.Size(250,260)
  128.     $MANAGE_VOLUME_INPUT.Text ="30";
  129.     $Form.Controls.Add($MANAGE_VOLUME_INPUT)
  130.  
  131.  
  132.     # Add an OK button
  133.     $OKButton = new-object System.Windows.Forms.Button
  134.     $OKButton.Location = new-object System.Drawing.Size(30,275)
  135.     $OKButton.Size = new-object System.Drawing.Size(100,40)
  136.     $OKButton.Text = "OK"
  137.     $OKButton.Add_Click({$Form.Close()})
  138.     $form.Controls.Add($OKButton)
  139.    
  140.     # Activate the form
  141.     $Form.Add_Shown({$Form.Activate()})
  142.     [void] $Form.ShowDialog()
  143.    
  144. $managePriority = $MANAGE_PRIORITY.Checked;
  145. $manageApps     = $MANAGE_APPS.Checked;
  146. $manageBinds    = $MANAGE_KEYBINDS.Checked;
  147. $manageVolume   = $MANAGE_VOLUME.Checked
  148.  
  149. $manageVolumeText = $MANAGE_VOLUME_INPUT.Text;
  150. $goodGamesVolume = ([int]$MANAGE_VOLUME_INPUT.Text)/100;
  151. $currentVolume = 0;
  152.  
  153. if($manageApps) {
  154.     [string[]]$apps = Get-Content -Path 'applications.txt'
  155. } else {
  156.     $apps = @();
  157. }
  158.  
  159. [string[]]$games = Get-Content -Path 'games.txt'
  160. $gameStr = $games -join ", ";
  161. $appsStr = $apps -join ", ";
  162.  
  163. function header {
  164.     cls
  165.     Write-Output "";
  166.     Write-Output "  Total Annihilation Manager v$VERSION, Made by Zombean#6280";
  167.     Write-Output "";
  168.     Write-Output "      high priority enabled:  $managePriority"
  169.     Write-Output "      app kill enabled:   $manageApps ($appsStr)"
  170.     Write-Output "      use keybinds:       $manageBinds"
  171.     Write-Output "      force volume:       $manageVolume ($manageVolumeText)"
  172.     Write-Output "      Games:          $gameStr";
  173.     Write-Output "";
  174.     Write-Output "";
  175.     Write-Output $global:consoleBuffer[0..15];
  176. };
  177.  
  178. $global:consoleBuffer = @();
  179. function console {
  180.     Param (
  181.         [string]$message,
  182.         [int]$max
  183.     )
  184.     $global:consoleBuffer = ,$message + $global:consoleBuffer
  185.    
  186. }
  187.  
  188. function MLog{
  189.     Param (
  190.         $message
  191.     )
  192.     $date = (Get-Date -Format "HH:mm:ss");
  193.     $ident = "TAM $VERSION"
  194.     $out= " [$ident $date]  $message"
  195.     console($out);
  196.     Add-Content -Path log\log.txt -Value $out
  197.     header
  198. }
  199.  
  200.  
  201.  
  202. function getProc {
  203.     Param (
  204.         $procName
  205.         )
  206.     return Get-WmiObject Win32_process -filter "name = '$procName.exe'";
  207. };
  208.  
  209. function tap {
  210.         param (
  211.         $game
  212.         )
  213.     try {
  214.         return (get-process -Name $game).priorityclass;
  215.         } catch {
  216.     }
  217.    
  218. };
  219.  
  220. function StartAHK {
  221.    
  222.     param (
  223.         $AHKPath
  224.     )
  225.     Start-Process -FilePath "rebinds\AHK\AutoHotkeyU64.exe" -ArgumentList "rebinds\$AHKPath";
  226.    
  227. }
  228.  
  229.  
  230.  
  231.  
  232. function AutoHotKeyManager  {
  233.    
  234.     MLog("Checking for hotkey scripts");
  235.     Get-ChildItem rebinds\* -Filter *.ahk | % {
  236.         $tempName = $_.Name;
  237.         MLog("starting script: $tempName");
  238.         StartAHK($tempName)
  239.         }
  240.    
  241. }
  242.  
  243. $global:appPaths = @();
  244.  
  245. function appHandler {
  246.     param (
  247.         $appName
  248.         )
  249.        
  250.        
  251.     if((getProc($appName))) {
  252.            
  253.         if(((getProc($appName)).Path[1].length) -eq 1) {
  254.             $appPath = ((getProc($appName)).Path);
  255.         } else {
  256.             $appPath= ((getProc($appName)).Path[1]);
  257.         }
  258.        
  259.         MLog("closing: $appName")
  260.         $global:appPaths += $appPath
  261.         (Invoke-Expression "taskkill /IM $appName.exe > null 2>&1") | Out-Null
  262.         (Invoke-Expression "taskkill /F /IM $appName.exe > null 2>&1") | Out-Null
  263.     }
  264. }
  265.  
  266. $lock_mode = 1;
  267. $activeGame = "";
  268.  
  269. header
  270.  
  271. While(1)
  272. {
  273.  
  274.     if($lock_mode) {
  275.         forEach($game in $games) {
  276.             if( (getProc($game)) -and $lock_mode) {
  277.                
  278.                 $lock_mode = 0;
  279.                 $activeGame = $game;
  280.                 MLog("$activeGame detected");
  281.                
  282.                 if($manageVolume) {
  283.                     $currentVolume = [audio]::Volume;
  284.                     [audio]::Volume = $goodGamesVolume;
  285.                     MLog("Changing volume from $currentVolume too $goodGamesVolume");
  286.                 }
  287.                
  288.                 if($manageBinds) {
  289.                 AutoHotKeyManager;
  290.                 }
  291.                
  292.                 if( ((tap($activeGame)) -ne 'High') -and ($managePriority)) {
  293.                    
  294.                     MLog("Setting high prorirty on $game.exe!");
  295.                     ((getProc($activeGame)).SetPriority(128)) | Out-Null;
  296.                 }
  297.                
  298.                 ForEach ($app in $apps) {
  299.                 appHandler($app);
  300.                 }
  301.                
  302.             }
  303.         }
  304.     } else {
  305.            
  306.         if(!(getProc($activeGame)) -and ($lock_mode -eq 0)) {
  307.  
  308.             $date = (Get-Date -Format "HH:mm:ss");
  309.             MLog("$activeGame.exe closed, Tidying Up!");
  310.            
  311.             if($manageVolume) {
  312.                 MLog("Changing volume from $goodGamesVolume too $currentVolume");
  313.                 [audio]::Volume = $currentVolume;
  314.             }
  315.            
  316.             if($global:appPaths.count -ne 0) {
  317.                
  318.                 MLog("Restarting apps");
  319.                 ForEach ($app in $global:appPaths) {
  320.                     Start-Process "$app" | Out-Null;
  321.                 }
  322.                 $global:appPaths = @()
  323.                
  324.             }
  325.            
  326.             $ahku = Get-WmiObject Win32_process -filter 'name = "AutoHotKeyU64.exe"';
  327.             if($ahku) {
  328.                 MLog("closing autohotkey's");
  329.                 (taskkill /F /IM AutoHotKeyU64.exe) | Out-Null;
  330.             }
  331.             header;
  332.             $lock_mode = 1;
  333.         }  
  334.     }
  335.     Start-Sleep -s 1
  336. }
Add Comment
Please, Sign In to add comment