aexlo 0.1.0

Run After Effects plugins without Adobe dependencies.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! `AEGP Persistent Data Suite` (version 3), the host-preferences store.
//!
//! In real After Effects this suite is a window onto the application preference
//! blob (the `Prefs.txt` files). aexlo is not After Effects and keeps no such
//! blob, so we back it with a process-wide **in-memory** store: reads of unknown
//! keys honestly report "not present" (and return the caller's default, per the
//! SDK contract), while writes round-trip within the session.
//!
//! ## Why this matters beyond preferences
//! Plugins built on the aescripts licensing library (DeepGlow2, and every other
//! aescripts product) call this suite from `getLicense` during
//! `PF_Cmd_SEQUENCE_SETUP` -- `AEGP_GetApplicationBlob`, then `AEGP_DoesKeyExist`
//! on their product section. If the suite is missing, the plugin's
//! `AEGP_SuiteHandler::LoadSuite` throws `MissingSuiteError`, and that C++
//! exception unwinds through our `extern "C"` boundary and aborts the process.
//! Providing a faithful (if empty) suite lets the license check run to
//! completion: a genuinely licensed plugin validates and renders clean; an
//! unlicensed one concludes "no license" and watermarks -- either way it *runs*
//! instead of aborting.

use std::collections::HashMap;
use std::ffi::CStr;
use std::sync::{LazyLock, Mutex, MutexGuard, PoisonError};

use after_effects_sys::{
	A_Boolean, A_Err, A_FpLong, A_char, A_long, A_u_long, AEGP_MemHandle, AEGP_PersistentBlobH,
	AEGP_PersistentDataSuite3, AEGP_PluginID,
};

/// A single stored preference value, tagged by the setter that wrote it.
enum Value {
	Data(Vec<u8>),
	Str(Vec<u8>),
	Long(A_long),
	FpLong(A_FpLong),
}

/// Process-wide preference store, keyed by `(section, value)`.
static STORE: LazyLock<Mutex<HashMap<(String, String), Value>>> = LazyLock::new(|| Mutex::new(HashMap::new()));

/// [`STORE`], usable even after a guarded callback panicked while holding it.
fn store() -> MutexGuard<'static, HashMap<(String, String), Value>> {
	STORE.lock().unwrap_or_else(PoisonError::into_inner)
}

/// Opaque, never-dereferenced token handed back as the "application blob".
///
/// Plugins treat `AEGP_PersistentBlobH` as opaque and only pass it back to us, so
/// any stable non-null address serves; we ignore it and operate on [`STORE`].
static BLOB_SENTINEL: u8 = 0;

fn blob_handle() -> AEGP_PersistentBlobH {
	&BLOB_SENTINEL as *const u8 as AEGP_PersistentBlobH
}

/// # Safety
/// `p` must be null or a valid null-terminated C string.
unsafe fn key(p: *const A_char) -> String {
	if p.is_null() {
		return String::new();
	}
	unsafe { CStr::from_ptr(p) }.to_string_lossy().into_owned()
}

/// Copy `bytes` (plus a NUL) into `bufZ`, honoring `buf_size`, and report the
/// size actually required via `actual` -- matching the `AEGP_GetString` contract
/// (an undersized buffer yields `""` and the needed size).
///
/// # Safety
/// `bufZ` must be null or writable for `buf_size` bytes; `actual` must be null or
/// a writable `A_u_long`.
unsafe fn write_str(bytes: &[u8], bufZ: *mut A_char, buf_size: A_u_long, actual: *mut A_u_long) {
	let needed = bytes.len() as A_u_long + 1;
	if !actual.is_null() {
		unsafe { *actual = needed };
	}
	if bufZ.is_null() || buf_size == 0 {
		return;
	}
	if needed <= buf_size {
		unsafe {
			std::ptr::copy_nonoverlapping(bytes.as_ptr() as *const A_char, bufZ, bytes.len());
			*bufZ.add(bytes.len()) = 0;
		}
	} else {
		unsafe { *bufZ = 0 };
	}
}

unsafe extern "C" fn get_application_blob(blobPH: *mut AEGP_PersistentBlobH) -> A_Err {
	crate::core::ffi::guard("get_application_blob", || {
		if !blobPH.is_null() {
			unsafe { *blobPH = blob_handle() };
		}
		0
	})
}

unsafe extern "C" fn get_num_sections(_blobH: AEGP_PersistentBlobH, num: *mut A_long) -> A_Err {
	crate::core::ffi::guard("get_num_sections", || {
		if !num.is_null() {
			let store = store();
			let mut sections: Vec<&String> = store.keys().map(|(s, _)| s).collect();
			sections.sort();
			sections.dedup();
			unsafe { *num = sections.len() as A_long };
		}
		0
	})
}

unsafe extern "C" fn get_section_key_by_index(
	_blobH: AEGP_PersistentBlobH,
	section_index: A_long,
	max_section_size: A_long,
	section_keyZ: *mut A_char,
) -> A_Err {
	crate::core::ffi::guard("get_section_key_by_index", || {
		let store = store();
		let mut sections: Vec<String> = store.keys().map(|(s, _)| s.clone()).collect();
		sections.sort();
		sections.dedup();
		let name = sections.get(section_index as usize).cloned().unwrap_or_default();
		unsafe {
			write_str(
				name.as_bytes(),
				section_keyZ,
				max_section_size.max(0) as A_u_long,
				std::ptr::null_mut(),
			)
		};
		0
	})
}

unsafe extern "C" fn does_key_exist(
	_blobH: AEGP_PersistentBlobH,
	section_keyZ: *const A_char,
	value_keyZ: *const A_char,
	existsPB: *mut A_Boolean,
) -> A_Err {
	crate::core::ffi::guard("does_key_exist", || {
		log::debug!("AEGP_DoesKeyExist [{}] {}", unsafe { key(section_keyZ) }, unsafe {
			key(value_keyZ)
		});
		let exists = {
			let store = store();
			store.contains_key(&(unsafe { key(section_keyZ) }, unsafe { key(value_keyZ) }))
		};
		if !existsPB.is_null() {
			unsafe { *existsPB = A_Boolean::from(exists) };
		}
		0
	})
}

unsafe extern "C" fn get_num_keys(
	_blobH: AEGP_PersistentBlobH,
	section_keyZ: *const A_char,
	num: *mut A_long,
) -> A_Err {
	crate::core::ffi::guard("get_num_keys", || {
		if !num.is_null() {
			let section = unsafe { key(section_keyZ) };
			let store = store();
			let count = store.keys().filter(|(s, _)| *s == section).count();
			unsafe { *num = count as A_long };
		}
		0
	})
}

unsafe extern "C" fn get_value_key_by_index(
	_blobH: AEGP_PersistentBlobH,
	section_keyZ: *const A_char,
	key_index: A_long,
	max_key_size: A_long,
	value_keyZ: *mut A_char,
) -> A_Err {
	crate::core::ffi::guard("get_value_key_by_index", || {
		let section = unsafe { key(section_keyZ) };
		let store = store();
		let mut values: Vec<String> = store
			.keys()
			.filter(|(s, _)| *s == section)
			.map(|(_, v)| v.clone())
			.collect();
		values.sort();
		let name = values.get(key_index as usize).cloned().unwrap_or_default();
		unsafe {
			write_str(
				name.as_bytes(),
				value_keyZ,
				max_key_size.max(0) as A_u_long,
				std::ptr::null_mut(),
			)
		};
		0
	})
}

unsafe extern "C" fn get_data_handle(
	_plugin_id: AEGP_PluginID,
	_blobH: AEGP_PersistentBlobH,
	_section_keyZ: *const A_char,
	_value_keyZ: *const A_char,
	_defaultH0: AEGP_MemHandle,
	valuePH: *mut AEGP_MemHandle,
) -> A_Err {
	crate::core::ffi::guard("get_data_handle", || {
		// We don't vend AEGP_MemHandles; report "no data" (NULL handle) per the SDK's
		// zero-sized-handle convention. Not reached by the license path.
		if !valuePH.is_null() {
			unsafe { *valuePH = std::ptr::null_mut() };
		}
		0
	})
}

unsafe extern "C" fn get_data(
	_blobH: AEGP_PersistentBlobH,
	section_keyZ: *const A_char,
	value_keyZ: *const A_char,
	data_sizeLu: A_u_long,
	defaultPV0: *const std::os::raw::c_void,
	bufPV: *mut std::os::raw::c_void,
) -> A_Err {
	crate::core::ffi::guard("get_data", || {
		let k = (unsafe { key(section_keyZ) }, unsafe { key(value_keyZ) });
		log::debug!("AEGP_PersistentData [{}] {}", k.0, k.1);
		let size = data_sizeLu as usize;
		let mut store = store();
		if let Some(Value::Data(bytes)) = store.get(&k)
			&& bytes.len() == size
			&& !bufPV.is_null()
		{
			unsafe { std::ptr::copy_nonoverlapping(bytes.as_ptr(), bufPV as *mut u8, size) };
			return 0;
		}
		// Not found (or size mismatch): fall back to the default, writing it back to
		// the store as the SDK specifies.
		let default: Vec<u8> = if defaultPV0.is_null() {
			vec![0u8; size]
		} else {
			unsafe { std::slice::from_raw_parts(defaultPV0 as *const u8, size) }.to_vec()
		};
		if !bufPV.is_null() && size > 0 {
			unsafe { std::ptr::copy_nonoverlapping(default.as_ptr(), bufPV as *mut u8, size) };
		}
		store.insert(k, Value::Data(default));
		0
	})
}

unsafe extern "C" fn get_string(
	_blobH: AEGP_PersistentBlobH,
	section_keyZ: *const A_char,
	value_keyZ: *const A_char,
	defaultZ0: *const A_char,
	buf_sizeLu: A_u_long,
	bufZ: *mut A_char,
	actual_buf_sizeLu0: *mut A_u_long,
) -> A_Err {
	crate::core::ffi::guard("get_string", || {
		let k = (unsafe { key(section_keyZ) }, unsafe { key(value_keyZ) });
		log::debug!("AEGP_PersistentData [{}] {}", k.0, k.1);
		let mut store = store();
		if let Some(Value::Str(bytes)) = store.get(&k) {
			unsafe { write_str(bytes, bufZ, buf_sizeLu, actual_buf_sizeLu0) };
			return 0;
		}
		let default = unsafe { key(defaultZ0) };
		unsafe { write_str(default.as_bytes(), bufZ, buf_sizeLu, actual_buf_sizeLu0) };
		store.insert(k, Value::Str(default.into_bytes()));
		0
	})
}

unsafe extern "C" fn get_long(
	_blobH: AEGP_PersistentBlobH,
	section_keyZ: *const A_char,
	value_keyZ: *const A_char,
	defaultL: A_long,
	valuePL: *mut A_long,
) -> A_Err {
	crate::core::ffi::guard("get_long", || {
		let k = (unsafe { key(section_keyZ) }, unsafe { key(value_keyZ) });
		log::debug!("AEGP_PersistentData [{}] {}", k.0, k.1);
		let mut store = store();
		let value = match store.get(&k) {
			Some(Value::Long(v)) => *v,
			_ => {
				store.insert(k, Value::Long(defaultL));
				defaultL
			}
		};
		if !valuePL.is_null() {
			unsafe { *valuePL = value };
		}
		0
	})
}

unsafe extern "C" fn get_fp_long(
	_blobH: AEGP_PersistentBlobH,
	section_keyZ: *const A_char,
	value_keyZ: *const A_char,
	defaultF: A_FpLong,
	valuePF: *mut A_FpLong,
) -> A_Err {
	crate::core::ffi::guard("get_fp_long", || {
		let k = (unsafe { key(section_keyZ) }, unsafe { key(value_keyZ) });
		log::debug!("AEGP_PersistentData [{}] {}", k.0, k.1);
		let mut store = store();
		let value = match store.get(&k) {
			Some(Value::FpLong(v)) => *v,
			_ => {
				store.insert(k, Value::FpLong(defaultF));
				defaultF
			}
		};
		if !valuePF.is_null() {
			unsafe { *valuePF = value };
		}
		0
	})
}

unsafe extern "C" fn set_data_handle(
	_blobH: AEGP_PersistentBlobH,
	_section_keyZ: *const A_char,
	_value_keyZ: *const A_char,
	_valueH: AEGP_MemHandle,
) -> A_Err {
	crate::core::ffi::guard("set_data_handle", || {
		// We can't read foreign AEGP_MemHandles without the memory suite; accept and
		// drop. Not reached by the license path.
		0
	})
}

unsafe extern "C" fn set_data(
	_blobH: AEGP_PersistentBlobH,
	section_keyZ: *const A_char,
	value_keyZ: *const A_char,
	data_sizeLu: A_u_long,
	dataPV: *const std::os::raw::c_void,
) -> A_Err {
	crate::core::ffi::guard("set_data", || {
		let k = (unsafe { key(section_keyZ) }, unsafe { key(value_keyZ) });
		log::debug!("AEGP_PersistentData [{}] {}", k.0, k.1);
		let bytes = if dataPV.is_null() {
			Vec::new()
		} else {
			unsafe { std::slice::from_raw_parts(dataPV as *const u8, data_sizeLu as usize) }.to_vec()
		};
		store().insert(k, Value::Data(bytes));
		0
	})
}

unsafe extern "C" fn set_string(
	_blobH: AEGP_PersistentBlobH,
	section_keyZ: *const A_char,
	value_keyZ: *const A_char,
	strZ: *const A_char,
) -> A_Err {
	crate::core::ffi::guard("set_string", || {
		let k = (unsafe { key(section_keyZ) }, unsafe { key(value_keyZ) });
		log::debug!("AEGP_PersistentData [{}] {}", k.0, k.1);
		STORE
			.lock()
			.unwrap()
			.insert(k, Value::Str(unsafe { key(strZ) }.into_bytes()));
		0
	})
}

unsafe extern "C" fn set_long(
	_blobH: AEGP_PersistentBlobH,
	section_keyZ: *const A_char,
	value_keyZ: *const A_char,
	valueL: A_long,
) -> A_Err {
	crate::core::ffi::guard("set_long", || {
		let k = (unsafe { key(section_keyZ) }, unsafe { key(value_keyZ) });
		log::debug!("AEGP_PersistentData [{}] {}", k.0, k.1);
		store().insert(k, Value::Long(valueL));
		0
	})
}

unsafe extern "C" fn set_fp_long(
	_blobH: AEGP_PersistentBlobH,
	section_keyZ: *const A_char,
	value_keyZ: *const A_char,
	valueF: A_FpLong,
) -> A_Err {
	crate::core::ffi::guard("set_fp_long", || {
		let k = (unsafe { key(section_keyZ) }, unsafe { key(value_keyZ) });
		log::debug!("AEGP_PersistentData [{}] {}", k.0, k.1);
		store().insert(k, Value::FpLong(valueF));
		0
	})
}

unsafe extern "C" fn delete_entry(
	_blobH: AEGP_PersistentBlobH,
	section_keyZ: *const A_char,
	value_keyZ: *const A_char,
) -> A_Err {
	crate::core::ffi::guard("delete_entry", || {
		let k = (unsafe { key(section_keyZ) }, unsafe { key(value_keyZ) });
		log::debug!("AEGP_PersistentData [{}] {}", k.0, k.1);
		store().remove(&k);
		0
	})
}

unsafe extern "C" fn get_prefs_directory(unicode_pathPH: *mut AEGP_MemHandle) -> A_Err {
	crate::core::ffi::guard("get_prefs_directory", || {
		// "empty string if no file": we have no prefs directory, so report none.
		if !unicode_pathPH.is_null() {
			unsafe { *unicode_pathPH = std::ptr::null_mut() };
		}
		0
	})
}

/// Build the `AEGP Persistent Data Suite` v3 vtable.
///
/// `const` so it can initialize the shared [`SUITE_CONTAINER`](crate::suites::SUITE_CONTAINER)
/// static; all per-call state lives in the [`STORE`] global, not the vtable.
pub(super) const fn create_persistent_data_suite_3() -> AEGP_PersistentDataSuite3 {
	AEGP_PersistentDataSuite3 {
		AEGP_GetApplicationBlob: Some(get_application_blob),
		AEGP_GetNumSections: Some(get_num_sections),
		AEGP_GetSectionKeyByIndex: Some(get_section_key_by_index),
		AEGP_DoesKeyExist: Some(does_key_exist),
		AEGP_GetNumKeys: Some(get_num_keys),
		AEGP_GetValueKeyByIndex: Some(get_value_key_by_index),
		AEGP_GetDataHandle: Some(get_data_handle),
		AEGP_GetData: Some(get_data),
		AEGP_GetString: Some(get_string),
		AEGP_GetLong: Some(get_long),
		AEGP_GetFpLong: Some(get_fp_long),
		AEGP_SetDataHandle: Some(set_data_handle),
		AEGP_SetData: Some(set_data),
		AEGP_SetString: Some(set_string),
		AEGP_SetLong: Some(set_long),
		AEGP_SetFpLong: Some(set_fp_long),
		AEGP_DeleteEntry: Some(delete_entry),
		AEGP_GetPrefsDirectory: Some(get_prefs_directory),
	}
}