You've already forked AstralRinth
forked from didirus/AstralRinth
Direct World Joining (#3457)
* Begin work on worlds backend * Finish implementing get_profile_worlds and get_server_status (except pinning) * Create TS types and manually copy unparsed chat components * Clippy fix * Update types.d.ts * Initial worlds UI work * Fix api::get_profile_worlds to take in a relative path * sanitize & security update * Fix sanitizePotentialFileUrl * Fix sanitizePotentialFileUrl (for real) * Fix empty motd causing error * Finally actually fix world icons * Fix world icon not being visible on non-Windows * Use the correct generics to take in AppHandle * Implement start_join_singleplayer_world and start_join_server for modern versions * Don't error if server has no cached icon * Migrate to own server pinging * Ignore missing server hidden field and missing saves dir * Update world list frontend * More frontend work * Server status player sample can be absent * Fix refresh state * Add get_profile_protocol_version * Add protocol_version column to database * SQL INTEGER is i64 in sqlx * sqlx prepare * Cache protocol version in database * Continue worlds UI work * Fix motds being bold * Remove legacy pinging and add a 30-second timeout * Remove pinned for now and match world (and server) parsing closer to spec * Move type ServerStatus to worlds.ts * Implement add_server_to_profile * Fix pack_status being ignored when joining from launcher * Make World path field be relative * Implement rename_world and reset_world_icon * Clippy fix * Fix rename_world * UI enhancements * Implement backup_world, which returns the backup size in bytes * Clippy fix * Return index when adding servers to profile * Fix backup * Implement delete_world * Implement edit_server_in_profile and remove_server_from_profile * Clippy fix * Log server joins * Add edit and delete support * Fix ts errors * Fix minecraft font * Switch font out for non-monospaced. * Fix font proper * Some more world cleanup, handle play state, check quickplay compatibility * Clear the cached protocol version when a profile's game version is changed * Fix tint colors in navbar * Fix server protocol version pinging * UI fixes * Fix protocol version handler * Fix MOTD parsing * Add worlds_updated profile event * fix pkg * Functional home screen with worlds * lint * Fix incorrect folder creation * Make items clickable * Add locked field to SingleplayerWorld indicating whether the world is locked by the game * Implement locking frontend * Fix locking condition * Split worlds_updated profile event into servers_updated and world_updated * Fix compile error * Use port from resolve SRV record * Fix serialization of ProfilePayload and ProfilePayloadType * Individual singleplayer world refreshing * Log when worlds are perceived to be updated * Push logging + total refresh lock * Unlisten fixes * Highlight current world when clicked * Launcher logs refactor (#3444) * Switch live log to use STDOUT * fix clippy, legacy logs support * Fix lint * Handle non-XML log messages in XML logging, and don't escape log messages into XML --------- Co-authored-by: Josiah Glosson <soujournme@gmail.com> * Update incompatibility text * Home page fixes, and unlock after close * Remove logging * Add join log database migration * Switch server join timing to being in the database instead of in a separate log file * Create optimized get_recent_worlds function that takes in a limit * Update dependencies and fix Cargo.lock * temp disable overflow menus * revert home page changes * Enable overflow menus again * Remove list * Revert * Push dev tools * Remove default filter * Disable debug renderer * Fix random app errors * Refactor * Fix missing computed import * Fix light mode issues * Fix TS errors * Lint * Fix bad link in change modpack version modal * fix lint * fix intl --------- Co-authored-by: Josiah Glosson <soujournme@gmail.com> Co-authored-by: Jai A <jaiagr+gpg@pm.me> Co-authored-by: Jai Agrawal <18202329+Geometrically@users.noreply.github.com>
This commit is contained in:
@@ -1,15 +1,23 @@
|
||||
use crate::event::emit::emit_process;
|
||||
use crate::event::ProcessPayloadType;
|
||||
use crate::event::emit::{emit_process, emit_profile};
|
||||
use crate::event::{ProcessPayloadType, ProfilePayloadType};
|
||||
use crate::profile;
|
||||
use crate::util::io::IOError;
|
||||
use chrono::{DateTime, Utc};
|
||||
use chrono::{DateTime, TimeZone, Utc};
|
||||
use dashmap::DashMap;
|
||||
use quick_xml::events::Event;
|
||||
use quick_xml::Reader;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::ExitStatus;
|
||||
use tokio::io::{AsyncBufReadExt, BufReader};
|
||||
use tokio::process::{Child, Command};
|
||||
use uuid::Uuid;
|
||||
|
||||
const LAUNCHER_LOG_PATH: &str = "launcher_log.txt";
|
||||
|
||||
pub struct ProcessManager {
|
||||
processes: DashMap<Uuid, Process>,
|
||||
}
|
||||
@@ -32,8 +40,16 @@ impl ProcessManager {
|
||||
profile_path: &str,
|
||||
mut mc_command: Command,
|
||||
post_exit_command: Option<String>,
|
||||
logs_folder: PathBuf,
|
||||
xml_logging: bool,
|
||||
) -> crate::Result<ProcessMetadata> {
|
||||
let mc_proc = mc_command.spawn().map_err(IOError::from)?;
|
||||
mc_command.stdout(std::process::Stdio::piped());
|
||||
mc_command.stderr(std::process::Stdio::piped());
|
||||
|
||||
let mut mc_proc = mc_command.spawn().map_err(IOError::from)?;
|
||||
|
||||
let stdout = mc_proc.stdout.take();
|
||||
let stderr = mc_proc.stderr.take();
|
||||
|
||||
let process = Process {
|
||||
metadata: ProcessMetadata {
|
||||
@@ -46,6 +62,65 @@ impl ProcessManager {
|
||||
|
||||
let metadata = process.metadata.clone();
|
||||
|
||||
if !logs_folder.exists() {
|
||||
tokio::fs::create_dir_all(&logs_folder)
|
||||
.await
|
||||
.map_err(|e| IOError::with_path(e, &logs_folder))?;
|
||||
}
|
||||
|
||||
let log_path = logs_folder.join(LAUNCHER_LOG_PATH);
|
||||
|
||||
{
|
||||
let mut log_file = OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.open(&log_path)
|
||||
.map_err(|e| IOError::with_path(e, &log_path))?;
|
||||
|
||||
// Initialize with timestamp header
|
||||
let now = chrono::Local::now();
|
||||
writeln!(
|
||||
log_file,
|
||||
"# Minecraft launcher log started at {}",
|
||||
now.format("%Y-%m-%d %H:%M:%S")
|
||||
)
|
||||
.map_err(|e| IOError::with_path(e, &log_path))?;
|
||||
writeln!(log_file, "# Profile: {} \n", profile_path)
|
||||
.map_err(|e| IOError::with_path(e, &log_path))?;
|
||||
writeln!(log_file).map_err(|e| IOError::with_path(e, &log_path))?;
|
||||
}
|
||||
|
||||
if let Some(stdout) = stdout {
|
||||
let log_path_clone = log_path.clone();
|
||||
|
||||
let profile_path = metadata.profile_path.clone();
|
||||
tokio::spawn(async move {
|
||||
Process::process_output(
|
||||
&profile_path,
|
||||
stdout,
|
||||
log_path_clone,
|
||||
xml_logging,
|
||||
)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
|
||||
if let Some(stderr) = stderr {
|
||||
let log_path_clone = log_path.clone();
|
||||
|
||||
let profile_path = metadata.profile_path.clone();
|
||||
tokio::spawn(async move {
|
||||
Process::process_output(
|
||||
&profile_path,
|
||||
stderr,
|
||||
log_path_clone,
|
||||
xml_logging,
|
||||
)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
|
||||
tokio::spawn(Process::sequential_process_manager(
|
||||
profile_path.to_string(),
|
||||
post_exit_command,
|
||||
@@ -120,7 +195,381 @@ struct Process {
|
||||
child: Child,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct Log4jEvent {
|
||||
timestamp: Option<String>,
|
||||
logger: Option<String>,
|
||||
level: Option<String>,
|
||||
thread: Option<String>,
|
||||
message: Option<String>,
|
||||
}
|
||||
|
||||
impl Process {
|
||||
async fn process_output<R>(
|
||||
profile_path: &str,
|
||||
reader: R,
|
||||
log_path: impl AsRef<Path>,
|
||||
xml_logging: bool,
|
||||
) where
|
||||
R: tokio::io::AsyncRead + Unpin,
|
||||
{
|
||||
let mut buf_reader = BufReader::new(reader);
|
||||
|
||||
if xml_logging {
|
||||
let mut reader = Reader::from_reader(buf_reader);
|
||||
reader.config_mut().enable_all_checks(false);
|
||||
|
||||
let mut buf = Vec::new();
|
||||
let mut current_event = Log4jEvent::default();
|
||||
let mut in_event = false;
|
||||
let mut in_message = false;
|
||||
let mut in_throwable = false;
|
||||
let mut current_content = String::new();
|
||||
|
||||
loop {
|
||||
match reader.read_event_into_async(&mut buf).await {
|
||||
Err(e) => {
|
||||
tracing::error!(
|
||||
"Error at position {}: {:?}",
|
||||
reader.buffer_position(),
|
||||
e
|
||||
);
|
||||
break;
|
||||
}
|
||||
// exits the loop when reaching end of file
|
||||
Ok(Event::Eof) => break,
|
||||
|
||||
Ok(Event::Start(e)) => {
|
||||
match e.name().as_ref() {
|
||||
b"log4j:Event" => {
|
||||
// Reset for new event
|
||||
current_event = Log4jEvent::default();
|
||||
in_event = true;
|
||||
|
||||
// Extract attributes
|
||||
for attr in e.attributes().flatten() {
|
||||
let key = String::from_utf8_lossy(
|
||||
attr.key.into_inner(),
|
||||
)
|
||||
.to_string();
|
||||
let value =
|
||||
String::from_utf8_lossy(&attr.value)
|
||||
.to_string();
|
||||
|
||||
match key.as_str() {
|
||||
"logger" => {
|
||||
current_event.logger = Some(value)
|
||||
}
|
||||
"level" => {
|
||||
current_event.level = Some(value)
|
||||
}
|
||||
"thread" => {
|
||||
current_event.thread = Some(value)
|
||||
}
|
||||
"timestamp" => {
|
||||
current_event.timestamp =
|
||||
Some(value)
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
b"log4j:Message" => {
|
||||
in_message = true;
|
||||
current_content = String::new();
|
||||
}
|
||||
b"log4j:Throwable" => {
|
||||
in_throwable = true;
|
||||
current_content = String::new();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(Event::End(e)) => {
|
||||
match e.name().as_ref() {
|
||||
b"log4j:Message" => {
|
||||
in_message = false;
|
||||
current_event.message =
|
||||
Some(current_content.clone());
|
||||
}
|
||||
b"log4j:Throwable" => {
|
||||
in_throwable = false;
|
||||
// Process and write the log entry
|
||||
let thread = current_event
|
||||
.thread
|
||||
.as_deref()
|
||||
.unwrap_or("");
|
||||
let level = current_event
|
||||
.level
|
||||
.as_deref()
|
||||
.unwrap_or("");
|
||||
let logger = current_event
|
||||
.logger
|
||||
.as_deref()
|
||||
.unwrap_or("");
|
||||
|
||||
if let Some(message) = ¤t_event.message {
|
||||
let formatted_time =
|
||||
Process::format_timestamp(
|
||||
current_event.timestamp.as_deref(),
|
||||
);
|
||||
let formatted_log = format!(
|
||||
"{} [{}] [{}{}]: {}\n",
|
||||
formatted_time,
|
||||
thread,
|
||||
if !logger.is_empty() {
|
||||
format!("{}/", logger)
|
||||
} else {
|
||||
String::new()
|
||||
},
|
||||
level,
|
||||
message.trim()
|
||||
);
|
||||
|
||||
// Write the log message
|
||||
if let Err(e) = Process::append_to_log_file(
|
||||
&log_path,
|
||||
&formatted_log,
|
||||
) {
|
||||
tracing::error!(
|
||||
"Failed to write to log file: {}",
|
||||
e
|
||||
);
|
||||
}
|
||||
|
||||
// Write the throwable if present
|
||||
if !current_content.is_empty() {
|
||||
if let Err(e) =
|
||||
Process::append_to_log_file(
|
||||
&log_path,
|
||||
¤t_content,
|
||||
)
|
||||
{
|
||||
tracing::error!("Failed to write throwable to log file: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
b"log4j:Event" => {
|
||||
in_event = false;
|
||||
// If no throwable was present, write the log entry at the end of the event
|
||||
if current_event.message.is_some()
|
||||
&& !in_throwable
|
||||
{
|
||||
let thread = current_event
|
||||
.thread
|
||||
.as_deref()
|
||||
.unwrap_or("");
|
||||
let level = current_event
|
||||
.level
|
||||
.as_deref()
|
||||
.unwrap_or("");
|
||||
let logger = current_event
|
||||
.logger
|
||||
.as_deref()
|
||||
.unwrap_or("");
|
||||
let message = current_event
|
||||
.message
|
||||
.as_deref()
|
||||
.unwrap_or("")
|
||||
.trim();
|
||||
|
||||
let formatted_time =
|
||||
Process::format_timestamp(
|
||||
current_event.timestamp.as_deref(),
|
||||
);
|
||||
let formatted_log = format!(
|
||||
"{} [{}] [{}{}]: {}\n",
|
||||
formatted_time,
|
||||
thread,
|
||||
if !logger.is_empty() {
|
||||
format!("{}/", logger)
|
||||
} else {
|
||||
String::new()
|
||||
},
|
||||
level,
|
||||
message
|
||||
);
|
||||
|
||||
// Write the log message
|
||||
if let Err(e) = Process::append_to_log_file(
|
||||
&log_path,
|
||||
&formatted_log,
|
||||
) {
|
||||
tracing::error!(
|
||||
"Failed to write to log file: {}",
|
||||
e
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(timestamp) =
|
||||
current_event.timestamp.as_deref()
|
||||
{
|
||||
if let Err(e) = Self::maybe_handle_server_join_logging(
|
||||
profile_path,
|
||||
timestamp,
|
||||
message
|
||||
).await {
|
||||
tracing::error!("Failed to handle server join logging: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(Event::Text(mut e)) => {
|
||||
if in_message || in_throwable {
|
||||
if let Ok(text) = e.unescape() {
|
||||
current_content.push_str(&text);
|
||||
}
|
||||
} else if !in_event
|
||||
&& !e.inplace_trim_end()
|
||||
&& !e.inplace_trim_start()
|
||||
{
|
||||
if let Ok(text) = e.unescape() {
|
||||
if let Err(e) = Process::append_to_log_file(
|
||||
&log_path,
|
||||
&format!("{text}\n"),
|
||||
) {
|
||||
tracing::error!(
|
||||
"Failed to write to log file: {}",
|
||||
e
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Event::CData(e)) => {
|
||||
if in_message || in_throwable {
|
||||
if let Ok(text) = e
|
||||
.escape()
|
||||
.map_err(|x| x.into())
|
||||
.and_then(|x| x.unescape())
|
||||
{
|
||||
current_content.push_str(&text);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
||||
buf.clear();
|
||||
}
|
||||
} else {
|
||||
let mut line = String::new();
|
||||
|
||||
while let Ok(bytes_read) = buf_reader.read_line(&mut line).await {
|
||||
if bytes_read == 0 {
|
||||
break; // End of stream
|
||||
}
|
||||
|
||||
if !line.is_empty() {
|
||||
if let Err(e) = Self::append_to_log_file(&log_path, &line) {
|
||||
tracing::warn!("Failed to write to log file: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
line.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn format_timestamp(timestamp: Option<&str>) -> String {
|
||||
if let Some(timestamp_str) = timestamp {
|
||||
if let Ok(timestamp_val) = timestamp_str.parse::<i64>() {
|
||||
let datetime_utc = if timestamp_val > i32::MAX as i64 {
|
||||
let secs = timestamp_val / 1000;
|
||||
let nsecs = ((timestamp_val % 1000) * 1_000_000) as u32;
|
||||
|
||||
chrono::DateTime::<Utc>::from_timestamp(secs, nsecs)
|
||||
.unwrap_or_default()
|
||||
} else {
|
||||
chrono::DateTime::<Utc>::from_timestamp(timestamp_val, 0)
|
||||
.unwrap_or_default()
|
||||
};
|
||||
|
||||
let datetime_local = datetime_utc.with_timezone(&chrono::Local);
|
||||
format!("[{}]", datetime_local.format("%H:%M:%S"))
|
||||
} else {
|
||||
"[??:??:??]".to_string()
|
||||
}
|
||||
} else {
|
||||
"[??:??:??]".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
fn append_to_log_file(
|
||||
path: impl AsRef<Path>,
|
||||
line: &str,
|
||||
) -> std::io::Result<()> {
|
||||
let mut file =
|
||||
OpenOptions::new().append(true).create(true).open(path)?;
|
||||
|
||||
file.write_all(line.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn maybe_handle_server_join_logging(
|
||||
profile_path: &str,
|
||||
timestamp: &str,
|
||||
message: &str,
|
||||
) -> crate::Result<()> {
|
||||
let Some(host_port_string) = message.strip_prefix("Connecting to ")
|
||||
else {
|
||||
return Ok(());
|
||||
};
|
||||
let Some((host, port_string)) = host_port_string.rsplit_once(", ")
|
||||
else {
|
||||
return Ok(());
|
||||
};
|
||||
let Some(port) = port_string.parse::<u16>().ok() else {
|
||||
return Ok(());
|
||||
};
|
||||
let timestamp = timestamp
|
||||
.parse::<i64>()
|
||||
.map(|x| x / 1000)
|
||||
.map_err(|x| {
|
||||
crate::ErrorKind::OtherError(format!(
|
||||
"Failed to parse timestamp: {x}"
|
||||
))
|
||||
})
|
||||
.and_then(|x| {
|
||||
Utc.timestamp_opt(x, 0).single().ok_or_else(|| {
|
||||
crate::ErrorKind::OtherError(
|
||||
"Failed to convert timestamp to DateTime".to_string(),
|
||||
)
|
||||
})
|
||||
})?;
|
||||
|
||||
let state = crate::State::get().await?;
|
||||
crate::state::server_join_log::JoinLogEntry {
|
||||
profile_path: profile_path.to_owned(),
|
||||
host: host.to_string(),
|
||||
port,
|
||||
join_time: timestamp,
|
||||
}
|
||||
.upsert(&state.pool)
|
||||
.await?;
|
||||
{
|
||||
let profile_path = profile_path.to_owned();
|
||||
let host = host.to_owned();
|
||||
tokio::spawn(async move {
|
||||
let _ = emit_profile(
|
||||
&profile_path,
|
||||
ProfilePayloadType::ServerJoined {
|
||||
host,
|
||||
port,
|
||||
timestamp,
|
||||
},
|
||||
)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Spawns a new child process and inserts it into the hashmap
|
||||
// Also, as the process ends, it spawns the follow-up process if it exists
|
||||
// By convention, ExitStatus is last command's exit status, and we exit on the first non-zero exit status
|
||||
@@ -204,6 +653,24 @@ impl Process {
|
||||
}
|
||||
});
|
||||
|
||||
let logs_folder = state.directories.profile_logs_dir(&profile_path);
|
||||
let log_path = logs_folder.join(LAUNCHER_LOG_PATH);
|
||||
|
||||
if log_path.exists() {
|
||||
if let Err(e) = Process::append_to_log_file(
|
||||
&log_path,
|
||||
&format!(
|
||||
"\n# Process exited with status: {}\n",
|
||||
mc_exit_status
|
||||
),
|
||||
) {
|
||||
tracing::warn!(
|
||||
"Failed to write exit status to log file: {}",
|
||||
e
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let _ = state.discord_rpc.clear_to_default(true).await;
|
||||
|
||||
let _ = state.friends_socket.update_status(None).await;
|
||||
|
||||
Reference in New Issue
Block a user