From ef5b900141765237afb749d3049dff88ada5afa2 Mon Sep 17 00:00:00 2001 From: Ben Huyck Date: Thu, 22 Oct 2026 16:48:15 +0000 Subject: [PATCH] Actions: Load Actions takes an encoding and reports the one it used (#6743) --- crates/engine/src/cmd/atn_cmds.rs | 4 +- crates/engine/src/cmd/atn_cmds/convert.rs | 26 ++++++++---- crates/engine/src/cmd/atn_cmds/files.rs | 11 +++++- crates/engine/src/cmd/atn_cmds/tests.rs | 48 +++++++++++++++++++++++ 4 files changed, 77 insertions(+), 12 deletions(-) diff --git a/crates/engine/src/cmd/atn_cmds.rs b/crates/engine/src/cmd/atn_cmds.rs index 7c717701bf..0187681923 100644 --- a/crates/engine/src/cmd/atn_cmds.rs +++ b/crates/engine/src/cmd/atn_cmds.rs @@ -39,7 +39,7 @@ mod tables; #[cfg(test)] use adjustments::{levels_from, selective_from}; -pub use convert::{Imported, Skipped, Unsupported, export, function_key_name, import, parse_function_key}; +pub use convert::{Imported, Skipped, Unsupported, export, function_key_name, import, import_with_encoding, parse_function_key}; #[cfg(test)] use convert::{MAX_NAME_CHARS, export_item, normalise}; #[cfg(test)] @@ -101,7 +101,7 @@ pub fn specs() -> Vec { label: "Load Actions…", menu: &[], shortcut: None, - params: r##"{"path":".atn file"?,"data":base64 bytes?,"set":name? (override the file's set name),"replace":bool=true (replace the actions of a set of the same name)} → {set, actions:[names], steps, unsupported:[{action, step, event, name}], functionKeys:[{action, key}], warnings}. Photoshop steps PhotoCraft cannot play become actions.photoshopStep steps (kept for Save Actions; they fail at playback with the step's name); tool selections and other steps with no document effect are imported switched off. Layer references by name replay in any document."##, + params: r##"{"path":".atn file"?,"data":base64 bytes?,"set":name? (override the file's set name),"replace":bool=true (replace the actions of a set of the same name),"encoding":"auto"|label="auto" (the encoding of 8-bit step names: shift_jis, gbk, big5, euc-kr, windows-1252, macintosh…; auto works it out)} → {set, actions:[names], steps, unsupported:[{action, step, event, name}], functionKeys:[{action, key}], warnings, encoding}. Photoshop steps PhotoCraft cannot play become actions.photoshopStep steps (kept for Save Actions; they fail at playback with the step's name); tool selections and other steps with no document effect are imported switched off. Layer references by name replay in any document."##, enabled: always, run: load, journal: false, diff --git a/crates/engine/src/cmd/atn_cmds/convert.rs b/crates/engine/src/cmd/atn_cmds/convert.rs index 79476cc5a7..ccdbcf7348 100644 --- a/crates/engine/src/cmd/atn_cmds/convert.rs +++ b/crates/engine/src/cmd/atn_cmds/convert.rs @@ -23,15 +23,18 @@ pub struct Imported { pub function_keys: Vec<(String, String)>, pub unsupported: Vec, pub warnings: Vec, + /// The encoding the file's 8-bit step names were read in (`"GBK"`, `"Shift_JIS"`, + /// `"macintosh"`…); `None` when none needed one. + pub encoding: Option, } -fn photoshop_step(item: &AtnItem) -> (String, Value) { +fn photoshop_step(item: &AtnItem, label: String) -> (String, Value) { let descriptor = item.descriptor.as_ref().map(|d| photocraft_paint::tile::b64_encode(&d.to_bytes())); let event_kind = match &item.event { AtnEvent::Code(_) => "code", AtnEvent::Name(_) => "name", }; - (PHOTOSHOP_STEP.into(), json!({"event": item.event.id(), "eventKind": event_kind, "name": item.label(), "descriptor": descriptor})) + (PHOTOSHOP_STEP.into(), json!({"event": item.event.id(), "eventKind": event_kind, "name": label, "descriptor": descriptor})) } /// Integral floats become integers (`2.0` → `2`), as the commands' own docs and recordings @@ -61,7 +64,7 @@ pub(super) fn clip_name(s: &str) -> String { s.chars().take(MAX_NAME_CHARS).collect() } -fn convert_action(a: &AtnAction, set: &str, unsupported: &mut Vec) -> Action { +fn convert_action(file: &AtnFile, a: &AtnAction, set: &str, unsupported: &mut Vec) -> Action { let name = clip_name(&a.name.to_string_lossy()); let mut action = Action::new(name.clone(), Vec::new()); action.set = Some(clip_name(set)); @@ -74,8 +77,9 @@ fn convert_action(a: &AtnAction, set: &str, unsupported: &mut Vec) let (new_steps, enabled) = match mapped { Mapped::Steps(s) => (s, item.enabled), Mapped::Unsupported { harmless } => { - unsupported.push(Unsupported { action: name.clone(), step: i, event: item.event.id(), name: item.label() }); - (vec![photoshop_step(item)], item.enabled && !harmless) + let label = file.item_label(item); + unsupported.push(Unsupported { action: name.clone(), step: i, event: item.event.id(), name: label.clone() }); + (vec![photoshop_step(item, label)], item.enabled && !harmless) } }; for mut s in new_steps { @@ -96,12 +100,18 @@ fn convert_action(a: &AtnAction, set: &str, unsupported: &mut Vec) /// Convert a `.atn` file. Fails only when the file is unreadable: every step imports, as a /// command or as a kept Photoshop step. pub fn import(bytes: &[u8]) -> std::result::Result { - let file = atn::parse(bytes).map_err(|e| e.to_string())?; + import_with_encoding(bytes, None) +} + +/// [`import`], reading 8-bit step names in `encoding` (a label such as `"shift_jis"` or +/// `"gbk"`) instead of working it out. An unknown label fails. +pub fn import_with_encoding(bytes: &[u8], encoding: Option<&str>) -> std::result::Result { + let file = atn::parse_with_encoding(bytes, encoding).map_err(|e| e.to_string())?; let set = file.name.to_string_lossy(); let set = if set.trim().is_empty() { DEFAULT_SET.to_string() } else { clip_name(&set) }; - let mut out = Imported { set: set.clone(), warnings: file.warnings.clone(), ..Default::default() }; + let mut out = Imported { set: set.clone(), warnings: file.warnings.clone(), encoding: file.encoding.clone(), ..Default::default() }; for a in &file.actions { - let action = convert_action(a, &set, &mut out.unsupported); + let action = convert_action(&file, a, &set, &mut out.unsupported); if a.function_key > 0 { out.function_keys.push((action.name.clone(), function_key_name(a.function_key, a.shift, a.command))); } diff --git a/crates/engine/src/cmd/atn_cmds/files.rs b/crates/engine/src/cmd/atn_cmds/files.rs index be7bd45151..c22b673deb 100644 --- a/crates/engine/src/cmd/atn_cmds/files.rs +++ b/crates/engine/src/cmd/atn_cmds/files.rs @@ -1,4 +1,4 @@ -use super::convert::{UniqueNames, export, import}; +use super::convert::{UniqueNames, export, import_with_encoding}; use super::{Action, DEFAULT_SET, EngineError, Result, Session, Value, bad, json}; pub(super) fn load(s: &mut Session, p: &Value) -> Result { @@ -6,8 +6,14 @@ pub(super) fn load(s: &mut Session, p: &Value) -> Result { if s.actions.recording.is_some() || s.actions.playing > 0 { return Err(bad(cmd, "stop recording or playing before loading actions")); } + let encoding = match p.get("encoding") { + None | Some(Value::Null) => None, + Some(Value::String(e)) if e == "auto" => None, + Some(Value::String(e)) if !e.is_empty() => Some(e.as_str()), + Some(_) => return Err(bad(cmd, "encoding must be \"auto\" or a label such as shift_jis, gbk, big5, euc-kr, windows-1252, macintosh")), + }; let (bytes, _) = crate::preset_import_cmds::file_bytes(p, cmd, ".atn file")?; - let mut imp = import(&bytes).map_err(|e| bad(cmd, e))?; + let mut imp = import_with_encoding(&bytes, encoding).map_err(|e| bad(cmd, e))?; match p.get("set") { None | Some(Value::Null) => {} Some(Value::String(n)) if !n.trim().is_empty() => { @@ -77,6 +83,7 @@ pub(super) fn load(s: &mut Session, p: &Value) -> Result { "unsupported": imp.unsupported, "functionKeys": keys.iter().map(|(n, k)| json!({"action": n, "key": k})).collect::>(), "warnings": imp.warnings, + "encoding": imp.encoding.as_deref().unwrap_or("UTF-8"), })) } diff --git a/crates/engine/src/cmd/atn_cmds/tests.rs b/crates/engine/src/cmd/atn_cmds/tests.rs index f58cfaef06..4a41a12e0f 100644 --- a/crates/engine/src/cmd/atn_cmds/tests.rs +++ b/crates/engine/src/cmd/atn_cmds/tests.rs @@ -3176,3 +3176,51 @@ fn long_names_are_clipped_instead_of_copied_into_every_diagnostic() { assert_eq!(imp.unsupported.len(), 50); assert!(imp.unsupported.iter().all(|u| u.action.chars().count() == MAX_NAME_CHARS)); } + +/// A set from Chinese Photoshop: UTF-16 set and action names, a step name in GBK (可选颜色, +/// the bytes from #6743) on an event PhotoCraft has no command for, with no descriptor. +fn gbk_set() -> Vec { + let step = AtnItem { dictionary: b"\xbf\xc9\xd1\xa1\xd1\xd5\xc9\xab".to_vec(), ..item("syntheticFill", "", None) }; + let file = AtnFile { + name: UnicodeString::new_nul("相馆"), + actions: vec![AtnAction { name: UnicodeString::new_nul("调白"), items: vec![step], ..Default::default() }], + ..Default::default() + }; + atn::write(&file).unwrap() +} + +#[test] +fn load_decodes_legacy_step_names() { + let mut s = Session::new(); + let r = s.execute("actions.load", json!({"data": b64(&gbk_set())})).unwrap(); + assert_eq!(r["encoding"], "GBK", "{r}"); + assert_eq!(r["unsupported"][0]["name"], "可选颜色", "{r}"); + s.execute("file.new", json!({"width": 8, "height": 8})).unwrap(); + let played = s.execute("actions.play", json!({"action": "调白"})).unwrap(); + assert!(played["failed"]["error"].as_str().unwrap().contains("可选颜色"), "{played}"); +} + +#[test] +fn load_encoding_override() { + let mut s = Session::new(); + let r = s.execute("actions.load", json!({"data": b64(&gbk_set()), "encoding": "shift_jis"})).unwrap(); + assert_eq!(r["encoding"], "Shift_JIS", "{r}"); + let r = s.execute("actions.load", json!({"data": b64(&gbk_set()), "encoding": "auto"})).unwrap(); + assert_eq!(r["encoding"], "GBK", "{r}"); +} + +#[test] +fn load_reports_utf8() { + let mut s = Session::new(); + let r = s.execute("actions.load", json!({"data": b64(&atn::write(&sample()).unwrap())})).unwrap(); + assert_eq!(r["encoding"], "UTF-8", "{r}"); +} + +#[test] +fn load_rejects_bad_encodings() { + let mut s = Session::new(); + for bad in [json!(3), json!(""), json!("nope"), json!("replacement"), json!(["gbk"])] { + assert!(s.execute("actions.load", json!({"data": b64(&gbk_set()), "encoding": bad})).is_err(), "{bad}"); + } + assert!(s.actions.list.is_empty(), "nothing is loaded when the encoding is bad"); +}