Skip to content

Commit e5cdafb

Browse files
committed
feat(unity): handle initial compile errors and first message detection
Add handling for initial compile errors before connection to Unity is established. Track first message received to trigger initial compile error request. This ensures we capture all compile errors even before full connection is established.
1 parent d0ec759 commit e5cdafb

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

src/unity_manager.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,26 @@ impl UnityManager {
258258
}
259259
});
260260
}
261+
UnityEvent::CompileErrors(log_container) => {
262+
// Handle initial compile errors from Unity
263+
// If we never received compile finish events
264+
// then this is the initial compile errors before we connect to Unity
265+
if let Ok(mut last_compilation_finished_guard) = last_compilation_finished.lock() {
266+
if last_compilation_finished_guard.is_none() {
267+
if let Ok(mut compile_errors_guard) = last_compile_errors.lock() {
268+
compile_errors_guard.clear();
269+
for log in &log_container.logs {
270+
let main_message = extract_main_message(&log.message);
271+
compile_errors_guard.push(main_message);
272+
}
273+
debug_log!("Initial compile errors received from Unity: {} errors", compile_errors_guard.len());
274+
}
275+
276+
// compile didn't happen just now, but we can use now as a fake compile time, it's ok
277+
*last_compilation_finished_guard = Some(SystemTime::now());
278+
}
279+
}
280+
}
261281
_ => {}
262282
}
263283
}
@@ -299,15 +319,6 @@ impl UnityManager {
299319
}
300320
}
301321

302-
/// Get the timestamp of the last compilation finished event
303-
pub fn get_last_compilation_finished(&self) -> Option<SystemTime> {
304-
if let Ok(compilation_guard) = self.last_compilation_finished.lock() {
305-
*compilation_guard
306-
} else {
307-
None
308-
}
309-
}
310-
311322
/// Get the compile errors from the last compilation
312323
pub fn get_last_compile_errors(&self) -> Vec<String> {
313324
if let Ok(compile_errors_guard) = self.last_compile_errors.lock() {

src/unity_messaging_client.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub struct UnityMessagingClient {
3131
last_compilation_finished: Arc<Mutex<Option<SystemTime>>>,
3232
/// Compile errors collected after compilation finishes
3333
last_compile_errors: Arc<Mutex<Vec<String>>>,
34+
/// Whether we have received the first message from Unity (used to trigger initial GetCompileErrors)
35+
first_message_received: Arc<Mutex<bool>>,
3436
}
3537

3638
impl UnityMessagingClient {
@@ -66,6 +68,7 @@ impl UnityMessagingClient {
6668
is_in_play_mode: Arc::new(Mutex::new(false)),
6769
last_compilation_finished: Arc::new(Mutex::new(None)),
6870
last_compile_errors: Arc::new(Mutex::new(Vec::new())),
71+
first_message_received: Arc::new(Mutex::new(false)),
6972
})
7073
}
7174

@@ -91,8 +94,7 @@ impl UnityMessagingClient {
9194
let is_online = self.is_online.clone();
9295
let current_test_run_id = self.current_test_run_id.clone();
9396
let is_in_play_mode = self.is_in_play_mode.clone();
94-
let last_compilation_finished = self.last_compilation_finished.clone();
95-
let last_compile_errors = self.last_compile_errors.clone();
97+
let first_message_received = self.first_message_received.clone();
9698

9799
// Spawn background task for message listening
98100
let task = tokio::spawn(async move {
@@ -103,7 +105,8 @@ impl UnityMessagingClient {
103105
last_response_time,
104106
is_online,
105107
current_test_run_id,
106-
is_in_play_mode
108+
is_in_play_mode,
109+
first_message_received
107110
)
108111
.await;
109112
});
@@ -163,6 +166,7 @@ impl UnityMessagingClient {
163166
is_online: Arc<Mutex<bool>>,
164167
current_test_run_id: Arc<Mutex<Option<String>>>,
165168
is_in_play_mode: Arc<Mutex<bool>>,
169+
first_message_received: Arc<Mutex<bool>>,
166170
) {
167171
let mut buffer = [0u8; 8192];
168172
let mut ping_interval = tokio::time::interval(Duration::from_secs(1));
@@ -196,6 +200,24 @@ impl UnityMessagingClient {
196200
info_log!("Unity {:?}: {}", message.message_type, message.value);
197201
}
198202

203+
let mut just_received_first_message = false;
204+
// Check if this is the first message from Unity (excluding Ping/Pong)
205+
if let Ok(mut first_received) = first_message_received.lock() {
206+
if !*first_received {
207+
*first_received = true;
208+
debug_log!("First message received from Unity, requesting initial compile errors");
209+
just_received_first_message = true;
210+
}
211+
}
212+
213+
if just_received_first_message {
214+
// Send GetCompileErrors request to get initial compile errors
215+
let get_compile_errors_message = Message::new(MessageType::GetCompileErrors, String::new());
216+
if let Err(e) = socket.send_to(&get_compile_errors_message.serialize(), unity_address).await {
217+
error_log!("Failed to send initial GetCompileErrors request: {}", e);
218+
}
219+
}
220+
199221
// Update last response time for any valid message
200222
if let Ok(mut time) = last_response_time.lock() {
201223
*time = Some(std::time::Instant::now());

0 commit comments

Comments
 (0)