Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple http #67

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ jobs:
publish-tauri:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: setup node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 18

- name: Cache node modules
uses: actions/cache@v3
uses: actions/cache@v4
env:
cache-name: cache-node-modules-v1
with:
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ jobs:
cancel-in-progress: true

steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18

- name: Cache node modules
uses: actions/cache@v3
uses: actions/cache@v4
env:
cache-name: cache-node-modules-v1
with:
Expand All @@ -44,14 +44,14 @@ jobs:
cancel-in-progress: true

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: setup node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 18

- name: Cache node modules
uses: actions/cache@v3
uses: actions/cache@v4
env:
cache-name: cache-node-modules-v1
with:
Expand Down
3 changes: 3 additions & 0 deletions Readme.md → README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ The build output can be found in `src-tauri/target/release`. The installer can b

Don't forget to run prettier with `yarn fix`.

On MacOS you need to comment out this line in main.rs around line 57
```.plugin(tauri_plugin_window_state::Builder::default().build())```

### Release
- Increase the version in files:
- `package.json`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "coh3-stats-desktop-app",
"private": true,
"version": "1.2.6",
"version": "1.2.7",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
28 changes: 27 additions & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "coh3-stats-desktop-app"
version = "1.2.6"
version = "1.2.7"
description = "COH3 Stats Desktop App"
authors = ["coh3stats team"]
license = ""
Expand All @@ -25,7 +25,7 @@ reqwest = { version = "0.11", features = ["json", "multipart"] }
rev_lines = "0.3"
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.2", features = [ "window-all", "path-all", "dialog-all", "shell-open", "clipboard-write-text", "http-all", "fs-all", "updater"] }
tauri = { version = "1.2", features = [ "process-all", "window-all", "path-all", "dialog-all", "shell-open", "clipboard-write-text", "http-all", "fs-all", "updater"] }
tauri-plugin-deep-link = "0.1"
tauri-plugin-fs-watch = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" }
tauri-plugin-log = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" }
Expand All @@ -38,6 +38,8 @@ vault = "8"
window-shadows = "0.2"
# monitoring by sentry
sentry = "0.32.2"
# For the OBS interface we need to expose webserver
tiny_http = "0.11"

[dev-dependencies]
criterion = { version = "0.4", features = ["html_reports"] }
Expand Down
40 changes: 40 additions & 0 deletions src-tauri/src/dp_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::path::PathBuf;

// COH3 Desktop App Utils
use tauri_plugin_store::{Store, StoreBuilder};
use log::{info, warn};
use tauri::{AppHandle, Runtime};


pub fn load_store<R: Runtime>(handle: AppHandle<R>) -> Store<R> {
let mut store = StoreBuilder::new(handle, PathBuf::from("config.dat")).build();

if let Err(err) = store.load() {
warn!("error loading store from disk: {err}");
info!("saving store file to disk");
if let Err(err) = store.save() {
warn!("error saving store file to disk: {err}");
}
}

store
}
Comment on lines +9 to +21
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this was how I was doing it originally, but it unfortunately doesn't play well with Javascript changes to the store because the JS changes don't update this Rust state struct. What I'd recommend is extracting this function instead: https://github.com/cohstats/coh3-stats-desktop-app/blob/master/src-tauri/src/plugins/cohdb/sync/mod.rs#L138

It's generic over any deserializable object, so it'll work for anything that can be deserialized from JSON (primitives, strings, and structs that derive Deserialize should all work here). You'll probably have to tell the compiler what type you're expecting though (you can see an example of that here: https://github.com/cohstats/coh3-stats-desktop-app/blob/master/src-tauri/src/plugins/cohdb/sync/mod.rs#L165).

I don't have time now but I can probably take a look at this sometime next week if you have trouble with it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks I will take a look, looking at my code I have no idea why is there the store.save() :D I guess I was happy that it works.

My implementation is counting with need to restart the APP, because I would have to handle it using events I guess and I don't think it's necessary right.



// This is stupid I can't figure out how to return different types from a function and work with it correctly :'(
// fn get_value_from_store<R: Runtime>(store: &Store<R>, key: String) -> Option<Result<String, bool>> {
// match store.get(key) {
// Some(value) => {
// Some(serde_json::from_value(value.clone()).map_err(|_| false))
// },
// None => None,
// }
// }


pub fn is_streamer_overlay_enabled<R: Runtime>(store: &Store<R>) -> bool {
match store.get("streamerOverlayEnabled".to_string()) {
Some(value) => serde_json::from_value(value.clone()).unwrap_or(false),
None => false,
}
}
2 changes: 2 additions & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod parse_log_file;
pub mod plugins;
pub mod overlay_server;
pub mod dp_utils;
36 changes: 32 additions & 4 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

extern crate machine_uid;

use coh3_stats_desktop_app::{parse_log_file, plugins::cohdb};
use log::error;
use coh3_stats_desktop_app::dp_utils::{is_streamer_overlay_enabled, load_store};
use coh3_stats_desktop_app::{parse_log_file, plugins::cohdb, overlay_server::run_http_server};
use log::{error, info};
use std::path::Path;
use std::thread;
use tauri::Manager;
use tauri_plugin_log::LogTarget;
use window_shadows::set_shadow;
Expand Down Expand Up @@ -53,6 +55,7 @@ fn main() {
.unwrap();
}))
.plugin(tauri_plugin_fs_watch::init())
// You need to comment out this line to run the app on MacOS
.plugin(tauri_plugin_window_state::Builder::default().build())
.plugin(tauri_plugin_store::Builder::default().build())
.plugin(cohdb::auth::init(
Expand All @@ -61,8 +64,10 @@ fn main() {
))
.plugin(coh3_stats_desktop_app::plugins::cohdb::sync::init())
.setup(setup)
.setup(setup_web_server)
.run(tauri::generate_context!())
.expect("error while running tauri application");

}

fn setup(app: &mut tauri::App) -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -84,11 +89,34 @@ fn setup(app: &mut tauri::App) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

fn setup_web_server(app: &mut tauri::App) -> Result<(), Box<dyn std::error::Error>> {
let app_handle = app.handle();


let store = load_store(app_handle.clone());
let is_enabled = is_streamer_overlay_enabled(&store);
//print is enabled
if is_enabled {
info!("Streamer overlay is enabled");
let mut file_path = app_handle.path_resolver().app_data_dir().unwrap();
file_path.push("streamerOverlay.html");
info!("Expecting the streamerOverlay at {:?}", file_path);

let _handle = thread::spawn(|| {
run_http_server(file_path);
});
} else {
info!("Streamer overlay is disabled");
}

Ok(())
}

/// returns the default expected log file path
#[tauri::command]
fn default_log_file_path() -> String {
let mut path = tauri::api::path::document_dir().unwrap();
path.push("My Games");
path.push("My Games"); // TODO: Is this "my games" also on non-English Windows?
path.push("Company of Heroes 3");
path.push("warnings.log");
path.display().to_string()
Expand All @@ -97,7 +125,7 @@ fn default_log_file_path() -> String {
#[tauri::command]
fn default_playback_path() -> String {
let mut path = tauri::api::path::document_dir().unwrap();
path.push("My Games");
path.push("My Games"); // TODO: Is this "my games" also on non-English Windows?
path.push("Company of Heroes 3");
path.push("playback");
path.display().to_string()
Expand Down
37 changes: 37 additions & 0 deletions src-tauri/src/overlay_server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::fs::File;
use std::panic;
use log::{error, info};
use std::path::PathBuf;

use tiny_http::{Server, Response, StatusCode};

pub fn run_http_server(streamer_overlay_path: PathBuf) {

let result = panic::catch_unwind(|| {

// Ideally we would allow setting up port in the settings
info!("Starting streamer overlay server on port 47824");
let server = Server::http("127.0.0.1:47824").unwrap();

for request in server.incoming_requests() {

let file = match File::open(&streamer_overlay_path) {
Ok(file) => file,
Err(_) => {
let response = Response::new_empty(StatusCode(404));
let _ = request.respond(response);
continue;
}
};

let response = Response::from_file(file);
let _ = request.respond(response);
}

});

if let Err(err) = result {
error!("Couldn't start the streamer overlay server: {:?}", err);
}

}
5 changes: 3 additions & 2 deletions src-tauri/src/plugins/cohdb/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl PluginState {
}

#[tauri::command]
async fn authenticate<R: Runtime>(handle: AppHandle<R>) -> Result<()> {
async fn authenticate<R: Runtime>(handle: AppHandle<R>) -> Result<String> {
let state = handle.state::<PluginState>();
let request = ActiveRequestState::new();

Expand All @@ -96,7 +96,8 @@ async fn authenticate<R: Runtime>(handle: AppHandle<R>) -> Result<()> {
*state.request.lock().await = Some(request);

info!("redirecting to auth URL: {auth_url}");
open(&handle.shell_scope(), auth_url, None).map_err(Shell)
open(&handle.shell_scope(), auth_url.clone(), None).map_err(Shell);
Ok(auth_url.to_string())
}

pub async fn retrieve_token<R: Runtime>(request: &str, handle: &AppHandle<R>) -> Result<()> {
Expand Down
5 changes: 4 additions & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"package": {
"productName": "Coh3 Stats Desktop App",
"version": "1.2.6"
"version": "1.2.7"
},
"tauri": {
"allowlist": {
Expand All @@ -33,6 +33,9 @@
},
"window": {
"all": true
},
"process": {
"all": true
}
},
"bundle": {
Expand Down
13 changes: 10 additions & 3 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@ info("Start frontend")
Sentry.init({
dsn: "https://88e8a309f91b8b5bb9a41dd14ff775b9@o4504995920543744.ingest.sentry.io/4506752563019776",
integrations: [Sentry.browserTracingIntegration()],
// Performance Monitoring
tracesSampleRate: 0.1, // Capture 100% of the transactions
// Set 'tracePropagationTargets' to control for which URLs distributed tracing should be enabled
tracesSampleRate: 0.1,
tracePropagationTargets: ["localhost"],
beforeSend(event, hint) {
// On macOS we do only development, we can ignore all development errors
if (event.contexts?.os?.name === "macOS") {
// Ignore the event
return null
}
// Otherwise, return the event as is
return event
},
})

events.init()
Expand Down
5 changes: 5 additions & 0 deletions src/streamer-overlay/configValues.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ const [getShowFlagsOverlay, useShowFlagsOverlay] = configValueFactory<boolean>(
const [getAlwaysShowOverlay, useAlwaysShowOverlay] =
configValueFactory<boolean>("alwaysShowOverlay", async () => false)

const [getStreamerOverlayEnabled, useStreamerOverlayEnabled] =
configValueFactory<boolean>("streamerOverlayEnabled", async () => false)

export {
getShowFlagsOverlay,
useShowFlagsOverlay,
getAlwaysShowOverlay,
useAlwaysShowOverlay,
getStreamerOverlayEnabled,
useStreamerOverlayEnabled,
}
Loading
Loading