diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 638486a..9ececc0 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -5,6 +5,7 @@ use clipforge::capture::{ use clipforge::database::ClipDatabase; use clipforge::game_detection::{default_profiles, detect_game}; use clipforge::game_watcher::running_processes; +use clipforge::gsi_config::write_gsi_config_templates; use clipforge::integrations::{ valve_gsi_raw_events, GameIntegration, IntegrationError, LeagueIntegration, LeagueLiveClientPoller, RawGameEvent, ValveGsiIntegration, @@ -83,6 +84,12 @@ struct ClipDto { color_class: String, } +#[derive(Debug, Serialize)] +struct GsiConfigDto { + game_id: String, + path: String, +} + #[tauri::command] fn get_status(runtime: State<'_, AppRuntime>) -> Result { let recorder = runtime.recorder.lock().map_err(|error| error.to_string())?; @@ -138,6 +145,21 @@ fn set_mic_enabled(enabled: bool, runtime: State<'_, AppRuntime>) -> Result) -> Result, String> { + write_gsi_config_templates(&runtime.library_root) + .map_err(|error| format!("Could not write GSI configs: {error}")) + .map(|configs| { + configs + .into_iter() + .map(|config| GsiConfigDto { + game_id: config.game_id, + path: config.path.display().to_string(), + }) + .collect() + }) +} + #[tauri::command] fn poll_auto_clip_events(runtime: State<'_, AppRuntime>) -> Result, String> { let (session_id, game_id, session_started_at) = { @@ -664,6 +686,7 @@ fn main() { list_clips, set_replay_buffer, set_mic_enabled, + write_gsi_configs, poll_auto_clip_events, save_manual_clip, start_capture, diff --git a/src/gsi_config.rs b/src/gsi_config.rs new file mode 100644 index 0000000..eab7c77 --- /dev/null +++ b/src/gsi_config.rs @@ -0,0 +1,103 @@ +use std::fs; +use std::io; +use std::path::{Path, PathBuf}; + +pub const GSI_ENDPOINT: &str = "http://127.0.0.1:49321"; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct WrittenGsiConfig { + pub game_id: String, + pub path: PathBuf, +} + +pub fn write_gsi_config_templates(root: &Path) -> io::Result> { + let config_root = root.join("integrations").join("valve-gsi"); + fs::create_dir_all(&config_root)?; + let cs2_path = config_root.join("gamestate_integration_clipforge_cs2.cfg"); + let dota_path = config_root.join("gamestate_integration_clipforge_dota2.cfg"); + fs::write(&cs2_path, cs2_config())?; + fs::write(&dota_path, dota2_config())?; + Ok(vec![ + WrittenGsiConfig { + game_id: "counter-strike-2".to_string(), + path: cs2_path, + }, + WrittenGsiConfig { + game_id: "dota-2".to_string(), + path: dota_path, + }, + ]) +} + +pub fn cs2_config() -> String { + format!( + r#""ClipForge" +{{ + "uri" "{GSI_ENDPOINT}" + "timeout" "5.0" + "buffer" "0.1" + "throttle" "0.1" + "heartbeat" "30.0" + "data" + {{ + "provider" "1" + "map" "1" + "round" "1" + "player_id" "1" + "player_state" "1" + "player_match_stats" "1" + }} +}} +"# + ) +} + +pub fn dota2_config() -> String { + format!( + r#""ClipForge" +{{ + "uri" "{GSI_ENDPOINT}" + "timeout" "5.0" + "buffer" "0.1" + "throttle" "0.1" + "heartbeat" "30.0" + "data" + {{ + "provider" "1" + "map" "1" + "player" "1" + "hero" "1" + "abilities" "1" + }} +}} +"# + ) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn templates_reference_local_receiver() { + assert!(cs2_config().contains(GSI_ENDPOINT)); + assert!(dota2_config().contains(GSI_ENDPOINT)); + } + + #[test] + fn writes_templates() { + let root = std::env::temp_dir().join(format!( + "clipforge-gsi-{}", + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap_or_default() + .as_millis() + )); + + let written = write_gsi_config_templates(&root).expect("write"); + + assert_eq!(written.len(), 2); + assert!(written.iter().all(|config| config.path.exists())); + let _ = fs::remove_dir_all(root); + } +} diff --git a/src/lib.rs b/src/lib.rs index 08dd642..a53ab8d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ pub mod capture; pub mod database; pub mod game_detection; pub mod game_watcher; +pub mod gsi_config; pub mod hotkeys; pub mod integrations; pub mod jobs; diff --git a/ui/src/main.ts b/ui/src/main.ts index b628a7b..d502ba2 100644 --- a/ui/src/main.ts +++ b/ui/src/main.ts @@ -66,6 +66,11 @@ type ClipDto = { color_class: string; }; +type GsiConfigDto = { + game_id: string; + path: string; +}; + type AppState = { activeView: "Library" | "Recording" | "Auto Clip" | "Uploads" | "Settings"; recordingState: RecordingState; @@ -82,6 +87,7 @@ type AppState = { uploadProvider: "catbox" | "litterbox" | "custom_http" | "lustful"; customUploadEndpoint: string; customUploadResponsePath: string; + gsiConfigStatus: string; selectedClipId: string; clips: Clip[]; autoEvents: AutoEvent[]; @@ -118,6 +124,7 @@ const state: AppState = { uploadProvider: "catbox", customUploadEndpoint: "", customUploadResponsePath: "url", + gsiConfigStatus: "", selectedClipId: "clip-1", clips: [ { @@ -403,6 +410,10 @@ function renderAutoClipView() { ) .join("")} +
+ +
+ ${state.gsiConfigStatus ? `

${state.gsiConfigStatus}

` : ""} `; } @@ -493,6 +504,10 @@ function bindEvents() { void uploadSelectedClip(); }); + appRoot.querySelector("[data-action='write-gsi-configs']")?.addEventListener("click", () => { + void writeGsiConfigs(); + }); + appRoot.querySelector("[data-action='delete']")?.addEventListener("click", () => { void deleteSelectedClip(); }); @@ -677,6 +692,21 @@ async function uploadSelectedClip() { } } +async function writeGsiConfigs() { + if (!tauriInvoke) { + return; + } + + try { + const configs = await tauriInvoke("write_gsi_configs"); + state.gsiConfigStatus = configs.map((config) => `${config.game_id}: ${config.path}`).join(" ยท "); + render(); + } catch (error) { + state.gsiConfigStatus = `Could not write GSI configs: ${String(error)}`; + render(); + } +} + async function deleteSelectedClip() { if (!tauriInvoke || !state.selectedClipId) { return;