Skip to content

Commit

Permalink
Properly extract out entity handling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
atampy25 committed Jul 17, 2024
1 parent b937b35 commit e739dd1
Show file tree
Hide file tree
Showing 11 changed files with 807 additions and 727 deletions.
16 changes: 4 additions & 12 deletions src-tauri/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,10 +796,8 @@ pub fn get_decorations(
.context("No resource property on object ZRuntimeResourceID")?
.as_str()
.context("Resource was not string")?
} else if let Some(x) = property_data.value.as_str() {
x
} else {
""
property_data.value.as_str().unwrap_or_default()
};

if let Some(entry) = hash_list.entries.get(&normalise_to_hash(res.to_owned()))
Expand All @@ -825,10 +823,8 @@ pub fn get_decorations(
.context("No resource property on object ZRuntimeResourceID")?
.as_str()
.context("Resource was not string")?
} else if let Some(x) = val.as_str() {
x
} else {
""
val.as_str().unwrap_or_default()
};

if let Some(entry) = hash_list.entries.get(&normalise_to_hash(res.to_owned()))
Expand Down Expand Up @@ -907,10 +903,8 @@ pub fn get_decorations(
.context("No resource property on object ZRuntimeResourceID")?
.as_str()
.context("Resource was not string")?
} else if let Some(x) = property_data.value.as_str() {
x
} else {
""
property_data.value.as_str().unwrap_or_default()
};

if let Some(entry) = hash_list.entries.get(&normalise_to_hash(res.to_owned()))
Expand All @@ -936,10 +930,8 @@ pub fn get_decorations(
.context("No resource property on object ZRuntimeResourceID")?
.as_str()
.context("Resource was not string")?
} else if let Some(x) = val.as_str() {
x
} else {
""
val.as_str().unwrap_or_default()
};

if let Some(entry) = hash_list.entries.get(&normalise_to_hash(res.to_owned()))
Expand Down
65 changes: 65 additions & 0 deletions src-tauri/src/event_handling/entity/general.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use anyhow::{anyhow, Context, Result};
use fn_error_context::context;
use tauri::{AppHandle, Manager};
use tryvial::try_fn;

use crate::{
model::{AppState, EditorData, EditorRequest, EntityEditorRequest, EntityGeneralEvent, EntityTreeRequest, Request},
send_request
};

#[try_fn]
#[context("Couldn't handle update content event")]
pub async fn handle(app: &AppHandle, event: EntityGeneralEvent) -> Result<()> {
let app_state = app.state::<AppState>();

match event {
EntityGeneralEvent::SetShowReverseParentRefs {
editor_id,
show_reverse_parent_refs
} => {
let mut editor_state = app_state.editor_states.get_mut(&editor_id).context("No such editor")?;

let settings = match editor_state.data {
EditorData::QNEntity { ref mut settings, .. } => settings,
EditorData::QNPatch { ref mut settings, .. } => settings,

_ => {
Err(anyhow!("Editor {} is not a QN editor", editor_id))?;
panic!();
}
};

settings.show_reverse_parent_refs = show_reverse_parent_refs;
}

EntityGeneralEvent::SetShowChangesFromOriginal {
editor_id,
show_changes_from_original
} => {
let mut editor_state = app_state.editor_states.get_mut(&editor_id).context("No such editor")?;

let settings = match editor_state.data {
EditorData::QNEntity { ref mut settings, .. } => settings,
EditorData::QNPatch { ref mut settings, .. } => settings,

_ => {
Err(anyhow!("Editor {} is not a QN editor", editor_id))?;
panic!();
}
};

settings.show_changes_from_original = show_changes_from_original;

send_request(
app,
Request::Editor(EditorRequest::Entity(EntityEditorRequest::Tree(
EntityTreeRequest::SetShowDiff {
editor_id,
show_diff: show_changes_from_original
}
)))
)?;
}
}
}
72 changes: 72 additions & 0 deletions src-tauri/src/event_handling/entity/meta_pane.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use anyhow::{anyhow, Context, Result};
use fn_error_context::context;
use quickentity_rs::qn_structs::{CommentEntity, Ref};
use tauri::{AppHandle, Manager};
use tryvial::try_fn;

use crate::{
entity::get_local_reference,
model::{
AppState, EditorData, EditorRequest, EntityEditorRequest, EntityMetaPaneEvent, EntityTreeRequest,
GlobalRequest, Request
},
send_request
};

#[try_fn]
#[context("Couldn't handle entity meta pane event")]
pub async fn handle(app: &AppHandle, event: EntityMetaPaneEvent) -> Result<()> {
let app_state = app.state::<AppState>();

match event {
EntityMetaPaneEvent::JumpToReference { editor_id, reference } => {
send_request(
app,
Request::Editor(EditorRequest::Entity(EntityEditorRequest::Tree(
EntityTreeRequest::Select {
editor_id,
id: Some(reference)
}
)))
)?;
}

EntityMetaPaneEvent::SetNotes {
editor_id,
entity_id,
notes
} => {
let mut editor_state = app_state.editor_states.get_mut(&editor_id).context("No such editor")?;

let entity = match editor_state.data {
EditorData::QNEntity { ref mut entity, .. } => entity,
EditorData::QNPatch { ref mut current, .. } => current,

_ => {
Err(anyhow!("Editor {} is not a QN editor", editor_id))?;
panic!();
}
};

// Remove comment referring to given entity
entity
.comments
.retain(|x| get_local_reference(&x.parent).map(|x| x != entity_id).unwrap_or(true));

// Add new comment
entity.comments.push(CommentEntity {
parent: Ref::Short(Some(entity_id)),
name: "Notes".into(),
text: notes
});

send_request(
app,
Request::Global(GlobalRequest::SetTabUnsaved {
id: editor_id.to_owned(),
unsaved: true
})
)?;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{

#[try_fn]
#[context("Couldn't handle entity metadata event")]
pub async fn handle_entity_metadata_event(app: &AppHandle, event: EntityMetadataEvent) -> Result<()> {
pub async fn handle(app: &AppHandle, event: EntityMetadataEvent) -> Result<()> {
let app_state = app.state::<AppState>();

match event {
Expand Down
43 changes: 43 additions & 0 deletions src-tauri/src/event_handling/entity/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use anyhow::Result;
use fn_error_context::context;
use tauri::AppHandle;
use tryvial::try_fn;

use crate::model::EntityEditorEvent;

pub mod general;
pub mod meta_pane;
pub mod metadata;
pub mod monaco;
pub mod overrides;
pub mod tree;

#[try_fn]
#[context("Couldn't handle entity editor event")]
pub async fn handle(app: &AppHandle, event: EntityEditorEvent) -> Result<()> {
match event {
EntityEditorEvent::General(event) => {
general::handle(app, event).await?;
}

EntityEditorEvent::Tree(event) => {
tree::handle(app, event).await?;
}

EntityEditorEvent::Monaco(event) => {
monaco::handle(app, event).await?;
}

EntityEditorEvent::MetaPane(event) => {
meta_pane::handle(app, event).await?;
}

EntityEditorEvent::Metadata(event) => {
metadata::handle(app, event).await?;
}

EntityEditorEvent::Overrides(event) => {
overrides::handle(app, event).await?;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ use crate::{
get_loaded_game_version,
model::{
AppSettings, AppState, EditorData, EditorRequest, EditorState, EditorType, EditorValidity, EntityEditorRequest,
EntityMonacoRequest, EntityTreeRequest, GlobalRequest, Request
EntityMonacoEvent, EntityMonacoRequest, EntityTreeRequest, GlobalRequest, Request
},
rpkg::{ensure_entity_in_cache, extract_latest_overview_info, normalise_to_hash},
rpkg::{extract_latest_overview_info, normalise_to_hash},
send_notification, send_request, start_task, Notification, NotificationKind
};

Expand Down Expand Up @@ -74,9 +74,112 @@ pub const SAFE_TO_SYNC: [&str; 43] = [
"SEntityTemplateReference"
];

#[try_fn]
#[context("Couldn't handle monaco event")]
pub async fn handle(app: &AppHandle, event: EntityMonacoEvent) -> Result<()> {
let app_state = app.state::<AppState>();

match event {
EntityMonacoEvent::UpdateContent {
editor_id,
entity_id,
content
} => {
update_content(app, editor_id, entity_id, content).await?;
}

EntityMonacoEvent::FollowReference { editor_id, reference } => {
send_request(
app,
Request::Editor(EditorRequest::Entity(EntityEditorRequest::Tree(
EntityTreeRequest::Select {
editor_id,
id: Some(reference)
}
)))
)?;
}

EntityMonacoEvent::OpenFactory { factory, .. } => {
open_factory(app, factory).await?;
}

EntityMonacoEvent::SignalPin {
editor_id,
entity_id,
pin,
output
} => {
let editor_state = app_state.editor_states.get(&editor_id).context("No such editor")?;

let entity = match editor_state.data {
EditorData::QNEntity { ref entity, .. } => entity,
EditorData::QNPatch { ref current, .. } => current,

_ => {
Err(anyhow!("Editor {} is not a QN editor", editor_id))?;
panic!();
}
};

app_state
.editor_connection
.signal_pin(&entity_id, &entity.blueprint_hash, &pin, output)
.await?;
}

EntityMonacoEvent::OpenResourceOverview { resource, .. } => {
if let Some(resource_reverse_dependencies) = app_state.resource_reverse_dependencies.load().as_ref() {
let resource = normalise_to_hash(resource);

if resource_reverse_dependencies.contains_key(&resource) {
let id = Uuid::new_v4();

app_state.editor_states.insert(
id.to_owned(),
EditorState {
file: None,
data: EditorData::ResourceOverview {
hash: resource.to_owned()
}
}
);

send_request(
app,
Request::Global(GlobalRequest::CreateTab {
id,
name: format!("Resource overview ({resource})"),
editor_type: EditorType::ResourceOverview
})
)?;
} else {
send_notification(
app,
Notification {
kind: NotificationKind::Error,
title: "Not a vanilla resource".into(),
subtitle: "This factory doesn't exist in the base game files.".into()
}
)?;
}
} else {
send_notification(
app,
Notification {
kind: NotificationKind::Error,
title: "No game selected".into(),
subtitle: "You can't open game files without a copy of the game selected.".into()
}
)?;
}
}
}
}

#[try_fn]
#[context("Couldn't handle update content event")]
pub async fn handle_updatecontent(app: &AppHandle, editor_id: Uuid, entity_id: String, content: String) -> Result<()> {
pub async fn update_content(app: &AppHandle, editor_id: Uuid, entity_id: String, content: String) -> Result<()> {
let app_settings = app.state::<ArcSwap<AppSettings>>();
let app_state = app.state::<AppState>();

Expand Down Expand Up @@ -391,7 +494,7 @@ pub async fn handle_updatecontent(app: &AppHandle, editor_id: Uuid, entity_id: S

#[try_fn]
#[context("Couldn't handle open factory event")]
pub async fn handle_openfactory(app: &AppHandle, factory: String) -> Result<()> {
pub async fn open_factory(app: &AppHandle, factory: String) -> Result<()> {
let app_settings = app.state::<ArcSwap<AppSettings>>();
let app_state = app.state::<AppState>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ pub fn send_overrides_decorations(app: &AppHandle, editor_id: Uuid, entity: &Ent

#[try_fn]
#[context("Couldn't handle entity overrides event")]
pub async fn handle_entity_overrides_event(app: &AppHandle, event: EntityOverridesEvent) -> Result<()> {
pub async fn handle(app: &AppHandle, event: EntityOverridesEvent) -> Result<()> {
let app_state = app.state::<AppState>();

match event {
Expand Down
Loading

0 comments on commit e739dd1

Please sign in to comment.