Advertisement
Thunder-Menu

CheckForGta5.ps1

Aug 4th, 2023 (edited)
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 9.60 KB | Source Code | 0 0
  1. Add-Type -AssemblyName System.Windows.Forms
  2.  
  3. # Définir le chemin du fichier de configuration JSON
  4. $configFile = "config.json"
  5.  
  6. # Fonction pour sauvegarder les paramètres dans un fichier JSON
  7. function SaveToFile($filePathSearch, $filePathExecute, $isAutoSearchChecked) {
  8.     $jsonObject = @{
  9.         "FilePathSearch" = $filePathSearch
  10.         "FilePathExecute" = $filePathExecute
  11.         "IsAutoSearchChecked" = $isAutoSearchChecked
  12.     } | ConvertTo-Json
  13.  
  14.     $jsonObject | Out-File -FilePath $configFile -Encoding UTF8
  15. }
  16.  
  17. # Fonction pour charger les paramètres depuis le fichier JSON
  18. function LoadFileSelection() {
  19.     if (Test-Path $configFile) {
  20.         $jsonObject = Get-Content -Path $configFile | ConvertFrom-Json
  21.         $filePathSearch = $jsonObject.FilePathSearch
  22.         $filePathExecute = $jsonObject.FilePathExecute
  23.         $isAutoSearchChecked = $jsonObject.IsAutoSearchChecked
  24.         return $filePathSearch, $filePathExecute, $isAutoSearchChecked
  25.     }
  26.     return $null, $null, $null
  27. }
  28.  
  29. # Fonction pour vérifier si un processus est en cours d'exécution
  30. function CheckProcessRunning($processName) {
  31.     $process = Get-Process -Name $processName -ErrorAction SilentlyContinue
  32.     return $process -ne $null
  33. }
  34.  
  35. # Fonction pour lancer le fichier sélectionné
  36. function StartSelectedFile($filePathExecute) {
  37.     if ($filePathExecute -and (Test-Path $filePathExecute)) {
  38.         $folderPath = Split-Path -Parent $filePathExecute
  39.         cd\
  40.         cd $folderPath
  41.         start $filePathExecute
  42.         Write-Host "Le fichier a été exécuté."
  43.     } else {
  44.         [System.Windows.Forms.MessageBox]::Show("Veuillez sélectionner un fichier à exécuter valide.", "Erreur", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
  45.     }
  46. }
  47.  
  48. # Crée un formulaire
  49. $form = New-Object Windows.Forms.Form
  50. $form.Text = "Gestion de GTA 5"
  51. $form.Size = New-Object Drawing.Size(600, 400)
  52. $form.StartPosition = "CenterScreen"
  53.  
  54. # Crée une case à cocher pour sélectionner le fichier .exe à rechercher dans Tasklist
  55. $checkBoxSelectFileSearch = New-Object Windows.Forms.CheckBox
  56. $checkBoxSelectFileSearch.Text = "Rechercher le fichier .exe dans Tasklist"
  57. $checkBoxSelectFileSearch.Location = New-Object Drawing.Point(20, 20)
  58.  
  59. # Crée une case à cocher pour sélectionner le fichier à exécuter
  60. $checkBoxSelectFileExecute = New-Object Windows.Forms.CheckBox
  61. $checkBoxSelectFileExecute.Text = "Sélectionner le fichier à exécuter"
  62. $checkBoxSelectFileExecute.Location = New-Object Drawing.Point(20, 50)
  63.  
  64. # Crée une case à cocher pour activer/désactiver la recherche automatique
  65. $checkBoxAutoSearch = New-Object Windows.Forms.CheckBox
  66. $checkBoxAutoSearch.Text = "Recherche automatique avec démarrage automatique"
  67. $checkBoxAutoSearch.Location = New-Object Drawing.Point(20, 80)
  68.  
  69. # Charge les chemins des fichiers sélectionnés et l'état de recherche depuis le fichier JSON
  70. $filePathSearch, $filePathExecute, $isAutoSearchChecked = LoadFileSelection
  71. $checkBoxSelectFileSearch.Checked = ($filePathSearch -ne $null)
  72. $checkBoxSelectFileExecute.Checked = ($filePathExecute -ne $null)
  73. $checkBoxAutoSearch.Checked = $isAutoSearchChecked
  74. $form.Controls.Add($checkBoxSelectFileSearch)
  75. $form.Controls.Add($checkBoxSelectFileExecute)
  76. $form.Controls.Add($checkBoxAutoSearch)
  77.  
  78. # Crée un champ de texte pour afficher/écrire le chemin du fichier .exe à rechercher
  79. $textBoxFilePathSearch = New-Object Windows.Forms.TextBox
  80. $textBoxFilePathSearch.Location = New-Object Drawing.Point(20, 120)
  81. $textBoxFilePathSearch.Width = 400
  82. $form.Controls.Add($textBoxFilePathSearch)
  83.  
  84. # Crée un champ de texte pour afficher/écrire le chemin du fichier à exécuter
  85. $textBoxFilePathExecute = New-Object Windows.Forms.TextBox
  86. $textBoxFilePathExecute.Location = New-Object Drawing.Point(20, 150)
  87. $textBoxFilePathExecute.Width = 400
  88. $form.Controls.Add($textBoxFilePathExecute)
  89.  
  90. # Crée un bouton pour démarrer le jeu
  91. $buttonStart = New-Object Windows.Forms.Button
  92. $buttonStart.Text = "Démarrer"
  93. $buttonStart.Location = New-Object Drawing.Point(20, 190)
  94. $form.Controls.Add($buttonStart)
  95.  
  96. # Crée un label pour afficher le temps restant du minuteur
  97. $labelTimer = New-Object Windows.Forms.Label
  98. $labelTimer.Text = "Temps restant : 120 secondes"
  99. $labelTimer.Location = New-Object Drawing.Point(20, 220)
  100. $form.Controls.Add($labelTimer)
  101.  
  102. # Crée un minuteur pour vérifier périodiquement si le processus est en cours d'exécution
  103. $timer = New-Object System.Windows.Forms.Timer
  104. $timer.Interval = 1000  # 1 seconde
  105. $currentTimerCount = 120
  106. $form.Add_Shown({
  107.     $currentTimerCount = 120
  108.     $timer.Start()
  109. })
  110.  
  111. # Gère l'événement Tick du minuteur pour vérifier le processus et lancer le fichier sélectionné
  112. $timer.Add_Tick({
  113.     if ($checkBoxAutoSearch.Checked) {
  114.         if ($filePathSearch -and (Test-Path $filePathSearch)) {
  115.             $isProcessRunning = CheckProcessRunning (Get-Item $filePathSearch).BaseName
  116.             if (-not $isProcessRunning) {
  117.                 if ($filePathExecute -and (Test-Path $filePathExecute)) {
  118.                     $buttonStart.Hide()  # Cacher le bouton de démarrage
  119.                     # Décompte de 120 secondes avant de lancer le fichier sélectionné
  120.                     $currentTimerCount = 120  # Réinitialiser le décompte à 120 secondes
  121.                     while ($currentTimerCount -gt 0) {
  122.                         $labelTimer.Text = "Temps restant : $currentTimerCount secondes"
  123.                         Start-Sleep -Seconds 1
  124.                         $currentTimerCount--
  125.                     }
  126.                     StartSelectedFile $filePathExecute
  127.                     $buttonStart.Show()  # Afficher le bouton de démarrage après le lancement
  128.                 }
  129.             }
  130.         }
  131.     }
  132. })
  133.  
  134. # Gère l'événement Click du bouton "Démarrer"
  135. $buttonStart.Add_Click({
  136.     StartSelectedFile $filePathExecute
  137. })
  138.  
  139. # Gère l'événement CheckedChanged de la case à cocher pour afficher le OpenFileDialog pour le fichier à rechercher
  140. $checkBoxSelectFileSearch.Add_CheckedChanged({
  141.     $textBoxFilePathSearch.Enabled = $checkBoxSelectFileSearch.Checked
  142.  
  143.     if ($checkBoxSelectFileSearch.Checked) {
  144.         $openFileDialogSearch = New-Object Windows.Forms.OpenFileDialog
  145.         $openFileDialogSearch.Title = "Sélectionner le fichier à rechercher"
  146.         $openFileDialogSearch.Filter = "Exécutables (*.exe)|*.exe|Tous les fichiers (*.*)|*.*"
  147.         if ($openFileDialogSearch.ShowDialog() -eq [Windows.Forms.DialogResult]::OK) {
  148.             $filePathSearch = $openFileDialogSearch.FileName
  149.             $textBoxFilePathSearch.Text = $filePathSearch
  150.             # Sauvegarder l'état de la case à cocher dans le fichier JSON
  151.             SaveToFile $filePathSearch $filePathExecute $checkBoxAutoSearch.Checked
  152.         }
  153.     } else {
  154.         $filePathSearch = $null
  155.         $textBoxFilePathSearch.Text = ""
  156.         # Sauvegarder l'état de la case à cocher dans le fichier JSON
  157.         SaveToFile $filePathSearch $filePathExecute $checkBoxAutoSearch.Checked
  158.     }
  159. })
  160.  
  161. # Gère l'événement CheckedChanged de la case à cocher pour afficher le OpenFileDialog pour le fichier à exécuter
  162. $checkBoxSelectFileExecute.Add_CheckedChanged({
  163.     $textBoxFilePathExecute.Enabled = $checkBoxSelectFileExecute.Checked
  164.  
  165.     if ($checkBoxSelectFileExecute.Checked) {
  166.         $openFileDialogExecute = New-Object Windows.Forms.OpenFileDialog
  167.         $openFileDialogExecute.Title = "Sélectionner le fichier à exécuter"
  168.         $openFileDialogExecute.Filter = "Exécutables (*.exe)|*.exe|Tous les fichiers (*.*)|*.*"
  169.         if ($openFileDialogExecute.ShowDialog() -eq [Windows.Forms.DialogResult]::OK) {
  170.             $filePathExecute = $openFileDialogExecute.FileName
  171.             $textBoxFilePathExecute.Text = $filePathExecute
  172.             # Sauvegarder l'état de la case à cocher dans le fichier JSON
  173.             SaveToFile $filePathSearch $filePathExecute $checkBoxAutoSearch.Checked
  174.         }
  175.     } else {
  176.         $filePathExecute = $null
  177.         $textBoxFilePathExecute.Text = ""
  178.         # Sauvegarder l'état de la case à cocher dans le fichier JSON
  179.         SaveToFile $filePathSearch $filePathExecute $checkBoxAutoSearch.Checked
  180.     }
  181. })
  182.  
  183. # Gère l'événement CheckedChanged de la case à cocher pour la recherche automatique
  184. $checkBoxAutoSearch.Add_CheckedChanged({
  185.     # Sauvegarder l'état de la case à cocher dans le fichier JSON
  186.     SaveToFile $filePathSearch $filePathExecute $checkBoxAutoSearch.Checked
  187.  
  188.     if ($checkBoxAutoSearch.Checked) {
  189.         # Cacher le bouton "Démarrer" lorsque la recherche automatique est activée
  190.         $buttonStart.Hide()
  191.         # Vérifier et lancer le fichier sélectionné automatiquement si la case "Recherche automatique" est cochée
  192.         $timer.Start()
  193.     } else {
  194.         # Afficher le bouton "Démarrer" lorsque la recherche automatique est désactivée
  195.         $buttonStart.Show()
  196.         # Arrêter la vérification automatique si la case "Recherche automatique" est désactivée
  197.         $timer.Stop()
  198.     }
  199. })
  200.  
  201. # Ajouter ceci pour arrêter le minuteur lorsque la case à cocher est décochée
  202. $checkBoxAutoSearch.Add_CheckedChanged({
  203.     if (-not $checkBoxAutoSearch.Checked) {
  204.         $timer.Stop()
  205.     }
  206. })
  207.  
  208. # Mettre à jour l'affichage initial des contrôles en fonction de l'état des cases à cocher
  209. $textBoxFilePathSearch.Text = $filePathSearch
  210. $textBoxFilePathSearch.Enabled = $checkBoxSelectFileSearch.Checked
  211.  
  212. $textBoxFilePathExecute.Text = $filePathExecute
  213. $textBoxFilePathExecute.Enabled = $checkBoxSelectFileExecute.Checked
  214.  
  215. # Charger le formulaire
  216. $form.ShowDialog() | Out-Null
  217.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement