-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.rs
More file actions
340 lines (301 loc) · 11.7 KB
/
main.rs
File metadata and controls
340 lines (301 loc) · 11.7 KB
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
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
use tauri::Manager;
use tracing_appender::{non_blocking::WorkerGuard, rolling};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
mod commands;
mod error;
mod legacy_patcher;
#[cfg(debug_assertions)]
mod log_layer;
mod mods;
mod overlay;
pub mod patcher;
mod state;
mod utils;
mod workshop;
use mods::{ModLibrary, ModLibraryState};
use patcher::PatcherState;
use state::SettingsState;
use workshop::{Workshop, WorkshopState};
/// Perform first-run initialization:
/// - If league_path is not set, attempt auto-detection
/// - If auto-detection succeeds, save the path
fn initialize_first_run(app_handle: &tauri::AppHandle, settings_state: &SettingsState) {
let mut settings = match settings_state.0.lock() {
Ok(s) => s,
Err(e) => {
tracing::error!("Failed to lock settings: {}", e);
return;
}
};
// Skip if league path is already configured
if settings.league_path.is_some() {
tracing::info!("League path already configured, skipping auto-detection");
return;
}
tracing::info!("Attempting auto-detection of League installation...");
// Use ltk_mod_core for detection
if let Some(exe_path) = ltk_mod_core::auto_detect_league_path() {
let path = std::path::Path::new(exe_path.as_str());
// Navigate from "Game/League of Legends.exe" to installation root
if let Some(install_root) = path.parent().and_then(|p| p.parent()) {
tracing::info!("Auto-detected League at: {:?}", install_root);
settings.league_path = Some(install_root.to_path_buf());
settings.first_run_complete = true;
// Persist the detected path
if let Err(e) = state::save_settings_to_disk(app_handle, &settings) {
tracing::error!("Failed to save auto-detected settings: {}", e);
}
}
} else {
tracing::info!("Auto-detection did not find League installation");
}
}
fn main() {
// Initialize logging (stdout + persistent file).
//
// If you need more/less verbosity at runtime, set `RUST_LOG`, e.g.:
// - `RUST_LOG=ltk_manager=trace,tauri=info`
// - `RUST_LOG=ltk_manager=debug,tauri=warn`
#[cfg(debug_assertions)]
let (_file_guard, log_path, app_handle_holder) = init_logging();
#[cfg(not(debug_assertions))]
let (_file_guard, log_path) = init_logging();
tracing::info!("Starting LTK Manager v{}", env!("CARGO_PKG_VERSION"));
if let Some(ref p) = log_path {
tracing::info!("Log directory: {}", p.display());
cleanup_old_logs(p, 7);
}
tauri::Builder::default()
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_updater::Builder::new().build())
.plugin(tauri_plugin_process::init())
.setup(move |app| {
let app_handle = app.handle();
// Populate the app handle for the log layer in debug builds
#[cfg(debug_assertions)]
let _ = app_handle_holder.set(app_handle.clone());
// Create individual states
let settings_state = SettingsState::new(app_handle);
let patcher_state = PatcherState::new();
let mod_library = ModLibraryState(ModLibrary::new(app_handle));
let workshop = WorkshopState(Workshop::new(app_handle));
// Run first-run initialization (auto-detect League path)
initialize_first_run(app_handle, &settings_state);
// Manage each state separately
app.manage(settings_state);
app.manage(patcher_state);
app.manage(mod_library);
app.manage(workshop);
Ok(())
})
.invoke_handler(tauri::generate_handler![
// App
commands::get_app_info,
// Settings
commands::get_settings,
commands::save_settings,
commands::auto_detect_league_path,
commands::validate_league_path,
commands::check_setup_required,
// Mods
commands::get_installed_mods,
commands::install_mod,
commands::install_mods,
commands::uninstall_mod,
commands::toggle_mod,
commands::inspect_modpkg,
commands::get_mod_thumbnail,
commands::get_storage_directory,
commands::reorder_mods,
// Migration
commands::scan_cslol_mods,
commands::import_cslol_mods,
// Patcher
commands::start_patcher,
commands::stop_patcher,
commands::get_patcher_status,
// Profiles
commands::list_mod_profiles,
commands::get_active_mod_profile,
commands::create_mod_profile,
commands::delete_mod_profile,
commands::switch_mod_profile,
commands::rename_mod_profile,
// Shell
commands::reveal_in_explorer,
// Workshop
commands::get_workshop_projects,
commands::create_workshop_project,
commands::get_workshop_project,
commands::save_project_config,
commands::rename_workshop_project,
commands::delete_workshop_project,
commands::pack_workshop_project,
commands::import_from_modpkg,
commands::peek_fantome,
commands::import_from_fantome,
commands::import_from_git_repo,
commands::validate_project,
commands::set_project_thumbnail,
commands::get_project_thumbnail,
commands::save_layer_string_overrides,
commands::create_project_layer,
commands::delete_project_layer,
commands::reorder_project_layers,
commands::update_layer_description,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
#[cfg(debug_assertions)]
fn init_logging() -> (
Option<WorkerGuard>,
Option<std::path::PathBuf>,
std::sync::Arc<std::sync::OnceLock<tauri::AppHandle>>,
) {
let env_filter = tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "ltk_manager=debug,ltk_overlay=info,tauri=info".into());
let stdout_layer = tracing_subscriber::fmt::layer();
let (file_guard, file_layer, log_path) = match default_log_dir() {
Some(log_dir) => {
if let Err(e) = std::fs::create_dir_all(&log_dir) {
eprintln!(
"Failed to create log directory {}: {}",
log_dir.display(),
e
);
(None, None, None)
} else {
let file_appender = rolling::RollingFileAppender::builder()
.rotation(rolling::Rotation::DAILY)
.filename_prefix("ltk-manager")
.filename_suffix("log")
.build(&log_dir)
.expect("failed to create log file appender");
let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
let layer = tracing_subscriber::fmt::layer()
.with_writer(non_blocking)
.with_ansi(false);
(Some(guard), Some(layer), Some(log_dir))
}
}
None => (None, None, None),
};
let app_handle_holder = std::sync::Arc::new(std::sync::OnceLock::new());
let tauri_log_layer = log_layer::TauriLogLayer::new(app_handle_holder.clone());
let registry = tracing_subscriber::registry()
.with(env_filter)
.with(stdout_layer)
.with(tauri_log_layer);
if let Some(layer) = file_layer {
registry.with(layer).init();
} else {
registry.init();
}
(file_guard, log_path, app_handle_holder)
}
#[cfg(not(debug_assertions))]
fn init_logging() -> (Option<WorkerGuard>, Option<std::path::PathBuf>) {
let env_filter = tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "ltk_manager=debug,ltk_overlay=info,tauri=info".into());
let stdout_layer = tracing_subscriber::fmt::layer();
let (file_guard, file_layer, log_path) = match default_log_dir() {
Some(log_dir) => {
if let Err(e) = std::fs::create_dir_all(&log_dir) {
eprintln!(
"Failed to create log directory {}: {}",
log_dir.display(),
e
);
(None, None, None)
} else {
let file_appender = rolling::RollingFileAppender::builder()
.rotation(rolling::Rotation::DAILY)
.filename_prefix("ltk-manager")
.filename_suffix("log")
.build(&log_dir)
.expect("failed to create log file appender");
let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
let layer = tracing_subscriber::fmt::layer()
.with_writer(non_blocking)
.with_ansi(false);
(Some(guard), Some(layer), Some(log_dir))
}
}
None => (None, None, None),
};
let registry = tracing_subscriber::registry()
.with(env_filter)
.with(stdout_layer);
if let Some(layer) = file_layer {
registry.with(layer).init();
} else {
registry.init();
}
(file_guard, log_path)
}
/// Delete log files older than `max_age_days` from the log directory.
fn cleanup_old_logs(log_dir: &std::path::Path, max_age_days: u64) {
let max_age = std::time::Duration::from_secs(max_age_days * 24 * 60 * 60);
let entries = match std::fs::read_dir(log_dir) {
Ok(e) => e,
Err(e) => {
tracing::warn!("Failed to read log directory for cleanup: {}", e);
return;
}
};
for entry in entries.flatten() {
let path = entry.path();
let file_name = match path.file_name().and_then(|n| n.to_str()) {
Some(n) => n.to_string(),
None => continue,
};
// Only target dated log files (e.g. "ltk-manager.2026-02-17.log")
if !file_name.starts_with("ltk-manager.") || !file_name.ends_with(".log") {
continue;
}
let modified = match entry.metadata().and_then(|m| m.modified()) {
Ok(t) => t,
Err(_) => continue,
};
let age = match std::time::SystemTime::now().duration_since(modified) {
Ok(d) => d,
Err(_) => continue,
};
if age > max_age {
if let Err(e) = std::fs::remove_file(&path) {
tracing::warn!("Failed to delete old log file {}: {}", path.display(), e);
} else {
tracing::info!("Deleted old log file: {}", path.display());
}
}
}
}
pub(crate) fn default_log_dir() -> Option<std::path::PathBuf> {
// Match `tauri.conf.json` identifier so logs sit next to `settings.json` on Windows.
const IDENTIFIER: &str = "dev.leaguetoolkit.manager";
if let Ok(appdata) = std::env::var("APPDATA") {
return Some(
std::path::PathBuf::from(appdata)
.join(IDENTIFIER)
.join("logs"),
);
}
// Best-effort fallback for non-Windows environments.
if let Ok(home) = std::env::var("HOME") {
return Some(
std::path::PathBuf::from(home)
.join(".local")
.join("share")
.join(IDENTIFIER)
.join("logs"),
);
}
Some(std::env::temp_dir().join("ltk-manager"))
}