|
| 1 | +use std::{mem::transmute, sync::Arc, time::Duration}; |
| 2 | + |
| 3 | +use game_detector::InstalledGame; |
| 4 | +use windows::Win32::{ |
| 5 | + Foundation::DXGI_STATUS_OCCLUDED, |
| 6 | + Graphics::{ |
| 7 | + Direct3D11::ID3D11Texture2D, |
| 8 | + Dxgi::{ |
| 9 | + Common::DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_PRESENT_TEST, DXGI_SWAP_EFFECT_SEQUENTIAL, |
| 10 | + }, |
| 11 | + }, |
| 12 | +}; |
| 13 | +use winit::{ |
| 14 | + dpi::PhysicalSize, |
| 15 | + event::{Event, WindowEvent}, |
| 16 | + event_loop::{ControlFlow, EventLoop}, |
| 17 | + platform::run_return::EventLoopExtRunReturn, |
| 18 | +}; |
| 19 | + |
| 20 | +use crate::{ |
| 21 | + icons::{ICON_CONTROLLER, ICON_MICROSOFT, ICON_STEAM}, |
| 22 | + overlays::{ |
| 23 | + big_button::BigButton, |
| 24 | + gui::{GuiManager, PreDrawResult}, |
| 25 | + }, |
| 26 | + render::DeviceContextSwapchain, |
| 27 | + resources::Resources, |
| 28 | +}; |
| 29 | + |
| 30 | +/// Creates a temporary window with egui to select a game installation |
| 31 | +/// This function should not be called in another render loop, as it will hang until this function completes |
| 32 | +pub fn select_game_installation(event_loop: &mut EventLoop<()>) -> anyhow::Result<String> { |
| 33 | + let window = winit::window::WindowBuilder::new() |
| 34 | + .with_title("Alkahest") |
| 35 | + .with_inner_size(PhysicalSize::new(320, 320)) |
| 36 | + .with_min_inner_size(PhysicalSize::new(320, 480)) |
| 37 | + .build(event_loop)?; |
| 38 | + |
| 39 | + let window = Arc::new(window); |
| 40 | + |
| 41 | + let dcs = Arc::new(DeviceContextSwapchain::create(&window)?); |
| 42 | + let mut gui = GuiManager::create(&window, dcs.clone()); |
| 43 | + let mut empty_resources = Resources::default(); |
| 44 | + |
| 45 | + let mut present_parameters = 0; |
| 46 | + let mut selected_path = Err(anyhow::anyhow!("No game installation selected")); |
| 47 | + |
| 48 | + let mut installations = game_detector::find_all_games(); |
| 49 | + installations.retain(|i| match i { |
| 50 | + InstalledGame::Steam(a) => a.appid == 1085660, |
| 51 | + InstalledGame::EpicGames(m) => m.display_name == "Destiny 2", |
| 52 | + InstalledGame::MicrosoftStore(p) => p.app_name == "Destiny2PCbasegame", |
| 53 | + _ => false, |
| 54 | + }); |
| 55 | + |
| 56 | + event_loop.run_return(|event, _, control_flow| match &event { |
| 57 | + Event::WindowEvent { event, .. } => { |
| 58 | + let _ = gui.handle_event(event); |
| 59 | + |
| 60 | + match event { |
| 61 | + WindowEvent::Resized(new_dims) => unsafe { |
| 62 | + let _ = gui |
| 63 | + .renderer |
| 64 | + .resize_buffers(transmute(&dcs.swap_chain), || { |
| 65 | + *dcs.swapchain_target.write() = None; |
| 66 | + dcs.swap_chain |
| 67 | + .ResizeBuffers( |
| 68 | + 1, |
| 69 | + new_dims.width, |
| 70 | + new_dims.height, |
| 71 | + DXGI_FORMAT_B8G8R8A8_UNORM, |
| 72 | + 0, |
| 73 | + ) |
| 74 | + .expect("Failed to resize swapchain"); |
| 75 | + |
| 76 | + let bb: ID3D11Texture2D = dcs.swap_chain.GetBuffer(0).unwrap(); |
| 77 | + |
| 78 | + let new_rtv = dcs.device.CreateRenderTargetView(&bb, None).unwrap(); |
| 79 | + |
| 80 | + dcs.context() |
| 81 | + .OMSetRenderTargets(Some(&[Some(new_rtv.clone())]), None); |
| 82 | + |
| 83 | + *dcs.swapchain_target.write() = Some(new_rtv); |
| 84 | + |
| 85 | + transmute(0i32) |
| 86 | + }) |
| 87 | + .unwrap(); |
| 88 | + }, |
| 89 | + WindowEvent::CloseRequested => { |
| 90 | + *control_flow = ControlFlow::Exit; |
| 91 | + } |
| 92 | + _ => (), |
| 93 | + } |
| 94 | + } |
| 95 | + Event::RedrawRequested(..) => { |
| 96 | + gui.draw_frame( |
| 97 | + window.clone(), |
| 98 | + &mut empty_resources, |
| 99 | + |ctx, _resources| { |
| 100 | + egui::CentralPanel::default().show(ctx, |ui| { |
| 101 | + ui.heading("Select Destiny 2 installation"); |
| 102 | + for i in &installations { |
| 103 | + let (icon, store_name, path) = match i { |
| 104 | + InstalledGame::Steam(a) => { |
| 105 | + (ICON_STEAM, "Steam", a.game_path.clone()) |
| 106 | + } |
| 107 | + InstalledGame::EpicGames(e) => { |
| 108 | + (ICON_CONTROLLER, "Epic Games", e.install_location.clone()) |
| 109 | + } |
| 110 | + InstalledGame::MicrosoftStore(p) => { |
| 111 | + (ICON_MICROSOFT, "Microsoft Store", p.path.clone()) |
| 112 | + } |
| 113 | + _ => continue, |
| 114 | + }; |
| 115 | + |
| 116 | + if BigButton::new(icon, store_name) |
| 117 | + .with_subtext(&path) |
| 118 | + .full_width() |
| 119 | + .ui(ui) |
| 120 | + .clicked() |
| 121 | + { |
| 122 | + selected_path = Ok(path.clone()); |
| 123 | + *control_flow = ControlFlow::Exit; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + // if BigButton::new(ICON_FOLDER_OPEN, "Browse") |
| 128 | + // .full_width() |
| 129 | + // .ui(ui) |
| 130 | + // .clicked() |
| 131 | + // { |
| 132 | + // let dialog = native_dialog::FileDialog::new() |
| 133 | + // .set_title("Select Destiny 2 packages directory") |
| 134 | + // .show_open_single_dir()?; |
| 135 | + // } |
| 136 | + }); |
| 137 | + |
| 138 | + PreDrawResult::Continue |
| 139 | + }, |
| 140 | + |_, _| {}, |
| 141 | + ); |
| 142 | + |
| 143 | + unsafe { |
| 144 | + if dcs |
| 145 | + .swap_chain |
| 146 | + .Present(DXGI_SWAP_EFFECT_SEQUENTIAL.0 as _, present_parameters) |
| 147 | + == DXGI_STATUS_OCCLUDED |
| 148 | + { |
| 149 | + present_parameters = DXGI_PRESENT_TEST; |
| 150 | + std::thread::sleep(Duration::from_millis(50)); |
| 151 | + } else { |
| 152 | + present_parameters = 0; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + Event::MainEventsCleared => { |
| 157 | + window.request_redraw(); |
| 158 | + } |
| 159 | + _ => (), |
| 160 | + }); |
| 161 | + |
| 162 | + selected_path |
| 163 | +} |
0 commit comments