-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
196 lines (164 loc) · 7.31 KB
/
Program.cs
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
using System;
using System.IO;
using System.Windows.Forms;
using System.Configuration;
using Microsoft.WindowsAPICodePack.Dialogs;
namespace TF2SpamScriptGen
{
static class Program
{
// use this wait time if the one the user enters isn't valid
// (for instance, if it contains a letter instead of a number)
const int DEFAULT_WAIT_TIME = 200;
// settings for generated script
static string scriptsFolder;
static string keyToBind;
static string waitTime;
// form elements
static TextBox linesInputTextBox;
static TextBox scriptsFolderTextBox;
static Button scriptsFolderButton;
static NumericUpDown waitTimeInput;
static TextBox keyToBindTextBox;
static Button generateButton;
static Button saveSettingsButton;
static Button loadButton;
[STAThread]
static void Main(string[] args)
{
// set settings
scriptsFolder = ConfigurationManager.AppSettings.Get("scriptsFolder");
keyToBind = ConfigurationManager.AppSettings.Get("keyToBind");
waitTime = ConfigurationManager.AppSettings.Get("waitTime");
Form1 form1 = new Form1();
linesInputTextBox = (TextBox)form1.Controls.Find("linesInputTextBox", false)[0];
scriptsFolderTextBox = (TextBox)form1.Controls.Find("scriptsFolderTextBox", false)[0];
scriptsFolderButton = (Button)form1.Controls.Find("scriptsFolderButton", false)[0];
waitTimeInput = (NumericUpDown)form1.Controls.Find("waitTimeInput", false)[0];
keyToBindTextBox = (TextBox)form1.Controls.Find("keyToBindTextBox", false)[0];
generateButton = (Button)form1.Controls.Find("generateButton", false)[0];
saveSettingsButton = (Button)form1.Controls.Find("saveSettingsButton", false)[0];
loadButton = (Button)form1.Controls.Find("loadButton", false)[0];
// set wait time box value
try
{
waitTimeInput.Value = int.Parse(waitTime);
}
catch
{
waitTimeInput.Value = DEFAULT_WAIT_TIME;
waitTime = DEFAULT_WAIT_TIME.ToString();
}
// set other input boxes text
scriptsFolderTextBox.Text = scriptsFolder;
keyToBindTextBox.Text = keyToBind;
// add events
scriptsFolderButton.Click += new EventHandler(OnScriptsFolderButtonClick);
generateButton.Click += new EventHandler(OnGenerateButtonClick);
saveSettingsButton.Click += new EventHandler(OnSaveSettingsButtonClick);
loadButton.Click += new EventHandler(OnLoadButtonClick);
linesInputTextBox.KeyDown += new KeyEventHandler(OnLinesInputTextBoxKeyDown);
// run form
Application.Run(form1);
}
static void OnLinesInputTextBoxKeyDown(object sender, KeyEventArgs e)
{
// allow the use of ctrl+A to select all, which isn't usually possible by default
if(e.Modifiers.HasFlag(Keys.Control) && e.KeyCode == Keys.A)
{
e.SuppressKeyPress = true;
linesInputTextBox.SelectAll();
}
}
static void OnScriptsFolderButtonClick(object sender, EventArgs e)
{
// change script folder
CommonOpenFileDialog dialog = new CommonOpenFileDialog();
dialog.InitialDirectory = scriptsFolder;
dialog.IsFolderPicker = true;
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
dialog.InitialDirectory = dialog.FileName;
scriptsFolderTextBox.Text = dialog.FileName;
}
}
static void OnSaveSettingsButtonClick(object sender, EventArgs e)
{
UpdateSettingsVars();
ConfigurationManager.AppSettings.Set("scriptsFolder", scriptsFolder);
ConfigurationManager.AppSettings.Set("keyToBind", keyToBind);
ConfigurationManager.AppSettings.Set("waitTime", waitTime);
}
static void OnGenerateButtonClick(object sender, EventArgs e)
{
CommonSaveFileDialog dialog = new CommonSaveFileDialog();
dialog.InitialDirectory = scriptsFolder;
dialog.DefaultExtension = "cfg";
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
UpdateSettingsVars();
string[] lines = linesInputTextBox.Lines;
string[] scriptLines = GenerateScriptLines(lines);
File.WriteAllLines(dialog.FileName, scriptLines);
}
}
static void OnLoadButtonClick(object sender, EventArgs e)
{
CommonOpenFileDialog dialog = new CommonOpenFileDialog();
dialog.InitialDirectory = scriptsFolder;
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
linesInputTextBox.Lines = GetInputFromScript(dialog.FileName);
}
}
static void UpdateSettingsVars()
{
scriptsFolder = scriptsFolderTextBox.Text;
keyToBind = keyToBindTextBox.Text;
waitTime = waitTimeInput.Value.ToString();
}
static string[] GenerateScriptLines(string[] lines)
{
int setLineCount;
if (lines.Length % 5 == 0)
setLineCount = lines.Length / 5;
else
setLineCount = (int)Math.Ceiling((double)lines.Length / 5.0);
string[] scriptLines = new string[lines.Length + setLineCount +1];
scriptLines[0] = "bind " + keyToBind + " \"spamSet0\"";
for (int i = 0; i < lines.Length; i++)
{
scriptLines[i + 1] = "alias spam" + (i) + " \"say " + lines[i].Replace("\"", "'") + "\"";
}
for (int i = 0; i < setLineCount; i++)
{
string newLine = "alias spamSet" + i + " \"";
for (int o = 0; o < 5; o++)
{
newLine += "spam" + ((i * 5) + o) + "; wait " + waitTime + "; ";
if ((i * 5) + o >= lines.Length - 1)
break;
}
if (i != setLineCount - 1)
newLine += "spamSet" + (i + 1) + ";";
newLine += "\"";
scriptLines[lines.Length + 1 + i] = newLine;
}
return scriptLines;
}
static string[] GetInputFromScript(string scriptFilePath)
{
string[] scriptLines = File.ReadAllLines(scriptFilePath);
// modify the script line count to get the line count of the plain text.
int lineCount = scriptLines.Length - 2;
lineCount = (int)((float)lineCount * (5f / 6f)) + 1;
string[] lines = new string[lineCount];
for(int i = 0; i < lineCount; i++)
{
// turn from a script line back to just the plain text
lines[i] = scriptLines[i + 1].Split('"')[1].Remove(0, 4);
}
return lines;
}
}
}