-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFindForm.cs
More file actions
238 lines (191 loc) · 6.71 KB
/
FindForm.cs
File metadata and controls
238 lines (191 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using gta5refactor.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace gta5refactor
{
public partial class FindForm : Form
{
private MainForm OwnerForm;
volatile bool AbortOperation = false;
bool InProgress = false;
public FindForm(MainForm owner)
{
InitializeComponent();
OwnerForm = owner;
ScriptFolderTextBox.Text = Settings.Default.ScriptFolder;
}
private void FolderBrowseButton_Click(object sender, EventArgs e)
{
FolderBrowserDialog.SelectedPath = ScriptFolderTextBox.Text;
DialogResult res = FolderBrowserDialog.ShowDialog();
if (res == DialogResult.OK)
{
ScriptFolderTextBox.Text = FolderBrowserDialog.SelectedPath;
}
}
private void AbortButton_Click(object sender, EventArgs e)
{
AbortOperation = true;
}
private void FindButton_Click(object sender, EventArgs e)
{
if (InProgress)
{
return;
}
if (string.IsNullOrEmpty(FindTextBox.Text))
{
MessageBox.Show("Please enter a search term.");
return;
}
Find();
}
private void ReplaceButton_Click(object sender, EventArgs e)
{
MessageBox.Show("Sorry, not yet implemented!");
}
private void Find()
{
AbortOperation = false;
InProgress = true;
string scriptfolder = ScriptFolderTextBox.Text;
bool casesensitive = CaseSensitiveCheckBox.Checked;
string searchterm = casesensitive ? FindTextBox.Text : FindTextBox.Text.ToLower();
MatchesListView.Items.Clear();
FunctionTextBox.Text = string.Empty;
Task.Run(() =>
{
UpdateStatus("Task beginning...");
DateTime starttime = DateTime.Now;
List<FindMatch> matches = new List<FindMatch>();
string[] scriptfiles = Directory.GetFiles(scriptfolder);
foreach (string scriptfile in scriptfiles)
{
if (AbortOperation)
{
UpdateStatus("Search aborted.");
FindComplete(matches);
return;
}
string filel = scriptfile.ToLower();
if (!(filel.EndsWith(".c") || filel.EndsWith(".c4"))) continue;
UpdateStatus("Searching " + scriptfile);
ScriptFile sf = new ScriptFile(scriptfile);
string[] lines = File.ReadAllLines(scriptfile);
for (int i = 0; i < lines.Length; i++)
{
if (AbortOperation)
{
UpdateStatus("Search aborted.");
FindComplete(matches);
return;
}
string line = lines[i];
string searchtext = casesensitive ? line : line.ToLower();
int foundindex = searchtext.IndexOf(searchterm);
if (foundindex >= 0)
{
//there's a match on this line. record it for posterity
FindMatch match = new FindMatch();
match.File = sf;
match.Line = i;
match.Char = foundindex;
matches.Add(match);
FoundMatch(match);
}
}
}
UpdateStatus(string.Format("Find complete. {0} matches found.", matches.Count));
FindComplete(matches);
});
}
private void FindComplete(List<FindMatch> matches)
{
try
{
if (InvokeRequired)
{
Invoke(new Action(() => { FindComplete(matches); }));
}
else
{
InProgress = false;
}
}
catch { }
}
private void FoundMatch(FindMatch match)
{
try
{
if (InvokeRequired)
{
Invoke(new Action(() => { FoundMatch(match); }));
}
else
{
ListViewItem lvi = new ListViewItem(new string[] { match.File.Name, match.Line.ToString(), match.Char.ToString() });
lvi.Tag = match;
MatchesListView.Items.Add(lvi);
}
}
catch { }
}
private void UpdateStatus(string text)
{
try
{
if (InvokeRequired)
{
Invoke(new Action(() => { UpdateStatus(text); }));
}
else
{
StatusLabel.Text = text;
}
}
catch { }
}
private void MatchesListView_SelectedIndexChanged(object sender, EventArgs e)
{
if (MatchesListView.SelectedItems.Count != 1)
{
FunctionTextBox.Text = string.Empty;
return;
}
ListViewItem selitem = MatchesListView.SelectedItems[0];
FindMatch fm = (FindMatch)selitem.Tag;
ScriptFunction func = fm.File.TryGetFunction(fm.Line, true);
if (func == null)
{
FunctionTextBox.Text = string.Empty;
return;
}
FunctionTextBox.Text = func.FunctionText;
int lineoff = fm.Line - func.StartLine;
int charoff = fm.Char;
for (int i = 0; i < lineoff; i++)
{
charoff += fm.File.FileLines[i + func.StartLine].Length + 2;
}
//FunctionTextBox.Select(charoff, FindTextBox.Text.Length);
FunctionTextBox.SelectionStart = charoff;
FunctionTextBox.SelectionLength = FindTextBox.Text.Length;
FunctionTextBox.ScrollToCaret();
}
}
public class FindMatch
{
public ScriptFile File { get; set; }
public int Line { get; set; }
public int Char { get; set; }
}
}