-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern.rs
392 lines (361 loc) · 12.4 KB
/
pattern.rs
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
//! Pattern command
use crate::{Context, Error};
use ::pattern as pattern_draw;
pub use pattern_draw::{Error as PatternError, Noteskin};
use poise::serenity_prelude as serenity;
pub struct NoteskinProvider {
dbz: pattern_draw::Noteskin,
lambda: pattern_draw::Noteskin,
wafles: pattern_draw::Noteskin,
delta_note: pattern_draw::Noteskin,
sbz: pattern_draw::Noteskin,
mbz: pattern_draw::Noteskin,
eo_baner: pattern_draw::Noteskin,
rustmania: pattern_draw::Noteskin,
}
impl NoteskinProvider {
pub fn load() -> Self {
Self {
dbz: Noteskin::read_ldur_with_6k(
64,
"assets/noteskin/dbz-notes.png",
"assets/noteskin/dbz-receptor.png",
"assets/noteskin/dbz-mine.png",
),
delta_note: Noteskin::read_pump(
64,
"assets/noteskin/deltanote-center-notes.png",
"assets/noteskin/deltanote-center-receptor.png",
"assets/noteskin/deltanote-corner-notes.png",
"assets/noteskin/deltanote-corner-receptor.png",
"assets/noteskin/deltanote-mine.png",
),
sbz: Noteskin::read_bar(
64,
"assets/noteskin/sbz-notes.png",
"assets/noteskin/sbz-receptor.png",
"assets/noteskin/dbz-mine.png",
),
mbz: Noteskin::read_bar(
64,
"assets/noteskin/mbz-notes.png",
"assets/noteskin/mbz-receptor.png",
"assets/noteskin/dbz-mine.png",
),
lambda: {
let mut lambda = Noteskin::read_ldur_with_6k(
128,
"assets/noteskin/lambda-notes.png",
"assets/noteskin/lambda-receptor.png",
"assets/noteskin/lambda-mine.png",
);
lambda.resize_sprites(64);
lambda
},
wafles: Noteskin::read_ldur_with_6k(
64,
"assets/noteskin/wafles-notes.png",
"assets/noteskin/wafles-receptor.png",
"assets/noteskin/wafles-mine.png",
),
eo_baner: Noteskin::read_ldur(
120,
"assets/noteskin/eobaner-note-left.png",
"assets/noteskin/eobaner-receptor-left.png",
"assets/noteskin/eobaner-note-down.png",
"assets/noteskin/eobaner-receptor-down.png",
"assets/noteskin/eobaner-note-up.png",
"assets/noteskin/eobaner-receptor-up.png",
"assets/noteskin/eobaner-note-right.png",
"assets/noteskin/eobaner-receptor-right.png",
"assets/noteskin/eobaner-mine.png",
),
rustmania: {
let mut rustmania = Noteskin::read_ldur_with_6k(
224,
"assets/noteskin/rustmania-notes.png",
"assets/noteskin/rustmania-receptor.png",
"assets/noteskin/rustmania-mine.png",
);
rustmania.turn_sprites_upside_down(); // I made an oopsie in gimp
rustmania
},
}
}
}
async fn always_true(_: Context<'_>) -> Result<bool, Error> {
Ok(true)
}
/// Visualize note patterns
#[poise::command(prefix_command, slash_command, track_edits, check = "always_true")]
pub async fn pattern(
ctx: Context<'_>,
#[rest]
#[description = "Pattern string to render"]
pattern: String,
) -> Result<(), Error> {
// People are supposed to write `+help pattern` but some write `+pattern help` so let's help
// them as well :)
if pattern.eq_ignore_ascii_case("help") {
super::help::send_help(ctx, true).await?;
return Ok(());
}
let mut noteskin_override = None;
let mut keymode_override = None;
let mut snap = etterna::Snap::_16th.into();
let mut vertical_spacing_multiplier = 1.0;
let mut scroll_direction = ctx
.data()
.lock_data()
.scroll(ctx.author().id)
.unwrap_or(etterna::ScrollDirection::Upscroll);
let mut segments = Vec::new();
let extract_snap = |string: &str, user_intended: &mut bool| {
const ENDINGS: &[&str] = &["st", "sts", "nd", "nds", "rd", "rds", "th", "ths"];
let characters_to_truncate = ENDINGS
.iter()
.find(|&ending| string.ends_with(ending))?
.len();
// UNWRAP: we're only removing up to the string length, so we can't go out-of-bounds
let string_without_ending = string
.get(..(string.len() - characters_to_truncate))
.unwrap();
let snap: u32 = string_without_ending.parse().ok()?;
*user_intended = true;
pattern_draw::FractionalSnap::from_snap_number(snap)
};
let extract_noteskin = |string: &str, _user_intended: &mut bool| {
// make lowercase and remove all special characters
let mut normalized_noteskin_name = string.to_ascii_lowercase();
normalized_noteskin_name.retain(|c| c.is_alphanumeric());
match normalized_noteskin_name.as_str() {
"dbz" | "dividebyzero" => Some(&ctx.data().noteskin_provider.dbz),
"wafles" | "wafles3" => Some(&ctx.data().noteskin_provider.wafles),
"default" | "lambda" => Some(&ctx.data().noteskin_provider.lambda),
"deltanote" | "delta" => Some(&ctx.data().noteskin_provider.delta_note),
"sbz" | "subtractbyzero" => Some(&ctx.data().noteskin_provider.sbz),
"mbz" | "multiplybyzero" => Some(&ctx.data().noteskin_provider.mbz),
"eobaner" => Some(&ctx.data().noteskin_provider.eo_baner),
"rustmania" => Some(&ctx.data().noteskin_provider.rustmania),
_ => None,
}
};
let extract_vertical_spacing_multiplier = |string: &str, user_intended: &mut bool| {
if !string.ends_with('x') {
return None;
};
// UNWRAP: at this point the string must have 'x' at the end so we can safely strip one char
let vertical_spacing_multiplier: f32 =
string.get(..(string.len() - 1)).unwrap().parse().ok()?;
*user_intended = true;
if vertical_spacing_multiplier > 0.0 {
Some(vertical_spacing_multiplier)
} else {
None
}
};
let extract_scroll_direction =
|string: &str, _user_intended: &mut bool| match string.to_lowercase().as_str() {
"up" => Some(etterna::ScrollDirection::Upscroll),
"down" | "reverse" => Some(etterna::ScrollDirection::Downscroll),
_ => None,
};
let extract_keymode = |string: &str, user_intended: &mut bool| {
if !(string.ends_with('k') || string.ends_with('K')) {
return None;
}
// UNWRAP: at this point the string must have 'k' at the end so we can safely strip one char
let keymode: u32 = string.get(..(string.len() - 1)).unwrap().parse().ok()?;
*user_intended = true;
if keymode > 0 {
Some(keymode)
} else {
None
}
};
let mut pattern_buffer = String::new();
for arg in pattern.split_whitespace() {
let mut did_user_intend = false;
if let Some(new_snap) = extract_snap(arg, &mut did_user_intend) {
if pattern_buffer.len() > 0 {
segments.push((pattern_buffer.clone(), snap));
pattern_buffer.clear();
}
snap = new_snap;
continue;
}
if did_user_intend {
poise::say_reply(ctx, format!("\"{}\" is not a valid snap", arg)).await?;
}
let mut did_user_intend = false;
if let Some(noteskin) = extract_noteskin(arg, &mut did_user_intend) {
noteskin_override = Some(noteskin);
continue;
}
if did_user_intend {
poise::say_reply(ctx, format!("\"{}\" is not a valid noteskin name", arg)).await?;
}
let mut did_user_intend = false;
if let Some(vertical_spacing_multiplier_override) =
extract_vertical_spacing_multiplier(arg, &mut did_user_intend)
{
vertical_spacing_multiplier = vertical_spacing_multiplier_override;
continue;
}
if did_user_intend {
poise::say_reply(ctx, format!("\"{}\" is not a valid zoom option", arg)).await?;
}
let mut did_user_intend = false;
if let Some(scroll_direction_override) = extract_scroll_direction(arg, &mut did_user_intend)
{
scroll_direction = scroll_direction_override;
continue;
}
if did_user_intend {
poise::say_reply(ctx, format!("\"{}\" is not a valid scroll direction", arg)).await?;
}
let mut did_user_intend = false;
if let Some(keymode) = extract_keymode(arg, &mut did_user_intend) {
keymode_override = Some(keymode);
continue;
}
if did_user_intend {
poise::say_reply(ctx, format!("\"{}\" is not a valid keymode", arg)).await?;
}
// if nothing matched, this is just an ordinary part of the pattern
pattern_buffer += arg;
}
if pattern_buffer.len() > 0 {
segments.push((pattern_buffer, snap));
}
let segments = if pattern.contains("ArrowVortex:notes:") {
let mut full_pattern = String::new();
for (partial_pattern, _) in &segments {
full_pattern += partial_pattern;
}
// Unwrap code block ticks
if let Some(s) = full_pattern
.strip_prefix("```")
.and_then(|s| s.strip_suffix("```"))
{
full_pattern = s.trim().to_owned();
}
let notes = match arrowvortex_clipboard::decode(full_pattern.as_bytes())
.map_err(|e| format!("Failed to decode ArrowVortex pattern: {}", e))?
{
arrowvortex_clipboard::DecodeResult::RowBasedNotes(notes) => notes,
arrowvortex_clipboard::DecodeResult::TimeBasedNotes(_) => {
return Err("Please change to row-based mode before copying the notes".into())
}
arrowvortex_clipboard::DecodeResult::TempoEvents(_) => {
return Err("Please paste note data, not tempo data".into())
}
};
let mut pattern = pattern_draw::Pattern { rows: Vec::new() };
let mut row = Vec::new();
let mut prev_pos = 0;
for note in notes {
if note.pos != prev_pos {
pattern.rows.push(pattern_draw::Row { notes: row });
row = Vec::new();
for _ in (prev_pos + 1)..note.pos {
pattern.rows.push(pattern_draw::Row { notes: Vec::new() });
}
prev_pos = note.pos;
}
let note_type = match note.kind {
arrowvortex_clipboard::NoteKind::Tap => pattern_draw::NoteType::Tap,
arrowvortex_clipboard::NoteKind::Mine => pattern_draw::NoteType::Mine,
_ => continue, // ignore unsupported note types
};
row.push((pattern_draw::Lane::Index(note.column as _), note_type));
}
if !row.is_empty() {
pattern.rows.push(pattern_draw::Row { notes: row });
}
vertical_spacing_multiplier /= 6.0;
vec![(pattern, etterna::Snap::_192th.into())]
} else {
segments
.into_iter()
.map(|(partial_pattern, snap)| (pattern_draw::parse_pattern(&partial_pattern), snap))
.collect()
};
let keymode = if let Some(keymode) = keymode_override {
keymode
} else {
let highest_lane = segments
.iter()
.flat_map(|(pattern, _)| &pattern.rows)
// if the user entered `+pattern ldr`, was the highest column 3, or 4? remember, the
// meaning of `r` depends on keymode, but we don't know the keymode yet. I've
// decided to assume 4k in the fallback case
.filter_map(|row| {
row.notes
.iter()
.map(|(lane, _note_type)| lane.column_number_with_keymode(4))
.max()
})
.max()
.ok_or(PatternError::EmptyPattern)?;
let keymode = (highest_lane + 1) as u32;
keymode.max(4) // clamp keymode to a minimum of 4k. yes, 3k exists, but it's so niche that even if only three lanes are populated, the pattern is probably meant to be 4k
};
let noteskin = if let Some(noteskin) = noteskin_override {
¬eskin
} else {
// choose a default noteskin
match keymode {
3 | 4 | 6 | 8 => &ctx.data().noteskin_provider.dbz,
5 | 10 => &ctx.data().noteskin_provider.delta_note,
7 | 9 => &ctx.data().noteskin_provider.sbz,
_ => &ctx.data().noteskin_provider.sbz, // fallback
}
};
let generated_pattern = pattern_draw::draw_pattern(pattern_draw::PatternRecipe {
noteskin,
scroll_direction,
keymode: keymode as usize, /* I thought I had changedit to u32 in pattern_draw???? */
vertical_spacing_multiplier,
pattern: &segments,
max_image_dimensions: (5000, 10000),
max_sprites: 1000,
})?;
let mut img_bytes = Vec::with_capacity(1_000_000); // preallocate 1 MB for the img
image::DynamicImage::ImageRgba8(generated_pattern)
.write_to(&mut img_bytes, image::ImageOutputFormat::Png)
.map_err(pattern_draw::Error::ImageError)?;
let image_attachment = serenity::AttachmentType::Bytes {
data: img_bytes.into(),
filename: "output.png".to_owned(),
};
match ctx {
poise::Context::Prefix(_) => {
poise::send_reply(ctx, |f| f.attachment(image_attachment)).await?;
}
poise::Context::Application(_) => {
// We need to send some initial response! Only follow up messages support
// attachments
poise::say_reply(ctx, format!("Pattern `{}`", pattern)).await?;
poise::send_reply(ctx, |f| f.attachment(image_attachment)).await?;
}
}
Ok(())
}
/// Change the scroll direction in subsequent pattern command calls
///
/// Call this command with `+scrollset [down/up]`
#[poise::command(prefix_command, aliases("setscroll"), track_edits, slash_command)]
pub async fn scrollset(
ctx: Context<'_>,
#[description = "Scroll direction"] scroll: String,
) -> Result<(), Error> {
let scroll = match scroll.to_lowercase().as_str() {
"down" | "downscroll" | "reverse" => etterna::ScrollDirection::Downscroll,
"up" | "upscroll" => etterna::ScrollDirection::Upscroll,
_ => return Err(format!("No such scroll '{}'", scroll).into()),
};
ctx.data().lock_data().set_scroll(ctx.author().id, scroll);
poise::say_reply(ctx, format!("Your scroll type is now {:?}", scroll)).await?;
Ok(())
}