Change notify system, refactoring

This commit is contained in:
SlothDpal
2024-05-22 10:33:04 +03:00
parent 54ded84224
commit 15b185f6f5
3 changed files with 85 additions and 69 deletions

103
Form1.cs
View File

@@ -18,17 +18,26 @@ namespace Process_Auto_Relaunch
{
public partial class Form1 : Form
{
private delegate void UpdateLogDelegate(string text, bool add_history = false);
private UpdateLogDelegate updateLogDelegate;
[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;
private DiscordSettings discordSettings;
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!="")
@@ -87,7 +96,7 @@ namespace Process_Auto_Relaunch
if (myBackgroundWorker.WorkerSupportsCancellation && myBackgroundWorker.IsBusy)
{
myBackgroundWorker.CancelAsync();
UpdateStatus("Îòìåíÿåì...");
UpdateStatus("Îòìåíÿåì...",NotifyLevel.logUpdateStatus);
}
}
@@ -136,24 +145,43 @@ namespace Process_Auto_Relaunch
/// </summary>
/// <param name="text">Òåêñò äëÿ îòîáðàæåíèÿ</param>
/// <param name="add_history">Ñîõðàíåíèå òåêñòà â îêíî èñòîðèè</param>
public void UpdateStatus(string text, bool add_history = false)
public void UpdateStatus( string text, NotifyLevel level )
{
if (!level.HasFlag(NotifyLevel.logAlways) && !level.HasFlag(NotifyLevel.logUpdateStatus)) return;
labelStatus.Text = text;
if (add_history)
{
HistoryLog(text);
}
}
private void HistoryLog(string 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 Status(string text, bool add_history = false)
public void SendDiscordMessage( string message, NotifyLevel level )
{
Invoke(updateLogDelegate, text, add_history);
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()
@@ -164,7 +192,7 @@ namespace Process_Auto_Relaunch
groupBoxProcessName.Enabled = !watching;
groupBoxProgramStart.Enabled = !watching;
groupBoxActions.Enabled = !watching;
btnShowDiscordSettings.Enabled = !watching;
btnShowDiscordSettings.Enabled = !watching; //îòêëþ÷àåì êíîïêó íàñòðîåê äèñêîðäà
Settings.Default.enableWatching = watching;
@@ -192,7 +220,6 @@ namespace Process_Auto_Relaunch
textBoxProcessName.Text = textBoxProcessName.Text.Remove(textBoxProcessName.Text.Length-4);
Settings.Default.startProgramPath = openFile.FileName;
Settings.Default.Save();
openFile.Dispose();
}
@@ -231,7 +258,7 @@ namespace Process_Auto_Relaunch
}
}
Status("Ïðîöåññ áûë çàïóùåí.", true);
Status("Ïðîöåññ áûë çàïóùåí.", NotifyLevel.logUpdateStatus);
Process.Start(path, args);
}
@@ -244,24 +271,23 @@ namespace Process_Auto_Relaunch
{
if (ProcessByNameIsRuning(textBoxProcessName.Text))
{
Status($"Ïðîöåññ {textBoxProcessName.Text} óæå çàïóùåí");
if (i < (int)numericUpDown1.Value) SendDiscordMessage($"Ïðîöåññ {textBoxProcessName.Text} çàïóùåí.",true);
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) SendDiscordMessage($"Ïðîöåññ {textBoxProcessName.Text} íå íàéäåí. Çàïóñê ÷åðåç {i}",true);
if (i==(int)numericUpDown1.Value) Status($"Ïðîöåññ {textBoxProcessName.Text} íå íàéäåí. Çàïóñê ÷åðåç {i} ñåê",NotifyLevel.logDiscord);
i--;
Status($"Ïðîöåññ {textBoxProcessName.Text} íå íàéäåí. Çàïóñê ÷åðåç {i}");
Status($"Ïðîöåññ {textBoxProcessName.Text} íå íàéäåí. Çàïóñê ÷åðåç {i}", NotifyLevel.logUpdateStatus);
}
if (i <= 0 || radioButtonRestartNow.Checked)
{
i = (int)numericUpDown1.Value;
Status("Çàïóñêàåì...");
SendDiscordMessage($"Çàïóñêàåì {textBoxProcessName.Text}",true);
Status($"Çàïóñêàåì {textBoxProcessName.Text}", NotifyLevel.logUpdateStatus|NotifyLevel.logDiscord);
ProcessStart(Settings.Default.startProgramPath, textBoxArguments.Text);
}
}
@@ -279,48 +305,23 @@ namespace Process_Auto_Relaunch
{
if (e.Cancelled)
{
Status("Íàáëþäåíèå îòìåíåíî.");
Status("Íàáëþäåíèå îòìåíåíî.",NotifyLevel.logUpdateStatus);
}
else if (e.Error != null)
{
MessageBox.Show("Error: " + e.Error.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Stop);
Status("Ïðîèçîøëà îøèáêà! Íàáëþäåíèå îñòàíîâëåíî.", true);
Status("Ïðîèçîøëà îøèáêà! Íàáëþäåíèå îñòàíîâëåíî.", NotifyLevel.logUpdateStatus|NotifyLevel.logDiscord);
radioButtonDisableWathing.Checked = true;
}
else
{
Status("Íàáëþäåíèå îñòàíîâëåíî.");
Status("Íàáëþäåíèå îñòàíîâëåíî.", NotifyLevel.logUpdateStatus|NotifyLevel.logDiscord);
}
}
/// <summary>
/// Îòïðàâêà ñîîáùåíèÿ â Äèñêîðä
/// </summary>
/// <param name="text">Òåêñò äëÿ îòïðàâêè</param>
public void SendDiscordMessage(string message, bool addToHistory = false)
{
if (Settings.Default.dwhEnabled && addToHistory)
{
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();
}
}
}
private void btnShowDiscordSettings_Click(object sender, EventArgs e)
{
DiscordSettings discordSettings;
discordSettings = new DiscordSettings();
discordSettings.ShowDialog(this);
discordSettings.Dispose();