rmcl 0.3.2

A fully featured Minecraft TUI launcher
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// live minecraft process log parser.
//
// disk launch logs stay raw, but the tui needs enough structure to color and
// group output sanely. this parser keeps stdout/stderr as a hint, frames
// multiline java/jvm/native bursts into events, then assigns a semantic level.

use libcasr::exception::Exception;
use libcasr::java::{JavaException, JavaStacktrace};
use libcasr::stacktrace::ParseStacktrace;
use regex::Regex;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LogStream {
    Stdout,
    Stderr,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LogLevel {
    Trace,
    Debug,
    Info,
    Warn,
    Error,
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct JavaParse {
    pub has_exception: bool,
    pub has_stacktrace: bool,
    pub stacktrace_frames: usize,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedLogEvent {
    pub level: LogLevel,
    pub primary_stream: LogStream,
    pub lines: Vec<String>,
    pub java: Option<JavaParse>,
}

#[derive(Debug, Clone)]
struct PendingEvent {
    level: LogLevel,
    primary_stream: LogStream,
    lines: Vec<String>,
    normalized_lines: Vec<String>,
    kind: EventKind,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum EventKind {
    Header,
    Java,
    JvmBurst,
    Native,
    Plain,
}

#[derive(Debug)]
pub struct MinecraftLogParser {
    current: Option<PendingEvent>,
    minecraft_header_re: Regex,
    plain_level_re: Regex,
    java_exception_re: Regex,
    java_continuation_re: Regex,
    jvm_burst_re: Regex,
    native_header_re: Regex,
}

impl Default for MinecraftLogParser {
    fn default() -> Self {
        Self::new()
    }
}

impl MinecraftLogParser {
    pub fn new() -> Self {
        Self {
            // header matching stays local because this parser is in the live
            // path; pulling in heavier pattern machinery would not help with
            // the multiline framing problem.
            minecraft_header_re: Regex::new(
                r"(?i)^\s*(?:\[[^\]]+\]\s*)*\[[^/\]]+/(?P<level>TRACE|DEBUG|INFO|WARN|ERROR|FATAL)\]\s*:",
            )
            .expect("valid minecraft header regex"),
            plain_level_re: Regex::new(
                r"(?i)^\s*(?:\[[^\]]+\]\s*)*(?P<level>TRACE|DEBUG|INFO|WARN|ERROR|FATAL)\b\s*(?:[:\]\-])?",
            )
            .expect("valid plain level regex"),
            java_exception_re: Regex::new(
                r"(?i)^\s*(?:Exception in thread .+|Caused by: |Suppressed: )?(?:[a-z_$][\w$]*\.)+(?:[A-Z][\w$]*(?:Exception|Error)|Throwable)(?::|\b)",
            )
            .expect("valid java exception regex"),
            java_continuation_re: Regex::new(
                r"^(?:\s+at\s+.+|\s*\.\.\.\s+\d+\s+more|Caused by:\s+.+|Suppressed:\s+.+)$",
            )
            .expect("valid java continuation regex"),
            jvm_burst_re: Regex::new(
                r"(?i)^\s*(?:Error:\s+.+|Unrecognized option:\s+.+|Could not create the Java Virtual Machine\.|A fatal exception has occurred\. Program will exit\.|Invalid maximum heap size:.+)$",
            )
            .expect("valid jvm burst regex"),
            native_header_re: Regex::new(r"^\[[A-Z]\]\s+\[[0-9:.]+\]\s+[^:]+:")
                .expect("valid native header regex"),
            current: None,
        }
    }

    pub fn push_line(&mut self, stream: LogStream, line: impl Into<String>) -> Vec<ParsedLogEvent> {
        let line = line.into();
        // classify against ansi-stripped text, but keep the original line for
        // display so the live log remains faithful to the child process.
        let normalized = fast_strip_ansi::strip_ansi_string(&line).into_owned();
        let analysis = self.analyze_line(stream, &normalized);

        let Some(current) = &mut self.current else {
            self.current = Some(PendingEvent::new(stream, line, normalized, analysis));
            return Vec::new();
        };

        // only hard starts split events. this keeps stacktraces and native
        // loader bursts together even when they arrive one line at a time.
        if should_split(current.kind, analysis.kind) {
            let finished = self.current.take().map(finish_event);
            self.current = Some(PendingEvent::new(stream, line, normalized, analysis));
            finished.into_iter().collect()
        } else {
            if analysis.level_priority() > current.level_priority() {
                current.level = analysis.level;
            }
            if matches!(current.kind, EventKind::Plain)
                && !matches!(analysis.kind, EventKind::Plain)
            {
                current.kind = analysis.kind;
            }
            current.lines.push(line);
            current.normalized_lines.push(normalized);
            Vec::new()
        }
    }

    pub fn flush(&mut self) -> Option<ParsedLogEvent> {
        self.current.take().map(finish_event)
    }

    pub fn has_pending(&self) -> bool {
        self.current.is_some()
    }

    fn analyze_line(&self, stream: LogStream, normalized: &str) -> LineAnalysis {
        if let Some(level) = self.header_level(normalized) {
            return LineAnalysis {
                level,
                kind: EventKind::Header,
            };
        }

        if self.java_exception_re.is_match(normalized) {
            return LineAnalysis {
                level: LogLevel::Error,
                kind: EventKind::Java,
            };
        }

        if self.java_continuation_re.is_match(normalized) {
            return LineAnalysis {
                level: LogLevel::Error,
                kind: EventKind::Java,
            };
        }

        if self.jvm_burst_re.is_match(normalized) {
            return LineAnalysis {
                level: LogLevel::Error,
                kind: EventKind::JvmBurst,
            };
        }

        if self.native_header_re.is_match(normalized) {
            return LineAnalysis {
                level: native_level(normalized).unwrap_or(LogLevel::Warn),
                kind: EventKind::Native,
            };
        }

        LineAnalysis {
            level: match stream {
                LogStream::Stdout => LogLevel::Info,
                LogStream::Stderr => LogLevel::Error,
            },
            kind: EventKind::Plain,
        }
    }

    fn header_level(&self, normalized: &str) -> Option<LogLevel> {
        self.minecraft_header_re
            .captures(normalized)
            .and_then(|caps| caps.name("level"))
            .or_else(|| {
                self.plain_level_re
                    .captures(normalized)
                    .and_then(|caps| caps.name("level"))
            })
            .and_then(|m| parse_level(m.as_str()))
    }
}

#[derive(Debug, Clone, Copy)]
struct LineAnalysis {
    level: LogLevel,
    kind: EventKind,
}

impl LineAnalysis {
    fn level_priority(self) -> u8 {
        level_priority(self.level)
    }
}

impl PendingEvent {
    fn new(stream: LogStream, line: String, normalized: String, analysis: LineAnalysis) -> Self {
        Self {
            level: analysis.level,
            primary_stream: stream,
            lines: vec![line],
            normalized_lines: vec![normalized],
            kind: analysis.kind,
        }
    }

    fn level_priority(&self) -> u8 {
        level_priority(self.level)
    }
}

fn should_split(current: EventKind, next: EventKind) -> bool {
    match next {
        EventKind::Header => true,
        EventKind::Native => !matches!(current, EventKind::Native),
        EventKind::JvmBurst => !matches!(current, EventKind::JvmBurst),
        EventKind::Java => !matches!(current, EventKind::Java | EventKind::Header),
        EventKind::Plain => false,
    }
}

fn finish_event(mut event: PendingEvent) -> ParsedLogEvent {
    let normalized_body = event.normalized_lines.join("\n");
    // libcasr is intentionally second-stage here: first frame the multiline
    // event ourselves, then ask libcasr whether the block is a java failure.
    let java = parse_java(&normalized_body);

    if java
        .as_ref()
        .is_some_and(|java| java.has_exception || java.has_stacktrace)
    {
        event.level = LogLevel::Error;
    }

    ParsedLogEvent {
        level: event.level,
        primary_stream: event.primary_stream,
        lines: event.lines,
        java,
    }
}

fn parse_java(body: &str) -> Option<JavaParse> {
    let stacktrace_entries = JavaStacktrace::extract_stacktrace(body).unwrap_or_default();
    let parsed_stacktrace = if stacktrace_entries.is_empty() {
        None
    } else {
        JavaStacktrace::parse_stacktrace(&stacktrace_entries).ok()
    };
    let has_exception = JavaException::parse_exception(body).is_some();
    let has_stacktrace = parsed_stacktrace
        .as_ref()
        .is_some_and(|stacktrace| !stacktrace.is_empty());

    if has_exception || has_stacktrace {
        Some(JavaParse {
            has_exception,
            has_stacktrace,
            stacktrace_frames: parsed_stacktrace.map_or(0, |stacktrace| stacktrace.len()),
        })
    } else {
        None
    }
}

fn parse_level(level: &str) -> Option<LogLevel> {
    match level.to_ascii_uppercase().as_str() {
        "TRACE" => Some(LogLevel::Trace),
        "DEBUG" => Some(LogLevel::Debug),
        "INFO" => Some(LogLevel::Info),
        "WARN" => Some(LogLevel::Warn),
        "ERROR" | "FATAL" => Some(LogLevel::Error),
        _ => None,
    }
}

fn native_level(line: &str) -> Option<LogLevel> {
    match line.chars().nth(1)? {
        'E' | 'F' => Some(LogLevel::Error),
        'W' => Some(LogLevel::Warn),
        'I' => Some(LogLevel::Info),
        'D' => Some(LogLevel::Debug),
        _ => None,
    }
}

fn level_priority(level: LogLevel) -> u8 {
    match level {
        LogLevel::Trace => 0,
        LogLevel::Debug => 1,
        LogLevel::Info => 2,
        LogLevel::Warn => 3,
        LogLevel::Error => 4,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn parse_all(lines: &[(LogStream, &str)]) -> Vec<ParsedLogEvent> {
        let mut parser = MinecraftLogParser::new();
        let mut events = Vec::new();
        for (stream, line) in lines {
            events.extend(parser.push_line(*stream, *line));
        }
        events.extend(parser.flush());
        events
    }

    #[test]
    fn classifies_minecraft_headers() {
        let events = parse_all(&[
            (LogStream::Stdout, "[Render thread/INFO]: hello"),
            (LogStream::Stdout, "[Render thread/WARN]: careful"),
            (LogStream::Stdout, "[Render thread/ERROR]: broken"),
            (LogStream::Stdout, "[Render thread/DEBUG]: noisy"),
            (LogStream::Stdout, "[Render thread/TRACE]: tiny"),
        ]);

        assert_eq!(events.len(), 5);
        assert_eq!(events[0].level, LogLevel::Info);
        assert_eq!(events[1].level, LogLevel::Warn);
        assert_eq!(events[2].level, LogLevel::Error);
        assert_eq!(events[3].level, LogLevel::Debug);
        assert_eq!(events[4].level, LogLevel::Trace);
    }

    #[test]
    fn explicit_stderr_info_stays_info() {
        let events = parse_all(&[(LogStream::Stderr, "[Render thread/INFO]: hello")]);

        assert_eq!(events[0].level, LogLevel::Info);
    }

    #[test]
    fn unstructured_stderr_falls_back_to_error() {
        let events = parse_all(&[(LogStream::Stderr, "native library failed")]);

        assert_eq!(events[0].level, LogLevel::Error);
    }

    #[test]
    fn groups_java_stacktrace() {
        let events = parse_all(&[
            (
                LogStream::Stderr,
                "Exception in thread \"main\" java.lang.RuntimeException: boom",
            ),
            (
                LogStream::Stderr,
                "\tat net.minecraft.client.Main.main(Main.java:42)",
            ),
            (
                LogStream::Stderr,
                "Caused by: java.lang.IllegalStateException: bad",
            ),
            (LogStream::Stderr, "\tat example.Mod.load(Mod.java:7)"),
            (LogStream::Stdout, "[Render thread/INFO]: after"),
        ]);

        assert_eq!(events.len(), 2);
        assert_eq!(events[0].level, LogLevel::Error);
        assert_eq!(events[0].lines.len(), 4);
        assert!(
            events[0]
                .java
                .as_ref()
                .is_some_and(|java| java.has_stacktrace)
        );
        assert_eq!(events[1].level, LogLevel::Info);
    }

    #[test]
    fn groups_jvm_startup_failure_burst() {
        let events = parse_all(&[
            (LogStream::Stderr, "Unrecognized option: --bad"),
            (
                LogStream::Stderr,
                "Could not create the Java Virtual Machine.",
            ),
            (
                LogStream::Stderr,
                "A fatal exception has occurred. Program will exit.",
            ),
        ]);

        assert_eq!(events.len(), 1);
        assert_eq!(events[0].level, LogLevel::Error);
        assert_eq!(events[0].lines.len(), 3);
    }

    #[test]
    fn colored_header_classifies_but_keeps_original_text() {
        let line = "\u{1b}[31m[Render thread/ERROR]: red\u{1b}[0m";
        let events = parse_all(&[(LogStream::Stdout, line)]);

        assert_eq!(events[0].level, LogLevel::Error);
        assert_eq!(events[0].lines[0], line);
    }
}