Skip to content

Commit

Permalink
Static utility class
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaifm committed Nov 14, 2022
1 parent 2add207 commit 8bb14f4
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 51 deletions.
14 changes: 12 additions & 2 deletions ElegantRecorder/ElegantRecorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void ReadRecordingHeaders()
stream.Position = 0;

var tag = "\"UIActions\":";
string header = global::ElegantRecorder.Recording.ReadUntil(stream, "\"UIActions\":");
string header = Util.ReadUntil(stream, "\"UIActions\":");
header += tag + "[]}";

string recName = Path.GetFileNameWithoutExtension(file);
Expand Down Expand Up @@ -269,6 +269,16 @@ private void LockUI(bool disabled)
textBoxNewRec.Enabled = !disabled;
}

public void RefreshCurrentRow(Recording rec)
{
if (rec.Triggers.Hotkey != 0)
dataGridViewRecordings.SelectedRows[0].Cells[1].Value = Util.HotkeyToString(rec.Triggers.Hotkey);
else
dataGridViewRecordings.SelectedRows[0].Cells[1].Value = "";

dataGridViewRecordings.SelectedRows[0].Cells[2].Value = rec.Encrypted ? Resources.lock_edit : Resources.empty;
}

private void buttonRecord_Click(object sender, EventArgs e)
{
Record();
Expand Down Expand Up @@ -391,7 +401,7 @@ private void buttonReplay_Click(object sender, EventArgs e)

public void Replay(string recording)
{
if (Replaying)
if (Recording || Replaying)
return;

ResetButtons();
Expand Down
5 changes: 3 additions & 2 deletions ElegantRecorder/ElegantTriggers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ElegantTriggers(ElegantRecorder elegantRecorder)
Rec.Load();

checkBoxHotkey.Checked = Rec.Triggers.HotkeyEnabled;
textBoxHotkey.Text = string.Join("+", ((Keys)Rec.Triggers.Hotkey).ToString().Split(", ").Reverse());
textBoxHotkey.Text = Util.HotkeyToString(Rec.Triggers.Hotkey);
hotkeyData = Rec.Triggers.Hotkey;
checkBoxTime.Checked = Rec.Triggers.TimeEnabled;
dateTimePickerDate.Value = Rec.Triggers.Date;
Expand Down Expand Up @@ -76,6 +76,7 @@ private void buttonCancel_Click(object sender, EventArgs e)
private void buttonOk_Click(object sender, EventArgs e)
{
SaveTriggers();
App.RefreshCurrentRow(Rec);
Close();
}

Expand Down Expand Up @@ -125,7 +126,7 @@ private void TriggerEditor_KeyDown(object sender, KeyEventArgs e)
if (e.KeyCode == Keys.ShiftKey || e.KeyCode == Keys.ControlKey || e.KeyCode == Keys.Menu)
return;

textBoxHotkey.Text = string.Join("+", e.KeyData.ToString().Split(", ").Reverse());
textBoxHotkey.Text = Util.HotkeyToString((int) e.KeyData);
hotkeyData = (int)e.KeyData;

e.SuppressKeyPress = true;
Expand Down
47 changes: 0 additions & 47 deletions ElegantRecorder/Recording.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,52 +326,5 @@ public void DisarmTriggers()
App.TriggerData.RecRec.Remove(Name);
}
}

public static string ReadUntil(FileStream Stream, string UntilText)
{
System.Text.StringBuilder builder = new System.Text.StringBuilder();
System.Text.StringBuilder returnTextBuilder = new System.Text.StringBuilder();
string returnText = string.Empty;
int size = Convert.ToInt32(UntilText.Length / (double)2) - 1;
byte[] buffer = new byte[size + 1];
int currentRead = -1;

while (currentRead != 0)
{
string collected = null;
string chars = null;
int foundIndex = -1;

currentRead = Stream.Read(buffer, 0, buffer.Length);
chars = System.Text.Encoding.Default.GetString(buffer, 0, currentRead);

builder.Append(chars);
returnTextBuilder.Append(chars);

collected = builder.ToString();
foundIndex = collected.IndexOf(UntilText);

if (foundIndex >= 0)
{
returnText = returnTextBuilder.ToString();

int indexOfSep = returnText.IndexOf(UntilText);
int cutLength = returnText.Length - indexOfSep;

returnText = returnText.Remove(indexOfSep, cutLength);

builder.Remove(0, foundIndex + UntilText.Length);

if (cutLength > UntilText.Length)
Stream.Position -= cutLength - UntilText.Length;

return returnText;
}
else if (!collected.Contains(UntilText.First()))
builder.Length = 0;
}

return string.Empty;
}
}
}
62 changes: 62 additions & 0 deletions ElegantRecorder/Util.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Windows.Forms;
using System.Linq;
using System.IO;
using System;

namespace ElegantRecorder
{
public static class Util
{
public static string HotkeyToString(int hotkey)
{
return string.Join("+", ((Keys)hotkey).ToString().Split(", ").Reverse());
}

public static string ReadUntil(FileStream Stream, string UntilText)
{
System.Text.StringBuilder builder = new System.Text.StringBuilder();
System.Text.StringBuilder returnTextBuilder = new System.Text.StringBuilder();
string returnText = string.Empty;
int size = Convert.ToInt32(UntilText.Length / (double)2) - 1;
byte[] buffer = new byte[size + 1];
int currentRead = -1;

while (currentRead != 0)
{
string collected = null;
string chars = null;
int foundIndex = -1;

currentRead = Stream.Read(buffer, 0, buffer.Length);
chars = System.Text.Encoding.Default.GetString(buffer, 0, currentRead);

builder.Append(chars);
returnTextBuilder.Append(chars);

collected = builder.ToString();
foundIndex = collected.IndexOf(UntilText);

if (foundIndex >= 0)
{
returnText = returnTextBuilder.ToString();

int indexOfSep = returnText.IndexOf(UntilText);
int cutLength = returnText.Length - indexOfSep;

returnText = returnText.Remove(indexOfSep, cutLength);

builder.Remove(0, foundIndex + UntilText.Length);

if (cutLength > UntilText.Length)
Stream.Position -= cutLength - UntilText.Length;

return returnText;
}
else if (!collected.Contains(UntilText.First()))
builder.Length = 0;
}

return string.Empty;
}
}
}

0 comments on commit 8bb14f4

Please sign in to comment.