mirror of
https://github.com/SlothDpal/Relaunch-Process.git
synced 2026-02-22 17:27:38 +03:00
331 lines
12 KiB
C#
331 lines
12 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.Diagnostics;
|
|
using System.Runtime.InteropServices;
|
|
using System.Security.Principal;
|
|
using System.Threading;
|
|
using System.Windows.Forms;
|
|
using RelaunchProcess.Properties;
|
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
|
using Discord;
|
|
using Discord.Webhook;
|
|
using System.Data;
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
using RelaunchProcess;
|
|
|
|
|
|
namespace Process_Auto_Relaunch
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
[Flags]
|
|
public enum NotifyLevel
|
|
{
|
|
logNone = 0,
|
|
logAlways = 1, // ïèñàòü âåçäå
|
|
logUpdateStatus = 2, // ïèñàòü â ñòðîêå ñîñòîÿíèÿ
|
|
logHistory = 4, // ïèñàòü â îêíå èñòîðèè ïåðåçàïóñêîâ
|
|
logDiscord = 8 // ïèñàòü â Äèñêîðä
|
|
}
|
|
private delegate void UpdateLogDelegate(string text, NotifyLevel level = NotifyLevel.logUpdateStatus);
|
|
private readonly UpdateLogDelegate updateLogDelegate;
|
|
private DiscordWebhook dwhHook;
|
|
private DiscordMessage dwhMessage;
|
|
|
|
public Form1()
|
|
{
|
|
InitializeComponent();
|
|
this.updateLogDelegate = this.UpdateStatus;
|
|
this.updateLogDelegate += this.SendDiscordMessage;
|
|
this.updateLogDelegate += this.HistoryLog;
|
|
myBackgroundWorker.WorkerSupportsCancellation = true;
|
|
dwhHook = new DiscordWebhook();
|
|
if ( Uri.IsWellFormedUriString(Settings.Default.dwhURL,UriKind.Absolute) && Settings.Default.dwhEnabled && Settings.Default.dwhURL!="")
|
|
{
|
|
dwhHook.Url = Settings.Default.dwhURL;
|
|
}
|
|
else if (Settings.Default.dwhEnabled) {
|
|
Debug.WriteLine($"Îøèáêà â URL âåá-õóêà ({Settings.Default.dwhURL}). Âûâîä â Discord îòêëþ÷åí.");
|
|
HistoryLog($"Îøèáêà â URL âåá-õóêà ({Settings.Default.dwhURL}). Âûâîä â Discord îòêëþ÷åí.");
|
|
Settings.Default.dwhEnabled = false;
|
|
Settings.Default.Save();
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ñîáûòèå çàïóñêà ôîðìû
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void Form1_Load(object sender, EventArgs e)
|
|
{
|
|
LoadOldState();
|
|
|
|
//MessageBox.Show(Environment.UserDomainName);
|
|
|
|
CheckProgramState();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Âîññòàíîâëåíèå íàñòðîåê
|
|
/// </summary>
|
|
private void LoadOldState()
|
|
{
|
|
if (Settings.Default.saveOldState)
|
|
{
|
|
radioButtonEnableWathing.Checked = Settings.Default.enableWatching;
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ìåòîä äëÿ ñîáûòèÿ îòêëþ÷åíèÿ
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void radioButtonDisableWathing_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
CheckProgramState();
|
|
|
|
if (!radioButtonDisableWathing.Checked)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (myBackgroundWorker.WorkerSupportsCancellation && myBackgroundWorker.IsBusy)
|
|
{
|
|
myBackgroundWorker.CancelAsync();
|
|
UpdateStatus("Îòìåíÿåì...",NotifyLevel.logUpdateStatus);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ìåòîä äëÿ ñîáûòèÿ âêëþ÷åíèÿ
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void radioButtonEnableWathing_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
if (!radioButtonEnableWathing.Checked)
|
|
{
|
|
return;
|
|
}
|
|
bool error = false;
|
|
|
|
if (String.IsNullOrEmpty(textBoxProcessName.Text))
|
|
{
|
|
error = true;
|
|
MessageBox.Show("Èìÿ ïðîöåññà íå ìîæåò áûòü ïóñòûì!" +
|
|
"\nÓêàæèòå èìÿ ïðîöåññà", "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
|
|
if (String.IsNullOrEmpty(Settings.Default.startProgramPath))
|
|
{
|
|
error = true;
|
|
MessageBox.Show("Ïðîãðàììà äëÿ çàïóñêà íå óêàçàíà." +
|
|
"\nÓêàæèòå ïðîãðàììó äëÿ çàïóñêà", "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
|
|
if (error)
|
|
{
|
|
radioButtonEnableWathing.Checked = false;
|
|
radioButtonDisableWathing.Checked = true;
|
|
return;
|
|
}
|
|
|
|
if (!myBackgroundWorker.IsBusy)
|
|
{
|
|
myBackgroundWorker.RunWorkerAsync();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Îáíîâëåíèå ñòàòóñà â ïðîãðàììå
|
|
/// </summary>
|
|
/// <param name="text">Òåêñò äëÿ îòîáðàæåíèÿ</param>
|
|
/// <param name="add_history">Ñîõðàíåíèå òåêñòà â îêíî èñòîðèè</param>
|
|
public void UpdateStatus( string text, NotifyLevel level )
|
|
{
|
|
if (!level.HasFlag(NotifyLevel.logAlways) && !level.HasFlag(NotifyLevel.logUpdateStatus)) return;
|
|
labelStatus.Text = text;
|
|
}
|
|
|
|
private void HistoryLog( string text, NotifyLevel level = NotifyLevel.logUpdateStatus )
|
|
{
|
|
if (!level.HasFlag(NotifyLevel.logAlways) && !level.HasFlag(NotifyLevel.logHistory)) return;
|
|
richTextBoxHistory.Text += DateTime.Now.ToString() + ": " + text + "\n";
|
|
}
|
|
|
|
public void SendDiscordMessage( string message, NotifyLevel level )
|
|
{
|
|
if (!level.HasFlag(NotifyLevel.logAlways) && !level.HasFlag(NotifyLevel.logDiscord)) return;
|
|
if (Settings.Default.dwhEnabled)
|
|
{
|
|
dwhHook.Url = Settings.Default.dwhURL;
|
|
dwhMessage.Username = "Relaunch process";
|
|
dwhMessage.Content = ":arrows_counterclockwise: " + message;
|
|
try
|
|
{
|
|
dwhHook.Send(dwhMessage);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
HistoryLog($"Discord messaging error: {ex.Message}");
|
|
Debug.WriteLine($"Discord messaging error: {ex.Message}");
|
|
Settings.Default.dwhEnabled = false;
|
|
Settings.Default.Save();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Status(string text, NotifyLevel level)
|
|
{
|
|
updateLogDelegate.Invoke(text, level);
|
|
}
|
|
|
|
private void CheckProgramState()
|
|
{
|
|
bool watching = radioButtonEnableWathing.Checked;
|
|
Debug.WriteLine($"Íàáëþäåíèå: {watching}");
|
|
|
|
groupBoxProcessName.Enabled = !watching;
|
|
groupBoxProgramStart.Enabled = !watching;
|
|
groupBoxActions.Enabled = !watching;
|
|
btnShowDiscordSettings.Enabled = !watching; //îòêëþ÷àåì êíîïêó íàñòðîåê äèñêîðäà
|
|
|
|
Settings.Default.enableWatching = watching;
|
|
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Âûáîð ôàéëà äëÿ çàïóñêà
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void buttonSetProgramStart_Click(object sender, EventArgs e)
|
|
{
|
|
OpenFileDialog openFile = new OpenFileDialog();
|
|
openFile.Filter = "Èñïîëíÿåìûå ôàéëû (*.exe)|*.exe";
|
|
openFile.Title = "Óêàæèòå ïðîãðàììó çàïóñêà";
|
|
|
|
if (openFile.ShowDialog() == DialogResult.Cancel)
|
|
{
|
|
return;
|
|
}
|
|
|
|
int lastSlash = openFile.FileName.LastIndexOf("\\");
|
|
textBoxProcessName.Text = openFile.FileName.Substring(lastSlash+1);
|
|
textBoxProcessName.Text = textBoxProcessName.Text.Remove(textBoxProcessName.Text.Length-4);
|
|
Settings.Default.startProgramPath = openFile.FileName;
|
|
Settings.Default.Save();
|
|
openFile.Dispose();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ñîáûòèå ïåðåä çàêðûòèåì ôîðìû
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
|
|
{
|
|
Settings.Default.Save();
|
|
}
|
|
|
|
private bool ProcessByNameIsRuning(string name)
|
|
{
|
|
var sessionid = Process.GetCurrentProcess().SessionId;
|
|
var processes = Process.GetProcessesByName(name);
|
|
foreach (var process in processes)
|
|
{
|
|
Debug.WriteLine($"Found proces: {process.ProcessName}. Session Id: {process.SessionId}. Current Session Id: {sessionid}");
|
|
if (process.SessionId == sessionid)
|
|
return true;
|
|
}
|
|
|
|
Debug.WriteLine($"Process {name} for current session id {sessionid} not found");
|
|
return false;
|
|
}
|
|
|
|
private void ProcessStart(string path, string args)
|
|
{
|
|
if (checkBoxCheckProcess.Checked)
|
|
{
|
|
if (ProcessByNameIsRuning(path))
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
Status("Ïðîöåññ áûë çàïóùåí.", NotifyLevel.logUpdateStatus);
|
|
Process.Start(path, args);
|
|
}
|
|
|
|
private void BackgroundWorkerDoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
|
|
{
|
|
BackgroundWorker worker = sender as BackgroundWorker;
|
|
int i = (int)numericUpDown1.Value;
|
|
|
|
while (!worker.CancellationPending)
|
|
{
|
|
if (ProcessByNameIsRuning(textBoxProcessName.Text))
|
|
{
|
|
Status($"Ïðîöåññ óæå çàïóùåí",NotifyLevel.logUpdateStatus);
|
|
if (i < (int)numericUpDown1.Value) SendDiscordMessage($"Ïðîöåññ {textBoxProcessName.Text} çàïóùåí.",NotifyLevel.logDiscord);
|
|
i = (int)numericUpDown1.Value;
|
|
}
|
|
else
|
|
{
|
|
if (radioButtonRestartTimer.Checked)
|
|
{
|
|
if (i==(int)numericUpDown1.Value) Status($"Ïðîöåññ {textBoxProcessName.Text} íå íàéäåí. Çàïóñê ÷åðåç {i} ñåê",NotifyLevel.logDiscord);
|
|
i--;
|
|
Status($"Ïðîöåññ {textBoxProcessName.Text} íå íàéäåí. Çàïóñê ÷åðåç {i}", NotifyLevel.logUpdateStatus);
|
|
}
|
|
|
|
if (i <= 0 || radioButtonRestartNow.Checked)
|
|
{
|
|
i = (int)numericUpDown1.Value;
|
|
Status($"Çàïóñêàåì {textBoxProcessName.Text}", NotifyLevel.logUpdateStatus|NotifyLevel.logDiscord);
|
|
ProcessStart(Settings.Default.startProgramPath, textBoxArguments.Text);
|
|
}
|
|
}
|
|
|
|
Thread.Sleep(1000);
|
|
}
|
|
|
|
if (worker.CancellationPending)
|
|
{
|
|
e.Cancel = true;
|
|
}
|
|
}
|
|
|
|
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
|
|
{
|
|
if (e.Cancelled)
|
|
{
|
|
Status("Íàáëþäåíèå îòìåíåíî.",NotifyLevel.logUpdateStatus);
|
|
}
|
|
else if (e.Error != null)
|
|
{
|
|
MessageBox.Show("Error: " + e.Error.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Stop);
|
|
Status("Ïðîèçîøëà îøèáêà! Íàáëþäåíèå îñòàíîâëåíî.", NotifyLevel.logUpdateStatus|NotifyLevel.logDiscord);
|
|
radioButtonDisableWathing.Checked = true;
|
|
}
|
|
else
|
|
{
|
|
Status("Íàáëþäåíèå îñòàíîâëåíî.", NotifyLevel.logUpdateStatus|NotifyLevel.logDiscord);
|
|
}
|
|
}
|
|
|
|
private void btnShowDiscordSettings_Click(object sender, EventArgs e)
|
|
{
|
|
DiscordSettings discordSettings;
|
|
discordSettings = new DiscordSettings();
|
|
discordSettings.ShowDialog(this);
|
|
discordSettings.Dispose();
|
|
}
|
|
}
|
|
}
|