noita_entangled_worlds/noita-proxy/src/lib.rs

670 lines
26 KiB
Rust
Raw Normal View History

use std::{
2024-05-27 16:36:15 +03:00
fmt::Display,
net::SocketAddr,
sync::{atomic::Ordering, Arc},
time::Duration,
};
2024-05-01 20:26:37 +03:00
use bitcode::{Decode, Encode};
2024-07-06 13:44:31 +03:00
use bookkeeping::noita_launcher::{LaunchTokenResult, NoitaLauncher};
2024-05-17 17:55:50 +03:00
use clipboard::{ClipboardContext, ClipboardProvider};
2024-06-08 23:25:40 +03:00
use eframe::egui::{
self, Align2, Button, Color32, DragValue, InnerResponse, Key, Margin, OpenUrl, Rect, RichText,
2024-07-02 01:32:51 +03:00
ScrollArea, Slider, TextureOptions, Ui, Vec2,
2024-06-08 23:25:40 +03:00
};
2024-06-21 23:59:44 +03:00
use egui_plot::{Plot, PlotPoint, PlotUi, Text};
2024-06-08 14:25:57 +03:00
use lang::{set_current_locale, tr, LANGS};
2024-05-23 21:02:39 +03:00
use mod_manager::{Modmanager, ModmanagerSettings};
2024-07-03 21:34:08 +03:00
use net::{omni::PeerVariant, steam_networking::ExtraPeerState, NetManagerInit};
2024-05-24 00:41:55 +03:00
use self_update::SelfUpdateManager;
2024-05-23 21:02:39 +03:00
use serde::{Deserialize, Serialize};
2024-05-17 17:55:50 +03:00
use steamworks::{LobbyId, SteamAPIInitError};
2024-05-11 22:15:21 +03:00
use tangled::Peer;
2024-05-17 17:55:50 +03:00
use tracing::info;
2024-06-08 14:25:57 +03:00
use unic_langid::LanguageIdentifier;
2024-05-01 20:26:37 +03:00
2024-07-04 23:27:35 +03:00
mod util;
2024-07-06 13:44:31 +03:00
use util::args::Args;
2024-07-04 23:27:35 +03:00
pub use util::{args, lang, steam_helper};
mod bookkeeping;
pub use bookkeeping::{mod_manager, releases, self_update};
2024-05-27 16:08:21 +03:00
pub mod net;
2024-05-01 20:26:37 +03:00
2024-07-02 00:45:30 +03:00
#[derive(Debug, Decode, Encode, Clone, Serialize, Deserialize)]
2024-05-01 20:26:37 +03:00
pub struct GameSettings {
seed: u64,
2024-05-13 21:00:00 +03:00
debug_mode: bool,
2024-06-06 15:25:01 +03:00
world_sync_version: u32,
2024-07-02 00:45:30 +03:00
player_tether: bool,
2024-07-02 01:32:51 +03:00
tether_length: u32,
2024-07-10 15:33:29 +03:00
use_constant_seed: bool,
2024-07-02 00:45:30 +03:00
}
impl Default for GameSettings {
fn default() -> Self {
GameSettings {
seed: 0,
debug_mode: false,
world_sync_version: 2,
player_tether: false,
2024-07-02 01:32:51 +03:00
tether_length: 750,
2024-07-10 15:33:29 +03:00
use_constant_seed: false,
2024-07-02 00:45:30 +03:00
}
}
2024-05-01 20:26:37 +03:00
}
enum AppState {
2024-05-23 21:02:39 +03:00
Connect,
ModManager,
2024-07-06 13:44:31 +03:00
Netman {
netman: Arc<net::NetManager>,
noita_launcher: NoitaLauncher,
},
Error {
message: String,
},
2024-05-24 00:41:55 +03:00
SelfUpdate,
2024-06-08 14:25:57 +03:00
LangPick,
2024-05-17 17:55:50 +03:00
}
2024-05-23 21:02:39 +03:00
#[derive(Debug, Serialize, Deserialize)]
struct AppSavedState {
2024-05-02 20:24:27 +03:00
addr: String,
2024-05-27 19:53:29 +03:00
nickname: Option<String>,
2024-05-31 16:40:18 +03:00
times_started: u32,
2024-06-08 14:25:57 +03:00
lang_id: Option<LanguageIdentifier>,
2024-07-02 00:45:30 +03:00
game_settings: GameSettings,
start_game_automatically: bool,
2024-05-23 21:02:39 +03:00
}
impl Default for AppSavedState {
fn default() -> Self {
2024-05-27 16:36:15 +03:00
Self {
2024-05-23 21:02:39 +03:00
addr: "127.0.0.1:5123".to_string(),
2024-05-27 19:53:29 +03:00
nickname: None,
2024-05-31 16:40:18 +03:00
times_started: 0,
2024-06-08 14:25:57 +03:00
lang_id: None,
2024-07-02 00:45:30 +03:00
game_settings: GameSettings::default(),
start_game_automatically: false,
2024-05-24 00:41:55 +03:00
}
2024-05-23 21:02:39 +03:00
}
2024-05-01 20:26:37 +03:00
}
2024-05-23 21:02:39 +03:00
pub struct App {
state: AppState,
modmanager: Modmanager,
2024-05-27 16:08:21 +03:00
steam_state: Result<steam_helper::SteamState, SteamAPIInitError>,
2024-05-23 21:02:39 +03:00
saved_state: AppSavedState,
2024-05-24 00:41:55 +03:00
modmanager_settings: ModmanagerSettings,
self_update: SelfUpdateManager,
2024-06-21 20:18:01 +03:00
show_map_plot: bool,
2024-07-10 15:33:29 +03:00
/// Show settings in netman screen?
show_settings: bool,
lobby_id_field: String,
2024-07-06 13:44:31 +03:00
args: Args,
2024-07-10 15:33:29 +03:00
/// `true` if we haven't started noita automatically yet.
can_start_automatically: bool,
2024-05-23 21:02:39 +03:00
}
const MODMANAGER: &str = "modman";
2024-05-31 16:40:18 +03:00
fn filled_group<R>(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse<R> {
let style = ui.style();
let frame = egui::Frame {
inner_margin: Margin::same(6.0), // same and symmetric looks best in corners when nesting groups
rounding: style.visuals.widgets.noninteractive.rounding,
stroke: style.visuals.widgets.noninteractive.bg_stroke,
fill: Color32::from_rgba_premultiplied(20, 20, 20, 180),
..Default::default()
};
frame.show(ui, add_contents)
}
2024-06-06 15:25:01 +03:00
fn heading_with_underline(ui: &mut Ui, text: impl Into<RichText>) {
ui.vertical_centered_justified(|ui| {
ui.heading(text);
});
ui.separator();
}
fn square_button_text(ui: &mut Ui, text: &str, size: f32) -> egui::Response {
2024-06-08 23:25:40 +03:00
let side = ui.available_width();
ui.add_sized([side, side], Button::new(RichText::new(text).size(size)))
2024-06-08 23:25:40 +03:00
}
2024-05-01 20:26:37 +03:00
impl App {
2024-07-06 13:44:31 +03:00
pub fn new(cc: &eframe::CreationContext<'_>, args: Args) -> Self {
2024-05-31 16:40:18 +03:00
let mut saved_state: AppSavedState = cc
2024-05-27 16:36:15 +03:00
.storage
.and_then(|storage| eframe::get_value(storage, eframe::APP_KEY))
.unwrap_or_default();
let modmanager_settings = cc
.storage
.and_then(|storage| eframe::get_value(storage, MODMANAGER))
2024-05-23 21:02:39 +03:00
.unwrap_or_default();
2024-05-31 16:40:18 +03:00
saved_state.times_started += 1;
2024-06-08 14:25:57 +03:00
let state = if let Some(lang_id) = &saved_state.lang_id {
set_current_locale(lang_id.clone());
AppState::ModManager
} else {
AppState::LangPick
};
2024-05-31 16:40:18 +03:00
egui_extras::install_image_loaders(&cc.egui_ctx);
2024-07-06 14:24:27 +03:00
2024-07-07 11:18:41 +03:00
cc.egui_ctx
.set_zoom_factor(args.ui_zoom_factor.unwrap_or(1.0));
2024-05-23 21:02:39 +03:00
info!("Creating the app...");
Self {
2024-06-08 14:25:57 +03:00
state,
2024-05-23 21:02:39 +03:00
modmanager: Modmanager::default(),
2024-05-27 16:36:15 +03:00
steam_state: steam_helper::SteamState::new(),
saved_state,
2024-05-23 21:02:39 +03:00
modmanager_settings,
2024-05-24 00:41:55 +03:00
self_update: SelfUpdateManager::new(),
2024-06-21 20:18:01 +03:00
show_map_plot: false,
2024-07-10 15:33:29 +03:00
show_settings: false,
lobby_id_field: "".to_string(),
2024-07-06 13:44:31 +03:00
args,
can_start_automatically: false,
2024-05-23 21:02:39 +03:00
}
}
2024-05-27 19:53:29 +03:00
fn get_netman_init(&self) -> NetManagerInit {
let steam_nickname = if let Ok(steam) = &self.steam_state {
Some(steam.get_user_name(steam.get_my_id()))
} else {
None
};
let my_nickname = self.saved_state.nickname.clone().or(steam_nickname);
NetManagerInit { my_nickname }
}
2024-07-06 13:44:31 +03:00
fn change_state_to_netman(&mut self, netman: Arc<net::NetManager>) {
self.state = AppState::Netman {
netman,
noita_launcher: NoitaLauncher::new(
&self.modmanager_settings.game_exe_path,
self.args.launch_cmd.as_ref().map(|x| x.as_str()),
),
};
self.can_start_automatically = true;
2024-07-06 13:44:31 +03:00
}
2024-05-01 20:26:37 +03:00
fn start_server(&mut self) {
2024-05-02 20:24:27 +03:00
let bind_addr = "0.0.0.0:5123".parse().unwrap();
let peer = Peer::host(bind_addr, None).unwrap();
2024-05-27 19:53:29 +03:00
let netman = net::NetManager::new(PeerVariant::Tangled(peer), self.get_netman_init());
2024-05-17 17:55:50 +03:00
self.set_netman_settings(&netman);
netman.clone().start();
2024-07-06 13:44:31 +03:00
self.change_state_to_netman(netman);
2024-05-17 17:55:50 +03:00
}
fn set_netman_settings(&mut self, netman: &Arc<net::NetManager>) {
let mut settings = netman.settings.lock().unwrap();
2024-07-02 00:45:30 +03:00
*settings = self.saved_state.game_settings.clone();
2024-07-10 15:33:29 +03:00
if !self.saved_state.game_settings.use_constant_seed {
2024-05-17 17:55:50 +03:00
settings.seed = rand::random();
} else {
info!("Using constant seed: {}", settings.seed);
2024-06-24 16:34:21 -04:00
}
netman.accept_local.store(true, Ordering::SeqCst);
2024-05-01 20:26:37 +03:00
}
2024-05-02 20:24:27 +03:00
fn start_connect(&mut self, addr: SocketAddr) {
let peer = Peer::connect(addr, None).unwrap();
2024-05-27 19:53:29 +03:00
let netman = net::NetManager::new(PeerVariant::Tangled(peer), self.get_netman_init());
2024-05-17 17:55:50 +03:00
netman.clone().start();
2024-07-06 13:44:31 +03:00
self.change_state_to_netman(netman);
2024-05-17 17:55:50 +03:00
}
fn start_steam_host(&mut self) {
let peer = net::steam_networking::SteamPeer::new_host(
steamworks::LobbyType::Private,
self.steam_state.as_ref().unwrap().client.clone(),
);
2024-05-27 19:53:29 +03:00
let netman = net::NetManager::new(PeerVariant::Steam(peer), self.get_netman_init());
2024-05-17 17:55:50 +03:00
self.set_netman_settings(&netman);
netman.clone().start();
2024-07-06 13:44:31 +03:00
self.change_state_to_netman(netman);
2024-05-17 17:55:50 +03:00
}
fn notify_error(&mut self, error: impl Display) {
self.state = AppState::Error {
message: error.to_string(),
}
}
fn start_steam_connect(&mut self, id: LobbyId) {
let peer = net::steam_networking::SteamPeer::new_connect(
id,
self.steam_state.as_ref().unwrap().client.clone(),
);
2024-07-03 21:34:08 +03:00
let netman = net::NetManager::new(PeerVariant::Steam(peer), self.get_netman_init());
netman.clone().start();
2024-07-06 13:44:31 +03:00
self.change_state_to_netman(netman);
2024-05-01 20:26:37 +03:00
}
2024-05-27 16:36:15 +03:00
2024-05-27 16:08:21 +03:00
fn connect_screen(&mut self, ctx: &egui::Context) {
2024-05-31 15:27:55 +03:00
egui::CentralPanel::default().show(ctx, |ui| {
2024-06-11 00:42:43 +03:00
if self.saved_state.times_started % 20 == 0 {
2024-05-31 16:40:18 +03:00
let image = egui::Image::new(egui::include_image!("../assets/longleg.png"))
2024-06-11 00:42:43 +03:00
.texture_options(TextureOptions::NEAREST);
2024-05-31 16:40:18 +03:00
image.paint_at(ui, ui.ctx().screen_rect());
2024-06-11 00:42:43 +03:00
} else {
draw_bg(ui);
2024-05-31 16:40:18 +03:00
}
2024-06-08 23:25:40 +03:00
let group_shrink = ui.spacing().item_spacing.x * 0.5;
2024-05-31 15:27:55 +03:00
let rect = ui.max_rect();
2024-06-11 00:42:43 +03:00
let (rect, bottom_panel) =
rect.split_top_bottom_at_y(rect.height() - (25.0 + group_shrink * 2.0));
2024-06-08 14:25:57 +03:00
let (rect, right_b_panel) =
2024-06-11 00:42:43 +03:00
rect.split_left_right_at_x(rect.width() - (50.0 + group_shrink * 2.0));
2024-05-31 15:27:55 +03:00
let (settings_rect, right) = rect.split_left_right_at_fraction(0.5);
let (steam_connect_rect, ip_connect_rect) = right.split_top_bottom_at_fraction(0.5);
2024-06-08 14:25:57 +03:00
2024-06-11 00:42:43 +03:00
ui.allocate_ui_at_rect(bottom_panel.shrink(group_shrink), |ui| {
filled_group(ui, |ui| {
ui.set_min_size(ui.available_size());
self.self_update.display_version(ui);
if self.self_update.request_update {
self.state = AppState::SelfUpdate;
}
});
});
2024-06-08 23:25:40 +03:00
ui.allocate_ui_at_rect(right_b_panel.shrink(group_shrink), |ui| {
2024-05-31 16:40:18 +03:00
filled_group(ui, |ui| {
2024-05-31 15:27:55 +03:00
ui.set_min_size(ui.available_size());
2024-06-06 15:25:01 +03:00
2024-06-09 17:58:24 +03:00
let lang_label = self
.saved_state
.lang_id
.clone()
.unwrap_or_default()
.language;
if square_button_text(ui, &lang_label.to_string().to_uppercase(), 21.0)
2024-06-08 23:25:40 +03:00
.on_hover_text(tr("button_set_lang"))
.clicked()
{
self.state = AppState::LangPick;
}
if square_button_text(ui, "Discord server", 10.0).clicked() {
ctx.open_url(OpenUrl::new_tab("https://discord.gg/uAK7utvVWN"));
}
2024-06-08 23:25:40 +03:00
let secret_active = ui.input(|i| i.modifiers.ctrl && i.key_down(Key::D));
if secret_active && ui.button("reset all data").clicked() {
self.saved_state = Default::default();
self.modmanager_settings = Default::default();
2024-06-08 14:25:57 +03:00
self.state = AppState::LangPick;
}
})
});
2024-06-08 23:25:40 +03:00
ui.allocate_ui_at_rect(settings_rect.shrink(group_shrink), |ui| {
2024-06-08 14:25:57 +03:00
filled_group(ui, |ui| {
ui.set_min_size(ui.available_size());
2024-07-10 15:33:29 +03:00
self.show_game_settings(ui);
2024-05-31 15:27:55 +03:00
});
});
2024-06-08 23:25:40 +03:00
ui.allocate_ui_at_rect(steam_connect_rect.shrink(group_shrink), |ui| {
2024-05-31 16:40:18 +03:00
filled_group(ui, |ui| {
2024-05-31 15:27:55 +03:00
ui.set_min_size(ui.available_size());
2024-06-06 15:25:01 +03:00
2024-06-08 14:25:57 +03:00
heading_with_underline(ui, tr("connect_steam"));
2024-06-06 15:25:01 +03:00
2024-05-31 15:27:55 +03:00
match &self.steam_state {
Ok(_) => {
2024-06-08 14:25:57 +03:00
if ui.button(tr("connect_steam_create")).clicked() {
2024-05-31 15:27:55 +03:00
self.start_steam_host();
}
2024-06-08 14:25:57 +03:00
if ui.button(tr("connect_steam_connect")).clicked() {
2024-05-31 15:27:55 +03:00
let id = ClipboardProvider::new()
.and_then(|mut ctx: ClipboardContext| ctx.get_contents());
2024-05-17 17:55:50 +03:00
match id {
2024-05-31 15:27:55 +03:00
Ok(id) => {
self.connect_to_steam_lobby(id);
2024-05-31 15:27:55 +03:00
}
2024-05-17 17:55:50 +03:00
Err(error) => self.notify_error(error),
}
}
if cfg!(target_os = "linux") {
ui.add_space(30.0);
ui.label(tr("connect_steam_workaround_label"));
ui.text_edit_singleline(&mut self.lobby_id_field);
if ui.button(tr("connect_steam_connect_2")).clicked() {
self.connect_to_steam_lobby(self.lobby_id_field.clone());
}
}
2024-05-31 15:27:55 +03:00
}
Err(err) => {
ui.label(format!("Could not init steam networking: {}", err));
2024-05-17 17:55:50 +03:00
}
}
2024-05-31 15:27:55 +03:00
});
});
2024-06-08 23:25:40 +03:00
ui.allocate_ui_at_rect(ip_connect_rect.shrink(group_shrink), |ui| {
2024-05-31 16:40:18 +03:00
filled_group(ui, |ui| {
2024-05-31 15:27:55 +03:00
ui.set_min_size(ui.available_size());
2024-06-06 15:25:01 +03:00
2024-06-08 14:25:57 +03:00
heading_with_underline(ui, tr("connect_ip"));
2024-06-06 15:25:01 +03:00
2024-06-09 17:58:24 +03:00
ui.label(tr("ip_note"));
if ui.button(tr("ip_host")).clicked() {
2024-05-31 15:27:55 +03:00
self.start_server();
}
ui.text_edit_singleline(&mut self.saved_state.addr);
let addr = self.saved_state.addr.parse();
ui.add_enabled_ui(addr.is_ok(), |ui| {
2024-06-09 17:58:24 +03:00
if ui.button(tr("ip_connect")).clicked() {
2024-05-31 15:27:55 +03:00
if let Ok(addr) = addr {
self.start_connect(addr);
}
}
});
});
});
2024-05-27 16:08:21 +03:00
});
}
2024-07-10 15:33:29 +03:00
fn show_game_settings(&mut self, ui: &mut Ui) {
heading_with_underline(ui, tr("connect_settings"));
ui.label(tr("connect_settings_debug"));
ui.checkbox(
&mut self.saved_state.game_settings.debug_mode,
tr("connect_settings_debug_en"),
);
ui.checkbox(
&mut self.saved_state.game_settings.use_constant_seed,
tr("connect_settings_debug_fixed_seed"),
);
ui.horizontal(|ui| {
ui.label(tr("connect_settings_seed"));
ui.add(DragValue::new(&mut self.saved_state.game_settings.seed));
});
ui.add_space(20.0);
ui.label(tr("connect_settings_wsv"));
ui.horizontal(|ui| {
ui.radio_value(
&mut self.saved_state.game_settings.world_sync_version,
1,
"v1",
);
ui.radio_value(
&mut self.saved_state.game_settings.world_sync_version,
2,
"v2",
);
});
ui.add_space(20.0);
ui.label(tr("connect_settings_player_tether_desc"));
ui.checkbox(
&mut self.saved_state.game_settings.player_tether,
tr("connect_settings_player_tether"),
);
ui.add(
Slider::new(&mut self.saved_state.game_settings.tether_length, 10..=5000)
.text(tr("connect_settings_player_tether_length")),
);
heading_with_underline(ui, tr("connect_settings_local"));
ui.checkbox(
&mut self.saved_state.start_game_automatically,
tr("connect_settings_autostart"),
);
}
fn connect_to_steam_lobby(&mut self, lobby_id: String) {
let id = lobby_id.trim().parse().map(LobbyId::from_raw);
match id {
Ok(id) => self.start_steam_connect(id),
Err(_error) => self.notify_error(tr("connect_steam_connect_invalid_lobby_id")),
}
}
2024-05-27 16:08:21 +03:00
}
2024-06-11 00:42:43 +03:00
fn draw_bg(ui: &mut Ui) {
let image = egui::Image::new(egui::include_image!("../assets/noita_ew_logo_sq.webp"))
.texture_options(TextureOptions::NEAREST);
let rect = ui.ctx().screen_rect();
let aspect_ratio = 1.0;
let new_height = f32::max(rect.width() * aspect_ratio, rect.height());
let new_width = new_height / aspect_ratio;
let rect = Rect::from_center_size(rect.center(), Vec2::new(new_width, new_height));
image.paint_at(ui, rect);
}
2024-05-27 16:08:21 +03:00
impl eframe::App for App {
2024-06-08 23:25:40 +03:00
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
2024-06-21 20:18:01 +03:00
ctx.request_repaint_after(Duration::from_millis(500));
2024-07-06 13:44:31 +03:00
match &mut self.state {
2024-05-27 16:08:21 +03:00
AppState::Connect => {
self.connect_screen(ctx);
2024-05-01 20:26:37 +03:00
}
2024-07-06 13:44:31 +03:00
AppState::Netman {
netman,
noita_launcher,
} => {
2024-07-03 21:34:08 +03:00
if let ExtraPeerState::CouldNotConnect(err) = netman.peer.state() {
self.notify_error(err);
return;
}
let stopped = netman.stopped.load(Ordering::Relaxed);
let accept_local = netman.accept_local.load(Ordering::Relaxed);
let local_connected = netman.local_connected.load(Ordering::Relaxed);
2024-05-27 16:36:15 +03:00
egui::TopBottomPanel::top("noita_status").show(ctx, |ui| {
ui.add_space(3.0);
if accept_local {
if local_connected {
2024-06-09 19:55:07 +03:00
ui.colored_label(Color32::GREEN, tr("noita_connected"));
2024-05-27 16:36:15 +03:00
} else {
2024-06-09 17:58:24 +03:00
ui.colored_label(Color32::YELLOW, tr("noita_can_connect"));
2024-05-27 16:08:21 +03:00
}
} else {
2024-06-09 17:58:24 +03:00
ui.label(tr("noita_not_yet"));
2024-05-27 16:08:21 +03:00
}
});
2024-05-27 16:36:15 +03:00
egui::SidePanel::left("players")
.resizable(false)
.exact_width(200.0)
.show(ctx, |ui| {
ui.add_space(3.0);
if netman.peer.is_steam() {
let steam = self.steam_state.as_mut().expect(
"steam should be available, as we are using steam networking",
);
ScrollArea::vertical().auto_shrink(false).show(ui, |ui| {
for peer in netman.peer.iter_peer_ids() {
let role = peer_role(peer, netman);
let username = steam.get_user_name(peer.into());
let avatar = steam.get_avatar(ctx, peer.into());
if let Some(avatar) = avatar {
avatar.display_with_labels(ui, &username, &role);
ui.add_space(5.0);
} else {
ui.label(&username);
}
2024-05-27 16:36:15 +03:00
}
});
2024-05-27 16:36:15 +03:00
} else {
for peer in netman.peer.iter_peer_ids() {
ui.label(peer.to_string());
}
}
});
2024-05-01 20:26:37 +03:00
egui::CentralPanel::default().show(ctx, |ui| {
if stopped {
ui.colored_label(Color32::LIGHT_RED, "Netmanager thread has stopped");
if let Some(err) = netman.error.lock().unwrap().as_ref() {
ui.label("With the following error:");
ui.label(err.to_string());
}
ui.separator();
}
2024-05-27 16:36:15 +03:00
2024-05-27 16:08:21 +03:00
if netman.peer.is_steam() {
if let Some(id) = netman.peer.lobby_id() {
2024-07-08 12:22:13 +03:00
if cfg!(target_os = "linux") {
ui.label(id.raw().to_string());
}
2024-06-09 17:58:24 +03:00
if ui.button(tr("netman_save_lobby")).clicked() {
2024-05-27 16:08:21 +03:00
let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap();
let _ = ctx.set_contents(id.raw().to_string());
}
2024-05-17 17:55:50 +03:00
}
2024-06-21 19:06:40 +03:00
} else {
2024-07-03 21:34:08 +03:00
ui.label(format!("Peer state: {:?}", netman.peer.state()));
2024-05-01 20:26:37 +03:00
}
ui.add_space(15.0);
if accept_local && !local_connected {
match noita_launcher.launch_token() {
LaunchTokenResult::Ok(mut token) => {
let start_auto = self.can_start_automatically && self.saved_state.start_game_automatically;
if start_auto || ui.button(tr("launcher_start_game")).clicked() {
info!("Starting the game now");
token.start_game();
self.can_start_automatically = false;
}
},
LaunchTokenResult::AlreadyStarted => {
ui.label(tr("launcher_already_started"));
},
LaunchTokenResult::CantStart => {
ui.label(tr("launcher_no_command"));
ui.label(tr("launcher_no_command_2"));
ui.label(tr("launcher_no_command_3"));
},
}
} else {
ui.label(tr("launcher_only_when_awaiting"));
2024-07-06 13:44:31 +03:00
}
ui.add_space(15.0);
2024-07-06 13:44:31 +03:00
2024-07-10 15:33:29 +03:00
if ui.button(tr("netman_show_settings")).clicked() {
self.show_settings = true;
}
2024-06-21 20:18:01 +03:00
if self.show_map_plot {
2024-06-21 22:01:53 +03:00
let build_fn = |plot: &mut PlotUi| {
netman.world_info.with_player_infos(|peer, info| {
let username = if netman.peer.is_steam() {
let steam = self.steam_state.as_mut().expect(
"steam should be available, as we are using steam networking",
);
steam.get_user_name(peer.into())
} else {
peer.to_hex()
};
plot.text(Text::new(PlotPoint::new(info.x, -info.y), username).highlight(true))
});
};
Plot::new("map").data_aspect(1.0).show(ui, build_fn);
} else if ui.button("Show debug plot").clicked() {
self.show_map_plot = true;
2024-06-21 20:18:01 +03:00
}
2024-05-11 18:06:48 +03:00
});
2024-07-10 15:33:29 +03:00
let mut show = self.show_settings;
let netman = netman.clone();
egui::Window::new(tr("connect_settings")).open(&mut show).show(ctx, |ui| {
self.show_game_settings(ui);
if ui.button(tr("netman_apply_settings")).clicked() {
*netman.pending_settings.lock().unwrap() = self.saved_state.game_settings.clone();
}
});
self.show_settings = show;
2024-05-01 20:26:37 +03:00
}
2024-05-17 17:55:50 +03:00
AppState::Error { message } => {
let add_contents = |ui: &mut Ui| {
ui.heading(tr("error_occured"));
2024-07-06 13:44:31 +03:00
ui.label(&*message);
ui.button(tr("button_back")).clicked()
};
if egui::CentralPanel::default().show(ctx, add_contents).inner {
2024-05-23 21:02:39 +03:00
self.state = AppState::Connect;
2024-05-17 17:55:50 +03:00
}
}
2024-05-23 21:02:39 +03:00
AppState::ModManager => {
2024-06-11 00:42:43 +03:00
egui::CentralPanel::default().show(ctx, draw_bg);
2024-06-09 17:16:33 +03:00
egui::Window::new(tr("modman"))
2024-05-27 16:36:15 +03:00
.auto_sized()
.anchor(Align2::CENTER_CENTER, [0.0, 0.0])
2024-05-23 21:02:39 +03:00
.show(ctx, |ui| {
2024-07-09 18:05:16 +03:00
ui.set_max_width(600.0);
2024-05-27 16:36:15 +03:00
self.modmanager.update(
ctx,
ui,
&mut self.modmanager_settings,
self.steam_state.as_mut().ok(),
)
2024-05-23 21:02:39 +03:00
});
2024-05-27 16:36:15 +03:00
if self.modmanager.is_done() {
self.state = AppState::Connect;
}
}
2024-05-24 00:41:55 +03:00
AppState::SelfUpdate => {
2024-06-11 00:42:43 +03:00
egui::CentralPanel::default().show(ctx, draw_bg);
2024-06-09 17:58:24 +03:00
egui::Window::new(tr("selfupdate"))
2024-05-27 16:36:15 +03:00
.auto_sized()
.anchor(Align2::CENTER_CENTER, [0.0, 0.0])
2024-05-24 00:41:55 +03:00
.show(ctx, |ui| {
2024-07-09 18:05:16 +03:00
ui.set_max_width(600.0);
2024-05-24 00:41:55 +03:00
self.self_update.self_update(ui);
});
2024-05-27 16:36:15 +03:00
}
2024-06-08 14:25:57 +03:00
AppState::LangPick => {
2024-06-11 00:42:43 +03:00
egui::CentralPanel::default().show(ctx, draw_bg);
2024-06-08 14:25:57 +03:00
egui::Window::new(tr("lang_picker"))
.auto_sized()
.anchor(Align2::CENTER_CENTER, [0.0, 0.0])
.show(ctx, |ui| {
for lang in &LANGS {
ui.set_max_width(200.0);
ui.vertical_centered_justified(|ui| {
if ui.button(lang.name()).clicked() {
self.saved_state.lang_id = Some(lang.id());
set_current_locale(lang.id())
}
});
}
if ui.button(tr("button_confirm")).clicked() {
self.state = AppState::ModManager;
}
});
}
2024-05-01 20:26:37 +03:00
};
}
2024-05-23 21:02:39 +03:00
fn save(&mut self, storage: &mut dyn eframe::Storage) {
eframe::set_value(storage, eframe::APP_KEY, &self.saved_state);
eframe::set_value(storage, MODMANAGER, &self.modmanager_settings);
}
}
2024-05-27 16:08:21 +03:00
2024-06-09 17:16:33 +03:00
fn peer_role(peer: net::omni::OmniPeerId, netman: &Arc<net::NetManager>) -> String {
2024-05-27 16:08:21 +03:00
if peer == netman.peer.host_id() {
2024-06-09 17:16:33 +03:00
tr("player_host")
} else if Some(peer) == netman.peer.my_id() {
tr("player_me")
2024-05-27 16:08:21 +03:00
} else {
tr("player_player")
2024-05-27 16:08:21 +03:00
}
}