From c034f8a2f90c31f0c3f6bc5ec1ec1f8dd3992b57 Mon Sep 17 00:00:00 2001 From: amizing25 Date: Sun, 9 Mar 2025 05:10:33 +0700 Subject: [PATCH] fix: modify file listener to ensure the file is modified --- gameserver/src/net/gateway.rs | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/gameserver/src/net/gateway.rs b/gameserver/src/net/gateway.rs index 2955d25..54ea29a 100644 --- a/gameserver/src/net/gateway.rs +++ b/gameserver/src/net/gateway.rs @@ -6,7 +6,7 @@ use std::{ Arc, atomic::{AtomicU32, Ordering}, }, - time::Duration, + time::{Duration, SystemTime}, }; use anyhow::Result; @@ -113,6 +113,11 @@ impl Gateway { tracing::info!("watching freesr-data.json changes"); let mut shutdown_rx = session.read().await.shutdown_rx.clone(); + + let mut last_modified = std::fs::metadata(path) + .and_then(|meta| meta.modified()) + .unwrap_or(SystemTime::UNIX_EPOCH); + loop { tokio::select! { res = rx.recv() => { @@ -122,17 +127,25 @@ impl Gateway { match res { Ok(events) => { if events - .iter() - .any(|p| p.path.file_name() == path.file_name()) - { - let mut session = session.write().await; - if let Some(json) = session.json_data.get_mut() { - let _ = json.update().await; - session.sync_player().await; - tracing::info!("json updated") + .iter() + .any(|p| p.path.file_name() == path.file_name()) + { + if let Ok(metadata) = std::fs::metadata(path) { + if let Ok(modified) = metadata.modified() { + if modified > last_modified { + last_modified = modified; + + let mut session = session.write().await; + if let Some(json) = session.json_data.get_mut() { + let _ = json.update().await; + session.sync_player().await; + tracing::info!("json updated"); + } + } + } + } } } - } Err(e) => eprintln!("json watcher error: {:?}", e), } }