Skip to content

Commit

Permalink
Merge pull request #260 from austenadler/feature/allow-disabling-gui
Browse files Browse the repository at this point in the history
Allow disabling gui at compile time
louis-e authored Jan 21, 2025
2 parents 911ec00 + ff7a090 commit 928f487
Showing 5 changed files with 233 additions and 63 deletions.
162 changes: 145 additions & 17 deletions Cargo.lock

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

13 changes: 10 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -11,6 +11,10 @@ readme = "README.md"
[profile.release]
lto = "thin"

[features]
default = ["gui"]
gui = ["dep:tauri", "dep:tauri-plugin-log", "dep:tauri-plugin-shell"]

[build-dependencies]
tauri-build = "2"

@@ -36,7 +40,10 @@ rfd = "0.15.1"
semver = "1.0.23"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tauri = "2"
tauri-plugin-log = { version = "2.2.0" }
tauri-plugin-shell = "2"
tauri = { version = "2", optional = true }
tauri-plugin-log = { version = "2.2.0", optional = true }
tauri-plugin-shell = { version = "2", optional = true }
tokio = { version = "1.42.0", features = ["full"] }

[target.'cfg(windows)'.dependencies]
windows = { version = "0.59.0", features = ["Win32_System_Console"] }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -100,7 +100,7 @@ Feel free to choose an item from the To-Do or Known Bugs list, or bring your own
#### How to contribute
This project is open source and welcomes contributions from everyone! Whether you're interested in fixing bugs, improving performance, adding new features, or enhancing documentation, your input is valuable. Simply fork the repository, make your changes, and submit a pull request. We encourage discussions and suggestions to ensure the project remains modular, optimized, and easy to use for the community. You can use the parameter --debug to get a more detailed output of the processed values, which can be helpful for debugging and development. Contributions of all levels are appreciated, and your efforts help improve this tool for everyone.

Build and run it using: ```cargo run --release -- --path="C:/YOUR_PATH/.minecraft/saves/worldname" --bbox="min_lng,min_lat,max_lng,max_lat"```<br>
Build and run it using: ```cargo run --release --no-default-features -- --path="C:/YOUR_PATH/.minecraft/saves/worldname" --bbox="min_lng,min_lat,max_lng,max_lat"```<br>
For the GUI: ```cargo run --release```<br>

After your pull request was merged, I will take care of regularly creating update releases which will include your changes.
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
fn main() {
#[cfg(feature = "gui")]
tauri_build::build()
}
118 changes: 76 additions & 42 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -10,7 +10,17 @@ mod element_processing;
mod floodfill;
mod ground;
mod osm_parser;
#[cfg(feature = "gui")]
mod progress;
// If the user does not want the GUI, it's easiest to just mock the progress module to do nothing
#[cfg(not(feature = "gui"))]
mod progress {
pub fn emit_gui_error(_message: &str) {}
pub fn emit_gui_progress_update(_progress: f64, _message: &str) {}
pub fn is_running_with_gui() -> bool {
false
}
}
mod retrieve_data;
mod version_check;
mod world_editor;
@@ -29,7 +39,10 @@ use std::{
panic,
path::{Path, PathBuf},
};
#[cfg(feature = "gui")]
use tauri_plugin_log::{Builder as LogBuilder, Target, TargetKind};
#[cfg(target_os = "windows")]
use windows::Win32::System::Console::{AttachConsole, FreeConsole, ATTACH_PARENT_PROCESS};

fn print_banner() {
let version: &str = env!("CARGO_PKG_VERSION");
@@ -55,6 +68,14 @@ fn print_banner() {
}

fn main() {
// If on Windows, free and reattach to the parent console when using as a CLI tool
// Either of these can fail, but if they do it is not an issue, so the return value is ignored
#[cfg(target_os = "windows")]
unsafe {
let _ = FreeConsole();
let _ = AttachConsole(ATTACH_PARENT_PROCESS);
}

// Parse arguments to decide whether to launch the UI or CLI
let raw_args: Vec<String> = std::env::args().collect();

@@ -122,53 +143,63 @@ fn main() {
let _ =
data_processing::generate_world(parsed_elements, &args, scale_factor_x, scale_factor_z);
} else {
// Launch the UI
println!("Launching UI...");

// Set a custom panic hook to log panic information
panic::set_hook(Box::new(|panic_info| {
let message = format!("Application panicked: {:?}", panic_info);
error!("{}", message);
}));

// Workaround WebKit2GTK issue with NVIDIA drivers (likely explicit sync related?)
// Source: https://github.com/tauri-apps/tauri/issues/10702
#[cfg(target_os = "linux")]
unsafe {
env::set_var("WEBKIT_DISABLE_DMABUF_RENDERER", "1");
#[cfg(not(feature = "gui"))]
{
panic!("This version of arnis was not built with GUI enabled");
}

tauri::Builder::default()
.plugin(
LogBuilder::default()
.level(LevelFilter::Warn)
.targets([
Target::new(TargetKind::LogDir {
file_name: Some("arnis".into()),
}),
Target::new(TargetKind::Stdout),
])
.build(),
)
.plugin(tauri_plugin_shell::init())
.invoke_handler(tauri::generate_handler![
gui_select_world,
gui_start_generation,
gui_get_version,
gui_check_for_updates
])
.setup(|app| {
let app_handle = app.handle();
let main_window = tauri::Manager::get_webview_window(app_handle, "main")
.expect("Failed to get main window");
progress::set_main_window(main_window);
Ok(())
})
.run(tauri::generate_context!())
.expect("Error while starting the application UI (Tauri)");
#[cfg(feature = "gui")]
{
// Launch the UI
println!("Launching UI...");

// Set a custom panic hook to log panic information
panic::set_hook(Box::new(|panic_info| {
let message = format!("Application panicked: {:?}", panic_info);
error!("{}", message);
std::process::exit(1);
}));

// Workaround WebKit2GTK issue with NVIDIA drivers (likely explicit sync related?)
// Source: https://github.com/tauri-apps/tauri/issues/10702 (TODO: Remove this later)
#[cfg(target_os = "linux")]
unsafe {
env::set_var("WEBKIT_DISABLE_DMABUF_RENDERER", "1");
}

tauri::Builder::default()
.plugin(
LogBuilder::default()
.level(LevelFilter::Warn)
.targets([
Target::new(TargetKind::LogDir {
file_name: Some("arnis".into()),
}),
Target::new(TargetKind::Stdout),
])
.build(),
)
.plugin(tauri_plugin_shell::init())
.invoke_handler(tauri::generate_handler![
gui_select_world,
gui_start_generation,
gui_get_version,
gui_check_for_updates
])
.setup(|app| {
let app_handle = app.handle();
let main_window = tauri::Manager::get_webview_window(app_handle, "main")
.expect("Failed to get main window");
progress::set_main_window(main_window);
Ok(())
})
.run(tauri::generate_context!())
.expect("Error while starting the application UI (Tauri)");
}
}
}

#[cfg(feature = "gui")]
#[tauri::command]
fn gui_select_world(generate_new: bool) -> Result<String, i32> {
// Determine the default Minecraft 'saves' directory based on the OS
@@ -344,11 +375,13 @@ fn create_new_world(base_path: &Path) -> Result<String, String> {
Ok(new_world_path.display().to_string())
}

#[cfg(feature = "gui")]
#[tauri::command]
fn gui_get_version() -> String {
env!("CARGO_PKG_VERSION").to_string()
}

#[cfg(feature = "gui")]
#[tauri::command]
fn gui_check_for_updates() -> Result<bool, String> {
match version_check::check_for_updates() {
@@ -357,6 +390,7 @@ fn gui_check_for_updates() -> Result<bool, String> {
}
}

#[cfg(feature = "gui")]
#[tauri::command]
fn gui_start_generation(
bbox_text: String,

0 comments on commit 928f487

Please sign in to comment.