Skip to content

Commit

Permalink
#842 Prevent ReaLearn from replacing itself, avoiding a potential crash
Browse files Browse the repository at this point in the history
  • Loading branch information
helgoboss committed May 23, 2023
1 parent b350962 commit 8beee7b
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 82 deletions.
12 changes: 6 additions & 6 deletions main/src/domain/backbone_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ use base::{
use crate::domain::{
AdditionalFeedbackEvent, ClipMatrixRef, ControlInput, DeviceControlInput, DeviceFeedbackOutput,
FeedbackOutput, InstanceId, InstanceState, InstanceStateChanged, NormalAudioHookTask,
NormalRealTimeTask, QualifiedClipMatrixEvent, RealearnClipMatrix, RealearnSourceState,
RealearnTargetState, ReaperTarget, ReaperTargetType, SafeLua, SharedInstanceState,
WeakInstanceState,
NormalRealTimeTask, ProcessorContext, QualifiedClipMatrixEvent, RealearnClipMatrix,
RealearnSourceState, RealearnTargetState, ReaperTarget, ReaperTargetType, SafeLua,
SharedInstanceState, WeakInstanceState,
};
use enum_iterator::IntoEnumIterator;
use pot::{PotFavorites, PotFilterExcludes};

use once_cell::sync::Lazy;
use playtime_clip_engine::rt::WeakMatrix;
use realearn_api::persistence::TargetTouchCause;
use reaper_high::{Reaper, Track};
use reaper_high::Reaper;
use std::cell::{Cell, Ref, RefCell, RefMut};
use std::collections::{HashMap, HashSet};
use std::hash::Hash;
Expand Down Expand Up @@ -243,19 +243,19 @@ impl BackboneState {
pub fn create_instance(
&self,
id: InstanceId,
processor_context: ProcessorContext,
instance_feedback_event_sender: SenderToNormalThread<InstanceStateChanged>,
clip_matrix_event_sender: SenderToNormalThread<QualifiedClipMatrixEvent>,
audio_hook_task_sender: SenderToRealTimeThread<NormalAudioHookTask>,
real_time_processor_sender: SenderToRealTimeThread<NormalRealTimeTask>,
this_track: Option<Track>,
) -> SharedInstanceState {
let instance_state = InstanceState::new(
id,
processor_context,
instance_feedback_event_sender,
clip_matrix_event_sender,
audio_hook_task_sender,
real_time_processor_sender,
this_track,
);
let shared_instance_state = Rc::new(RefCell::new(instance_state));
self.instance_states
Expand Down
31 changes: 21 additions & 10 deletions main/src/domain/instance_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use std::rc::{Rc, Weak};
use std::sync::RwLock;

use enum_map::EnumMap;
use reaper_high::{Fx, Track};
use reaper_high::Fx;
use rxrust::prelude::*;

use crate::base::Prop;
use crate::domain::{
AnyThreadBackboneState, BackboneState, Compartment, FxDescriptor, FxInputClipRecordTask,
GlobalControlAndFeedbackState, GroupId, HardwareInputClipRecordTask, InstanceId, MappingId,
MappingSnapshotContainer, NormalAudioHookTask, NormalRealTimeTask, QualifiedMappingId, Tag,
TagScope, TrackDescriptor, VirtualMappingSnapshotIdForLoad,
MappingSnapshotContainer, NormalAudioHookTask, NormalRealTimeTask, ProcessorContext,
QualifiedMappingId, Tag, TagScope, TrackDescriptor, VirtualMappingSnapshotIdForLoad,
};
use base::{tracing_debug, NamedChannelSender, SenderToNormalThread, SenderToRealTimeThread};
use playtime_clip_engine::base::{
Expand Down Expand Up @@ -41,6 +41,7 @@ pub type RealearnClipMatrix = Matrix<RealearnClipMatrixHandler>;
#[derive(Debug)]
pub struct InstanceState {
instance_id: InstanceId,
processor_context: ProcessorContext,
/// Owned clip matrix or reference to a clip matrix owned by another instance.
///
/// Persistent.
Expand All @@ -49,7 +50,6 @@ pub struct InstanceState {
clip_matrix_event_sender: SenderToNormalThread<QualifiedClipMatrixEvent>,
audio_hook_task_sender: SenderToRealTimeThread<NormalAudioHookTask>,
real_time_processor_sender: SenderToRealTimeThread<NormalRealTimeTask>,
this_track: Option<Track>,
slot_contents_changed_subject: LocalSubject<'static, (), ()>,
/// Which mappings are in which group.
///
Expand Down Expand Up @@ -206,20 +206,20 @@ pub struct MappingInfo {
impl InstanceState {
pub(super) fn new(
instance_id: InstanceId,
processor_context: ProcessorContext,
instance_feedback_event_sender: SenderToNormalThread<InstanceStateChanged>,
clip_matrix_event_sender: SenderToNormalThread<QualifiedClipMatrixEvent>,
audio_hook_task_sender: SenderToRealTimeThread<NormalAudioHookTask>,
real_time_processor_sender: SenderToRealTimeThread<NormalRealTimeTask>,
this_track: Option<Track>,
) -> Self {
Self {
instance_id,
processor_context,
clip_matrix_ref: None,
instance_feedback_event_sender,
clip_matrix_event_sender,
audio_hook_task_sender,
real_time_processor_sender,
this_track,
slot_contents_changed_subject: Default::default(),
mappings_by_group: Default::default(),
active_mapping_by_group: Default::default(),
Expand Down Expand Up @@ -287,7 +287,10 @@ impl InstanceState {
///
/// Returns an error if the necessary pot database is not available.
pub fn pot_unit(&mut self) -> Result<SharedRuntimePotUnit, &'static str> {
let integration = RealearnPotIntegration::new(self.instance_feedback_event_sender.clone());
let integration = RealearnPotIntegration::new(
self.processor_context.containing_fx().clone(),
self.instance_feedback_event_sender.clone(),
);
self.pot_unit.loaded(Box::new(integration))
}

Expand Down Expand Up @@ -434,7 +437,7 @@ impl InstanceState {
self.real_time_processor_sender.clone(),
self.clip_matrix_event_sender.clone(),
);
Matrix::new(clip_matrix_handler, self.this_track.clone())
Matrix::new(clip_matrix_handler, self.processor_context.track().cloned())
}

pub(super) fn set_clip_matrix_ref(&mut self, matrix_ref: Option<ClipMatrixRef>) {
Expand Down Expand Up @@ -733,12 +736,16 @@ pub enum ClipMatrixRelevance<'a> {
}

struct RealearnPotIntegration {
containing_fx: Fx,
sender: SenderToNormalThread<InstanceStateChanged>,
}

impl RealearnPotIntegration {
fn new(sender: SenderToNormalThread<InstanceStateChanged>) -> Self {
Self { sender }
fn new(containing_fx: Fx, sender: SenderToNormalThread<InstanceStateChanged>) -> Self {
Self {
containing_fx,
sender,
}
}
}

Expand Down Expand Up @@ -781,4 +788,8 @@ impl PotIntegration for RealearnPotIntegration {
PotStateChangedEvent::IndexesRebuilt,
));
}

fn protected_fx(&self) -> &Fx {
&self.containing_fx
}
}
2 changes: 1 addition & 1 deletion main/src/infrastructure/plugin/realearn_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,11 +416,11 @@ impl RealearnPlugin {
SenderToNormalThread::new_unbounded_channel("instance state change events");
let instance_state = BackboneState::get().create_instance(
instance_id,
processor_context.clone(),
instance_feedback_event_sender,
App::get().clip_matrix_event_sender().clone(),
App::get().normal_audio_hook_task_sender().clone(),
normal_real_time_task_sender.clone(),
processor_context.track().cloned(),
);
// Session (application - shared)
let session = Session::new(
Expand Down
Loading

0 comments on commit 8beee7b

Please sign in to comment.