Skip to content

Commit f809d52

Browse files
change duration token validation
1 parent 085503e commit f809d52

2 files changed

Lines changed: 42 additions & 49 deletions

File tree

‎Code/MethodSystem/Methods/BroadcastMethods/AnimatedBroadcastMethod.cs‎

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class AnimatedBroadcastMethod : SynchronousMethod, IAdditionalDescription
2121
new DurationArgument("duration"),
2222
new TextArgument("content")
2323
{
24-
Description =
24+
Description =
2525
"Use <charwait=x> and </charwait> tags to specify how much time each character will take to be printed, " +
2626
"or <wait=x> to specify a single wait action. " +
2727
"Example: \"<charwait=100ms>Slow print</charwait><br><wait=2s>waited 2 seconds for that!\""
@@ -32,23 +32,23 @@ public class AnimatedBroadcastMethod : SynchronousMethod, IAdditionalDescription
3232
DefaultValue = new(60, null)
3333
}
3434
];
35-
35+
3636
public override void Execute()
3737
{
3838
var players = Args.GetPlayers("players");
3939
var content = Args.GetText("content");
4040
var duration = Args.GetDuration("duration").TotalSeconds;
4141
var lineBreakLength = Args.GetInt("line break length");
42-
42+
4343
foreach (var plr in players)
4444
{
4545
plr.Connection.Send(new CassieTtsPayload(string.Empty, string.Empty, false));
4646
plr.SendCassieMessage(
47-
$"$SLEEP_{duration-1} .",
47+
$"$SLEEP_{duration-1} .",
4848
Helper.FormatToCassieCentralScreenSubtitles(
49-
content,
49+
content,
5050
lineBreakLength
51-
),
51+
),
5252
false,
5353
0
5454
);
@@ -58,15 +58,15 @@ public override void Execute()
5858
public string AdditionalDescription =>
5959
"Uses CASSIE to make an animated broadcast - if there is CASSIE playing, it will be stopped. " +
6060
"Keep custom formatting to a minimum, this system is very limited.";
61-
61+
6262
public static class Helper
6363
{
6464
public static string FormatToRawCassieSubtitles(string text, int lineBreakLength)
6565
{
6666
var result = new StringBuilder();
6767
var index = 72;
6868
var timePerCharStack = new Stack<TimeSpan>();
69-
69+
7070
foreach (var line in text.Split('\n'))
7171
{
7272
// Skip empty lines
@@ -75,11 +75,11 @@ public static string FormatToRawCassieSubtitles(string text, int lineBreakLength
7575
index -= 1;
7676
continue;
7777
}
78-
78+
7979
// Calculate actual length excluding HTML tags
8080
var len = CalculateTextLength(line);
8181
var parts = new List<string>();
82-
82+
8383
// Split long lines
8484
if (len > lineBreakLength)
8585
{
@@ -89,23 +89,23 @@ public static string FormatToRawCassieSubtitles(string text, int lineBreakLength
8989
{
9090
parts.Add(line);
9191
}
92-
92+
9393
// Add all parts to result with proper formatting
9494
foreach (var part in parts)
9595
{
9696
index -= 1;
9797
result.Append(FormatLine(part, index, timePerCharStack));
9898
}
9999
}
100-
100+
101101
return result.ToString();
102102
}
103-
103+
104104
private static int CalculateTextLength(string line)
105105
{
106106
var len = 0;
107107
var isTag = false;
108-
108+
109109
foreach (var c in line)
110110
{
111111
switch (c)
@@ -117,17 +117,17 @@ private static int CalculateTextLength(string line)
117117
isTag = false;
118118
continue;
119119
}
120-
120+
121121
if (!isTag) len++;
122122
}
123-
123+
124124
return len;
125125
}
126-
126+
127127
private static void SplitLongLine(string line, List<string> parts, int lineBreakLength)
128128
{
129129
int? lastUnusedSpaceIndex = null;
130-
130+
131131
for (var i = 0; i < line.Length; i++)
132132
{
133133
if (!char.IsWhiteSpace(line[i])) continue;
@@ -137,11 +137,11 @@ private static void SplitLongLine(string line, List<string> parts, int lineBreak
137137
lastUnusedSpaceIndex = i;
138138
continue;
139139
}
140-
140+
141141
var lastAvailableSpaceIndex = lastUnusedSpaceIndex ?? i;
142142
var leftPart = line[..lastAvailableSpaceIndex].Trim();
143143
parts.Add(leftPart);
144-
144+
145145
var rightPart = line[(lastAvailableSpaceIndex + 1)..].Trim();
146146
if (CalculateTextLength(rightPart) > lineBreakLength)
147147
{
@@ -151,10 +151,10 @@ private static void SplitLongLine(string line, List<string> parts, int lineBreak
151151
{
152152
parts.Add(rightPart);
153153
}
154-
154+
155155
return;
156156
}
157-
157+
158158
parts.Add(line);
159159
}
160160

@@ -163,12 +163,12 @@ private static string GetDelayString(TimeSpan time)
163163
var dots = new string('c', (int)Math.Round(time.TotalMilliseconds / 20, MidpointRounding.AwayFromZero));
164164
return $"<size=0>{dots}</size>";
165165
}
166-
166+
167167
private static string FormatLine(string text, int index, Stack<TimeSpan> activeDelayTags)
168168
{
169169
var openTags = new Regex(@"<charwait=((\d|\.)+(ms|s))>").Matches(text).Cast<Match>().ToArray();
170170
var closeTags = new Regex("</charwait>").Matches(text).Cast<Match>().ToArray();
171-
171+
172172
StringBuilder newText = new();
173173
var isTag = false;
174174
for (int i = 0; i < text.Length; i++)
@@ -183,31 +183,30 @@ private static string FormatLine(string text, int index, Stack<TimeSpan> activeD
183183

184184
if (openTags.FirstOrDefault(t => t.Index == i) is { } openTag)
185185
{
186-
if (DurationToken.Parse(openTag.Groups[1].Value).HasErrored(out _, out var nullableTimeSpan)
187-
|| nullableTimeSpan is not { } timeSpan)
186+
if (DurationToken.Parse(openTag.Groups[1].Value) is not { } timeSpan)
188187
{
189188
newText.Append(c);
190189
continue;
191190
}
192-
191+
193192
activeDelayTags.Push(timeSpan);
194193
i = openTag.Index + openTag.Length - 1;
195194
continue;
196195
}
197-
196+
198197
if (closeTags.FirstOrDefault(t => t.Index == i) is { } closeTag)
199198
{
200199
if (activeDelayTags.Count == 0)
201200
{
202201
newText.Append(c);
203202
continue;
204203
}
205-
204+
206205
activeDelayTags.Pop();
207206
i = closeTag.Index + closeTag.Length - 1;
208207
continue;
209208
}
210-
209+
211210
isTag = c switch
212211
{
213212
'<' => true,
@@ -227,15 +226,14 @@ private static string FormatLine(string text, int index, Stack<TimeSpan> activeD
227226

228227
foreach (var match in new Regex(@"<wait=(\d+(ms|s))>").Matches(newText.ToString()).Cast<Match>().ToArray())
229228
{
230-
if (DurationToken.Parse(match.Groups[1].Value).HasErrored(out _, out var nullableTimeSpan)
231-
|| nullableTimeSpan is not { } timeSpan)
229+
if (DurationToken.Parse(match.Groups[1].Value) is not { } timeSpan)
232230
{
233231
continue;
234232
}
235-
233+
236234
newText.Replace(match.Value, GetDelayString(timeSpan));
237235
}
238-
236+
239237
return $"<voffset={index}em>{newText}</voffset>\\n";
240238
}
241239

‎Code/TokenSystem/Tokens/ValueTokens/DurationToken.cs‎

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,16 @@ public class DurationToken : LiteralValueToken<DurationValue>
99
{
1010
protected override IParseResult InternalParse(Script scr)
1111
{
12-
if (Parse(RawRep).HasErrored(out var error, out var value))
13-
{
14-
return new Error(error);
15-
}
16-
17-
if (value is not { } timeSpan)
12+
if (Parse(RawRep) is not {} timeSpan)
1813
{
1914
return new Ignore();
2015
}
21-
16+
2217
Value = timeSpan;
2318
return new Success();
2419
}
2520

26-
public static TryGet<TimeSpan?> Parse(string value)
21+
public static TimeSpan? Parse(string value)
2722
{
2823
if (TimeSpan.TryParse(value, CultureInfo.InvariantCulture, out var result) && result.TotalMilliseconds > 0)
2924
{
@@ -33,18 +28,18 @@ protected override IParseResult InternalParse(Script scr)
3328
var unitIndex = Array.FindIndex(value.ToCharArray(), char.IsLetter);
3429
if (unitIndex == -1)
3530
{
36-
return null as TimeSpan?;
31+
return null;
3732
}
38-
33+
3934
string numberString = string.Join("", value.Take(unitIndex).ToArray());
4035
if (!double.TryParse(numberString, NumberStyles.Any, CultureInfo.InvariantCulture, out var valueAsDouble))
4136
{
42-
return null as TimeSpan?;
37+
return null;
4338
}
44-
39+
4540
if (valueAsDouble < 0)
4641
{
47-
return "Duration cannot be negative.";
42+
return null;
4843
}
4944

5045
var unit = value[unitIndex..];
@@ -55,7 +50,7 @@ protected override IParseResult InternalParse(Script scr)
5550
"m" => TimeSpan.FromMinutes(valueAsDouble),
5651
"h" => TimeSpan.FromHours(valueAsDouble),
5752
"d" => TimeSpan.FromDays(valueAsDouble),
58-
_ => $"Provided unit '{unit}' is not valid."
53+
_ => null
5954
};
6055
}
61-
}
56+
}

0 commit comments

Comments
 (0)