-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoopHandler.cs
156 lines (134 loc) · 4.3 KB
/
LoopHandler.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
namespace HTTPConsole;
using System;
using System.Collections.Generic;
using InputHandler;
public static class LoopHandler
{
public static void Loop(Func<string> getString, bool verbose, Uri uri, bool display, string pipePath)
{
WriteLine("Display counter? (y/n)");
bool displayCounter = Input.GetYN(getString);
WriteLine("Enter for statement");
string s = getString().Trim().ToLower();
string[] split = s.Split();
if (split[0] != "for")
{
WriteLine("Loop malformed, returning. Start your loop with \"for\"");
return;
}
int start = 0, end = -1;
bool infinite = split.Length == 1;
if (split.Length > 1) // Range
{
try
{
string[] rangeSplit = split[1].Split("..");
if (rangeSplit.Length == 1)
{
if (split[1].Length >= 3) infinite = split[1][^2..] == "..";
end = int.Parse(rangeSplit[0]);
}
else if (rangeSplit.Length == 2)
{
start = int.Parse(rangeSplit[0]);
end = int.Parse(rangeSplit[1]);
}
else throw new Exception();
}
catch
{
WriteLine("Loop range malformed, returning.");
return;
}
}
Func<int, int> increment = null;
if (split.Length > 2) // Increment
{
try
{
char c = split[2][0];
int by;
if (split[2].Length > 1)
{
by = int.Parse(split[2][1..]);
}
else
{
if (int.TryParse(split[2], out by))
{
increment = infinite || start <= end
? (x => x + 1)
: (x => x - 1);
goto Leave;
}
else
{
by = "*/^<>".Contains(c) ? 2 : 1;
}
}
increment = c switch
{
'+' => x => x + by,
'-' => x => x - by,
'*' => x => x * by,
'/' => x => x / by,
'^' => x => (int)Math.Pow(x, by),
'<' => x => x << by,
'>' => x => x >> by,
_ => throw new Exception()
};
if (c is '*' or '/' && start == 0) start = 1;
Leave:;
}
catch
{
WriteLine("Loop increment malformed, returning.");
return;
}
}
else
{
increment = infinite || start <= end
? (x => x + 1)
: (x => x - 1);
}
WriteLine("Enter special instructions (enter twice to continue)");
List<Condition> conditions = ConditionHandler.Handle(getString);
// display/break key/header/content/code/uri contains/is value
WriteLine("Enter macro (finish with END)");
List<string> macro = new();
string line;
do
{
line = getString();
line = line.Replace("/end", "end");
macro.Add(line);
}
while (line.ToLower() != "end");
WriteLine("Running...");
for (int i = start, j = 0; infinite || IsBetween(i, start, end); i = increment(i), j++)
{
if (displayCounter)
{
Program.BadLog($"TRIAL {j + 1} WITH COUNTER {i}", pipePath, true);
}
int c = 0; // current macro
try
{
Program.Command(() => macro[c++], false, uri, true, i, pipePath, conditions); // haha funny c++
}
catch (TimeoutException) // bad, don't be lazy
{
break;
}
}
void WriteLine(object x)
{
if (verbose) Console.WriteLine(x);
}
}
private static bool IsBetween(int x, int start, int end) =>
start < end
? start <= x && x <= end
: start >= x && x >= end;
}