more crash protection (#832)

* more crash protection

* more

* more
This commit is contained in:
Logan McNaughton 2026-05-03 16:02:17 +02:00 committed by GitHub
parent 17f3f2dc63
commit 12280bb35b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 84 additions and 44 deletions

View File

@ -85,8 +85,11 @@ impl Drop for Cheats {
fn write_cheats(cheats: &Cheats) {
let dirs = ui::get_dirs();
let file_path = dirs.config_dir.join("cheats.json");
let f = std::fs::File::create(file_path).unwrap();
serde_json::to_writer_pretty(f, &cheats).unwrap();
if let Ok(f) = std::fs::File::create(file_path)
&& let Err(e) = serde_json::to_writer_pretty(f, &cheats)
{
eprintln!("Error writing cheats: {}", e);
}
}
impl Cheats {
@ -115,8 +118,11 @@ impl Drop for Config {
fn write_config(config: &Config) {
let dirs = ui::get_dirs();
let file_path = dirs.config_dir.join("config.json");
let f = std::fs::File::create(file_path).unwrap();
serde_json::to_writer_pretty(f, &config).unwrap();
if let Ok(f) = std::fs::File::create(file_path)
&& let Err(e) = serde_json::to_writer_pretty(f, &config)
{
eprintln!("Error writing config: {}", e);
}
}
impl Config {

View File

@ -153,7 +153,9 @@ fn local_game_window(
let saves_path = dirs.data_dir.join("saves");
app.on_saves_folder_button_clicked(move || {
open::that_detached(saves_path.clone()).unwrap();
if let Err(e) = open::that_detached(saves_path.clone()) {
eprintln!("Error opening saves folder: {}", e);
}
});
file_dropped(app, controller_paths);
}
@ -448,22 +450,35 @@ pub fn save_settings(app: &AppWindow, controller_paths: &[Option<String>]) {
fn about_window(app: &AppWindow) {
app.on_wiki_button_clicked(move || {
open::that_detached("https://github.com/gopher64/gopher64/wiki").unwrap();
if let Err(e) = open::that_detached("https://github.com/gopher64/gopher64/wiki") {
eprintln!("Error opening wiki: {}", e);
}
});
app.on_discord_button_clicked(move || {
open::that_detached("https://discord.gg/9RGXq8W8JQ").unwrap();
if let Err(e) = open::that_detached("https://discord.gg/9RGXq8W8JQ") {
eprintln!("Error opening Discord: {}", e);
}
});
app.on_patreon_button_clicked(move || {
open::that_detached("https://patreon.com/loganmc10").unwrap();
if let Err(e) = open::that_detached("https://patreon.com/loganmc10") {
eprintln!("Error opening Patreon: {}", e);
}
});
app.on_github_sponsors_button_clicked(move || {
open::that_detached("https://github.com/sponsors/loganmc10").unwrap();
if let Err(e) = open::that_detached("https://github.com/sponsors/loganmc10") {
eprintln!("Error opening GitHub Sponsors: {}", e);
}
});
app.on_source_code_button_clicked(move || {
open::that_detached("https://github.com/gopher64/gopher64").unwrap();
if let Err(e) = open::that_detached("https://github.com/gopher64/gopher64") {
eprintln!("Error opening source code: {}", e);
}
});
app.on_newversion_button_clicked(move || {
open::that_detached("https://github.com/gopher64/gopher64/releases/latest").unwrap();
if let Err(e) = open::that_detached("https://github.com/gopher64/gopher64/releases/latest")
{
eprintln!("Error opening new version: {}", e);
}
});
app.set_version(format!("Version: {}", env!("GIT_DESCRIBE")).into());

View File

@ -1041,15 +1041,16 @@ pub fn init(ui: &mut ui::Ui) {
}
for i in 0..4 {
let controller_assignment = &ui.config.input.controller_assignment[i];
if controller_assignment.is_some() && ui.config.input.controller_enabled[i] {
if let Some(controller_assignment) = &ui.config.input.controller_assignment[i]
&& ui.config.input.controller_enabled[i]
{
let mut joystick_id = sdl3_sys::everything::SDL_JoystickID(0);
let assigned_path = controller_assignment.as_ref().unwrap();
for joystick in ui.input.joysticks.iter() {
let path = unsafe { sdl3_sys::joystick::SDL_GetJoystickPathForID(*joystick) };
if !path.is_null()
&& unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap() } == *assigned_path
&& unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap() }
== controller_assignment
&& unsafe { sdl3_sys::joystick::SDL_GetJoystickFromID(*joystick) }.is_null()
&& unsafe { sdl3_sys::gamepad::SDL_GetGamepadFromID(*joystick) }.is_null()
{

View File

@ -116,25 +116,27 @@ fn populate_server_names<T: ComponentHandle + NetplayPages + 'static>(weak: slin
tokio::spawn(async move {
let mut local_servers: Vec<(String, String)> = vec![];
let broadcast_sock = tokio::net::UdpSocket::bind((std::net::Ipv4Addr::UNSPECIFIED, 0))
.await
.unwrap();
broadcast_sock.set_broadcast(true).unwrap();
let data: [u8; 1] = [1];
let _ = broadcast_sock
.send_to(&data, (std::net::Ipv4Addr::BROADCAST, 45000))
.await;
let mut buffer = [0; 1024];
if let Ok(Ok(result)) = tokio::time::timeout(
std::time::Duration::from_millis(200),
broadcast_sock.recv(&mut buffer),
)
.await
if let Ok(broadcast_sock) =
tokio::net::UdpSocket::bind((std::net::Ipv4Addr::UNSPECIFIED, 0)).await
&& let Ok(_) = broadcast_sock.set_broadcast(true)
{
let data: std::collections::HashMap<String, String> =
serde_json::from_slice(&buffer[..result]).unwrap();
for server in data.iter() {
local_servers.push((server.0.into(), server.1.into()));
let data: [u8; 1] = [1];
let _ = broadcast_sock
.send_to(&data, (std::net::Ipv4Addr::BROADCAST, 45000))
.await;
let mut buffer = [0; 1024];
if let Ok(Ok(result)) = tokio::time::timeout(
std::time::Duration::from_millis(200),
broadcast_sock.recv(&mut buffer),
)
.await
&& let Ok(data) = serde_json::from_slice::<std::collections::HashMap<String, String>>(
&buffer[..result],
)
{
for server in data.iter() {
local_servers.push((server.0.into(), server.1.into()));
}
}
}
@ -1259,9 +1261,14 @@ pub fn netplay_window(app: &AppWindow, controller_paths: &[Option<String>]) {
});
app.on_netplay_discord_button_clicked(move || {
open::that_detached("https://discord.gg/JyW6ZgBUyS").unwrap();
if let Err(e) = open::that_detached("https://discord.gg/JyW6ZgBUyS") {
eprintln!("Error opening Discord: {}", e);
}
});
app.on_netplay_feedback_button_clicked(move || {
open::that_detached("https://github.com/gopher64/gopher64/discussions/453").unwrap();
if let Err(e) = open::that_detached("https://github.com/gopher64/gopher64/discussions/453")
{
eprintln!("Error opening feedback: {}", e);
}
});
}

View File

@ -82,20 +82,28 @@ pub fn ra_window(app: &ui::gui::AppWindow) {
leaderboard,
}
};
let f = std::fs::File::create(&file_path).unwrap();
serde_json::to_writer_pretty(f, &raconfig).unwrap();
if let Ok(f) = std::fs::File::create(&file_path)
&& let Err(e) = serde_json::to_writer_pretty(f, &raconfig)
{
eprintln!("Error writing RA config: {}", e);
}
});
app.on_ra_games_clicked(move || {
open::that_detached("https://retroachievements.org/system/2-nintendo-64/games").unwrap();
if let Err(e) =
open::that_detached("https://retroachievements.org/system/2-nintendo-64/games")
{
eprintln!("Error opening games: {}", e);
}
});
app.on_ra_show_profile_clicked(move || {
open::that_detached(format!(
if let Err(e) = open::that_detached(format!(
"https://retroachievements.org/user/{}",
retroachievements::get_username().unwrap_or_default()
))
.unwrap();
)) {
eprintln!("Error opening profile: {}", e);
}
});
}

View File

@ -49,8 +49,9 @@ pub struct Saves {
}
fn get_save_type(rom: &[u8], game_id: &str) -> Vec<SaveTypes> {
let header_type = std::str::from_utf8(rom[0x3C..0x3E].try_into().unwrap());
if header_type.is_ok() && header_type.unwrap() == "ED" {
if let Ok(header_type) = std::str::from_utf8(rom[0x3C..0x3E].try_into().unwrap())
&& header_type == "ED"
{
let save_type = rom[0x3F] >> 4;
match save_type {
0 => return vec![],
@ -509,6 +510,8 @@ fn write_save(ui: &mut ui::Ui, save_type: SaveTypes) {
let save_data = data.clone();
let save_path = path.to_path_buf();
tokio::spawn(async move {
tokio::fs::write(save_path, save_data).await.unwrap();
if let Err(e) = tokio::fs::write(save_path, save_data).await {
eprintln!("Error writing save: {}", e);
}
});
}