fn reload_plugin_host_state(
    plugin_host: Addr<TestPluginHost>,
    actor_system_thread: &ActorSystemThread,
) -> (HostContext, Command<Message>) {
    log::info!("Reloading plugin-host settings from disk");
    let home_dir =
        dirs::home_dir().expect("Failed to get user HOME directory. App will fail to work.");
    let home_config_dir = home_dir.join(".plugin-host-gui");
    std::fs::create_dir_all(&home_config_dir).expect("Failed to create configuration directory.");
    let audio_io_state_storage_path = home_config_dir
        .join("audio-io-state.json")
        .to_str()
        .unwrap()
        .to_string();
    let storage_config = StorageConfig {
        audio_io_state_storage_path,
    };
    let audio_io_service = {
        let plugin_host = plugin_host.clone();
        actor_system_thread.spawn_result(async move {
            let audio_thread = plugin_host
                .send(audio_io::GetAudioThreadMessage)
                .await
                .unwrap();
            AudioIOService::new(audio_thread, storage_config).start()
        })
    };
    let host_options_storage_path = home_config_dir
        .join("audio-thread-config.json")
        .to_str()
        .unwrap()
        .to_string();
    let host_options_service = HostOptionsService::new(host_options_storage_path);
    let host_state = host_options_service.fetch().unwrap_or_default();

    let command = {
        let host_state = host_state.clone();
        let audio_io_service = audio_io_service.clone();

        Command::perform(
            async move {
                if let Err(err) = audio_io_service.send(audio_io_service::ReloadMessage).await {
                    log::error!("Failed to reload audio options: {}", err);
                } else {
                    log::info!("Loaded audio options");
                }

                log::info!("Reloading audio plugin & file in background thread");
                if let Some(path) = &host_state.audio_input_file_path {
                    plugin_host
                        .send(SetAudioFilePathMessage(path.into()))
                        .await
                        .unwrap()
                        .map_err(|err| {
                            log::error!("Failed to set audio input {:?}", err);
                            err
                        })?;
                }

                if let Some(path) = &host_state.plugin_path {
                    plugin_host
                        .send(LoadPluginMessage {
                            plugin_path: path.into(),
                        })
                        .await
                        .unwrap()
                        .map_err(|err| {
                            log::error!("Failed to set audio input {:?}", err);
                            err
                        })?;

                    let audio_file: Shared<AudioFileProcessorHandle> =
                        ProcessorHandleRegistry::current()
                            .get("audio-file")
                            .unwrap();
                    audio_file.stop();
                }
                Ok(())
            },
            |result: Result<(), AudioHostPluginLoadError>| match result {
                Err(err) => Message::SetStatus(StatusBar::new(
                    format!("Failed to load plugin: {}", err),
                    status_bar::State::Error,
                )),
                Ok(_) => Message::ReadyForPlayback,
            },
        )
    };

    (
        HostContext {
            audio_io_service,
            host_options_service,
            host_state,
        },
        command,
    )
}
/// Load plugin-host state from JSON files when it starts. Do file decoding on a background thread.
Load plugin-host state from JSON files when it starts. Do file decoding on a background thread.
Load plugin - host state from JSON files when it starts . Do file decoding on a background thread .
fn reload_plugin_host_state(
    plugin_host: Addr<TestPluginHost>,
    actor_system_thread: &ActorSystemThread,
) -> (HostContext, Command<Message>) {
    log::info!("Reloading plugin-host settings from disk");
    let home_dir =
        dirs::home_dir().expect("Failed to get user HOME directory. App will fail to work.");
    let home_config_dir = home_dir.join(".plugin-host-gui");
    std::fs::create_dir_all(&home_config_dir).expect("Failed to create configuration directory.");
    let audio_io_state_storage_path = home_config_dir
        .join("audio-io-state.json")
        .to_str()
        .unwrap()
        .to_string();
    let storage_config = StorageConfig {
        audio_io_state_storage_path,
    };
    let audio_io_service = {
        let plugin_host = plugin_host.clone();
        actor_system_thread.spawn_result(async move {
            let audio_thread = plugin_host
                .send(audio_io::GetAudioThreadMessage)
                .await
                .unwrap();
            AudioIOService::new(audio_thread, storage_config).start()
        })
    };
    let host_options_storage_path = home_config_dir
        .join("audio-thread-config.json")
        .to_str()
        .unwrap()
        .to_string();
    let host_options_service = HostOptionsService::new(host_options_storage_path);
    let host_state = host_options_service.fetch().unwrap_or_default();
    let command = {
        let host_state = host_state.clone();
        let audio_io_service = audio_io_service.clone();
        Command::perform(
            async move {
                if let Err(err) = audio_io_service.send(audio_io_service::ReloadMessage).await {
                    log::error!("Failed to reload audio options: {}", err);
                } else {
                    log::info!("Loaded audio options");
                }
                log::info!("Reloading audio plugin & file in background thread");
                if let Some(path) = &host_state.audio_input_file_path {
                    plugin_host
                        .send(SetAudioFilePathMessage(path.into()))
                        .await
                        .unwrap()
                        .map_err(|err| {
                            log::error!("Failed to set audio input {:?}", err);
                            err
                        })?;
                }
                if let Some(path) = &host_state.plugin_path {
                    plugin_host
                        .send(LoadPluginMessage {
                            plugin_path: path.into(),
                        })
                        .await
                        .unwrap()
                        .map_err(|err| {
                            log::error!("Failed to set audio input {:?}", err);
                            err
                        })?;
                    let audio_file: Shared<AudioFileProcessorHandle> =
                        ProcessorHandleRegistry::current()
                            .get("audio-file")
                            .unwrap();
                    audio_file.stop();
                }
                Ok(())
            },
            |result: Result<(), AudioHostPluginLoadError>| match result {
                Err(err) => Message::SetStatus(StatusBar::new(
                    format!("Failed to load plugin: {}", err),
                    status_bar::State::Error,
                )),
                Ok(_) => Message::ReadyForPlayback,
            },
        )
    };
    (
        HostContext {
            audio_io_service,
            host_options_service,
            host_state,
        },
        command,
    )
}
fn reload_plugin_host_state ( plugin_host : Addr < TestPluginHost > , actor_system_thread : & ActorSystemThread , ) -> ( HostContext , Command < Message > ) { log :: info ! ( " " ) ; let home_dir = dirs :: home_dir ( ) . expect ( " " ) ; let home_config_dir = home_dir . join ( " " ) ; std :: fs :: create_dir_all ( & home_config_dir ) . expect ( " " ) ; let audio_io_state_storage_path = home_config_dir . join ( " " ) . to_str ( ) . unwrap ( ) . to_string ( ) ; let storage_config = StorageConfig { audio_io_state_storage_path , } ; let audio_io_service = { let plugin_host = plugin_host . clone ( ) ; actor_system_thread . spawn_result ( async move { let audio_thread = plugin_host . send ( audio_io :: GetAudioThreadMessage ) . await . unwrap ( ) ; AudioIOService :: new ( audio_thread , storage_config ) . start ( ) } ) } ; let host_options_storage_path = home_config_dir . join ( " " ) . to_str ( ) . unwrap ( ) . to_string ( ) ; let host_options_service = HostOptionsService :: new ( host_options_storage_path ) ; let host_state = host_options_service . fetch ( ) . unwrap_or_default ( ) ; let command = { let host_state = host_state . clone ( ) ; let audio_io_service = audio_io_service . clone ( ) ; Command :: perform ( async move { if let Err ( err ) = audio_io_service . send ( audio_io_service :: ReloadMessage ) . await { log :: error ! ( " " err ) ; } else { log :: info ! ( " " ) ; } log :: info ! ( " " ) ; if let Some ( path ) = & host_state . audio_input_file_path { plugin_host . send ( SetAudioFilePathMessage ( path . into ( ) ) ) . await . unwrap ( ) . map_err ( | err | { log :: error ! ( " " err ) ; err } ) ? ; } if let Some ( path ) = & host_state . plugin_path { plugin_host . send ( LoadPluginMessage { plugin_path : path . into ( ) , } ) . await . unwrap ( ) . map_err ( | err | { log :: error ! ( " " err ) ; err } ) ? ; let audio_file : Shared < AudioFileProcessorHandle > = ProcessorHandleRegistry :: current ( ) . get ( " " ) . unwrap ( ) ; audio_file . stop ( ) ; } Ok ( ( ) ) } , | result : Result < ( ) , AudioHostPluginLoadError > | match result { Err ( err ) => Message :: SetStatus ( StatusBar :: new ( format ! ( " " err ) , status_bar :: State :: Error , ) ) , Ok ( _ ) => Message :: ReadyForPlayback , } , ) } ; ( HostContext { audio_io_service , host_options_service , host_state , } , command , ) }
Load plugin-host state from JSON files when it starts.
Load plugin - host state from JSON files when it starts .
plugin_host
Addr<TestPluginHost>
actor_system_thread
&ActorSystemThread
plugin_host
Addr<TestPluginHost>
actor_system_thread
&ActorSystemThread