From 23585e738f13fe5f54caf470f900609ede2ac725 Mon Sep 17 00:00:00 2001 From: Johannes Merkt Date: Fri, 24 Mar 2023 16:36:06 +0100 Subject: [PATCH 1/4] Improve parser robustness, add logging functionality --- package.json | 3 +- src-tauri/Cargo.lock | 70 +++++++++++++++++++++++++++++++-- src-tauri/Cargo.toml | 2 + src-tauri/src/main.rs | 5 +++ src-tauri/src/parse_log_file.rs | 18 +++++++-- src/main.tsx | 3 ++ yarn.lock | 6 +++ 7 files changed, 100 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 96ce57f..0e487fd 100644 --- a/package.json +++ b/package.json @@ -24,13 +24,14 @@ "react-dom": "^18.2.0", "react-router-dom": "^6.4.5", "tauri-plugin-fs-watch-api": "https://github.com/tauri-apps/tauri-plugin-fs-watch", + "tauri-plugin-log-api": "https://github.com/tauri-apps/tauri-plugin-log", "tauri-plugin-store-api": "https://github.com/tauri-apps/tauri-plugin-store" }, "devDependencies": { "@tauri-apps/cli": "^1.2.2", + "@types/mixpanel-browser": "^2.38.1", "@types/react": "^18.0.26", "@types/react-dom": "^18.0.9", - "@types/mixpanel-browser": "^2.38.1", "@vitejs/plugin-react": "^3.0.0", "prettier": "^2.8.1", "typescript": "^4.9.3", diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index c84cd29..265c4aa 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -280,6 +280,16 @@ version = "3.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba" +[[package]] +name = "byte-unit" +version = "4.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da78b32057b8fdfc352504708feeba7216dcd65a2c9ab02978cbd288d1279b6c" +dependencies = [ + "serde", + "utf8-width", +] + [[package]] name = "bytemuck" version = "1.12.3" @@ -413,6 +423,7 @@ dependencies = [ name = "coh3-stats-desktop-app" version = "1.2.0" dependencies = [ + "log", "machine-uid", "nom", "notify", @@ -422,6 +433,7 @@ dependencies = [ "tauri", "tauri-build", "tauri-plugin-fs-watch", + "tauri-plugin-log", "tauri-plugin-single-instance", "tauri-plugin-store", "tauri-plugin-window-state", @@ -789,6 +801,15 @@ dependencies = [ "instant", ] +[[package]] +name = "fern" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9f0c14694cbd524c8720dd69b0e3179344f04ebb5f90f2e4a440c6ea3b2f1ee" +dependencies = [ + "log", +] + [[package]] name = "field-offset" version = "0.3.4" @@ -1569,6 +1590,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" dependencies = [ "cfg-if", + "value-bag", ] [[package]] @@ -1903,6 +1925,15 @@ dependencies = [ "syn", ] +[[package]] +name = "num_threads" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44" +dependencies = [ + "libc", +] + [[package]] name = "objc" version = "0.2.7" @@ -3164,7 +3195,7 @@ dependencies = [ [[package]] name = "tauri-plugin-fs-watch" version = "0.1.0" -source = "git+https://github.com/tauri-apps/plugins-workspace?branch=dev#c6502f673b9fc68c41f119a4520da5858929c66a" +source = "git+https://github.com/tauri-apps/plugins-workspace?branch=dev#5a6abd3203dc94c38f96d0c4bf7ecbef399f8c25" dependencies = [ "log", "notify", @@ -3175,10 +3206,25 @@ dependencies = [ "thiserror", ] +[[package]] +name = "tauri-plugin-log" +version = "0.1.0" +source = "git+https://github.com/tauri-apps/plugins-workspace?branch=dev#5a6abd3203dc94c38f96d0c4bf7ecbef399f8c25" +dependencies = [ + "byte-unit", + "fern", + "log", + "serde", + "serde_json", + "serde_repr", + "tauri", + "time", +] + [[package]] name = "tauri-plugin-single-instance" version = "0.1.0" -source = "git+https://github.com/tauri-apps/plugins-workspace?branch=dev#c6502f673b9fc68c41f119a4520da5858929c66a" +source = "git+https://github.com/tauri-apps/plugins-workspace?branch=dev#5a6abd3203dc94c38f96d0c4bf7ecbef399f8c25" dependencies = [ "log", "serde", @@ -3192,7 +3238,7 @@ dependencies = [ [[package]] name = "tauri-plugin-store" version = "0.1.0" -source = "git+https://github.com/tauri-apps/plugins-workspace?branch=dev#c6502f673b9fc68c41f119a4520da5858929c66a" +source = "git+https://github.com/tauri-apps/plugins-workspace?branch=dev#5a6abd3203dc94c38f96d0c4bf7ecbef399f8c25" dependencies = [ "log", "serde", @@ -3359,6 +3405,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a561bf4617eebd33bca6434b988f39ed798e527f51a1e797d0ee4f61c0a38376" dependencies = [ "itoa 1.0.4", + "libc", + "num_threads", "serde", "time-core", "time-macros", @@ -3555,6 +3603,12 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" +[[package]] +name = "utf8-width" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5190c9442dcdaf0ddd50f37420417d219ae5261bbf5db120d0f9bab996c9cba1" + [[package]] name = "uuid" version = "0.8.2" @@ -3576,6 +3630,16 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +[[package]] +name = "value-bag" +version = "1.0.0-alpha.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2209b78d1249f7e6f3293657c9779fe31ced465df091bbd433a1cf88e916ec55" +dependencies = [ + "ctor", + "version_check", +] + [[package]] name = "vcpkg" version = "0.2.15" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 5af203d..a3ec0c6 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -25,6 +25,8 @@ tauri-plugin-window-state = "0.1" tauri-plugin-fs-watch = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" } tauri-plugin-store = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" } tauri-plugin-single-instance = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" } +log = "^0.4" +tauri-plugin-log = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" } machine-uid = "0.2.0" [features] diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index b2c6a4d..7f1d146 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -9,6 +9,7 @@ use tauri_plugin_fs_watch; use std::path::Path; use tauri::Manager; use window_shadows::set_shadow; +use tauri_plugin_log::{LogTarget}; mod parse_log_file; #[derive(Clone, serde::Serialize)] @@ -29,6 +30,10 @@ fn main() { app.emit_all("single-instance", Payload { args: argv, cwd }).unwrap(); })) + .plugin(tauri_plugin_log::Builder::default().targets([ + LogTarget::LogDir, + LogTarget::Stdout, + ]).build()) .plugin(tauri_plugin_fs_watch::init()) .plugin(tauri_plugin_window_state::Builder::default().build()) .plugin(tauri_plugin_store::Builder::default().build()) diff --git a/src-tauri/src/parse_log_file.rs b/src-tauri/src/parse_log_file.rs index f977634..faf172f 100644 --- a/src-tauri/src/parse_log_file.rs +++ b/src-tauri/src/parse_log_file.rs @@ -3,6 +3,7 @@ use rev_lines::RevLines; use nom; use std::io::BufReader; use std::fs::File; +use log::{info}; #[derive(Serialize, Deserialize, Clone)] pub enum GameState { @@ -128,7 +129,7 @@ pub fn parse_log_file_reverse(path: String) -> LogFileData { if param == "GAME" { if let Ok((steam_name, _)) = get_game_player_name(tail) { player_name = steam_name.to_string(); - //println!("Steam name {}", steam_name); + println!("Steam name {}", steam_name); break; } if let Ok((game_language, _)) = get_game_language(tail) { @@ -240,6 +241,7 @@ pub fn parse_log_file_reverse(path: String) -> LogFileData { let game_state = determine_game_state(game_running, game_ended, game_loading, game_started); let left_team = get_team_data(left); let right_team = get_team_data(right); + info!("Log file parsed: Found {} players", left_team.players.len() + right_team.players.len()); LogFileData { game_state, game_type: determine_game_type(&left_team, &right_team), @@ -355,7 +357,8 @@ fn get_game_sub_param(game_param_tail: &str) -> nom::IResult<&str, &str> { fn get_game_player_name(game_param_tail: &str) -> nom::IResult<&str, ()> { let (name_tail, _) = nom::bytes::complete::tag("Current Steam name is [")(game_param_tail)?; - let (_, name) = nom::bytes::complete::take_until1("]")(name_tail)?; + let (name, _) = get_till_last_tag(name_tail, "]")?; + //let (_, name) = nom::bytes::complete::take_until1("]")(name_tail)?; Ok((name,())) } @@ -385,7 +388,7 @@ fn get_game_over(mod_param_tail: &str) -> nom::IResult<&str, &str> { } fn get_last_separated_by_space(line: &str) -> nom::IResult<&str, &str> { - let (tail, front) = nom::bytes::complete::take_until1(" ")(line)?; + let (tail, front) = nom::bytes::complete::take_until(" ")(line)?; let (tail, _) = nom::bytes::complete::tag(" ")(tail)?; if let Ok((tail, _)) = get_last_separated_by_space(tail) { return Ok((tail, &line[0..(line.len() - tail.len() - 1)])) @@ -393,6 +396,15 @@ fn get_last_separated_by_space(line: &str) -> nom::IResult<&str, &str> { Ok((tail, front)) } +fn get_till_last_tag<'a>(line: &'a str, tag: &'a str) -> nom::IResult<&'a str, &'a str> { + let (tail, front) = nom::bytes::complete::take_until(tag)(line)?; + let (tail, _) = nom::bytes::complete::tag(tag)(tail)?; + if let Ok((front, tail)) = get_till_last_tag(tail, tag) { + return Ok((front, tail)) + } + Ok((front, tail)) +} + fn get_without_leading_space(line: &str) -> nom::IResult<&str, ()> { let (without_space, _) = nom::bytes::complete::tag(" ")(line)?; Ok((without_space, ())) diff --git a/src/main.tsx b/src/main.tsx index 550efb5..d1f66b9 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -6,6 +6,9 @@ import { renderStreamerHTML } from "./streamer-overlay/renderStreamerOverlay" import events from "./mixpanel/mixpanel" import { listen } from "@tauri-apps/api/event" import { appWindow } from "@tauri-apps/api/window" +import { trace, info, error, attachConsole } from "tauri-plugin-log-api" + +info("Start frontend") events.init() diff --git a/yarn.lock b/yarn.lock index 6ad60c4..1b27e1a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1313,6 +1313,12 @@ tabbable@^6.0.1: dependencies: "@tauri-apps/api" "^1.2.0" +"tauri-plugin-log-api@https://github.com/tauri-apps/tauri-plugin-log": + version "0.0.0" + resolved "https://github.com/tauri-apps/tauri-plugin-log#05a9bfd9edb9b5f4ab95412bb607691708b65a25" + dependencies: + "@tauri-apps/api" "^1.2.0" + "tauri-plugin-store-api@https://github.com/tauri-apps/tauri-plugin-store": version "0.0.0" resolved "https://github.com/tauri-apps/tauri-plugin-store#201639d72bc8c4dec6f40ff7c02162c7926bfa3d" From ecf72601607f3157aa6ae134295490f3431e7a14 Mon Sep 17 00:00:00 2001 From: Johannes Merkt Date: Fri, 24 Mar 2023 18:27:49 +0100 Subject: [PATCH 2/4] Fix dont show ELO without rank --- src/components/PlayerCard.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/components/PlayerCard.tsx b/src/components/PlayerCard.tsx index f0baf23..454e155 100644 --- a/src/components/PlayerCard.tsx +++ b/src/components/PlayerCard.tsx @@ -78,7 +78,11 @@ export const PlayerCard: React.FC = ({ - + From c8cbf33627c1413d52c0ac7c629b279bb67bef12 Mon Sep 17 00:00:00 2001 From: Johannes Merkt Date: Fri, 24 Mar 2023 19:09:36 +0100 Subject: [PATCH 3/4] Improve parser code --- src-tauri/src/parse_log_file.rs | 75 ++++++++++++++------------------- 1 file changed, 31 insertions(+), 44 deletions(-) diff --git a/src-tauri/src/parse_log_file.rs b/src-tauri/src/parse_log_file.rs index faf172f..213e99f 100644 --- a/src-tauri/src/parse_log_file.rs +++ b/src-tauri/src/parse_log_file.rs @@ -79,63 +79,29 @@ pub fn parse_log_file_reverse(path: String) -> LogFileData { let mut language_code = "".to_string(); // Read log file in reverse order line by line for line in rev_lines { + // Is the line when the game is being closed correctly if nom::bytes::complete::tag::<&str, &str, ()>("Application closed")(line.as_str()).is_ok() { - //println!("Found application closed"); game_running = false; continue; } if let Ok((tail, parsed_timestamp)) = get_timestamped_line(line.as_str()) { + + // Is the line where a game starts if is_game_start_line(tail) { timestamp = parsed_timestamp.to_string(); - //println!("Timestamp {}", timestamp); - + continue; } + + // Is the line that logs the player steam id if let Ok((steam_id, _)) = get_game_player_steam_id(tail) { player_steam_id = steam_id.to_string(); - //println!("Game steam {}", steam_id); + continue; } - // these logs have been removed by relic - /*if let Ok((tail, _)) = get_match_started_line(tail) { - //println!("Match started {}", tail); - if let Ok((tail, relic_id)) = nom::bytes::complete::take_until1::<&str, &str, ()>(" /steam/")(tail) { - if let Ok((tail, _)) = nom::bytes::complete::tag::<&str, &str, ()>(" /steam/")(tail) { - //println!("Match started relic {}", relic_id); - if let Ok((tail, steam_id)) = nom::bytes::complete::take_until1::<&str, &str, ()>("], ")(tail) { - if let Ok((tail, _)) = nom::bytes::complete::tag::<&str, &str, ()>("], slot = ")(tail) { - //println!("Match started steam {}", steam_id); - if let Ok((tail, _position_str)) = nom::bytes::complete::take_until1::<&str, &str, ()>(", ranking = ")(tail) { - if let Ok((rank_str, _)) = nom::bytes::complete::tag::<&str, &str, ()>(", ranking = ")(tail) { - if let Ok(rank) = rank_str.parse::() { - //println!("Match started slot {}", position); - //println!("Match started rank {}", rank); - if let Some(index) = left.iter().position(|p| *p.relic_id == relic_id.to_string()) { - left[index].steam_id = steam_id.to_string(); - left[index].rank = rank; - } - if let Some(index) = right.iter().position(|p| *p.relic_id == relic_id.to_string()) { - right[index].steam_id = steam_id.to_string(); - right[index].rank = rank; - } - } - } - } - } - } - } - } - }*/ + if let Ok((tail, param)) = get_param_line(tail) { if param == "GAME" { - if let Ok((steam_name, _)) = get_game_player_name(tail) { - player_name = steam_name.to_string(); - println!("Steam name {}", steam_name); - break; - } - if let Ok((game_language, _)) = get_game_language(tail) { - language_code = game_language.to_string(); - //println!("Game language {}", game_language); - } + if let Ok((tail, sub_param)) = get_game_sub_param(tail) { if sub_param == "Scenario" { if let Ok((parsed_map, _)) = get_map_name(tail) { @@ -223,6 +189,15 @@ pub fn parse_log_file_reverse(path: String) -> LogFileData { } } } + + // Is the line that logs the playing players name + } else if let Ok((steam_name, _)) = get_game_player_name(tail) { + player_name = steam_name.to_string(); + break; + + // Is the line that logs the games language + } else if let Ok((game_language, _)) = get_game_language(tail) { + language_code = game_language.to_string(); } } else if param == "MOD" { if let Ok((duration_str, _)) = get_game_over(tail) { @@ -408,4 +383,16 @@ fn get_till_last_tag<'a>(line: &'a str, tag: &'a str) -> nom::IResult<&'a str, & fn get_without_leading_space(line: &str) -> nom::IResult<&str, ()> { let (without_space, _) = nom::bytes::complete::tag(" ")(line)?; Ok((without_space, ())) -} \ No newline at end of file +} + +/*fn test_logging_solution(line: &str) -> nom::IResult<&str, ()> { + let test = nom::bytes::complete::tag("Applsdasdasded")(line); + let result = match test { + Ok((tail, _)) => tail, + Err(e) => { + info!("Failed!"); + return Err(e) + } + }; + Ok(("without_space", ())) +}*/ \ No newline at end of file From c3f2df2d9327db235f94eba6de650e4093720ec0 Mon Sep 17 00:00:00 2001 From: Johannes Merkt Date: Fri, 24 Mar 2023 19:11:22 +0100 Subject: [PATCH 4/4] Bump version --- package.json | 2 +- src-tauri/Cargo.lock | 2 +- src-tauri/Cargo.toml | 2 +- src-tauri/tauri.conf.json | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 0e487fd..689d6d6 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "coh3-stats-desktop-app", "private": true, - "version": "1.2.0", + "version": "1.2.1", "type": "module", "scripts": { "dev": "vite", diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 265c4aa..22f0cd1 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -421,7 +421,7 @@ dependencies = [ [[package]] name = "coh3-stats-desktop-app" -version = "1.2.0" +version = "1.2.1" dependencies = [ "log", "machine-uid", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index a3ec0c6..6d28893 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "coh3-stats-desktop-app" -version = "1.2.0" +version = "1.2.1" description = "A Tauri App" authors = ["you"] license = "" diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 6a93b5f..c194f03 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -7,7 +7,7 @@ }, "package": { "productName": "Coh3 Stats Desktop App", - "version": "1.2.0" + "version": "1.2.1" }, "tauri": { "allowlist": {